Skip to content

Commit

Permalink
feat: LDP-1689: Add database connection trait. Update CI to php8.1. (#24
Browse files Browse the repository at this point in the history
)

* 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.
  • Loading branch information
StavoveiC authored Oct 11, 2022
1 parent d0a3c5b commit 270b2bf
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/PHPUnit-CodeStyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)\""
Expand Down
14 changes: 9 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -33,5 +32,10 @@
"cs": "phpcs --colors",
"cbf": "phpcbf",
"test": "phpunit"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
45 changes: 45 additions & 0 deletions src/Core/Database/DatabaseConnectionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace drunomics\ServiceUtils\Core\Database;

use Drupal\Core\Database\Connection;

/**
* Allows setter injection and simple usage of the service.
*/
trait DatabaseConnectionTrait {

/**
* The database connection service.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;

/**
* Sets database connection.
*
* @param \Drupal\Core\Database\Connection $connection
* The database connection service.
*
* @return \Drupal\Core\Database\Connection
*/
public function setDatabaseConnection(Connection $connection) {
$this->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;
}

}
73 changes: 73 additions & 0 deletions tests/src/Core/Database/DatabaseConnectionTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace drunomics\ServiceUtils\Tests\Core\Database;

use drunomics\ServiceUtils\Core\Database\DatabaseConnectionTrait;
use Drupal\Core\DependencyInjection\Container;
use Drupal\Core\Database\Connection;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

/**
* @coversDefaultClass \drunomics\ServiceUtils\Core\Database\DatabaseConnectionTrait
* @group ServiceUtils
*/
class DatabaseConnectionTraitTest extends TestCase {

use DatabaseConnectionTrait;
use ProphecyTrait;

/**
* The id of the trait's service.
*
* @var string
*/
protected $serviceId = 'database';

/**
* @covers ::getDatabaseConnection
*/
public function testGetter() {
// Verify the container is used once and the right service is returned.
$service = $this->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;
}

}

0 comments on commit 270b2bf

Please sign in to comment.