class DatabaseEventsTest

Same name in other branches
  1. 10 core/tests/Drupal/Tests/Core/Database/DatabaseEventsTest.php \Drupal\Tests\Core\Database\DatabaseEventsTest

@coversDefaultClass \Drupal\Core\Database\Connection

@group Database

Hierarchy

Expanded class hierarchy of DatabaseEventsTest

File

core/tests/Drupal/Tests/Core/Database/DatabaseEventsTest.php, line 23

Namespace

Drupal\Tests\Core\Database
View source
class DatabaseEventsTest extends UnitTestCase {
    
    /**
     * A database connection.
     */
    protected Connection $connection;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->connection = new StubConnection($this->createMock(StubPDO::class), []);
    }
    
    /**
     * @covers ::isEventEnabled
     * @covers ::enableEvents
     * @covers ::disableEvents
     */
    public function testEventEnablingAndDisabling() : void {
        $this->connection
            ->enableEvents(StatementEvent::all());
        $this->assertTrue($this->connection
            ->isEventEnabled(StatementExecutionStartEvent::class));
        $this->assertTrue($this->connection
            ->isEventEnabled(StatementExecutionEndEvent::class));
        $this->assertTrue($this->connection
            ->isEventEnabled(StatementExecutionFailureEvent::class));
        $this->connection
            ->disableEvents([
            StatementExecutionEndEvent::class,
        ]);
        $this->assertTrue($this->connection
            ->isEventEnabled(StatementExecutionStartEvent::class));
        $this->assertFalse($this->connection
            ->isEventEnabled(StatementExecutionEndEvent::class));
        $this->assertTrue($this->connection
            ->isEventEnabled(StatementExecutionFailureEvent::class));
        $this->connection
            ->disableEvents(StatementEvent::all());
        $this->assertFalse($this->connection
            ->isEventEnabled(StatementExecutionStartEvent::class));
        $this->assertFalse($this->connection
            ->isEventEnabled(StatementExecutionEndEvent::class));
        $this->assertFalse($this->connection
            ->isEventEnabled(StatementExecutionFailureEvent::class));
    }
    
    /**
     * @covers ::enableEvents
     */
    public function testEnableInvalidEvent() : void {
        $this->expectException(\AssertionError::class);
        $this->expectExceptionMessage('Event class foo does not exist');
        $this->connection
            ->enableEvents([
            'foo',
        ]);
    }
    
    /**
     * @covers ::disableEvents
     */
    public function testDisableInvalidEvent() : void {
        $this->expectException(\AssertionError::class);
        $this->expectExceptionMessage('Event class bar does not exist');
        $this->connection
            ->disableEvents([
            'bar',
        ]);
    }
    
    /**
     * @covers ::dispatchEvent
     */
    public function testEventDispatchingWhenNoContainerAvailable() : void {
        $this->expectException(EventException::class);
        $this->expectExceptionMessage('The event dispatcher service is not available. Database API events can only be fired if the container is initialized');
        $this->connection
            ->dispatchEvent($this->createMock(DatabaseEvent::class));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
DatabaseEventsTest::$connection protected property A database connection.
DatabaseEventsTest::setUp protected function Overrides UnitTestCase::setUp
DatabaseEventsTest::testDisableInvalidEvent public function @covers ::disableEvents
DatabaseEventsTest::testEnableInvalidEvent public function @covers ::enableEvents
DatabaseEventsTest::testEventDispatchingWhenNoContainerAvailable public function @covers ::dispatchEvent
DatabaseEventsTest::testEventEnablingAndDisabling public function @covers ::isEventEnabled
@covers ::enableEvents
@covers ::disableEvents
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.