-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
128 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |