Skip to content

Commit ffa62d6

Browse files
committed
Updates lock file for latest deps
Fixes integration test, but actual classes needs replaced with mocks where needed. Factory test needs to be written. Finishes implementing Pdo related factories Removes the various Test traits and replaces them with \Container\TestAsset\SetupTrait Continues the widespread refactoring of types across most interfaces/classes Signed-off-by: Joey Smith <jsmith@webinertia.net> Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent 6b06e00 commit ffa62d6

21 files changed

+260
-231
lines changed

composer.lock

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Container/PdoDriverFactory.php

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

55
namespace Laminas\Db\Adapter\Mysql\Container;
66

7+
use Laminas\Db\Adapter\Driver\PdoDriverInterface;
8+
use Laminas\Db\Adapter\Driver\Pdo;
9+
use Laminas\Db\Adapter\Mysql\Driver\Pdo\Connection;
10+
use Laminas\Db\Adapter\Mysql\Driver\Pdo\Pdo as PdoDriver;
11+
use Laminas\Db\Container\AdapterManager;
12+
use Psr\Container\ContainerInterface;
13+
714
final class PdoDriverFactory
815
{
9-
16+
public function __invoke(ContainerInterface $container): PdoDriverInterface
17+
{
18+
/** @var AdapterManager $adapterManager */
19+
$adapterManager = $container->get(AdapterManager::class);
20+
return new PdoDriver(
21+
$adapterManager->get(Connection::class),
22+
$adapterManager->get(Pdo\Statement::class),
23+
$adapterManager->get(Pdo\Result::class)
24+
);
25+
}
1026
}

src/Container/PdoResultFactory.php

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

55
namespace Laminas\Db\Adapter\Mysql\Container;
66

7+
use Laminas\Db\Adapter\Driver\ResultInterface;
8+
use Laminas\Db\Adapter\Driver\Pdo\Result;
9+
use Psr\Container\ContainerInterface;
10+
711
final class PdoResultFactory
812
{
9-
13+
public function __invoke(ContainerInterface $container): ResultInterface&Result
14+
{
15+
return new Result();
16+
}
1017
}

src/Container/PdoStatementFactory.php

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

55
namespace Laminas\Db\Adapter\Mysql\Container;
66

7+
use Laminas\Db\Adapter\Driver\Pdo\Statement;
8+
use Psr\Container\ContainerInterface;
9+
710
final class PdoStatementFactory
811
{
9-
12+
public function __invoke(ContainerInterface $container): Statement
13+
{
14+
$options = $container->get('config')['db']['options'] ?? false;
15+
return new Statement($options);
16+
}
1017
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LaminasIntegrationTest\Db\Adapter\Mysql\Container;
6+
7+
use Laminas\Db\Adapter\Mysql\Container\AdapterFactory;
8+
use Laminas\Db\Adapter\Mysql\Container\AdapterManagerDelegator;
9+
use Laminas\Db\Adapter\AdapterInterface;
10+
use Laminas\Db\Container\AdapterManager;
11+
use Laminas\Db\Container\AdapterManagerFactory;
12+
use Laminas\ServiceManager\ServiceManager;
13+
use PHPUnit\Framework\TestCase;
14+
use Psr\Container\ContainerInterface;
15+
16+
final class AdapterFactoryTest extends TestCase
17+
{
18+
19+
public function testFactoryReturnsAdapterInterface(): void
20+
{
21+
$this->markTestIncomplete(
22+
'This test is incomplete and needs to be implemented.'
23+
);
24+
}
25+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LaminasIntegrationTest\Db\Adapter\Mysql\Container\TestAsset;
6+
7+
use Laminas\Db\Adapter\Driver\DriverInterface;
8+
use Laminas\Db\Adapter\Adapter;
9+
use Laminas\Db\Adapter\AdapterInterface;
10+
use Laminas\Db\Adapter\AdapterServiceFactory;
11+
use Laminas\Db\Adapter\Mysql\Container\AdapterManagerDelegator;
12+
use Laminas\Db\Adapter\Mysql\Driver\Mysqli\Mysqli;
13+
use Laminas\Db\Adapter\Mysql\Driver\Pdo\Pdo;
14+
use Laminas\Db\Container\AdapterManager;
15+
use Laminas\Db\Container\AdapterManagerFactory;
16+
use Laminas\ServiceManager\ServiceManager;
17+
use Laminas\Stdlib\ArrayUtils;
18+
19+
use function getenv;
20+
21+
/**
22+
* This trait provides a setup method for integration tests that require
23+
* a database adapter configuration.
24+
*
25+
* It initializes the service manager with a database configuration,
26+
* allowing for the creation of an adapter manager and the retrieval
27+
* of an adapter instance.
28+
*/
29+
trait SetupTrait
30+
{
31+
protected array $config = ['db' => []];
32+
33+
protected ?AdapterInterface $adapter;
34+
35+
protected AdapterManager $adapterManager;
36+
37+
protected DriverInterface|string|null $driver = null;
38+
39+
protected function setUp(): void
40+
{
41+
$this->getAdapter();
42+
}
43+
44+
protected function getAdapter(array $config = []): AdapterInterface&Adapter
45+
{
46+
$baseConfig = [
47+
'db' => [
48+
'driver' => $this->driver ?? Pdo::class,
49+
'connection' => [
50+
'hostname' => (string) getenv('TESTS_LAMINAS_DB_ADAPTER_MYSQL_HOSTNAME') ?: 'localhost',
51+
'username' => (string) getenv('TESTS_LAMINAS_DB_ADAPTER_MYSQL_USERNAME'),
52+
'password' => (string) getenv('TESTS_LAMINAS_DB_ADAPTER_MYSQL_PASSWORD'),
53+
'database' => (string) getenv('TESTS_LAMINAS_DB_ADAPTER_MYSQL_DATABASE'),
54+
'port' => (string) getenv('TESTS_LAMINAS_DB_ADAPTER_MYSQL_PORT') ?: '3306',
55+
'charset' => 'utf8',
56+
'driver_options' => [],
57+
],
58+
'options' => [
59+
'buffer_results' => false,
60+
],
61+
],
62+
];
63+
64+
if ($config !== []) {
65+
// If the config is not empty, merge it with the base config
66+
// to allow for overriding or extending the default configuration.
67+
$baseConfig = ArrayUtils::merge($baseConfig, $config);
68+
}
69+
$this->config = ArrayUtils::merge($this->config, $baseConfig);
70+
71+
$container = new ServiceManager([
72+
'services' => [
73+
'config' => $this->config,
74+
],
75+
'factories' => [
76+
AdapterInterface::class => AdapterServiceFactory::class,
77+
AdapterManager::class => AdapterManagerFactory::class,
78+
],
79+
'delegators' => [
80+
AdapterManager::class => [
81+
AdapterManagerDelegator::class,
82+
],
83+
],
84+
]);
85+
86+
$this->adapterManager = $container->get(AdapterManager::class);
87+
$this->adapter = $this->adapterManager->get(AdapterInterface::class);
88+
89+
return $this->adapter;
90+
}
91+
92+
protected function getConfig(): array
93+
{
94+
return $this->config;
95+
}
96+
97+
protected function getHostname(): string
98+
{
99+
return $this->getConfig()['db']['connection']['hostname'];
100+
}
101+
}

test/integration/Driver/Mysqli/ConnectionTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace LaminasIntegrationTest\Db\Adapter\Mysql\Driver\Mysqli;
66

77
use Laminas\Db\Adapter\Mysql\Driver\Mysqli\Connection;
8+
use LaminasIntegrationTest\Db\Adapter\Mysql\Container\TestAsset\SetupTrait;
89
use PHPUnit\Framework\Attributes\CoversMethod;
910
use PHPUnit\Framework\Attributes\Group;
1011
use PHPUnit\Framework\TestCase;
@@ -16,11 +17,11 @@
1617
#[CoversMethod(Connection::class, 'isConnected')]
1718
final class ConnectionTest extends TestCase
1819
{
19-
use TraitSetup;
20+
use SetupTrait;
2021

2122
public function testConnectionOk(): void
2223
{
23-
$connection = new Connection($this->variables);
24+
$connection = new Connection($this->getConfig()['db']['connection']);
2425
$connection->connect();
2526

2627
self::assertTrue($connection->isConnected());

test/integration/Driver/Mysqli/TableGatewayTest.php

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace LaminasIntegrationTest\Db\Adapter\Mysql\Driver\Mysqli;
66

7-
use Laminas\Db\Adapter\Mysql\Adapter;
7+
use Laminas\Db\Adapter\AdapterInterface;
8+
use Laminas\Db\Adapter\Mysql\Driver\Mysqli\Mysqli;
89
use Laminas\Db\ResultSet\AbstractResultSet;
910
use Laminas\Db\TableGateway\TableGateway;
11+
use LaminasIntegrationTest\Db\Adapter\Mysql\Container\TestAsset\SetupTrait;
1012
use PHPUnit\Framework\Attributes\CoversMethod;
1113
use PHPUnit\Framework\TestCase;
1214

@@ -15,23 +17,34 @@
1517
#[CoversMethod(TableGateway::class, 'select')]
1618
final class TableGatewayTest extends TestCase
1719
{
18-
use TraitSetup;
20+
use SetupTrait;
1921

2022
/**
2123
* @see https://github.com/zendframework/zend-db/issues/330
2224
*/
2325
public function testSelectWithEmptyCurrentWithBufferResult(): void
2426
{
25-
$adapter = new Adapter([
26-
'driver' => 'mysqli',
27-
'database' => $this->variables['database'],
28-
'hostname' => $this->variables['hostname'],
29-
'username' => $this->variables['username'],
30-
'password' => $this->variables['password'],
31-
'options' => ['buffer_results' => true],
27+
// $adapter = new Adapter([
28+
// 'driver' => 'mysqli',
29+
// 'database' => $this->variables['database'],
30+
// 'hostname' => $this->variables['hostname'],
31+
// 'username' => $this->variables['username'],
32+
// 'password' => $this->variables['password'],
33+
// 'options' => ['buffer_results' => true],
34+
// ]);
35+
/** @var AdapterInterface $adapter */
36+
$adapter = $this->getAdapter([
37+
'db' => [
38+
'driver' => Mysqli::class,
39+
'options' => [
40+
'buffer_results' => true,
41+
],
42+
],
3243
]);
44+
3345
$tableGateway = new TableGateway('test', $adapter);
34-
$rowset = $tableGateway->select('id = 0');
46+
/** @var AbstractResultSet $rowset */
47+
$rowset = $tableGateway->select('id = 0');
3548
$this->assertEquals(true, $rowset->isBuffered());
3649

3750
$this->assertNull($rowset->current());
@@ -44,20 +57,20 @@ public function testSelectWithEmptyCurrentWithBufferResult(): void
4457
*/
4558
public function testSelectWithEmptyCurrentWithoutBufferResult(): void
4659
{
47-
$adapter = new Adapter([
48-
'driver' => 'mysqli',
49-
'database' => $this->variables['database'],
50-
'hostname' => $this->variables['hostname'],
51-
'username' => $this->variables['username'],
52-
'password' => $this->variables['password'],
53-
'options' => ['buffer_results' => false],
60+
/** @var AdapterInterface $adapter */
61+
$adapter = $this->getAdapter([
62+
'db' => [
63+
'driver' => 'mysqli',
64+
'options' => [
65+
'buffer_results' => false,
66+
],
67+
],
5468
]);
5569
$tableGateway = new TableGateway('test', $adapter);
70+
/** @var AbstractResultSet $rowset */
5671
$rowset = $tableGateway->select('id = 0');
5772
$this->assertEquals(false, $rowset->isBuffered());
5873

59-
/** @todo Have resultset implememt Iterator */
60-
/** @psalm-suppress UndefinedInterfaceMethod */
6174
$this->assertNull($rowset->current());
6275

6376
$adapter->getDriver()->getConnection()->disconnect();

0 commit comments

Comments
 (0)