Skip to content

Commit

Permalink
Rename ConnectionAttributes as ConnectionDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Mar 9, 2023
1 parent f439105 commit 9a4e28f
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 41 deletions.
6 changes: 3 additions & 3 deletions lib/ActiveRecord/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

namespace ICanBoogie\ActiveRecord;

use ICanBoogie\ActiveRecord\Config\ConnectionAttributes;
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;

final class Config
{
public const DEFAULT_CONNECTION_ID = 'primary';

/**
* @param array{
* connections: array<string, ConnectionAttributes>,
* connections: array<string, ConnectionDefinition>,
* models: array<string, ModelDefinition>,
* } $an_array
*/
Expand All @@ -29,7 +29,7 @@ public static function __set_state(array $an_array): self
}

/**
* @param array<string, ConnectionAttributes> $connections
* @param array<string, ConnectionDefinition> $connections
* @param array<string, ModelDefinition> $models
*/
public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ICanBoogie\ActiveRecord\Config;

final class ConnectionAttributes
final class ConnectionDefinition
{
public const DEFAULT_CHARSET_AND_COLLATE = "utf8/general_ci";

Expand Down
10 changes: 5 additions & 5 deletions lib/ActiveRecord/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use ICanBoogie\ActiveRecord\Config\Association;
use ICanBoogie\ActiveRecord\Config\AssociationBuilder;
use ICanBoogie\ActiveRecord\Config\BelongsToAssociation;
use ICanBoogie\ActiveRecord\Config\ConnectionAttributes;
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
use ICanBoogie\ActiveRecord\Config\HasManyAssociation;
use ICanBoogie\ActiveRecord\Config\InvalidConfig;
use ICanBoogie\ActiveRecord\Config\TransientAssociation;
Expand All @@ -39,7 +39,7 @@ final class ConfigBuilder
private const REGEXP_TIMEZONE = '/^[-+]\d{2}:\d{2}$/';

/**
* @var array<string, ConnectionAttributes>
* @var array<string, ConnectionDefinition>
*/
private array $connections = [];

Expand Down Expand Up @@ -224,12 +224,12 @@ public function add_connection(
string|null $username = null,
string|null $password = null,
string|null $table_name_prefix = null,
string $charset_and_collate = ConnectionAttributes::DEFAULT_CHARSET_AND_COLLATE,
string $time_zone = ConnectionAttributes::DEFAULT_TIMEZONE,
string $charset_and_collate = ConnectionDefinition::DEFAULT_CHARSET_AND_COLLATE,
string $time_zone = ConnectionDefinition::DEFAULT_TIMEZONE,
): self {
$this->assert_time_zone($time_zone);

$this->connections[$id] = new ConnectionAttributes(
$this->connections[$id] = new ConnectionDefinition(
id: $id,
dsn: $dsn,
username: $username,
Expand Down
18 changes: 9 additions & 9 deletions lib/ActiveRecord/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace ICanBoogie\ActiveRecord;

use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\ActiveRecord\Config\ConnectionAttributes;
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
use PDO;
use PDOException;
use Throwable;
Expand Down Expand Up @@ -97,27 +97,27 @@ private function lazy_get_driver(): Driver
* @link http://www.php.net/manual/en/pdo.construct.php
* @link http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
*/
public function __construct(ConnectionAttributes $attributes)
public function __construct(ConnectionDefinition $definition)
{
unset($this->driver); // to trigger lazy loading

$this->id = $attributes->id;
$dsn = $attributes->dsn;
$this->id = $definition->id;
$dsn = $definition->dsn;

$this->table_name_prefix = $attributes->table_name_prefix
? $attributes->table_name_prefix . '_'
$this->table_name_prefix = $definition->table_name_prefix
? $definition->table_name_prefix . '_'
: '';

[ $this->charset, $this->collate ] = extract_charset_and_collate(
$attributes->charset_and_collate ?? $attributes::DEFAULT_CHARSET_AND_COLLATE
$definition->charset_and_collate ?? $definition::DEFAULT_CHARSET_AND_COLLATE
);

$this->timezone = $attributes->time_zone;
$this->timezone = $definition->time_zone;
$this->driver_name = $this->resolve_driver_name($dsn);

$options = $this->make_options();

$this->pdo = new PDO($dsn, $attributes->username, $attributes->password, $options);
$this->pdo = new PDO($dsn, $definition->username, $definition->password, $options);

$this->after_connection();
}
Expand Down
12 changes: 6 additions & 6 deletions lib/ActiveRecord/ConnectionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use ArrayAccess;
use ArrayIterator;
use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\ActiveRecord\Config\ConnectionAttributes;
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
use InvalidArgumentException;
use IteratorAggregate;
use PDOException;
Expand All @@ -25,7 +25,7 @@
/**
* Connection collection.
*
* @property-read array<string, ConnectionAttributes> $definitions Connection definitions.
* @property-read array<string, ConnectionDefinition> $definitions Connection definitions.
* @property-read Connection[] $established Established connections.
*
* @implements ArrayAccess<string, Connection>
Expand All @@ -42,7 +42,7 @@ class ConnectionCollection implements ArrayAccess, IteratorAggregate, Connection
/**
* Connections definitions.
*
* @var array<string, ConnectionAttributes>
* @var array<string, ConnectionDefinition>
*/
private array $attributes;

Expand Down Expand Up @@ -71,7 +71,7 @@ private function get_established(): array
}

/**
* @param array<string, ConnectionAttributes> $attributes_by_id
* @param array<string, ConnectionDefinition> $attributes_by_id
* Where _key_ is a connection identifier.
*/
public function __construct(array $attributes_by_id)
Expand Down Expand Up @@ -111,8 +111,8 @@ public function offsetSet(mixed $offset, mixed $value): void
throw new ConnectionAlreadyEstablished($offset);
}

if (!$value instanceof ConnectionAttributes) {
$expected = ConnectionAttributes::class;
if (!$value instanceof ConnectionDefinition) {
$expected = ConnectionDefinition::class;
$actual = get_debug_type($value);
throw new InvalidArgumentException("Expected '$expected' got '$actual'");
}
Expand Down
14 changes: 7 additions & 7 deletions tests/ActiveRecord/ConnectionCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace ICanBoogie\ActiveRecord;

use ICanBoogie\ActiveRecord\Config\ConnectionAttributes;
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
use PHPUnit\Framework\TestCase;

use function uniqid;
Expand All @@ -23,8 +23,8 @@ final class ConnectionCollectionTest extends TestCase
protected function setUp(): void
{
$this->connections = new ConnectionCollection([
'one' => new ConnectionAttributes(id: 'one', dsn: 'sqlite::memory:'),
'bad' => new ConnectionAttributes(id: 'bad', dsn: 'mysql:dbname=bad_database' . uniqid()),
'one' => new ConnectionDefinition(id: 'one', dsn: 'sqlite::memory:'),
'bad' => new ConnectionDefinition(id: 'bad', dsn: 'mysql:dbname=bad_database' . uniqid()),
]);
}

Expand Down Expand Up @@ -71,7 +71,7 @@ public function test_should_throw_an_exception_on_getting_undefined_connection()

public function test_should_set_connection_while_it_is_not_established(): void
{
$this->connections['two'] = new ConnectionAttributes('two', 'sqlite::memory:');
$this->connections['two'] = new ConnectionDefinition('two', 'sqlite::memory:');

$this->assertInstanceOf(Connection::class, $this->connections['two']);
}
Expand All @@ -87,7 +87,7 @@ public function test_should_throw_an_exception_on_setting_invalid_connection():

public function test_should_unset_connection_definition(): void
{
$this->connections['two'] = new ConnectionAttributes('two', 'sqlite::memory:');
$this->connections['two'] = new ConnectionDefinition('two', 'sqlite::memory:');

unset($this->connections['two']);

Expand Down Expand Up @@ -120,8 +120,8 @@ public function test_get_established(): void
{
$connections = new ConnectionCollection([

'one' => new ConnectionAttributes('one', 'sqlite::memory:'),
'two' => new ConnectionAttributes('two', 'sqlite::memory:'),
'one' => new ConnectionDefinition('one', 'sqlite::memory:'),
'two' => new ConnectionDefinition('two', 'sqlite::memory:'),

]);

Expand Down
4 changes: 2 additions & 2 deletions tests/ActiveRecord/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ICanBoogie\ActiveRecord;

use ICanBoogie\ActiveRecord\Config\ConnectionAttributes;
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
use PHPUnit\Framework\TestCase;

final class ConnectionTest extends TestCase
Expand All @@ -15,7 +15,7 @@ protected function setUp(): void
$this->id = 'db' . uniqid();

$this->connection = new Connection(
new ConnectionAttributes(
new ConnectionDefinition(
id: $this->id,
dsn:'sqlite::memory:',
charset_and_collate: 'ascii/bin',
Expand Down
8 changes: 4 additions & 4 deletions tests/ActiveRecord/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace ICanBoogie\ActiveRecord;

use ICanBoogie\ActiveRecord\Config\ConnectionAttributes;
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
use ICanBoogie\ActiveRecord\Driver\MySQLDriver;
use ICanBoogie\ActiveRecord\Driver\SQLiteDriver;
use PHPUnit\Framework\TestCase;
Expand All @@ -32,19 +32,19 @@ public function test_create_table_and_indexes(
array $expected,
): void {
$connection = new class (
new ConnectionAttributes('', 'sqlite::memory:'),
new ConnectionDefinition('', 'sqlite::memory:'),
$this,
$expected,
) extends Connection {
/**
* @param array<string> $expected
*/
public function __construct(
ConnectionAttributes $attributes,
ConnectionDefinition $definition,
private readonly TestCase $test,
private readonly array $expected,
) {
parent::__construct($attributes);
parent::__construct($definition);
}

private int $i = 0;
Expand Down
4 changes: 2 additions & 2 deletions tests/ActiveRecord/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace ICanBoogie\ActiveRecord;

use ICanBoogie\ActiveRecord\Config\ConnectionAttributes;
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
use PHPUnit\Framework\TestCase;

final class StatementTest extends TestCase
Expand All @@ -20,7 +20,7 @@ final class StatementTest extends TestCase

public static function setupBeforeClass(): void
{
self::$connection = $connection = new Connection(new ConnectionAttributes('id', 'sqlite::memory:'));
self::$connection = $connection = new Connection(new ConnectionDefinition('id', 'sqlite::memory:'));

$connection('CREATE TABLE test (a INTEGER PRIMARY KEY ASC, b INTEGER, c VARCHAR(20))');
$connection('INSERT INTO test (b,c) VALUES(1, "one")');
Expand Down
4 changes: 2 additions & 2 deletions tests/ActiveRecord/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace ICanBoogie\ActiveRecord;

use ICanBoogie\ActiveRecord\Config\ConnectionAttributes;
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;

Expand All @@ -25,7 +25,7 @@ final class TableTest extends TestCase
public static function setUpBeforeClass(): void
{
self::$connection = $connection = new Connection(
new ConnectionAttributes(
new ConnectionDefinition(
id: '',
dsn: 'sqlite::memory:',
table_name_prefix: 'prefix'
Expand Down

0 comments on commit 9a4e28f

Please sign in to comment.