From 270b2bf623d4853f927ae8d230675589650d3080 Mon Sep 17 00:00:00 2001 From: StavoveiC <94967064+StavoveiC@users.noreply.github.com> Date: Tue, 11 Oct 2022 12:01:19 +0300 Subject: [PATCH] feat: LDP-1689: Add database connection trait. Update CI to php8.1. (#24) * feat: LDP-1689: Add database connection trait. * LDP-1689: Resolve composer dependencies so that drupal10 works. * LDP-1689: Update php version. * LDP-1689: Update php version. * LDP-1689: Allow dealerdirect/phpcodesniffer-composer-installer plugin. --- .github/workflows/PHPUnit-CodeStyle.yml | 2 +- composer.json | 14 ++-- src/Core/Database/DatabaseConnectionTrait.php | 45 ++++++++++++ .../Database/DatabaseConnectionTraitTest.php | 73 +++++++++++++++++++ 4 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 src/Core/Database/DatabaseConnectionTrait.php create mode 100644 tests/src/Core/Database/DatabaseConnectionTraitTest.php diff --git a/.github/workflows/PHPUnit-CodeStyle.yml b/.github/workflows/PHPUnit-CodeStyle.yml index b17b756..586f9c4 100644 --- a/.github/workflows/PHPUnit-CodeStyle.yml +++ b/.github/workflows/PHPUnit-CodeStyle.yml @@ -16,7 +16,7 @@ jobs: - uses: actions/checkout@v2 - name: Setup PHP run: | - sudo update-alternatives --set php /usr/bin/php7.4 + sudo update-alternatives --set php /usr/bin/php8.1 - name: "Determine composer cache directory" id: "determine-composer-cache-directory" run: "echo \"::set-output name=directory::$(composer config cache-dir)\"" diff --git a/composer.json b/composer.json index a877f1a..d93e54b 100644 --- a/composer.json +++ b/composer.json @@ -14,14 +14,13 @@ "autoload": { "psr-4": { "drunomics\\ServiceUtils\\": "src/" } }, - "require": { - "drupal/core": "10.0.*" - }, + "minimum-stability": "dev", "require-dev": { "phpunit/phpunit": "^9.5.0", - "drupal/pathauto": "^1.8", + "drupal/pathauto": "^1.11", "drupal/coder": "^8.3", - "phpspec/prophecy-phpunit": "^2.0" + "phpspec/prophecy-phpunit": "^2.0", + "drupal/core": "^10" }, "repositories": { "0": { @@ -33,5 +32,10 @@ "cs": "phpcs --colors", "cbf": "phpcbf", "test": "phpunit" + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/src/Core/Database/DatabaseConnectionTrait.php b/src/Core/Database/DatabaseConnectionTrait.php new file mode 100644 index 0000000..df29241 --- /dev/null +++ b/src/Core/Database/DatabaseConnectionTrait.php @@ -0,0 +1,45 @@ +connection = $connection; + return $this; + } + + /** + * Gets database connection service. + * + * @return \Drupal\Core\Database\Connection + * The database connection service. + */ + public function getDatabaseConnection() { + if (empty($this->connection)) { + $this->connection = \Drupal::service('database'); + } + return $this->connection; + } + +} diff --git a/tests/src/Core/Database/DatabaseConnectionTraitTest.php b/tests/src/Core/Database/DatabaseConnectionTraitTest.php new file mode 100644 index 0000000..19236b9 --- /dev/null +++ b/tests/src/Core/Database/DatabaseConnectionTraitTest.php @@ -0,0 +1,73 @@ +mockContainerWithFakeService(['calls' => 1]); + $this->assertsame($service, $this->getDatabaseConnection()); + // Multiple calls should fetch the service from the container only once. + $this->getDatabaseConnection(); + } + + /** + * @covers ::setDatabaseConnection + */ + public function testSetter() { + // Verify the set service is returned. + $this->mockContainerWithFakeService(['calls' => 0]); + $service = $this->prophesize() + ->willExtend(Connection::class) + ->reveal(); + $this->setDatabaseConnection($service); + $this->assertsame($service, $this->getDatabaseConnection()); + } + + /** + * Helper to mock the container with a stub service. + * + * @param int[] $options + * An array with the following keys: + * - calls: The number of calls to get the service the mocked container + * expects. + * + * @return object + * The fake service returned by the container. + */ + protected function mockContainerWithFakeService(array $options) { + $service = new \Stdclass(); + $container = $this->prophesize(Container::class); + $prophecy = $container->get($this->serviceId); + /** @var \Prophecy\Prophecy\MethodProphecy $prophecy */ + $prophecy->shouldBeCalledTimes($options['calls']); + $prophecy->willReturn($service); + \Drupal::setContainer($container->reveal()); + return $service; + } + +}