3 namespace Drupal\Tests\comment\Unit;
5 use Drupal\comment\CommentStatistics;
6 use Drupal\Tests\UnitTestCase;
9 * @coversDefaultClass \Drupal\comment\CommentStatistics
12 class CommentStatisticsUnitTest extends UnitTestCase {
17 * @var \Drupal\Core\Database\Statement
22 * Mock select interface.
24 * @var \Drupal\Core\Database\Query\SelectInterface
29 * Mock database connection.
31 * @var \Drupal\Core\Database\Connection
36 * CommentStatistics service under test.
38 * @var \Drupal\comment\CommentStatisticsInterface
40 protected $commentStatistics;
43 * Counts calls to fetchAssoc().
47 protected $calls_to_fetch;
50 * Sets up required mocks and the CommentStatistics service under test.
52 protected function setUp() {
53 $this->statement = $this->getMockBuilder('Drupal\Core\Database\Driver\sqlite\Statement')
54 ->disableOriginalConstructor()
57 $this->statement->expects($this->any())
58 ->method('fetchObject')
59 ->will($this->returnCallback([$this, 'fetchObjectCallback']));
61 $this->select = $this->getMockBuilder('Drupal\Core\Database\Query\Select')
62 ->disableOriginalConstructor()
65 $this->select->expects($this->any())
67 ->will($this->returnSelf());
69 $this->select->expects($this->any())
71 ->will($this->returnSelf());
73 $this->select->expects($this->any())
75 ->will($this->returnValue($this->statement));
77 $this->database = $this->getMockBuilder('Drupal\Core\Database\Connection')
78 ->disableOriginalConstructor()
81 $this->database->expects($this->once())
83 ->will($this->returnValue($this->select));
85 $this->commentStatistics = new CommentStatistics($this->database, $this->getMock('Drupal\Core\Session\AccountInterface'), $this->getMock('Drupal\Core\Entity\EntityManagerInterface'), $this->getMock('Drupal\Core\State\StateInterface'));
89 * Tests the read method.
91 * @see \Drupal\comment\CommentStatistics::read()
96 public function testRead() {
97 $this->calls_to_fetch = 0;
98 $results = $this->commentStatistics->read(['1' => 'boo', '2' => 'foo'], 'snafoos');
99 $this->assertEquals($results, ['something', 'something-else']);
103 * Return value callback for fetchObject() function on mocked object.
105 * @return bool|string
106 * 'Something' on first, 'something-else' on second and FALSE for the
107 * other calls to function.
109 public function fetchObjectCallback() {
110 $this->calls_to_fetch++;
111 switch ($this->calls_to_fetch) {
116 return 'something-else';