Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Container/PdoConnectionInterfaceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ public function __invoke(
string $requestedName,
?array $options = null
): PdoConnectionInterface&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);
}
}
25 changes: 18 additions & 7 deletions test/integration/Container/ConnectionInterfaceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace PhpDbIntegrationTest\Mysql\Container;

use PhpDb\Adapter\AdapterInterface;
use PhpDb\Adapter\Driver\ConnectionInterface;
use PhpDb\Adapter\Exception\InvalidConnectionParametersException;
use PhpDb\Mysql\Connection;
use PhpDb\Mysql\Container\ConnectionInterfaceFactory;
use PHPUnit\Framework\Attributes;
Expand All @@ -21,16 +23,25 @@

public function testInvokeReturnsMysqliConnection(): void
{
$this->getAdapter([
'db' => [
'driver' => 'Mysqli',
],
]);

$factory = new ConnectionInterfaceFactory();
$connection = $factory($this->container, Connection::class);
$connection = $factory(
$this->container,
Connection::class,
$this->config[AdapterInterface::class]
);

self::assertInstanceOf(ConnectionInterface::class, $connection);
self::assertInstanceOf(Connection::class, $connection);
}

public function testInvokeThrowsExceptionWithoutConnectionConfig(): void
{
$this->expectException(InvalidConnectionParametersException::class);

$factory = new ConnectionInterfaceFactory();

Check failure on line 41 in test/integration/Container/ConnectionInterfaceFactoryTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPCodeSniffer [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Equals sign not aligned correctly; expected 1 space but found 4 spaces
$factory(
$this->container,
Connection::class
);
}
}
27 changes: 21 additions & 6 deletions test/integration/Container/DriverInterfaceFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<?php

declare(strict_types=1);

namespace PhpDbIntegrationTest\Mysql\Container;

use PhpDb\Adapter\AdapterInterface;
use PhpDb\Adapter\Driver\DriverInterface;
use PhpDb\Adapter\Exception\InvalidConnectionParametersException;

Check failure on line 9 in test/integration/Container/DriverInterfaceFactoryTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPCodeSniffer [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Type PhpDb\Adapter\Exception\InvalidConnectionParametersException is not used in this file.
use PhpDb\Exception\ContainerException;
use PhpDb\Mysql\Connection;
use PhpDb\Mysql\Container\ConnectionInterfaceFactory;

Check failure on line 12 in test/integration/Container/DriverInterfaceFactoryTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPCodeSniffer [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Type PhpDb\Mysql\Container\ConnectionInterfaceFactory is not used in this file.
use PhpDb\Mysql\Container\DriverInterfaceFactory;
use PhpDb\Mysql\Driver;
use PHPUnit\Framework\Attributes;
Expand All @@ -21,14 +26,24 @@

public function testFactoryReturnsMysqliDriver(): void
{
$this->getAdapter([
'db' => [
'driver' => 'Mysqli',
],
]);
$factory = new DriverInterfaceFactory();
$driver = $factory($this->container);
$driver = $factory(
$this->container,

Check failure on line 31 in test/integration/Container/DriverInterfaceFactoryTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPStan [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Ignored error pattern #^Parameter \#1 \$container of callable PhpDb\\Mysql\\Container\\DriverInterfaceFactory expects Laminas\\ServiceManager\\ServiceManager, Psr\\Container\\ContainerInterface given\.$# (argument.type) in path /github/workspace/test/integration/Container/DriverInterfaceFactoryTest.php is expected to occur 1 time, but occurred 2 times.

Check failure on line 31 in test/integration/Container/DriverInterfaceFactoryTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PhpStan [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Ignored error pattern #^Parameter \#1 \$container of callable PhpDb\\Mysql\\Container\\DriverInterfaceFactory expects Laminas\\ServiceManager\\ServiceManager, Psr\\Container\\ContainerInterface given\.$# (argument.type) in path /github/workspace/test/integration/Container/DriverInterfaceFactoryTest.php is expected to occur 1 time, but occurred 2 times.
DriverInterface::class,
$this->config[AdapterInterface::class]
);
self::assertInstanceOf(DriverInterface::class, $driver);
$this->assertInstanceOf(Driver::class, $driver);
}

public function testInvokeThrowsExceptionWithoutConnectionConfig(): void
{
$this->expectException(ContainerException::class);

$factory = new DriverInterfaceFactory();

Check failure on line 43 in test/integration/Container/DriverInterfaceFactoryTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPCodeSniffer [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Equals sign not aligned correctly; expected 1 space but found 4 spaces
$factory(
$this->container,

Check failure on line 45 in test/integration/Container/DriverInterfaceFactoryTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPStan [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #1 $container of callable PhpDb\Mysql\Container\DriverInterfaceFactory expects Laminas\ServiceManager\ServiceManager, Psr\Container\ContainerInterface given.

Check failure on line 45 in test/integration/Container/DriverInterfaceFactoryTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PhpStan [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #1 $container of callable PhpDb\Mysql\Container\DriverInterfaceFactory expects Laminas\ServiceManager\ServiceManager, Psr\Container\ContainerInterface given.
Connection::class
);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

declare(strict_types=1);
Expand All @@ -21,7 +21,7 @@
public function testFactoryReturnsMysqlMetadata(): void
{
$factory = new MetadataInterfaceFactory();
$metadata = $factory($this->container);
$metadata = $factory($this->container, MetadataInterface::class);
self::assertInstanceOf(MetadataInterface::class, $metadata);
self::assertInstanceOf(Source::class, $metadata);
}
Expand Down
19 changes: 18 additions & 1 deletion test/integration/Container/PdoConnectionInterfaceFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

declare(strict_types=1);

namespace PhpDbIntegrationTest\Mysql\Container;

use PhpDb\Adapter\AdapterInterface;
use PhpDb\Adapter\Driver\ConnectionInterface;
use PhpDb\Adapter\Driver\PdoConnectionInterface;
use PhpDb\Adapter\Exception\InvalidConnectionParametersException;
use PhpDb\Mysql\Container\PdoConnectionInterfaceFactory;
use PhpDb\Mysql\Pdo\Connection;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -24,9 +26,24 @@
public function testInvokeReturnsPdoConnection(): void
{
$factory = new PdoConnectionInterfaceFactory();
$instance = $factory($this->container);
$instance = $factory(
$this->container,
PdoConnectionInterface::class,
$this->config[AdapterInterface::class]
);
self::assertInstanceOf(ConnectionInterface::class, $instance);
self::assertInstanceOf(PdoConnectionInterface::class, $instance);
self::assertInstanceOf(Connection::class, $instance);
}

public function testInvokeThrowsExceptionWithoutConnectionConfig(): void
{
$this->expectException(InvalidConnectionParametersException::class);

$factory = new PdoConnectionInterfaceFactory();
$factory(
$this->container,
PdoConnectionInterface::class
);
}
}
8 changes: 7 additions & 1 deletion test/integration/Container/PdoDriverInterfaceFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

declare(strict_types=1);

namespace PhpDbIntegrationTest\Mysql\Container;

use PhpDb\Adapter\AdapterInterface;
use PhpDb\Adapter\Driver\DriverInterface;

Check failure on line 8 in test/integration/Container/PdoDriverInterfaceFactoryTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPCodeSniffer [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Type PhpDb\Adapter\Driver\DriverInterface is not used in this file.
use PhpDb\Adapter\Driver\PdoDriverInterface;
use PhpDb\Mysql\Container\PdoDriverInterfaceFactory;
use PhpDb\Mysql\Pdo\Driver;
Expand All @@ -23,7 +25,11 @@
public function testInvokeReturnsPdoDriver(): void
{
$factory = new PdoDriverInterfaceFactory();
$instance = $factory($this->container);
$instance = $factory(
$this->container,
PdoDriverInterface::class,
$this->config[AdapterInterface::class]
);

self::assertInstanceOf(PdoDriverInterface::class, $instance);
self::assertInstanceOf(Driver::class, $instance);
Expand Down
7 changes: 6 additions & 1 deletion test/integration/Container/PdoStatementFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

declare(strict_types=1);

namespace PhpDbIntegrationTest\Mysql\Container;

use PhpDb\Adapter\AdapterInterface;
use PhpDb\Adapter\Driver\Pdo\Statement;
use PhpDb\Adapter\Driver\StatementInterface;
use PhpDb\Mysql\Container\PdoStatementFactory;
Expand All @@ -23,7 +24,11 @@
public function testInvokeReturnsPdoStatement(): void
{
$factory = new PdoStatementFactory();
$statement = $factory($this->container);
$statement = $factory(
$this->container,
StatementInterface::class,
$this->config[AdapterInterface::class]
);
self::assertInstanceOf(StatementInterface::class, $statement);
self::assertInstanceOf(Statement::class, $statement);
}
Expand Down
15 changes: 14 additions & 1 deletion test/integration/Container/PlatformInterfaceFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php

declare(strict_types=1);

namespace PhpDbIntegrationTest\Mysql\Container;

use PhpDb\Adapter\AdapterInterface;
use PhpDb\Adapter\Driver\DriverInterface;

Check failure on line 8 in test/integration/Container/PlatformInterfaceFactoryTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPCodeSniffer [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Type PhpDb\Adapter\Driver\DriverInterface is not used in this file.
use PhpDb\Adapter\Platform\PlatformInterface;
use PhpDb\Mysql\AdapterPlatform;
use PhpDb\Mysql\Container\DriverInterfaceFactory;

Check failure on line 11 in test/integration/Container/PlatformInterfaceFactoryTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPCodeSniffer [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Type PhpDb\Mysql\Container\DriverInterfaceFactory is not used in this file.
use PhpDb\Mysql\Container\PlatformInterfaceFactory;
use PhpDb\Mysql\Pdo\Driver as PdoDriver;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\Group;
Expand All @@ -22,8 +26,17 @@

public function testInvokeReturnsPlatformInterfaceWhenDbDriverIsPdo(): void
{
$adapter = $this->getAdapter(['driver' => PdoDriver::class]);

$this->config[AdapterInterface::class]['driver'] = $adapter->getDriver();

$factory = new PlatformInterfaceFactory();
$instance = $factory($this->container);
$instance = $factory(
$this->container,
PlatformInterface::class,
$this->config[AdapterInterface::class]
);

self::assertInstanceOf(PlatformInterface::class, $instance);
self::assertInstanceOf(AdapterPlatform::class, $instance);
}
Expand Down
7 changes: 6 additions & 1 deletion test/integration/Container/StatementInterfaceFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

declare(strict_types=1);

namespace PhpDbIntegrationTest\Mysql\Container;

use PhpDb\Adapter\AdapterInterface;
use PhpDb\Adapter\Driver\StatementInterface;
use PhpDb\Mysql\Container\StatementInterfaceFactory;
use PhpDb\Mysql\Statement;
Expand Down Expand Up @@ -31,7 +32,11 @@
]);

$factory = new StatementInterfaceFactory();
$statement = $factory($this->container);
$statement = $factory(
$this->container,
StatementInterface::class,
$this->config[AdapterInterface::class]
);

self::assertInstanceOf(StatementInterface::class, $statement);
self::assertInstanceOf(Statement::class, $statement);
Expand Down
124 changes: 100 additions & 24 deletions test/integration/Pdo/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,112 @@ public function testConnectMethodReturnsConnectionInterface(): void
$connection->disconnect();
}

/**
* @todo Implement testBeginTransaction().
*/
public function testBeginTransaction(): never
public function testBeginTransaction(): void
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$connection = $this->getAdapter()->getDriver()->getConnection();
$connection->connect();

self::assertTrue($connection->isConnected());
self::assertFalse($connection->inTransaction());

$result = $connection->beginTransaction();

self::assertInstanceOf(Connection::class, $result);
self::assertTrue($connection->inTransaction());

$connection->rollback();
self::assertFalse($connection->inTransaction());
$connection->disconnect();
}

/**
* @todo Implement testCommit().
*/
public function testCommit(): never
public function testCommit(): void
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$connection = $this->getAdapter()->getDriver()->getConnection();
$connection->connect();
self::assertTrue($connection->isConnected());

$connection->beginTransaction();
self::assertTrue($connection->inTransaction());

$connection->execute("INSERT INTO test (name, value) VALUES ('tx_commit', 'test')");

$result = $connection->commit();
self::assertInstanceOf(Connection::class, $result);
self::assertFalse($connection->inTransaction());

$result = $connection->execute("SELECT COUNT(*) AS cnt FROM test WHERE name = 'tx_commit'");
self::assertSame(1, $result->getResource()->fetchColumn());

$connection->execute("DELETE FROM test WHERE name = 'tx_commit'");
$connection->disconnect();
}

/**
* @todo Implement testRollback().
*/
public function testRollback(): never
public function testRollback(): void
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
$connection = $this->getAdapter()->getDriver()->getConnection();
$connection->connect();
self::assertTrue($connection->isConnected());

$connection->beginTransaction();
self::assertTrue($connection->inTransaction());

$connection->execute("INSERT INTO test (name, value) VALUES ('tx_rollback', 'test')");

$result = $connection->rollback();
self::assertInstanceOf(Connection::class, $result);
self::assertFalse($connection->inTransaction());

$result = $connection->execute("SELECT COUNT(*) AS cnt FROM test WHERE name = 'tx_rollback'");
self::assertSame(0, $result->getResource()->fetchColumn());

$connection->disconnect();
}

public function testAutocommitRestoredAfterCommit(): void
{
/** @var Connection $connection */
$connection = $this->getAdapter()->getDriver()->getConnection();
$connection->connect();
self::assertTrue($connection->isConnected());

$connection->beginTransaction();
self::assertTrue($connection->inTransaction());
$connection->commit();
self::assertFalse($connection->inTransaction());

$connection->execute("INSERT INTO test (name, value) VALUES ('tx_autocommit', 'test')");

$connection->disconnect();

$connection->connect();
$result = $connection->execute("SELECT COUNT(*) AS cnt FROM test WHERE name = 'tx_autocommit'");
self::assertSame(1, $result->getResource()->fetchColumn());

$connection->execute("DELETE FROM test WHERE name = 'tx_autocommit'");
$connection->disconnect();
}

public function testAutocommitRestoredAfterRollback(): void
{
/** @var Connection $connection */
$connection = $this->getAdapter()->getDriver()->getConnection();
$connection->connect();
self::assertTrue($connection->isConnected());

$connection->beginTransaction();
self::assertTrue($connection->inTransaction());
$connection->rollback();
self::assertFalse($connection->inTransaction());

$connection->execute("INSERT INTO test (name, value) VALUES ('tx_autocommit_rb', 'test')");

$connection->disconnect();

$connection->connect();
$result = $connection->execute("SELECT COUNT(*) AS cnt FROM test WHERE name = 'tx_autocommit_rb'");
self::assertSame(1, $result->getResource()->fetchColumn());

$connection->execute("DELETE FROM test WHERE name = 'tx_autocommit_rb'");
$connection->disconnect();
}
}
2 changes: 2 additions & 0 deletions test/integration/Pdo/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@
*/
public function testSelectWithNotPermittedBindParamName(): void
{
$this->markTestIncomplete('Incorrect bound param name characters are not caught in a raw query.');

$this->expectException(RuntimeException::class);

Check failure on line 85 in test/integration/Pdo/QueryTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPStan [8.2, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Unreachable statement - code above always terminates.

Check failure on line 85 in test/integration/Pdo/QueryTest.php

View workflow job for this annotation

GitHub Actions / QA Checks (PhpStan [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Unreachable statement - code above always terminates.
$this->getAdapter()->query('SET @@session.time_zone = :tz$', [':tz$' => 'SYSTEM']);
}

Expand Down
Loading