Skip to content

Commit cfcf2e3

Browse files
authored
Merge pull request #45 from simon-mundy/mysql-testing
- Added testing for Container factories for correct option parameters
2 parents a34ffb5 + 43c80e8 commit cfcf2e3

14 files changed

Lines changed: 209 additions & 83 deletions

phpstan-baseline.neon

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -222,63 +222,39 @@ parameters:
222222
count: 1
223223
path: test/integration/AdapterPlatformTest.php
224224

225-
-
226-
message: '#^Callable PhpDb\\Mysql\\Container\\DriverInterfaceFactory invoked with 1 parameter, 2\-3 required\.$#'
227-
identifier: arguments.count
228-
count: 1
229-
path: test/integration/Container/DriverInterfaceFactoryTest.php
230-
231225
-
232226
message: '#^Parameter \#1 \$container of callable PhpDb\\Mysql\\Container\\DriverInterfaceFactory expects Laminas\\ServiceManager\\ServiceManager, Psr\\Container\\ContainerInterface given\.$#'
233227
identifier: argument.type
234-
count: 1
228+
count: 2
235229
path: test/integration/Container/DriverInterfaceFactoryTest.php
236230

237-
-
238-
message: '#^Callable PhpDb\\Mysql\\Container\\MetadataInterfaceFactory invoked with 1 parameter, 2\-3 required\.$#'
239-
identifier: arguments.count
240-
count: 1
241-
path: test/integration/Container/MetadataInterfaceFactoryTest.php
242-
243-
-
244-
message: '#^Callable PhpDb\\Mysql\\Container\\PdoConnectionInterfaceFactory invoked with 1 parameter, 2\-3 required\.$#'
245-
identifier: arguments.count
246-
count: 1
247-
path: test/integration/Container/PdoConnectionInterfaceFactoryTest.php
248-
249-
-
250-
message: '#^Callable PhpDb\\Mysql\\Container\\PdoDriverInterfaceFactory invoked with 1 parameter, 2\-3 required\.$#'
251-
identifier: arguments.count
252-
count: 1
253-
path: test/integration/Container/PdoDriverInterfaceFactoryTest.php
254-
255231
-
256232
message: '#^Parameter \#1 \$container of callable PhpDb\\Mysql\\Container\\PdoDriverInterfaceFactory expects Laminas\\ServiceManager\\ServiceManager, Psr\\Container\\ContainerInterface given\.$#'
257233
identifier: argument.type
258234
count: 1
259235
path: test/integration/Container/PdoDriverInterfaceFactoryTest.php
260236

261237
-
262-
message: '#^Callable PhpDb\\Mysql\\Container\\PdoStatementFactory invoked with 1 parameter, 2\-3 required\.$#'
263-
identifier: arguments.count
238+
message: '#^Call to static method PHPUnit\\Framework\\Assert\:\:assertIsInt\(\) with int will always evaluate to true\.$#'
239+
identifier: staticMethod.alreadyNarrowedType
264240
count: 1
265-
path: test/integration/Container/PdoStatementFactoryTest.php
241+
path: test/integration/Pdo/ConnectionTest.php
266242

267243
-
268-
message: '#^Callable PhpDb\\Mysql\\Container\\PlatformInterfaceFactory invoked with 1 parameter, 2\-3 required\.$#'
269-
identifier: arguments.count
244+
message: '#^Unreachable statement \- code above always terminates\.$#'
245+
identifier: deadCode.unreachable
270246
count: 1
271-
path: test/integration/Container/PlatformInterfaceFactoryTest.php
247+
path: test/integration/Pdo/QueryTest.php
272248

273249
-
274-
message: '#^Callable PhpDb\\Mysql\\Container\\StatementInterfaceFactory invoked with 1 parameter, 2\-3 required\.$#'
275-
identifier: arguments.count
250+
message: '#^Unreachable statement \- code above always terminates\.$#'
251+
identifier: deadCode.unreachable
276252
count: 1
277-
path: test/integration/Container/StatementInterfaceFactoryTest.php
253+
path: test/integration/Pdo/TableGatewayTest.php
278254

279255
-
280-
message: '#^Call to static method PHPUnit\\Framework\\Assert\:\:assertIsInt\(\) with int will always evaluate to true\.$#'
281-
identifier: staticMethod.alreadyNarrowedType
256+
message: '#^Unreachable statement \- code above always terminates\.$#'
257+
identifier: deadCode.unreachable
282258
count: 1
283-
path: test/integration/Pdo/ConnectionTest.php
259+
path: test/integration/TableGatewayTest.php
284260

src/Container/PdoConnectionInterfaceFactory.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ public function __invoke(
1818
string $requestedName,
1919
?array $options = null
2020
): PdoConnectionInterface&Connection {
21-
if (! is_array($options['connection']) || $options['connection'] === []) {
21+
$conn = $options['connection'] ?? [];
22+
if (! is_array($conn) || $conn === []) {
2223
throw new InvalidConnectionParametersException(
2324
'Connection configuration must be an array of parameters passed via $options["connection"]',
24-
$options['connection']
25+
$conn
2526
);
2627
}
2728

28-
return new Connection($options['connection']);
29+
return new Connection($conn);
2930
}
3031
}

test/integration/Container/ConnectionInterfaceFactoryTest.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace PhpDbIntegrationTest\Mysql\Container;
66

7+
use PhpDb\Adapter\AdapterInterface;
78
use PhpDb\Adapter\Driver\ConnectionInterface;
9+
use PhpDb\Adapter\Exception\InvalidConnectionParametersException;
810
use PhpDb\Mysql\Connection;
911
use PhpDb\Mysql\Container\ConnectionInterfaceFactory;
1012
use PHPUnit\Framework\Attributes;
@@ -21,16 +23,25 @@ final class ConnectionInterfaceFactoryTest extends TestCase
2123

2224
public function testInvokeReturnsMysqliConnection(): void
2325
{
24-
$this->getAdapter([
25-
'db' => [
26-
'driver' => 'Mysqli',
27-
],
28-
]);
29-
3026
$factory = new ConnectionInterfaceFactory();
31-
$connection = $factory($this->container, Connection::class);
27+
$connection = $factory(
28+
$this->container,
29+
Connection::class,
30+
$this->config[AdapterInterface::class]
31+
);
3232

3333
self::assertInstanceOf(ConnectionInterface::class, $connection);
3434
self::assertInstanceOf(Connection::class, $connection);
3535
}
36+
37+
public function testInvokeThrowsExceptionWithoutConnectionConfig(): void
38+
{
39+
$this->expectException(InvalidConnectionParametersException::class);
40+
41+
$factory = new ConnectionInterfaceFactory();
42+
$factory(
43+
$this->container,
44+
Connection::class
45+
);
46+
}
3647
}

test/integration/Container/DriverInterfaceFactoryTest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace PhpDbIntegrationTest\Mysql\Container;
66

7+
use PhpDb\Adapter\AdapterInterface;
78
use PhpDb\Adapter\Driver\DriverInterface;
9+
use PhpDb\Exception\ContainerException;
10+
use PhpDb\Mysql\Connection;
811
use PhpDb\Mysql\Container\DriverInterfaceFactory;
912
use PhpDb\Mysql\Driver;
1013
use PHPUnit\Framework\Attributes;
@@ -21,14 +24,24 @@ final class DriverInterfaceFactoryTest extends TestCase
2124

2225
public function testFactoryReturnsMysqliDriver(): void
2326
{
24-
$this->getAdapter([
25-
'db' => [
26-
'driver' => 'Mysqli',
27-
],
28-
]);
2927
$factory = new DriverInterfaceFactory();
30-
$driver = $factory($this->container);
28+
$driver = $factory(
29+
$this->container,
30+
DriverInterface::class,
31+
$this->config[AdapterInterface::class]
32+
);
3133
self::assertInstanceOf(DriverInterface::class, $driver);
3234
$this->assertInstanceOf(Driver::class, $driver);
3335
}
36+
37+
public function testInvokeThrowsExceptionWithoutConnectionConfig(): void
38+
{
39+
$this->expectException(ContainerException::class);
40+
41+
$factory = new DriverInterfaceFactory();
42+
$factory(
43+
$this->container,
44+
Connection::class
45+
);
46+
}
3447
}

test/integration/Container/MetadataInterfaceFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class MetadataInterfaceFactoryTest extends TestCase
2121
public function testFactoryReturnsMysqlMetadata(): void
2222
{
2323
$factory = new MetadataInterfaceFactory();
24-
$metadata = $factory($this->container);
24+
$metadata = $factory($this->container, MetadataInterface::class);
2525
self::assertInstanceOf(MetadataInterface::class, $metadata);
2626
self::assertInstanceOf(Source::class, $metadata);
2727
}

test/integration/Container/PdoConnectionInterfaceFactoryTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace PhpDbIntegrationTest\Mysql\Container;
66

7+
use PhpDb\Adapter\AdapterInterface;
78
use PhpDb\Adapter\Driver\ConnectionInterface;
89
use PhpDb\Adapter\Driver\PdoConnectionInterface;
10+
use PhpDb\Adapter\Exception\InvalidConnectionParametersException;
911
use PhpDb\Mysql\Container\PdoConnectionInterfaceFactory;
1012
use PhpDb\Mysql\Pdo\Connection;
1113
use PHPUnit\Framework\Attributes\CoversClass;
@@ -24,9 +26,24 @@ final class PdoConnectionInterfaceFactoryTest extends TestCase
2426
public function testInvokeReturnsPdoConnection(): void
2527
{
2628
$factory = new PdoConnectionInterfaceFactory();
27-
$instance = $factory($this->container);
29+
$instance = $factory(
30+
$this->container,
31+
PdoConnectionInterface::class,
32+
$this->config[AdapterInterface::class]
33+
);
2834
self::assertInstanceOf(ConnectionInterface::class, $instance);
2935
self::assertInstanceOf(PdoConnectionInterface::class, $instance);
3036
self::assertInstanceOf(Connection::class, $instance);
3137
}
38+
39+
public function testInvokeThrowsExceptionWithoutConnectionConfig(): void
40+
{
41+
$this->expectException(InvalidConnectionParametersException::class);
42+
43+
$factory = new PdoConnectionInterfaceFactory();
44+
$factory(
45+
$this->container,
46+
PdoConnectionInterface::class
47+
);
48+
}
3249
}

test/integration/Container/PdoDriverInterfaceFactoryTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpDbIntegrationTest\Mysql\Container;
66

7+
use PhpDb\Adapter\AdapterInterface;
78
use PhpDb\Adapter\Driver\PdoDriverInterface;
89
use PhpDb\Mysql\Container\PdoDriverInterfaceFactory;
910
use PhpDb\Mysql\Pdo\Driver;
@@ -23,7 +24,11 @@ final class PdoDriverInterfaceFactoryTest extends TestCase
2324
public function testInvokeReturnsPdoDriver(): void
2425
{
2526
$factory = new PdoDriverInterfaceFactory();
26-
$instance = $factory($this->container);
27+
$instance = $factory(
28+
$this->container,
29+
PdoDriverInterface::class,
30+
$this->config[AdapterInterface::class]
31+
);
2732

2833
self::assertInstanceOf(PdoDriverInterface::class, $instance);
2934
self::assertInstanceOf(Driver::class, $instance);

test/integration/Container/PdoStatementFactoryTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpDbIntegrationTest\Mysql\Container;
66

7+
use PhpDb\Adapter\AdapterInterface;
78
use PhpDb\Adapter\Driver\Pdo\Statement;
89
use PhpDb\Adapter\Driver\StatementInterface;
910
use PhpDb\Mysql\Container\PdoStatementFactory;
@@ -23,7 +24,11 @@ final class PdoStatementFactoryTest extends TestCase
2324
public function testInvokeReturnsPdoStatement(): void
2425
{
2526
$factory = new PdoStatementFactory();
26-
$statement = $factory($this->container);
27+
$statement = $factory(
28+
$this->container,
29+
StatementInterface::class,
30+
$this->config[AdapterInterface::class]
31+
);
2732
self::assertInstanceOf(StatementInterface::class, $statement);
2833
self::assertInstanceOf(Statement::class, $statement);
2934
}

test/integration/Container/PlatformInterfaceFactoryTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace PhpDbIntegrationTest\Mysql\Container;
66

7+
use PhpDb\Adapter\AdapterInterface;
78
use PhpDb\Adapter\Platform\PlatformInterface;
89
use PhpDb\Mysql\AdapterPlatform;
910
use PhpDb\Mysql\Container\PlatformInterfaceFactory;
11+
use PhpDb\Mysql\Pdo\Driver as PdoDriver;
1012
use PHPUnit\Framework\Attributes\CoversClass;
1113
use PHPUnit\Framework\Attributes\CoversMethod;
1214
use PHPUnit\Framework\Attributes\Group;
@@ -22,8 +24,17 @@ final class PlatformInterfaceFactoryTest extends TestCase
2224

2325
public function testInvokeReturnsPlatformInterfaceWhenDbDriverIsPdo(): void
2426
{
27+
$adapter = $this->getAdapter(['driver' => PdoDriver::class]);
28+
29+
$this->config[AdapterInterface::class]['driver'] = $adapter->getDriver();
30+
2531
$factory = new PlatformInterfaceFactory();
26-
$instance = $factory($this->container);
32+
$instance = $factory(
33+
$this->container,
34+
PlatformInterface::class,
35+
$this->config[AdapterInterface::class]
36+
);
37+
2738
self::assertInstanceOf(PlatformInterface::class, $instance);
2839
self::assertInstanceOf(AdapterPlatform::class, $instance);
2940
}

test/integration/Container/StatementInterfaceFactoryTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpDbIntegrationTest\Mysql\Container;
66

7+
use PhpDb\Adapter\AdapterInterface;
78
use PhpDb\Adapter\Driver\StatementInterface;
89
use PhpDb\Mysql\Container\StatementInterfaceFactory;
910
use PhpDb\Mysql\Statement;
@@ -31,7 +32,11 @@ public function testInvokeReturnsMysqliStatement(): void
3132
]);
3233

3334
$factory = new StatementInterfaceFactory();
34-
$statement = $factory($this->container);
35+
$statement = $factory(
36+
$this->container,
37+
StatementInterface::class,
38+
$this->config[AdapterInterface::class]
39+
);
3540

3641
self::assertInstanceOf(StatementInterface::class, $statement);
3742
self::assertInstanceOf(Statement::class, $statement);

0 commit comments

Comments
 (0)