Skip to content

Commit

Permalink
feat: LDP-1689: Add database connection trait. (#23)
Browse files Browse the repository at this point in the history
* feat: LDP-1689: Add database connection trait.

* LDP-1689: Fix phpcs errors.

* test: LDP-1689: Add tests for database connection trait.

* LDP-1689: Improve test for watchdog errors.
  • Loading branch information
StavoveiC authored Oct 6, 2022
1 parent d565b1b commit 48900cd
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
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 48900cd

Please sign in to comment.