Skip to content

Commit

Permalink
Harmonize StaticConnectionProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Nov 2, 2024
1 parent 9f7a379 commit e8ce5ce
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
25 changes: 8 additions & 17 deletions lib/ActiveRecord/StaticConnectionProvider.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
<?php

/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ICanBoogie\ActiveRecord;

use Closure;
use LogicException;

/**
* Provides a {@link Connection} instance.
* Provides a {@see Connection} instance.
*/
final class StaticConnectionProvider
{
Expand All @@ -26,15 +17,15 @@ final class StaticConnectionProvider
private static ?ConnectionProvider $provider = null;

/**
* Defines the {@link ConnectionProvider} factory.
* Defines the {@see ConnectionProvider} factory.
*
* @param (callable(): ConnectionProvider) $factory
* The factory is invoked once: the first time {@link connection_for_id} is invoked.
*
* @return (callable(): ConnectionProvider)|null
* The previous factory, or `null` if none was defined.
*/
public static function define(callable $factory): ?callable
public static function set(callable $factory): ?callable
{
$previous = self::$factory;

Expand All @@ -45,19 +36,19 @@ public static function define(callable $factory): ?callable
}

/**
* Returns the current {@link ConnectionProvider} factory.
* Returns the current {@see ConnectionProvider} factory.
*
* @return (callable(): ConnectionProvider)|null
*/
public static function defined(): ?callable
public static function get(): ?callable
{
return self::$factory;
}

/**
* Undefines the {@link ConnectionProvider} factory.
* Resets the {@see ConnectionProvider} factory.
*/
public static function undefine(): void
public static function reset(): void
{
self::$factory = null;
self::$provider = null;
Expand All @@ -71,7 +62,7 @@ public static function connection_for_id(string $id): Connection
{
$factory = self::$factory
?? throw new LogicException(
"No factory defined yet. Please define one with `StaticConnectionProvider::define()`"
"No factory defined yet. Please define one with `StaticConnectionProvider::set()`"
);

return (self::$provider ??= $factory())->connection_for_id($id);
Expand Down
37 changes: 37 additions & 0 deletions tests/StaticConnectionProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Test\ICanBoogie;

use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
use ICanBoogie\ActiveRecord\ConnectionCollection;
use ICanBoogie\ActiveRecord\StaticConnectionProvider;
use PHPUnit\Framework\TestCase;

final class StaticConnectionProviderTest extends TestCase
{
public function test_set_unset(): void
{
$factory = fn() => new ConnectionCollection([]);
StaticConnectionProvider::set($factory);
$this->assertNotNull(StaticConnectionProvider::get());

StaticConnectionProvider::reset();
$this->assertNull(StaticConnectionProvider::get());
}

public function test_connection_for_id(): void
{
$id = "foo";
$factory = fn() => new ConnectionCollection([
new ConnectionDefinition(
id: $id,
dsn: "sqlite::memory:"
)
]);
StaticConnectionProvider::set($factory);

$actual = StaticConnectionProvider::connection_for_id($id);

$this->assertEquals($id, $actual->id);
}
}

0 comments on commit e8ce5ce

Please sign in to comment.