class CommentStatisticsUnitTest

Same name in other branches
  1. 9 core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php \Drupal\Tests\comment\Unit\CommentStatisticsUnitTest
  2. 8.9.x core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php \Drupal\Tests\comment\Unit\CommentStatisticsUnitTest
  3. 10 core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php \Drupal\Tests\comment\Unit\CommentStatisticsUnitTest

@coversDefaultClass \Drupal\comment\CommentStatistics @group comment

Hierarchy

Expanded class hierarchy of CommentStatisticsUnitTest

File

core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php, line 16

Namespace

Drupal\Tests\comment\Unit
View source
class CommentStatisticsUnitTest extends UnitTestCase {
    
    /**
     * Mock statement.
     *
     * @var \Drupal\Core\Database\StatementInterface
     */
    protected $statement;
    
    /**
     * Mock select interface.
     *
     * @var \Drupal\Core\Database\Query\SelectInterface
     */
    protected $select;
    
    /**
     * Mock database connection.
     *
     * @var \Drupal\Core\Database\Connection
     */
    protected $database;
    
    /**
     * CommentStatistics service under test.
     *
     * @var \Drupal\comment\CommentStatisticsInterface
     */
    protected $commentStatistics;
    
    /**
     * Counts calls to fetchAssoc().
     *
     * @var int
     */
    protected $callsToFetch;
    
    /**
     * Sets up required mocks and the CommentStatistics service under test.
     */
    protected function setUp() : void {
        parent::setUp();
        $this->statement = $this->getMockBuilder('Drupal\\sqlite\\Driver\\Database\\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')
            ->willReturnSelf();
        $this->select
            ->expects($this->any())
            ->method('condition')
            ->willReturnSelf();
        $this->select
            ->expects($this->any())
            ->method('execute')
            ->willReturn($this->statement);
        $this->database = $this->getMockBuilder('Drupal\\Core\\Database\\Connection')
            ->disableOriginalConstructor()
            ->getMock();
        $this->database
            ->expects($this->once())
            ->method('select')
            ->willReturn($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->createMock(TimeInterface::class), $this->database);
    }
    
    /**
     * Tests the read method.
     *
     * @see \Drupal\comment\CommentStatistics::read()
     *
     * @group Drupal
     * @group Comment
     */
    public function testRead() : void {
        $this->callsToFetch = 0;
        $results = $this->commentStatistics
            ->read([
            '1' => 'boo',
            '2' => 'foo',
        ], 'snafus');
        $this->assertEquals([
            'something',
            'something-else',
        ], $results);
    }
    
    /**
     * Return value callback for fetchObject() function on mocked object.
     *
     * @return bool|string
     *   'Something' on first, 'something-else' on second and FALSE for the
     *   other calls to function.
     */
    public function fetchObjectCallback() {
        $this->callsToFetch++;
        switch ($this->callsToFetch) {
            case 1:
                return 'something';
            case 2:
                return 'something-else';
            default:
                return FALSE;
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
CommentStatisticsUnitTest::$callsToFetch protected property Counts calls to fetchAssoc().
CommentStatisticsUnitTest::$commentStatistics protected property CommentStatistics service under test.
CommentStatisticsUnitTest::$database protected property Mock database connection.
CommentStatisticsUnitTest::$select protected property Mock select interface.
CommentStatisticsUnitTest::$statement protected property Mock statement.
CommentStatisticsUnitTest::fetchObjectCallback public function Return value callback for fetchObject() function on mocked object.
CommentStatisticsUnitTest::setUp protected function Sets up required mocks and the CommentStatistics service under test. Overrides UnitTestCase::setUp
CommentStatisticsUnitTest::testRead public function Tests the read method.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.