Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / comment / tests / src / Unit / CommentStatisticsUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Unit;
4
5 use Drupal\comment\CommentStatistics;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\comment\CommentStatistics
10  * @group comment
11  */
12 class CommentStatisticsUnitTest extends UnitTestCase {
13
14   /**
15    * Mock statement.
16    *
17    * @var \Drupal\Core\Database\Statement
18    */
19   protected $statement;
20
21   /**
22    * Mock select interface.
23    *
24    * @var \Drupal\Core\Database\Query\SelectInterface
25    */
26   protected $select;
27
28   /**
29    * Mock database connection.
30    *
31    * @var \Drupal\Core\Database\Connection
32    */
33   protected $database;
34
35   /**
36    * CommentStatistics service under test.
37    *
38    * @var \Drupal\comment\CommentStatisticsInterface
39    */
40   protected $commentStatistics;
41
42   /**
43    * Counts calls to fetchAssoc().
44    *
45    * @var int
46    */
47   protected $calls_to_fetch;
48
49   /**
50    * Sets up required mocks and the CommentStatistics service under test.
51    */
52   protected function setUp() {
53     $this->statement = $this->getMockBuilder('Drupal\Core\Database\Driver\sqlite\Statement')
54       ->disableOriginalConstructor()
55       ->getMock();
56
57     $this->statement->expects($this->any())
58       ->method('fetchObject')
59       ->will($this->returnCallback([$this, 'fetchObjectCallback']));
60
61     $this->select = $this->getMockBuilder('Drupal\Core\Database\Query\Select')
62       ->disableOriginalConstructor()
63       ->getMock();
64
65     $this->select->expects($this->any())
66       ->method('fields')
67       ->will($this->returnSelf());
68
69     $this->select->expects($this->any())
70       ->method('condition')
71       ->will($this->returnSelf());
72
73     $this->select->expects($this->any())
74       ->method('execute')
75       ->will($this->returnValue($this->statement));
76
77     $this->database = $this->getMockBuilder('Drupal\Core\Database\Connection')
78       ->disableOriginalConstructor()
79       ->getMock();
80
81     $this->database->expects($this->once())
82       ->method('select')
83       ->will($this->returnValue($this->select));
84
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'));
86   }
87
88   /**
89    * Tests the read method.
90    *
91    * @see \Drupal\comment\CommentStatistics::read()
92    *
93    * @group Drupal
94    * @group Comment
95    */
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']);
100   }
101
102   /**
103    * Return value callback for fetchObject() function on mocked object.
104    *
105    * @return bool|string
106    *   'Something' on first, 'something-else' on second and FALSE for the
107    *   other calls to function.
108    */
109   public function fetchObjectCallback() {
110     $this->calls_to_fetch++;
111     switch ($this->calls_to_fetch) {
112       case 1:
113         return 'something';
114
115       case 2:
116         return 'something-else';
117
118       default:
119         return FALSE;
120     }
121   }
122
123 }