Skip to content

Commit

Permalink
test(database): add seperate initializer for FrameworkIntegrationTest…
Browse files Browse the repository at this point in the history
…Case
  • Loading branch information
blackshadev committed Jan 17, 2025
1 parent b0381eb commit 7f1ba39
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
35 changes: 35 additions & 0 deletions src/Tempest/Database/src/CachedConnectionInitializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Tempest\Database;

use Tempest\Container\Container;
use Tempest\Container\Initializer;
use Tempest\Container\Singleton;

/**
* Reuses the same connection instance based on a static variable instead of the container.
*
* Used in testing where each test can have its own container instance.
*/
final class CachedConnectionInitializer implements Initializer
{
private static Connection|null $instance = null;

public function __construct(private readonly ConnectionInitializer $initializer)
{
}

#[Singleton]
public function initialize(Container $container): Connection
{
if (self::$instance !== null) {
return self::$instance;
}

self::$instance = $this->initializer->initialize($container);

return self::$instance;
}
}
10 changes: 0 additions & 10 deletions src/Tempest/Database/src/ConnectionInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,17 @@
use Tempest\Container\Container;
use Tempest\Container\Initializer;
use Tempest\Container\Singleton;
use Tempest\Core\AppConfig;

final class ConnectionInitializer implements Initializer
{
private static Connection|null $instance = null;

#[Singleton]
public function initialize(Container $container): Connection
{
// Reuse same connection instance in unit tests
if (self::$instance !== null && $container->get(AppConfig::class)->environment->isTesting()) {
return self::$instance;
}

$databaseConfig = $container->get(DatabaseConfig::class);

$connection = new PDOConnection($databaseConfig->connection());
$connection->connect();

self::$instance = $connection;

return $connection;
}
}
2 changes: 2 additions & 0 deletions tests/Integration/FrameworkIntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Tempest\Core\Application;
use Tempest\Core\ShellExecutor;
use Tempest\Core\ShellExecutors\NullShellExecutor;
use Tempest\Database\CachedConnectionInitializer;
use Tempest\Database\DatabaseConfig;
use Tempest\Database\Migrations\MigrationManager;
use Tempest\Discovery\DiscoveryLocation;
Expand Down Expand Up @@ -45,6 +46,7 @@ protected function setUp(): void
$this->console = new ConsoleTester($this->container);

// Database
$this->container->addInitializer(CachedConnectionInitializer::class);
$databaseConfigPath = __DIR__ . '/../Fixtures/Config/database.config.php';

if (! file_exists($databaseConfigPath)) {
Expand Down

0 comments on commit 7f1ba39

Please sign in to comment.