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