diff --git a/composer.json b/composer.json index 2f9b561..189dc47 100644 --- a/composer.json +++ b/composer.json @@ -62,7 +62,7 @@ "scripts": { "check": [ "@cs-check", - "@static-analysis", + "@sa", "@test", "@test-integration" ], @@ -71,8 +71,9 @@ "test": "phpunit --colors=always --testsuite \"unit test\"", "test-coverage": "phpunit --colors=always --coverage-clover clover.xml", "test-integration": "phpunit --colors=always --testsuite \"integration test\"", - "static-analysis": "vendor/bin/phpstan analyse --memory-limit=256M", - "sa-generate-baseline": "vendor/bin/phpstan analyse --memory-limit=256M --generate-baseline", + "sa": "vendor/bin/phpstan analyse --memory-limit=256M", + "sa-gen-baseline": "vendor/bin/phpstan analyse --memory-limit=256M --generate-baseline", + "sa-verbose": "vendor/bin/phpstan analyse --memory-limit=256M -vv", "upload-coverage": "coveralls -v" } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index af96370..e975329 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -4,172 +4,281 @@ parameters: message: '#^Method PhpDb\\Adapter\\Profiler\\ProfilerInterface\:\:profilerFinish\(\) invoked with 1 parameter, 0 required\.$#' identifier: arguments.count count: 1 - path: src/Driver/Mysqli/Connection.php + path: src/Connection.php - - message: '#^Property PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Connection\:\:\$driver \(PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Mysqli\) does not accept PhpDb\\Adapter\\Driver\\DriverInterface\.$#' + message: '#^Property PhpDb\\Mysql\\Connection\:\:\$driver \(PhpDb\\Mysql\\Driver\) does not accept PhpDb\\Adapter\\Driver\\DriverInterface\.$#' identifier: assign.propertyType count: 1 - path: src/Driver/Mysqli/Connection.php + path: src/Connection.php - - message: '#^Property PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Connection\:\:\$resource \(mysqli\) does not accept null\.$#' + message: '#^Property PhpDb\\Mysql\\Connection\:\:\$resource \(mysqli\) does not accept null\.$#' identifier: assign.propertyType count: 1 - path: src/Driver/Mysqli/Connection.php + path: src/Connection.php - message: '#^Strict comparison using \!\=\= between null and null will always evaluate to false\.$#' identifier: notIdentical.alwaysFalse count: 1 - path: src/Driver/Mysqli/Connection.php + path: src/Connection.php - message: '#^Variable \$names in PHPDoc tag @var does not match assigned variable \$findParameterValue\.$#' identifier: varTag.differentVariable count: 1 - path: src/Driver/Mysqli/Connection.php + path: src/Connection.php - - message: '#^Instanceof between PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Connection and PhpDb\\Adapter\\Driver\\DriverAwareInterface will always evaluate to true\.$#' + message: '#^PHPDoc tag @var for variable \$connectionInstance contains unknown class PhpDb\\Mysql\\Driver\\ConnectionInterface\.$#' + identifier: class.notFound + count: 1 + path: src/Container/DriverInterfaceFactory.php + + - + message: '#^PHPDoc tag @var for variable \$resultInstance contains unknown class PhpDb\\Mysql\\Driver\\ResultInterface\.$#' + identifier: class.notFound + count: 1 + path: src/Container/DriverInterfaceFactory.php + + - + message: '#^PHPDoc tag @var for variable \$statementInstance contains unknown class PhpDb\\Mysql\\Driver\\StatementInterface\.$#' + identifier: class.notFound + count: 1 + path: src/Container/DriverInterfaceFactory.php + + - + message: '#^Instanceof between PhpDb\\Mysql\\Connection and PhpDb\\Adapter\\Driver\\DriverAwareInterface will always evaluate to true\.$#' identifier: instanceof.alwaysTrue count: 1 - path: src/Driver/Mysqli/Mysqli.php + path: src/Driver.php - - message: '#^Instanceof between PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Connection and PhpDb\\Adapter\\Profiler\\ProfilerAwareInterface will always evaluate to true\.$#' + message: '#^Instanceof between PhpDb\\Mysql\\Connection and PhpDb\\Adapter\\Profiler\\ProfilerAwareInterface will always evaluate to true\.$#' identifier: instanceof.alwaysTrue count: 1 - path: src/Driver/Mysqli/Mysqli.php + path: src/Driver.php - - message: '#^Instanceof between PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Statement and PhpDb\\Adapter\\Driver\\DriverAwareInterface will always evaluate to true\.$#' + message: '#^Instanceof between PhpDb\\Mysql\\Statement and PhpDb\\Adapter\\Driver\\DriverAwareInterface will always evaluate to true\.$#' identifier: instanceof.alwaysTrue count: 1 - path: src/Driver/Mysqli/Mysqli.php + path: src/Driver.php - - message: '#^Instanceof between PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Statement and PhpDb\\Adapter\\Profiler\\ProfilerAwareInterface will always evaluate to true\.$#' + message: '#^Instanceof between PhpDb\\Mysql\\Statement and PhpDb\\Adapter\\Profiler\\ProfilerAwareInterface will always evaluate to true\.$#' identifier: instanceof.alwaysTrue count: 1 - path: src/Driver/Mysqli/Mysqli.php + path: src/Driver.php - - message: '#^Method PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Mysqli\:\:getResultPrototype\(\) should return PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Result but returns PhpDb\\Adapter\\Driver\\ResultInterface\.$#' + message: '#^Method PhpDb\\Mysql\\Driver\:\:getResultPrototype\(\) should return PhpDb\\Mysql\\Result but returns PhpDb\\Adapter\\Driver\\ResultInterface\.$#' identifier: return.type count: 1 - path: src/Driver/Mysqli/Mysqli.php + path: src/Driver.php - - message: '#^Parameter \#1 \$resource \(mysqli\|mysqli_stmt\|PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\mysqli_result\) of method PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Mysqli\:\:createResult\(\) should be compatible with parameter \$resource \(resource\) of method PhpDb\\Adapter\\Driver\\DriverInterface\:\:createResult\(\)$#' + message: '#^Parameter \#1 \$resource \(mysqli\|mysqli_stmt\|PhpDb\\Mysql\\mysqli_result\) of method PhpDb\\Mysql\\Driver\:\:createResult\(\) should be compatible with parameter \$resource \(resource\) of method PhpDb\\Adapter\\Driver\\DriverInterface\:\:createResult\(\)$#' identifier: method.childParameterType count: 1 - path: src/Driver/Mysqli/Mysqli.php + path: src/Driver.php - - message: '#^Parameter \$resource of method PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Mysqli\:\:createResult\(\) has invalid type PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\mysqli_result\.$#' + message: '#^Parameter \$resource of method PhpDb\\Mysql\\Driver\:\:createResult\(\) has invalid type PhpDb\\Mysql\\mysqli_result\.$#' identifier: class.notFound count: 1 - path: src/Driver/Mysqli/Mysqli.php + path: src/Driver.php + + - + message: '#^Parameter \#2 \$array of function implode expects array\, array\\> given\.$#' + identifier: argument.type + count: 2 + path: src/Metadata/Source.php + + - + message: '#^Parameter \#2 \$array of function implode expects array\, array\\> given\.$#' + identifier: argument.type + count: 4 + path: src/Metadata/Source.php + + - + message: '#^Variable \$isFK might not be defined\.$#' + identifier: variable.undefined + count: 1 + path: src/Metadata/Source.php + + - + message: '#^Variable \$name might not be defined\.$#' + identifier: variable.undefined + count: 2 + path: src/Metadata/Source.php + + - + message: '#^Call to function is_string\(\) with string will always evaluate to true\.$#' + identifier: function.alreadyNarrowedType + count: 1 + path: src/Pdo/Connection.php + + - + message: '#^Parameter \#1 \$resource \(PDOStatement\) of method PhpDb\\Mysql\\Pdo\\Driver\:\:createResult\(\) should be compatible with parameter \$resource \(resource\) of method PhpDb\\Adapter\\Driver\\DriverInterface\:\:createResult\(\)$#' + identifier: method.childParameterType + count: 3 + path: src/Pdo/Driver.php - message: '#^Instanceof between mysqli_stmt and mysqli_stmt will always evaluate to true\.$#' identifier: instanceof.alwaysTrue count: 2 - path: src/Driver/Mysqli/Result.php + path: src/Result.php - - message: '#^Method PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Result\:\:initialize\(\) has PhpDb\\Adapter\\Exception\\InvalidArgumentException in PHPDoc @throws tag but it''s not thrown\.$#' + message: '#^Method PhpDb\\Mysql\\Result\:\:initialize\(\) has PhpDb\\Adapter\\Exception\\InvalidArgumentException in PHPDoc @throws tag but it''s not thrown\.$#' identifier: throws.unusedType count: 1 - path: src/Driver/Mysqli/Result.php + path: src/Result.php - - message: '#^Method PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Result\:\:isBuffered\(\) never returns null so it can be removed from the return type\.$#' + message: '#^Method PhpDb\\Mysql\\Result\:\:isBuffered\(\) never returns null so it can be removed from the return type\.$#' identifier: return.unusedType count: 1 - path: src/Driver/Mysqli/Result.php + path: src/Result.php - message: '#^Result of && is always false\.$#' identifier: booleanAnd.alwaysFalse count: 1 - path: src/Driver/Mysqli/Result.php + path: src/Result.php - message: '#^Instanceof between PhpDb\\Adapter\\ParameterContainer and PhpDb\\Adapter\\ParameterContainer will always evaluate to true\.$#' identifier: instanceof.alwaysTrue count: 1 - path: src/Driver/Mysqli/Statement.php + path: src/Statement.php - message: '#^Instanceof between mysqli_stmt and mysqli_stmt will always evaluate to true\.$#' identifier: instanceof.alwaysTrue count: 1 - path: src/Driver/Mysqli/Statement.php + path: src/Statement.php - - message: '#^Method PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Statement\:\:execute\(\) never returns null so it can be removed from the return type\.$#' + message: '#^Method PhpDb\\Mysql\\Statement\:\:execute\(\) never returns null so it can be removed from the return type\.$#' identifier: return.unusedType count: 1 - path: src/Driver/Mysqli/Statement.php + path: src/Statement.php - - message: '#^Method PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Statement\:\:getParameterContainer\(\) never returns null so it can be removed from the return type\.$#' + message: '#^Method PhpDb\\Mysql\\Statement\:\:getParameterContainer\(\) never returns null so it can be removed from the return type\.$#' identifier: return.unusedType count: 1 - path: src/Driver/Mysqli/Statement.php + path: src/Statement.php - - message: '#^Method PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Statement\:\:getSql\(\) never returns null so it can be removed from the return type\.$#' + message: '#^Method PhpDb\\Mysql\\Statement\:\:getSql\(\) never returns null so it can be removed from the return type\.$#' identifier: return.unusedType count: 1 - path: src/Driver/Mysqli/Statement.php + path: src/Statement.php - - message: '#^Property PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Statement\:\:\$driver \(PhpDb\\Adapter\\Mysql\\Driver\\Mysqli\\Mysqli\) does not accept PhpDb\\Adapter\\Driver\\DriverInterface\.$#' + message: '#^Property PhpDb\\Mysql\\Statement\:\:\$driver \(PhpDb\\Mysql\\Driver\) does not accept PhpDb\\Adapter\\Driver\\DriverInterface\.$#' identifier: assign.propertyType count: 1 - path: src/Driver/Mysqli/Statement.php + path: src/Statement.php - - message: '#^Call to function is_string\(\) with string will always evaluate to true\.$#' - identifier: function.alreadyNarrowedType + message: '#^Call to an undefined method PhpDb\\Mysql\\Driver\:\:quoteValue\(\)\.$#' + identifier: method.notFound + count: 2 + path: test/integration/AdapterPlatformTest.php + + - + message: '#^Parameter \#1 \$connection of class PhpDb\\Mysql\\Driver constructor expects PhpDb\\Mysql\\Connection, PDO given\.$#' + identifier: argument.type count: 1 - path: src/Driver/Pdo/Connection.php + path: test/integration/AdapterPlatformTest.php - - message: '#^Parameter \#1 \$resource \(PDOStatement\) of method PhpDb\\Adapter\\Mysql\\Driver\\Pdo\\Pdo\:\:createResult\(\) should be compatible with parameter \$resource \(resource\) of method PhpDb\\Adapter\\Driver\\DriverInterface\:\:createResult\(\)$#' - identifier: method.childParameterType - count: 3 - path: src/Driver/Pdo/Pdo.php + message: '#^Parameter \#1 \$connection of class PhpDb\\Mysql\\Driver constructor expects PhpDb\\Mysql\\Connection, mysqli given\.$#' + identifier: argument.type + count: 1 + path: test/integration/AdapterPlatformTest.php - - message: '#^Parameter \#2 \$array of function implode expects array\, array\\> given\.$#' + message: '#^Parameter \#2 \$statementPrototype of class PhpDb\\Mysql\\Driver constructor expects PhpDb\\Mysql\\Statement, PhpDb\\Adapter\\Driver\\Pdo\\Statement given\.$#' identifier: argument.type - count: 2 - path: src/Metadata/Source/MysqlMetadata.php + count: 1 + path: test/integration/AdapterPlatformTest.php - - message: '#^Parameter \#2 \$array of function implode expects array\, array\\> given\.$#' + message: '#^Property PhpDbIntegrationTest\\Mysql\\Platform\\AdapterPlatformTest\:\:\$adapters \(array\\) does not accept array\\.$#' + identifier: assign.propertyType + count: 1 + path: test/integration/AdapterPlatformTest.php + + - + message: '#^Property PhpDbIntegrationTest\\Mysql\\Platform\\AdapterPlatformTest\:\:\$adapters \(array\\) does not accept array\\.$#' + identifier: assign.propertyType + count: 1 + path: test/integration/AdapterPlatformTest.php + + - + message: '#^Callable PhpDb\\Mysql\\Container\\DriverInterfaceFactory invoked with 1 parameter, 2\-3 required\.$#' + identifier: arguments.count + count: 1 + path: test/integration/Container/DriverInterfaceFactoryTest.php + + - + message: '#^Parameter \#1 \$container of callable PhpDb\\Mysql\\Container\\DriverInterfaceFactory expects Laminas\\ServiceManager\\ServiceManager, Psr\\Container\\ContainerInterface given\.$#' identifier: argument.type - count: 4 - path: src/Metadata/Source/MysqlMetadata.php + count: 1 + path: test/integration/Container/DriverInterfaceFactoryTest.php - - message: '#^Variable \$isFK might not be defined\.$#' - identifier: variable.undefined + message: '#^Callable PhpDb\\Mysql\\Container\\MetadataInterfaceFactory invoked with 1 parameter, 2\-3 required\.$#' + identifier: arguments.count count: 1 - path: src/Metadata/Source/MysqlMetadata.php + path: test/integration/Container/MetadataInterfaceFactoryTest.php - - message: '#^Variable \$name might not be defined\.$#' - identifier: variable.undefined - count: 2 - path: src/Metadata/Source/MysqlMetadata.php + message: '#^Callable PhpDb\\Mysql\\Container\\PdoConnectionInterfaceFactory invoked with 1 parameter, 2\-3 required\.$#' + identifier: arguments.count + count: 1 + path: test/integration/Container/PdoConnectionInterfaceFactoryTest.php + + - + message: '#^Callable PhpDb\\Mysql\\Container\\PdoDriverInterfaceFactory invoked with 1 parameter, 2\-3 required\.$#' + identifier: arguments.count + count: 1 + path: test/integration/Container/PdoDriverInterfaceFactoryTest.php + + - + message: '#^Parameter \#1 \$container of callable PhpDb\\Mysql\\Container\\PdoDriverInterfaceFactory expects Laminas\\ServiceManager\\ServiceManager, Psr\\Container\\ContainerInterface given\.$#' + identifier: argument.type + count: 1 + path: test/integration/Container/PdoDriverInterfaceFactoryTest.php + + - + message: '#^Callable PhpDb\\Mysql\\Container\\PdoStatementFactory invoked with 1 parameter, 2\-3 required\.$#' + identifier: arguments.count + count: 1 + path: test/integration/Container/PdoStatementFactoryTest.php + + - + message: '#^Callable PhpDb\\Mysql\\Container\\PlatformInterfaceFactory invoked with 1 parameter, 2\-3 required\.$#' + identifier: arguments.count + count: 1 + path: test/integration/Container/PlatformInterfaceFactoryTest.php + + - + message: '#^Callable PhpDb\\Mysql\\Container\\StatementInterfaceFactory invoked with 1 parameter, 2\-3 required\.$#' + identifier: arguments.count + count: 1 + path: test/integration/Container/StatementInterfaceFactoryTest.php - message: '#^Call to static method PHPUnit\\Framework\\Assert\:\:assertIsInt\(\) with int will always evaluate to true\.$#' identifier: staticMethod.alreadyNarrowedType count: 1 - path: test/integration/Driver/Pdo/ConnectionTest.php + path: test/integration/Pdo/ConnectionTest.php + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index dc7fc0e..b218937 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -11,7 +11,7 @@ failOnWarning="true"> - + diff --git a/src/Connection.php b/src/Connection.php index fd18080..1cd009f 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -5,6 +5,7 @@ namespace PhpDb\Mysql; use Exception as GenericException; +use mysqli; use Override; use PhpDb\Adapter\Driver\AbstractConnection; use PhpDb\Adapter\Driver\ConnectionInterface; @@ -27,7 +28,7 @@ class Connection extends AbstractConnection implements DriverAwareInterface { protected Driver $driver; - /** @var \mysqli */ + /** @var mysqli */ protected $resource; /** @@ -36,11 +37,11 @@ class Connection extends AbstractConnection implements DriverAwareInterface * @throws InvalidArgumentException */ public function __construct( - array|\mysqli|null $connectionInfo = null + array|mysqli|null $connectionInfo = null ) { if (is_array($connectionInfo)) { $this->setConnectionParameters($connectionInfo); - } elseif ($connectionInfo instanceof \mysqli) { + } elseif ($connectionInfo instanceof mysqli) { $this->setResource($connectionInfo); } elseif (null !== $connectionInfo) { throw new Exception\InvalidArgumentException( @@ -75,7 +76,7 @@ public function getCurrentSchema(): string|false * * @return $this Provides a fluent interface */ - public function setResource(\mysqli $resource): static + public function setResource(mysqli $resource): static { $this->resource = $resource; @@ -86,7 +87,7 @@ public function setResource(\mysqli $resource): static #[Override] public function connect(): ConnectionInterface { - if ($this->resource instanceof \mysqli) { + if ($this->resource instanceof mysqli) { return $this; } @@ -185,14 +186,14 @@ public function connect(): ConnectionInterface /** @inheritDoc */ public function isConnected(): bool { - return $this->resource instanceof \mysqli; + return $this->resource instanceof mysqli; } /** @inheritDoc */ #[Override] public function disconnect(): ConnectionInterface { - if ($this->resource instanceof \mysqli) { + if ($this->resource instanceof mysqli) { $this->resource->close(); } $this->resource = null; @@ -285,10 +286,10 @@ public function getLastGeneratedValue(?string $name = null): string|int|false|nu * * todo: why do we have this random method here? * - * @return \mysqli + * @return mysqli */ protected function createResource() { - return new \mysqli(); + return new mysqli(); } } diff --git a/src/Container/ConnectionInterfaceFactory.php b/src/Container/ConnectionInterfaceFactory.php index 148a492..3712fef 100644 --- a/src/Container/ConnectionInterfaceFactory.php +++ b/src/Container/ConnectionInterfaceFactory.php @@ -9,6 +9,8 @@ use PhpDb\Mysql\Connection; use Psr\Container\ContainerInterface; +use function is_array; + final class ConnectionInterfaceFactory { public function __invoke( @@ -16,13 +18,14 @@ public function __invoke( string $requestedName, ?array $options = null ): ConnectionInterface&Connection { - if (! is_array($options['connection']) || $options['connection'] === []) { + $conn = $options['connection'] ?? []; + if (! is_array($conn) || $conn === []) { throw new InvalidConnectionParametersException( 'Connection configuration must be an array of parameters passed via $options["connection"]', - $options['connection'] + $conn ); } - return new Connection($options['connection']); + return new Connection($conn); } } diff --git a/src/Container/DriverInterfaceFactory.php b/src/Container/DriverInterfaceFactory.php index 2b4dd2e..ac41816 100644 --- a/src/Container/DriverInterfaceFactory.php +++ b/src/Container/DriverInterfaceFactory.php @@ -25,7 +25,7 @@ public function __invoke( throw ContainerException::forService( Driver::class, self::class, - '$options["connection"] must containe an array of connection configuration.' + '$options["connection"] must contain an array of connection configuration.' ); } diff --git a/src/Container/PdoConnectionInterfaceFactory.php b/src/Container/PdoConnectionInterfaceFactory.php index ed5889a..c9f3d87 100644 --- a/src/Container/PdoConnectionInterfaceFactory.php +++ b/src/Container/PdoConnectionInterfaceFactory.php @@ -5,22 +5,26 @@ namespace PhpDb\Mysql\Container; use PhpDb\Adapter\Driver\PdoConnectionInterface; +use PhpDb\Adapter\Exception\InvalidConnectionParametersException; use PhpDb\Mysql\Pdo\Connection; use Psr\Container\ContainerInterface; +use function is_array; + final class PdoConnectionInterfaceFactory { - public function __invoke(ContainerInterface $container): PdoConnectionInterface&Connection - { - /** @var array $config */ - $config = $container->get('config'); - - /** @var array $dbConfig */ - $dbConfig = $config['db'] ?? []; - - /** @var array $connectionConfig */ - $connectionConfig = $dbConfig['connection'] ?? []; + public function __invoke( + ContainerInterface $container, + string $requestedName, + ?array $options = null + ): PdoConnectionInterface&Connection { + if (! is_array($options['connection']) || $options['connection'] === []) { + throw new InvalidConnectionParametersException( + 'Connection configuration must be an array of parameters passed via $options["connection"]', + $options['connection'] + ); + } - return new Connection($connectionConfig); + return new Connection($options['connection']); } } diff --git a/src/Container/PdoDriverInterfaceFactory.php b/src/Container/PdoDriverInterfaceFactory.php index a52fbd1..1460219 100644 --- a/src/Container/PdoDriverInterfaceFactory.php +++ b/src/Container/PdoDriverInterfaceFactory.php @@ -4,32 +4,51 @@ namespace PhpDb\Mysql\Container; -use PhpDb\Adapter\Driver\PdoConnectionInterface; +use Laminas\ServiceManager\ServiceManager; use PhpDb\Adapter\Driver\Pdo\Result; use PhpDb\Adapter\Driver\Pdo\Statement; +use PhpDb\Adapter\Driver\PdoConnectionInterface; use PhpDb\Adapter\Driver\PdoDriverInterface; use PhpDb\Adapter\Driver\ResultInterface; use PhpDb\Adapter\Driver\StatementInterface; +use PhpDb\Exception\ContainerException; use PhpDb\Mysql\Pdo\Connection; use PhpDb\Mysql\Pdo\Driver; use Psr\Container\ContainerInterface; final class PdoDriverInterfaceFactory { - public function __invoke(ContainerInterface $container): PdoDriverInterface&Driver - { + public function __invoke( + ContainerInterface&ServiceManager $container, + string $requestedName, + ?array $options = null + ): PdoDriverInterface&Driver { + if (! isset($options['connection'])) { + throw ContainerException::forService( + Driver::class, + self::class, + '$options["connection"] must contain an array of connection configuration.' + ); + } /** @var PdoConnectionInterface&Connection $connectionInstance */ - $connectionInstance = $container->get(Connection::class); + $connectionInstance = $container->build(Connection::class, $options); /** @var StatementInterface&Statement $statementInstance */ - $statementInstance = $container->get(Statement::class); + $statementInstance = $container->build( + Statement::class, + $options['options'] ?? [] + ); /** @var ResultInterface&Result $resultInstance */ - $resultInstance = $container->get(Result::class); + $resultInstance = $container->has(ResultInterface::class) + ? $container->get(ResultInterface::class) + : new Result(); + return new Driver( $connectionInstance, $statementInstance, - $resultInstance + $resultInstance, + $options['pdo_features'] ?? [] ); } } diff --git a/src/Container/PdoStatementFactory.php b/src/Container/PdoStatementFactory.php index d9ddb14..190a74f 100644 --- a/src/Container/PdoStatementFactory.php +++ b/src/Container/PdoStatementFactory.php @@ -10,17 +10,11 @@ final class PdoStatementFactory { - public function __invoke(ContainerInterface $container): StatementInterface&Statement - { - /** @var array $config */ - $config = $container->get('config'); - - /** @var array $dbConfig */ - $dbConfig = $config['db'] ?? []; - - /** @var array $options */ - $options = $dbConfig['options'] ?? []; - + public function __invoke( + ContainerInterface $container, + string $requestedName, + ?array $options = null + ): StatementInterface&Statement { return new Statement(options: $options); } } diff --git a/src/Driver.php b/src/Driver.php index 74f86a1..407dbc1 100644 --- a/src/Driver.php +++ b/src/Driver.php @@ -4,6 +4,7 @@ namespace PhpDb\Mysql; +use mysqli; use mysqli_stmt; use PhpDb\Adapter\Driver\ConnectionInterface; use PhpDb\Adapter\Driver\DriverAwareInterface; @@ -96,7 +97,7 @@ public function getConnection(): ConnectionInterface&Connection /** * Create statement * - * @param \mysqli|mysqli_stmt|string $sqlOrResource + * @param mysqli|mysqli_stmt|string $sqlOrResource */ public function createStatement($sqlOrResource = null): StatementInterface&Statement { @@ -117,7 +118,7 @@ public function createStatement($sqlOrResource = null): StatementInterface&State if (! $this->connection->isConnected()) { $this->connection->connect(); } - /** @var \mysqli $resource */ + /** @var mysqli $resource */ $resource = $this->connection->getResource(); $statement->initialize($resource); } @@ -127,7 +128,7 @@ public function createStatement($sqlOrResource = null): StatementInterface&State /** * Create result * - * @param \mysqli|mysqli_result|mysqli_stmt $resource + * @param mysqli|mysqli_result|mysqli_stmt $resource */ public function createResult($resource, ?bool $isBuffered = null): ResultInterface&Result { diff --git a/src/Pdo/Pdo.php b/src/Pdo/Driver.php similarity index 100% rename from src/Pdo/Pdo.php rename to src/Pdo/Driver.php diff --git a/src/Statement.php b/src/Statement.php index ed51854..24c63b1 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -4,6 +4,7 @@ namespace PhpDb\Mysql; +use mysqli; use mysqli_stmt; use Override; use PhpDb\Adapter\Driver\DriverAwareInterface; @@ -22,7 +23,7 @@ final class Statement implements StatementInterface, DriverAwareInterface, ProfilerAwareInterface { - protected \mysqli $mysqli; + protected mysqli $mysqli; protected Driver $driver; @@ -59,7 +60,7 @@ public function getProfiler(): ?ProfilerInterface return $this->profiler; } - public function initialize(\mysqli $mysqli): static + public function initialize(mysqli $mysqli): static { $this->mysqli = $mysqli; return $this; diff --git a/test/integration/Platform/MysqlTest.php b/test/integration/AdapterPlatformTest.php similarity index 63% rename from test/integration/Platform/MysqlTest.php rename to test/integration/AdapterPlatformTest.php index d79aba7..8c28d22 100644 --- a/test/integration/Platform/MysqlTest.php +++ b/test/integration/AdapterPlatformTest.php @@ -2,14 +2,18 @@ declare(strict_types=1); -namespace PhpDbIntegrationTest\Adapter\Mysql\Platform; +namespace PhpDbIntegrationTest\Mysql\Platform; +use mysqli; use Override; use PhpDb\Adapter\Driver\Pdo; -use PhpDb\Adapter\Mysql\Driver\Mysqli; -use PhpDb\Adapter\Mysql\Driver\Pdo\Connection; -use PhpDb\Adapter\Mysql\Driver\Pdo\Pdo as PdoDriver; -use PhpDb\Adapter\Mysql\Platform\Mysql; +use PhpDb\Mysql\AdapterPlatform; +use PhpDb\Mysql\Connection; +use PhpDb\Mysql\Driver; +use PhpDb\Mysql\Pdo\Connection as PdoConnection; +use PhpDb\Mysql\Pdo\Driver as PdoDriver; +use PhpDb\Mysql\Result; +use PhpDb\Mysql\Statement; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; @@ -18,11 +22,11 @@ use function getenv; #[Group('integration')] -#[CoversMethod(Mysqli\Mysqli::class, 'quoteValue')] +#[CoversMethod(Driver::class, 'quoteValue')] #[CoversMethod(PdoDriver::class, 'quoteValue')] -final class MysqlTest extends TestCase +final class AdapterPlatformTest extends TestCase { - /** @var array */ + /** @var array */ public array $adapters = []; protected array $mysqliParams; @@ -42,12 +46,7 @@ protected function setUp(): void 'database' => getenv('TESTS_PHPDB_ADAPTER_MYSQL_DATABASE'), ]; - $this->adapters['mysqli'] = new \mysqli( - $this->mysqliParams['hostname'], - $this->mysqliParams['username'], - $this->mysqliParams['password'], - $this->mysqliParams['database'] - ); + $this->adapters['mysqli'] = new Connection($this->mysqliParams); } if (extension_loaded('pdo')) { @@ -58,12 +57,7 @@ protected function setUp(): void 'database' => getenv('TESTS_PHPDB_ADAPTER_MYSQL_DATABASE'), ]; - $this->adapters['pdo_mysql'] = new \PDO( - 'mysql:host=' . $this->pdoParams['hostname'] . ';dbname=' - . $this->pdoParams['database'], - $this->pdoParams['username'], - $this->pdoParams['password'] - ); + $this->adapters['pdo_mysql'] = new PdoConnection($this->pdoParams); } } @@ -72,18 +66,22 @@ protected function setUp(): void */ public function testQuoteValueWithMysqli() { - if (! $this->adapters['mysqli'] instanceof \Mysqli) { + if (! $this->adapters['mysqli'] instanceof Mysqli) { $this->markTestSkipped('MySQL (Mysqli) not configured in unit test configuration file'); } - $mysql = new Mysql($this->adapters['mysqli']); + $mysql = new Driver( + $this->adapters['mysqli'], + new Statement(), + new Result() + ); $value = $mysql->quoteValue('value'); self::assertEquals('\'value\'', $value); - $mysql = new Mysql( - new Mysqli\Mysqli( - new Mysqli\Connection($this->mysqliParams), - new Mysqli\Statement(), - new Mysqli\Result() + $mysql = new AdapterPlatform( + new Driver( + new Connection($this->mysqliParams), + new Statement(), + new Result() ) ); $value = $mysql->quoteValue('value'); @@ -98,13 +96,17 @@ public function testQuoteValueWithPdoMysql() if (! $this->adapters['pdo_mysql'] instanceof \PDO) { $this->markTestSkipped('MySQL (PDO_Mysql) not configured in unit test configuration file'); } - $mysql = new Mysql($this->adapters['pdo_mysql']); + $mysql = new Driver( + $this->adapters['pdo_mysql'], + new Pdo\Statement(), + new Pdo\Result() + ); $value = $mysql->quoteValue('value'); self::assertEquals('\'value\'', $value); - $mysql = new Mysql( + $mysql = new AdapterPlatform( new PdoDriver( - new Connection($this->pdoParams), + new PdoConnection($this->pdoParams), new Pdo\Statement(), new Pdo\Result() ) diff --git a/test/integration/Driver/Mysqli/ConnectionTest.php b/test/integration/ConnectionTest.php similarity index 82% rename from test/integration/Driver/Mysqli/ConnectionTest.php rename to test/integration/ConnectionTest.php index 7d679b3..4318dd4 100644 --- a/test/integration/Driver/Mysqli/ConnectionTest.php +++ b/test/integration/ConnectionTest.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace PhpDbIntegrationTest\Adapter\Mysql\Driver\Mysqli; +namespace PhpDbIntegrationTest\Mysql; -use PhpDb\Adapter\Mysql\Driver\Mysqli\Connection; -use PhpDbIntegrationTest\Adapter\Mysql\Container\TestAsset\SetupTrait; +use PhpDb\Mysql\Connection; +use PhpDbIntegrationTest\Mysql\Container\TestAsset\SetupTrait; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; diff --git a/test/integration/Container/MysqliConnectionFactoryTest.php b/test/integration/Container/ConnectionInterfaceFactoryTest.php similarity index 63% rename from test/integration/Container/MysqliConnectionFactoryTest.php rename to test/integration/Container/ConnectionInterfaceFactoryTest.php index ba5ad21..034623f 100644 --- a/test/integration/Container/MysqliConnectionFactoryTest.php +++ b/test/integration/Container/ConnectionInterfaceFactoryTest.php @@ -2,20 +2,20 @@ declare(strict_types=1); -namespace PhpDbIntegrationTest\Adapter\Mysql\Container; +namespace PhpDbIntegrationTest\Mysql\Container; use PhpDb\Adapter\Driver\ConnectionInterface; -use PhpDb\Adapter\Mysql\Container\MysqliConnectionFactory; -use PhpDb\Adapter\Mysql\Driver\Mysqli\Connection; +use PhpDb\Mysql\Connection; +use PhpDb\Mysql\Container\ConnectionInterfaceFactory; use PHPUnit\Framework\Attributes; use PHPUnit\Framework\TestCase; -#[Attributes\CoversClass(MysqliConnectionFactory::class)] -#[Attributes\CoversMethod(MysqliConnectionFactory::class, '__invoke')] +#[Attributes\CoversClass(ConnectionInterfaceFactory::class)] +#[Attributes\CoversMethod(ConnectionInterfaceFactory::class, '__invoke')] #[Attributes\Group('container')] #[Attributes\Group('integration')] #[Attributes\Group('integration-mysqli')] -final class MysqliConnectionFactoryTest extends TestCase +final class ConnectionInterfaceFactoryTest extends TestCase { use TestAsset\SetupTrait; @@ -27,7 +27,7 @@ public function testInvokeReturnsMysqliConnection(): void ], ]); - $factory = new MysqliConnectionFactory(); + $factory = new ConnectionInterfaceFactory(); $connection = $factory($this->container, Connection::class); self::assertInstanceOf(ConnectionInterface::class, $connection); diff --git a/test/integration/Container/MysqliDriverFactoryTest.php b/test/integration/Container/DriverInterfaceFactoryTest.php similarity index 57% rename from test/integration/Container/MysqliDriverFactoryTest.php rename to test/integration/Container/DriverInterfaceFactoryTest.php index 4bcf801..84b9586 100644 --- a/test/integration/Container/MysqliDriverFactoryTest.php +++ b/test/integration/Container/DriverInterfaceFactoryTest.php @@ -2,20 +2,20 @@ declare(strict_types=1); -namespace PhpDbIntegrationTest\Adapter\Mysql\Container; +namespace PhpDbIntegrationTest\Mysql\Container; use PhpDb\Adapter\Driver\DriverInterface; -use PhpDb\Adapter\Mysql\Container\MysqliDriverFactory; -use PhpDb\Adapter\Mysql\Driver\Mysqli\Mysqli; +use PhpDb\Mysql\Container\DriverInterfaceFactory; +use PhpDb\Mysql\Driver; use PHPUnit\Framework\Attributes; use PHPUnit\Framework\TestCase; -#[Attributes\CoversClass(MysqliDriverFactory::class)] -#[Attributes\CoversMethod(MysqliDriverFactory::class, '__invoke')] +#[Attributes\CoversClass(DriverInterfaceFactory::class)] +#[Attributes\CoversMethod(DriverInterfaceFactory::class, '__invoke')] #[Attributes\Group('container')] #[Attributes\Group('integration')] #[Attributes\Group('integration-mysqli')] -final class MysqliDriverFactoryTest extends TestCase +final class DriverInterfaceFactoryTest extends TestCase { use TestAsset\SetupTrait; @@ -26,9 +26,9 @@ public function testFactoryReturnsMysqliDriver(): void 'driver' => 'Mysqli', ], ]); - $factory = new MysqliDriverFactory(); + $factory = new DriverInterfaceFactory(); $driver = $factory($this->container); self::assertInstanceOf(DriverInterface::class, $driver); - $this->assertInstanceOf(Mysqli::class, $driver); + $this->assertInstanceOf(Driver::class, $driver); } } diff --git a/test/integration/Container/MetadataInterfaceFactoryTest.php b/test/integration/Container/MetadataInterfaceFactoryTest.php new file mode 100644 index 0000000..f0af0d1 --- /dev/null +++ b/test/integration/Container/MetadataInterfaceFactoryTest.php @@ -0,0 +1,28 @@ +container); + self::assertInstanceOf(MetadataInterface::class, $metadata); + self::assertInstanceOf(Source::class, $metadata); + } +} diff --git a/test/integration/Container/MysqlMetadataFactoryTest.php b/test/integration/Container/MysqlMetadataFactoryTest.php deleted file mode 100644 index e29858d..0000000 --- a/test/integration/Container/MysqlMetadataFactoryTest.php +++ /dev/null @@ -1,28 +0,0 @@ -container); - self::assertInstanceOf(MetadataInterface::class, $metadata); - self::assertInstanceOf(MysqlMetadata::class, $metadata); - } -} diff --git a/test/integration/Container/MysqliResultFactoryTest.php b/test/integration/Container/MysqliResultFactoryTest.php deleted file mode 100644 index 30ed58a..0000000 --- a/test/integration/Container/MysqliResultFactoryTest.php +++ /dev/null @@ -1,30 +0,0 @@ -container); - - self::assertInstanceOf(ResultInterface::class, $result); - self::assertInstanceOf(Result::class, $result); - } -} diff --git a/test/integration/Container/PdoConnectionFactoryTest.php b/test/integration/Container/PdoConnectionInterfaceFactoryTest.php similarity index 65% rename from test/integration/Container/PdoConnectionFactoryTest.php rename to test/integration/Container/PdoConnectionInterfaceFactoryTest.php index 463e463..8d4a4ab 100644 --- a/test/integration/Container/PdoConnectionFactoryTest.php +++ b/test/integration/Container/PdoConnectionInterfaceFactoryTest.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace PhpDbIntegrationTest\Adapter\Mysql\Container; +namespace PhpDbIntegrationTest\Mysql\Container; use PhpDb\Adapter\Driver\ConnectionInterface; use PhpDb\Adapter\Driver\PdoConnectionInterface; -use PhpDb\Adapter\Mysql\Container\PdoConnectionFactory; -use PhpDb\Adapter\Mysql\Driver\Pdo\Connection; +use PhpDb\Mysql\Container\PdoConnectionInterfaceFactory; +use PhpDb\Mysql\Pdo\Connection; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\Group; @@ -15,15 +15,15 @@ #[Group('container')] #[Group('integration')] -#[CoversClass(PdoConnectionFactory::class)] -#[CoversMethod(PdoConnectionFactory::class, '__invoke')] -final class PdoConnectionFactoryTest extends TestCase +#[CoversClass(PdoConnectionInterfaceFactory::class)] +#[CoversMethod(PdoConnectionInterfaceFactory::class, '__invoke')] +final class PdoConnectionInterfaceFactoryTest extends TestCase { use TestAsset\SetupTrait; public function testInvokeReturnsPdoConnection(): void { - $factory = new PdoConnectionFactory(); + $factory = new PdoConnectionInterfaceFactory(); $instance = $factory($this->container); self::assertInstanceOf(ConnectionInterface::class, $instance); self::assertInstanceOf(PdoConnectionInterface::class, $instance); diff --git a/test/integration/Container/PdoDriverFactoryTest.php b/test/integration/Container/PdoDriverInterfaceFactoryTest.php similarity index 72% rename from test/integration/Container/PdoDriverFactoryTest.php rename to test/integration/Container/PdoDriverInterfaceFactoryTest.php index 2606306..ae8a50c 100644 --- a/test/integration/Container/PdoDriverFactoryTest.php +++ b/test/integration/Container/PdoDriverInterfaceFactoryTest.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace PhpDbIntegrationTest\Adapter\Mysql\Container; +namespace PhpDbIntegrationTest\Mysql\Container; use PhpDb\Adapter\Driver\PdoDriverInterface; -use PhpDb\Adapter\Mysql\Container\PdoDriverInterfaceFactory; -use PhpDb\Adapter\Mysql\Driver\Pdo\Pdo; +use PhpDb\Mysql\Container\PdoDriverInterfaceFactory; +use PhpDb\Mysql\Pdo\Driver; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\Group; @@ -16,7 +16,7 @@ #[Group('integration')] #[CoversClass(PdoDriverInterfaceFactory::class)] #[CoversMethod(PdoDriverInterfaceFactory::class, '__invoke')] -final class PdoDriverFactoryTest extends TestCase +final class PdoDriverInterfaceFactoryTest extends TestCase { use TestAsset\SetupTrait; @@ -26,6 +26,6 @@ public function testInvokeReturnsPdoDriver(): void $instance = $factory($this->container); self::assertInstanceOf(PdoDriverInterface::class, $instance); - self::assertInstanceOf(Pdo::class, $instance); + self::assertInstanceOf(Driver::class, $instance); } } diff --git a/test/integration/Container/PdoResultFactoryTest.php b/test/integration/Container/PdoResultFactoryTest.php deleted file mode 100644 index 81d3177..0000000 --- a/test/integration/Container/PdoResultFactoryTest.php +++ /dev/null @@ -1,31 +0,0 @@ -container); - - self::assertInstanceOf(ResultInterface::class, $result); - self::assertInstanceOf(Result::class, $result); - } -} diff --git a/test/integration/Container/PdoStatementFactoryTest.php b/test/integration/Container/PdoStatementFactoryTest.php index 21e8712..b48edd0 100644 --- a/test/integration/Container/PdoStatementFactoryTest.php +++ b/test/integration/Container/PdoStatementFactoryTest.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace PhpDbIntegrationTest\Adapter\Mysql\Container; +namespace PhpDbIntegrationTest\Mysql\Container; use PhpDb\Adapter\Driver\Pdo\Statement; use PhpDb\Adapter\Driver\StatementInterface; -use PhpDb\Adapter\Mysql\Container\PdoStatementFactory; +use PhpDb\Mysql\Container\PdoStatementFactory; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\Group; diff --git a/test/integration/Container/PlatformInterfaceFactoryTest.php b/test/integration/Container/PlatformInterfaceFactoryTest.php index a5e1cf5..c542239 100644 --- a/test/integration/Container/PlatformInterfaceFactoryTest.php +++ b/test/integration/Container/PlatformInterfaceFactoryTest.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace PhpDbIntegrationTest\Adapter\Mysql\Container; +namespace PhpDbIntegrationTest\Mysql\Container; -use PhpDb\Adapter\Mysql\Container\PlatformInterfaceFactory; -use PhpDb\Adapter\Mysql\Platform\Mysql; use PhpDb\Adapter\Platform\PlatformInterface; +use PhpDb\Mysql\AdapterPlatform; +use PhpDb\Mysql\Container\PlatformInterfaceFactory; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\Group; @@ -25,6 +25,6 @@ public function testInvokeReturnsPlatformInterfaceWhenDbDriverIsPdo(): void $factory = new PlatformInterfaceFactory(); $instance = $factory($this->container); self::assertInstanceOf(PlatformInterface::class, $instance); - self::assertInstanceOf(Mysql::class, $instance); + self::assertInstanceOf(AdapterPlatform::class, $instance); } } diff --git a/test/integration/Container/MysqliStatementFactoryTest.php b/test/integration/Container/StatementInterfaceFactoryTest.php similarity index 65% rename from test/integration/Container/MysqliStatementFactoryTest.php rename to test/integration/Container/StatementInterfaceFactoryTest.php index 419a0c1..a45d009 100644 --- a/test/integration/Container/MysqliStatementFactoryTest.php +++ b/test/integration/Container/StatementInterfaceFactoryTest.php @@ -2,20 +2,20 @@ declare(strict_types=1); -namespace PhpDbIntegrationTest\Adapter\Mysql\Container; +namespace PhpDbIntegrationTest\Mysql\Container; use PhpDb\Adapter\Driver\StatementInterface; -use PhpDb\Adapter\Mysql\Container\MysqliStatementFactory; -use PhpDb\Adapter\Mysql\Driver\Mysqli\Statement; +use PhpDb\Mysql\Container\StatementInterfaceFactory; +use PhpDb\Mysql\Statement; use PHPUnit\Framework\Attributes; use PHPUnit\Framework\TestCase; -#[Attributes\CoversClass(MysqliStatementFactory::class)] -#[Attributes\CoversMethod(MysqliStatementFactory::class, '__invoke')] +#[Attributes\CoversClass(StatementInterfaceFactory::class)] +#[Attributes\CoversMethod(StatementInterfaceFactory::class, '__invoke')] #[Attributes\Group('container')] #[Attributes\Group('integration')] #[Attributes\Group('integration-mysqli')] -final class MysqliStatementFactoryTest extends TestCase +final class StatementInterfaceFactoryTest extends TestCase { use TestAsset\SetupTrait; @@ -30,7 +30,7 @@ public function testInvokeReturnsMysqliStatement(): void ], ]); - $factory = new MysqliStatementFactory(); + $factory = new StatementInterfaceFactory(); $statement = $factory($this->container); self::assertInstanceOf(StatementInterface::class, $statement); diff --git a/test/integration/Container/TestAsset/SetupTrait.php b/test/integration/Container/TestAsset/SetupTrait.php index aff2cf1..a9bb19f 100644 --- a/test/integration/Container/TestAsset/SetupTrait.php +++ b/test/integration/Container/TestAsset/SetupTrait.php @@ -2,15 +2,15 @@ declare(strict_types=1); -namespace PhpDbIntegrationTest\Adapter\Mysql\Container\TestAsset; +namespace PhpDbIntegrationTest\Mysql\Container\TestAsset; use Laminas\ServiceManager\ServiceManager; use Laminas\Stdlib\ArrayUtils; use PhpDb\Adapter\AdapterInterface; use PhpDb\Adapter\Driver\DriverInterface; -use PhpDb\Adapter\Mysql\ConfigProvider; -use PhpDb\Adapter\Mysql\Driver\Pdo\Pdo; use PhpDb\ConfigProvider as LaminasDbConfigProvider; +use PhpDb\Mysql\ConfigProvider; +use PhpDb\Mysql\Pdo\Driver; use Psr\Container\ContainerInterface; use function getenv; @@ -25,7 +25,7 @@ */ trait SetupTrait { - protected array $config = ['db' => []]; + protected array $config = [AdapterInterface::class => []]; protected ?AdapterInterface $adapter; @@ -42,8 +42,8 @@ protected function setUp(): void protected function getAdapter(array $config = []): AdapterInterface { $connectionConfig = [ - 'db' => [ - 'driver' => $this->driver ?? Pdo::class, + AdapterInterface::class => [ + 'driver' => $this->driver ?? Driver::class, 'connection' => [ 'hostname' => (string) getenv('TESTS_PHPDB_ADAPTER_MYSQL_HOSTNAME') ?: 'localhost', 'username' => (string) getenv('TESTS_PHPDB_ADAPTER_MYSQL_USERNAME'), @@ -98,6 +98,6 @@ protected function getConfig(): array protected function getHostname(): string { - return $this->getConfig()['db']['connection']['hostname']; + return $this->getConfig()[AdapterInterface::class]['connection']['hostname']; } } diff --git a/test/integration/Driver/Pdo/AdapterTest.php b/test/integration/Driver/Pdo/AdapterTest.php deleted file mode 100644 index 66b4ffc..0000000 --- a/test/integration/Driver/Pdo/AdapterTest.php +++ /dev/null @@ -1,15 +0,0 @@ -getAdapter([ 'db' => [ - 'driver' => Pdo::class, + 'driver' => Driver::class, ], ]); $adapter->getDriver()->getConnection()->connect(); diff --git a/test/integration/Pdo/AdapterTest.php b/test/integration/Pdo/AdapterTest.php new file mode 100644 index 0000000..53d6774 --- /dev/null +++ b/test/integration/Pdo/AdapterTest.php @@ -0,0 +1,15 @@ +getAdapter(); + $adapter = $this->getAdapter(['db' => ['driver' => Driver::class]]); $tableGateway = new TableGateway('test', $adapter); $this->assertInstanceOf(TableGateway::class, $tableGateway); } public function testSelect(): void { - $tableGateway = new TableGateway('test', $this->getAdapter()); + $tableGateway = new TableGateway('test', $this->getAdapter(['db' => ['driver' => Driver::class]])); /** @var ResultSet $rowset */ $rowset = $tableGateway->select(); $this->assertTrue(count($rowset) > 0); @@ -52,7 +53,7 @@ public function testSelect(): void public function testInsert(): void { - $tableGateway = new TableGateway('test', $this->getAdapter()); + $tableGateway = new TableGateway('test', $this->getAdapter(['db' => ['driver' => Driver::class]])); $tableGateway->select(); $data = [ @@ -77,7 +78,7 @@ public function testInsert(): void */ public function testInsertWithExtendedCharsetFieldName(): int|string { - $tableGateway = new TableGateway('test_charset', $this->getAdapter()); + $tableGateway = new TableGateway('test_charset', $this->getAdapter(['db' => ['driver' => Driver::class]])); $affectedRows = $tableGateway->insert([ 'field$' => 'test_value1', @@ -91,7 +92,7 @@ public function testInsertWithExtendedCharsetFieldName(): int|string #[Depends('testInsertWithExtendedCharsetFieldName')] public function testUpdateWithExtendedCharsetFieldName(mixed $id): void { - $tableGateway = new TableGateway('test_charset', $this->getAdapter()); + $tableGateway = new TableGateway('test_charset', $this->getAdapter(['db' => ['driver' => Driver::class]])); $data = [ 'field$' => 'test_value3', @@ -113,12 +114,12 @@ public function testUpdateWithExtendedCharsetFieldName(mixed $id): void public function testTableGatewayWithMetadataFeature(array|string|TableIdentifier $table): void { /** @var AdapterInterface&SchemaAwareInterface&Adapter $adapter */ - $adapter = $this->getAdapter(['db' => ['driver' => 'pdo']]); + $adapter = $this->getAdapter(['db' => ['driver' => Driver::class]]); $tableGateway = new TableGateway( $table, $adapter, new MetadataFeature( - new MysqlMetadata($adapter), + new Source($adapter), ) ); diff --git a/test/integration/Driver/Mysqli/TableGatewayTest.php b/test/integration/TableGatewayTest.php similarity index 88% rename from test/integration/Driver/Mysqli/TableGatewayTest.php rename to test/integration/TableGatewayTest.php index de68b5e..fdae1fd 100644 --- a/test/integration/Driver/Mysqli/TableGatewayTest.php +++ b/test/integration/TableGatewayTest.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace PhpDbIntegrationTest\Adapter\Mysql\Driver\Mysqli; +namespace PhpDbIntegrationTest\Mysql; use PhpDb\Adapter\Adapter; use PhpDb\Adapter\AdapterInterface; -use PhpDb\Adapter\Mysql\Driver\Mysqli\Mysqli; +use PhpDb\Mysql\Driver; use PhpDb\ResultSet\AbstractResultSet; use PhpDb\TableGateway\TableGateway; -use PhpDbIntegrationTest\Adapter\Mysql\Container\TestAsset\SetupTrait; +use PhpDbIntegrationTest\Mysql\Container\TestAsset\SetupTrait; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\TestCase; @@ -28,7 +28,7 @@ public function testSelectWithEmptyCurrentWithBufferResult(): void /** @var AdapterInterface&Adapter $adapter */ $adapter = $this->getAdapter([ 'db' => [ - 'driver' => Mysqli::class, + 'driver' => Driver::class, 'options' => [ 'buffer_results' => true, ], @@ -53,7 +53,7 @@ public function testSelectWithEmptyCurrentWithoutBufferResult(): void /** @var AdapterInterface&Adapter $adapter */ $adapter = $this->getAdapter([ 'db' => [ - 'driver' => 'mysqli', + 'driver' => Driver::class, 'options' => [ 'buffer_results' => false, ], diff --git a/test/unit/Platform/MysqlTest.php b/test/unit/AdapterPlatformTest.php similarity index 89% rename from test/unit/Platform/MysqlTest.php rename to test/unit/AdapterPlatformTest.php index 2da029c..38f8237 100644 --- a/test/unit/Platform/MysqlTest.php +++ b/test/unit/AdapterPlatformTest.php @@ -2,29 +2,30 @@ declare(strict_types=1); -namespace PhpDbTest\Adapter\Mysql\Platform; +namespace PhpDbTest\Mysql\Platform; use Override; use PhpDb\Adapter\Driver\Pdo\Result; use PhpDb\Adapter\Driver\Pdo\Statement; -use PhpDb\Adapter\Mysql\Driver\Pdo; -use PhpDb\Adapter\Mysql\Platform\Mysql; +use PhpDb\Mysql\AdapterPlatform; +use PhpDb\Mysql\Pdo\Connection; +use PhpDb\Mysql\Pdo\Driver; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\TestCase; -#[CoversMethod(Mysql::class, 'getName')] -#[CoversMethod(Mysql::class, 'getQuoteIdentifierSymbol')] -#[CoversMethod(Mysql::class, 'quoteIdentifier')] -#[CoversMethod(Mysql::class, 'quoteIdentifierChain')] -#[CoversMethod(Mysql::class, 'getQuoteValueSymbol')] -#[CoversMethod(Mysql::class, 'quoteValue')] -#[CoversMethod(Mysql::class, 'quoteTrustedValue')] -#[CoversMethod(Mysql::class, 'quoteValueList')] -#[CoversMethod(Mysql::class, 'getIdentifierSeparator')] -#[CoversMethod(Mysql::class, 'quoteIdentifierInFragment')] -final class MysqlTest extends TestCase +#[CoversMethod(AdapterPlatform::class, 'getName')] +#[CoversMethod(AdapterPlatform::class, 'getQuoteIdentifierSymbol')] +#[CoversMethod(AdapterPlatform::class, 'quoteIdentifier')] +#[CoversMethod(AdapterPlatform::class, 'quoteIdentifierChain')] +#[CoversMethod(AdapterPlatform::class, 'getQuoteValueSymbol')] +#[CoversMethod(AdapterPlatform::class, 'quoteValue')] +#[CoversMethod(AdapterPlatform::class, 'quoteTrustedValue')] +#[CoversMethod(AdapterPlatform::class, 'quoteValueList')] +#[CoversMethod(AdapterPlatform::class, 'getIdentifierSeparator')] +#[CoversMethod(AdapterPlatform::class, 'quoteIdentifierInFragment')] +final class AdapterPlatformTest extends TestCase { - protected Mysql $platform; + protected AdapterPlatform $platform; /** * Sets up the fixture, for example, opens a network connection. @@ -33,12 +34,12 @@ final class MysqlTest extends TestCase #[Override] protected function setUp(): void { - $pdo = new Pdo\Pdo( - $this->createMock(Pdo\Connection::class), + $pdo = new Driver( + $this->createMock(Connection::class), $this->createMock(Statement::class), $this->createMock(Result::class), ); - $this->platform = new Mysql($pdo); + $this->platform = new AdapterPlatform($pdo); } public function testGetName(): void diff --git a/test/unit/Driver/Mysqli/ConnectionTest.php b/test/unit/ConnectionTest.php similarity index 98% rename from test/unit/Driver/Mysqli/ConnectionTest.php rename to test/unit/ConnectionTest.php index 5a30ee0..8648437 100644 --- a/test/unit/Driver/Mysqli/ConnectionTest.php +++ b/test/unit/ConnectionTest.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace PhpDbTest\Adapter\Mysql\Driver\Mysqli; +namespace PhpDbTest\Mysql; use mysqli; use Override; -use PhpDb\Adapter\Mysql\Driver\Mysqli\Connection; use PhpDb\Exception\RuntimeException; +use PhpDb\Mysql\Connection; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\RequiresPhpExtension; use PHPUnit\Framework\MockObject\MockObject; diff --git a/test/unit/Driver/Pdo/ConnectionTest.php b/test/unit/Pdo/ConnectionTest.php similarity index 97% rename from test/unit/Driver/Pdo/ConnectionTest.php rename to test/unit/Pdo/ConnectionTest.php index 17d53bc..036f96e 100644 --- a/test/unit/Driver/Pdo/ConnectionTest.php +++ b/test/unit/Pdo/ConnectionTest.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace PhpDbTest\Adapter\Mysql\Driver\Pdo; +namespace PhpDbTest\Mysql\Pdo; use Exception; use Override; use PhpDb\Adapter\Driver\PdoConnectionInterface; use PhpDb\Adapter\Exception\InvalidConnectionParametersException; use PhpDb\Adapter\Exception\RuntimeException; -use PhpDb\Adapter\Mysql\Driver\Pdo\Connection; +use PhpDb\Mysql\Pdo\Connection; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; diff --git a/test/unit/Driver/Pdo/ConnectionTransactionsTest.php b/test/unit/Pdo/ConnectionTransactionsTest.php similarity index 97% rename from test/unit/Driver/Pdo/ConnectionTransactionsTest.php rename to test/unit/Pdo/ConnectionTransactionsTest.php index efc6f94..38fd598 100644 --- a/test/unit/Driver/Pdo/ConnectionTransactionsTest.php +++ b/test/unit/Pdo/ConnectionTransactionsTest.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace PhpDbTest\Adapter\Mysql\Driver\Pdo; +namespace PhpDbTest\Mysql\Pdo; use Override; use PhpDb\Adapter\Driver\AbstractConnection; use PhpDb\Adapter\Exception\RuntimeException; -use PhpDb\Adapter\Mysql\Driver\Pdo\Connection; -use PhpDbTest\Adapter\Mysql\TestAsset\ConnectionWrapper; +use PhpDb\Mysql\Pdo\Connection; +use PhpDbTest\Mysql\Pdo\TestAsset\ConnectionWrapper; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\TestCase; diff --git a/test/unit/Driver/Pdo/PdoTest.php b/test/unit/Pdo/DriverTest.php similarity index 88% rename from test/unit/Driver/Pdo/PdoTest.php rename to test/unit/Pdo/DriverTest.php index cb45b81..0096646 100644 --- a/test/unit/Driver/Pdo/PdoTest.php +++ b/test/unit/Pdo/DriverTest.php @@ -2,23 +2,24 @@ declare(strict_types=1); -namespace PhpDbTest\Adapter\Mysql\Driver\Pdo; +namespace PhpDbTest\Mysql\Pdo; use Override; use PhpDb\Adapter\Driver\DriverInterface; use PhpDb\Adapter\Driver\Pdo\Result; use PhpDb\Adapter\Driver\Pdo\Statement; -use PhpDb\Adapter\Mysql\Driver\Pdo; use PhpDb\Exception\RuntimeException; +use PhpDb\Mysql\Pdo\Connection; +use PhpDb\Mysql\Pdo\Driver; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; -#[CoversMethod(Pdo\Pdo::class, 'getDatabasePlatformName')] -#[CoversMethod(Pdo\Pdo::class, 'getResultPrototype')] -final class PdoTest extends TestCase +#[CoversMethod(Driver::class, 'getDatabasePlatformName')] +#[CoversMethod(Driver::class, 'getResultPrototype')] +final class DriverTest extends TestCase { - protected Pdo\Pdo $pdo; + protected Driver $pdo; /** * Sets up the fixture, for example, opens a network connection. @@ -27,10 +28,10 @@ final class PdoTest extends TestCase #[Override] protected function setUp(): void { - $connection = $this->createMock(Pdo\Connection::class); + $connection = $this->createMock(Connection::class); $statement = $this->createMock(Statement::class); $result = $this->createMock(Result::class); - $this->pdo = new Pdo\Pdo( + $this->pdo = new Driver( $connection, $statement, $result diff --git a/test/unit/Driver/Pdo/ResultTest.php b/test/unit/Pdo/ResultTest.php similarity index 98% rename from test/unit/Driver/Pdo/ResultTest.php rename to test/unit/Pdo/ResultTest.php index a29a4b8..2e67947 100644 --- a/test/unit/Driver/Pdo/ResultTest.php +++ b/test/unit/Pdo/ResultTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace PhpDbTest\Adapter\Mysql\Driver\Pdo; +namespace PhpDbTest\Mysql\Pdo; use PDO; use PDOStatement; diff --git a/test/unit/Driver/Pdo/StatementIntegrationTest.php b/test/unit/Pdo/StatementIntegrationTest.php similarity index 94% rename from test/unit/Driver/Pdo/StatementIntegrationTest.php rename to test/unit/Pdo/StatementIntegrationTest.php index b2f28aa..1d3eebd 100644 --- a/test/unit/Driver/Pdo/StatementIntegrationTest.php +++ b/test/unit/Pdo/StatementIntegrationTest.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace PhpDbTest\Adapter\Mysql\Driver\Pdo; +namespace PhpDbTest\Mysql\Pdo; use Override; use PDO; use PDOStatement; use PhpDb\Adapter\Driver\Pdo\Statement; -use PhpDb\Adapter\Mysql\Driver\Pdo\Pdo as PdoDriver; -use PhpDbTest\Adapter\Mysql\Driver\Pdo\TestAsset; +use PhpDb\Mysql\Pdo\Driver as PdoDriver; +use PhpDbTest\Mysql\Pdo\TestAsset; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; diff --git a/test/unit/Driver/Pdo/StatementTest.php b/test/unit/Pdo/StatementTest.php similarity index 95% rename from test/unit/Driver/Pdo/StatementTest.php rename to test/unit/Pdo/StatementTest.php index ae59589..1ff8be7 100644 --- a/test/unit/Driver/Pdo/StatementTest.php +++ b/test/unit/Pdo/StatementTest.php @@ -2,16 +2,16 @@ declare(strict_types=1); -namespace PhpDbTest\Adapter\Mysql\Driver\Pdo; +namespace PhpDbTest\Mysql\Pdo; use Override; use PDOStatement; use PhpDb\Adapter\Driver\Pdo\Result; use PhpDb\Adapter\Driver\Pdo\Statement; use PhpDb\Adapter\Driver\PdoDriverInterface; -use PhpDb\Adapter\Mysql\Driver\Pdo\Connection; -use PhpDb\Adapter\Mysql\Driver\Pdo\Pdo; use PhpDb\Adapter\ParameterContainer; +use PhpDb\Mysql\Pdo\Connection; +use PhpDb\Mysql\Pdo\Driver; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\TestCase; @@ -26,7 +26,7 @@ #[CoversMethod(Statement::class, 'execute')] final class StatementTest extends TestCase { - protected ?Pdo $pdo; + protected ?Driver $pdo; protected Statement $statement; /** @@ -37,7 +37,7 @@ final class StatementTest extends TestCase protected function setUp(): void { $this->statement = new Statement(); - $this->pdo = new Pdo( + $this->pdo = new Driver( $this->createMock(Connection::class), $this->statement, $this->createMock(Result::class), diff --git a/test/unit/TestAsset/ConnectionWrapper.php b/test/unit/Pdo/TestAsset/ConnectionWrapper.php similarity index 63% rename from test/unit/TestAsset/ConnectionWrapper.php rename to test/unit/Pdo/TestAsset/ConnectionWrapper.php index 26fe813..f89a1e9 100644 --- a/test/unit/TestAsset/ConnectionWrapper.php +++ b/test/unit/Pdo/TestAsset/ConnectionWrapper.php @@ -2,12 +2,12 @@ declare(strict_types=1); -namespace PhpDbTest\Adapter\Mysql\TestAsset; +namespace PhpDbTest\Mysql\Pdo\TestAsset; -use PhpDb\Adapter\Mysql\Driver\Pdo\Connection; +use PhpDb\Mysql\Pdo\Connection; /** - * Test asset class used only by {@see \PhpDbTest\Adapter\Driver\Pdo\ConnectionTransactionsTest} + * Test asset class used only by {@see \PhpDbTest\Mysql\TestAsset\ConnectionTransactionsTest} */ final class ConnectionWrapper extends Connection { diff --git a/test/unit/Driver/Pdo/TestAsset/CtorlessPdo.php b/test/unit/Pdo/TestAsset/CtorlessPdo.php similarity index 89% rename from test/unit/Driver/Pdo/TestAsset/CtorlessPdo.php rename to test/unit/Pdo/TestAsset/CtorlessPdo.php index f0cc479..7826375 100644 --- a/test/unit/Driver/Pdo/TestAsset/CtorlessPdo.php +++ b/test/unit/Pdo/TestAsset/CtorlessPdo.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace PhpDbTest\Adapter\Mysql\Driver\Pdo\TestAsset; +namespace PhpDbTest\Mysql\Pdo\TestAsset; use Override; use PDO; diff --git a/test/unit/Driver/TestAsset/PdoMock.php b/test/unit/Pdo/TestAsset/PdoMock.php similarity index 87% rename from test/unit/Driver/TestAsset/PdoMock.php rename to test/unit/Pdo/TestAsset/PdoMock.php index 5bf2a82..412ad28 100644 --- a/test/unit/Driver/TestAsset/PdoMock.php +++ b/test/unit/Pdo/TestAsset/PdoMock.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace PhpDbTest\Adapter\Mysql\Driver\TestAsset; +namespace PhpDbTest\Mysql\Pdo\TestAsset; use PDO; diff --git a/test/unit/TestAsset/PdoStubDriver.php b/test/unit/Pdo/TestAsset/PdoStubDriver.php similarity index 88% rename from test/unit/TestAsset/PdoStubDriver.php rename to test/unit/Pdo/TestAsset/PdoStubDriver.php index ce57aff..4e3531c 100644 --- a/test/unit/TestAsset/PdoStubDriver.php +++ b/test/unit/Pdo/TestAsset/PdoStubDriver.php @@ -1,6 +1,8 @@