CommentStatisticsUnitTest.php in Drupal 9
File
core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php
View source
<?php
namespace Drupal\Tests\comment\Unit;
use Drupal\comment\CommentStatistics;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
class CommentStatisticsUnitTest extends UnitTestCase {
protected $statement;
protected $select;
protected $database;
protected $commentStatistics;
protected $calls_to_fetch;
protected function setUp() : void {
$this->statement = $this
->getMockBuilder('Drupal\\Core\\Database\\Driver\\sqlite\\Statement')
->disableOriginalConstructor()
->getMock();
$this->statement
->expects($this
->any())
->method('fetchObject')
->willReturnCallback([
$this,
'fetchObjectCallback',
]);
$this->select = $this
->getMockBuilder('Drupal\\Core\\Database\\Query\\Select')
->disableOriginalConstructor()
->getMock();
$this->select
->expects($this
->any())
->method('fields')
->will($this
->returnSelf());
$this->select
->expects($this
->any())
->method('condition')
->will($this
->returnSelf());
$this->select
->expects($this
->any())
->method('execute')
->will($this
->returnValue($this->statement));
$this->database = $this
->getMockBuilder('Drupal\\Core\\Database\\Connection')
->disableOriginalConstructor()
->getMock();
$this->database
->expects($this
->once())
->method('select')
->will($this
->returnValue($this->select));
$this->commentStatistics = new CommentStatistics($this->database, $this
->createMock('Drupal\\Core\\Session\\AccountInterface'), $this
->createMock(EntityTypeManagerInterface::class), $this
->createMock('Drupal\\Core\\State\\StateInterface'), $this->database);
}
public function testRead() {
$this->calls_to_fetch = 0;
$results = $this->commentStatistics
->read([
'1' => 'boo',
'2' => 'foo',
], 'snafus');
$this
->assertEquals([
'something',
'something-else',
], $results);
}
public function fetchObjectCallback() {
$this->calls_to_fetch++;
switch ($this->calls_to_fetch) {
case 1:
return 'something';
case 2:
return 'something-else';
default:
return FALSE;
}
}
}