annotate core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php @ 19:fa3358dc1485 tip

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