Skip to content

Commit d284346

Browse files
committed
Gets unit test working. Still needs work and testing expanded.
Signed-off-by: Joey Smith <jsmith@webinertia.net> Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent 76edd34 commit d284346

29 files changed

+1557
-1028
lines changed

test/integration/Container/TestAsset/SetupTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use PhpDb\Adapter\Driver\DriverInterface;
1212
use PhpDb\Adapter\Sqlite\ConfigProvider;
1313
use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo;
14+
use PhpDb\ConfigProvider as LaminasDbConfigProvider;
1415
use PhpDb\Container\AdapterManager;
15-
use PhpDb\Container\ConfigProvider as LaminasDbConfigProvider;
1616
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
1717
use Psr\Container\ContainerInterface;
1818

test/unit/AdapterServiceFactoryTest.php

Lines changed: 0 additions & 113 deletions
This file was deleted.

test/unit/AdapterTest.php

Lines changed: 38 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace PhpDbTest\Adapter\Sqlite;
46

5-
use InvalidArgumentException;
67
use Override;
7-
use PhpDb\Adapter\AdapterInterface;
8-
use PhpDb\Adapter\Driver\ConnectionInterface;
8+
use PhpDb\Adapter\Adapter;
99
use PhpDb\Adapter\Driver\DriverInterface;
10+
use PhpDb\Adapter\Driver\Pdo\AbstractPdoConnection;
11+
use PhpDb\Adapter\Driver\Pdo\Statement;
1012
use PhpDb\Adapter\Driver\PdoDriverInterface;
1113
use PhpDb\Adapter\Driver\ResultInterface;
1214
use PhpDb\Adapter\Driver\StatementInterface;
13-
use PhpDb\Adapter\ParameterContainer;
1415
use PhpDb\Adapter\Profiler;
15-
use PhpDb\Adapter\Sqlite\Adapter;
1616
use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo;
17-
use PhpDb\Adapter\Sqlite\Driver\Pdo\Statement;
1817
use PhpDb\Adapter\Sqlite\Platform\Sqlite as SqlitePlatform;
19-
use PhpDb\ResultSet\ResultSet;
2018
use PhpDb\ResultSet\ResultSetInterface;
21-
use PhpDbTest\Adapter\Sqlite\TestAsset\TemporaryResultSet;
2219
use PHPUnit\Framework\Attributes\CoversMethod;
23-
use PHPUnit\Framework\Attributes\Group;
2420
use PHPUnit\Framework\Attributes\TestDox;
2521
use PHPUnit\Framework\MockObject\Exception;
2622
use PHPUnit\Framework\MockObject\MockObject;
2723
use PHPUnit\Framework\TestCase;
2824

29-
use function extension_loaded;
30-
3125
#[CoversMethod(Adapter::class, 'setProfiler')]
3226
#[CoversMethod(Adapter::class, 'getProfiler')]
3327
#[CoversMethod(Adapter::class, 'createDriver')]
@@ -45,47 +39,50 @@ final class AdapterTest extends TestCase
4539

4640
protected SqlitePlatform $mockPlatform;
4741

48-
protected ConnectionInterface&MockObject $mockConnection;
42+
protected AbstractPdoConnection&MockObject $mockConnection;
4943

5044
protected StatementInterface&MockObject $mockStatement;
5145

52-
protected Adapter $adapter;
46+
protected ResultSetInterface&MockObject $mockResultSet;
5347

54-
#[TestDox('unit test: Test setProfiler() will store profiler')]
55-
public function testSetProfiler(): void
56-
{
57-
$ret = $this->adapter->setProfiler(new Profiler\Profiler());
58-
self::assertSame($this->adapter, $ret);
59-
}
48+
protected Adapter $adapter;
6049

61-
#[TestDox('unit test: Test getProfiler() will store profiler')]
62-
public function testGetProfiler(): void
50+
/**
51+
* @throws \Exception
52+
*/
53+
#[Override]
54+
protected function setUp(): void
6355
{
64-
$this->adapter->setProfiler($profiler = new Profiler\Profiler());
65-
self::assertSame($profiler, $this->adapter->getProfiler());
56+
$this->mockConnection = $this->createMock(AbstractPdoConnection::class);
57+
$this->mockPlatform = new SqlitePlatform($this->createMock(PdoDriverInterface::class));
58+
$this->mockStatement = $this->getMockBuilder(Statement::class)->getMock();
59+
$this->mockResultSet = $this->getMockBuilder(ResultSetInterface::class)->getMock();
60+
$resultInterface = $this->getMockBuilder(ResultInterface::class)->getMock();
61+
$this->mockDriver = $this->getMockBuilder(Pdo::class)
62+
->setConstructorArgs([
63+
$this->mockConnection,
64+
$this->mockStatement,
65+
$resultInterface,
66+
])
67+
->getMock();
6668

67-
$adapter = new Adapter(['driver' => $this->mockDriver, 'profiler' => true], $this->mockPlatform);
68-
self::assertInstanceOf(Profiler\Profiler::class, $adapter->getProfiler());
69-
}
69+
$this->mockDriver->method('getDatabasePlatformName')->willReturn('Sqlite');
70+
$this->mockDriver->method('checkEnvironment')->willReturn(true);
71+
$this->mockDriver->method('getConnection')->willReturn($this->mockConnection);
72+
$this->mockDriver->method('createStatement')->willReturn($this->mockStatement);
7073

71-
#[TestDox('unit test: Test createDriverFromParameters() will create proper driver type')]
72-
public function testCreateDriver(): void
73-
{
74-
if (extension_loaded('pdo')) {
75-
$adapter = new Adapter(['driver' => 'pdo_sqlite'], $this->mockPlatform);
76-
self::assertInstanceOf(Pdo::class, $adapter->driver);
77-
unset($adapter);
78-
}
74+
$this->adapter = new Adapter(
75+
$this->mockDriver,
76+
$this->mockPlatform,
77+
$this->mockResultSet
78+
);
7979
}
8080

81-
#[TestDox('unit test: Test createPlatformFromDriver() will create proper platform from driver')]
82-
public function testCreatePlatform(): void
81+
#[TestDox('unit test: Test setProfiler() will store profiler')]
82+
public function testSetProfiler(): void
8383
{
84-
$driver = clone $this->mockDriver;
85-
$driver->expects($this->any())->method('getDatabasePlatformName')->willReturn('Sqlite');
86-
$adapter = new Adapter($driver);
87-
self::assertInstanceOf(SqlitePlatform::class, $adapter->platform);
88-
unset($adapter, $driver);
84+
$ret = $this->adapter->setProfiler(new Profiler\Profiler());
85+
self::assertSame($this->adapter, $ret);
8986
}
9087

9188
#[TestDox('unit test: Test getDriver() will return driver object')]
@@ -100,12 +97,6 @@ public function testGetPlatform(): void
10097
self::assertSame($this->mockPlatform, $this->adapter->getPlatform());
10198
}
10299

103-
#[TestDox('unit test: Test getPlatform() returns platform object')]
104-
public function testGetQueryResultSetPrototype(): void
105-
{
106-
self::assertInstanceOf(ResultSetInterface::class, $this->adapter->getQueryResultSetPrototype());
107-
}
108-
109100
#[TestDox('unit test: Test getCurrentSchema() returns current schema from connection object')]
110101
public function testGetCurrentSchema(): void
111102
{
@@ -123,29 +114,6 @@ public function testQueryWhenPreparedProducesStatement(): void
123114
self::assertSame($this->mockStatement, $s);
124115
}
125116

126-
/**
127-
* @throws Exception
128-
* @throws \Exception
129-
*/
130-
#[Group('#210')]
131-
public function testProducedResultSetPrototypeIsDifferentForEachQuery(): void
132-
{
133-
$statement = $this->createMock(StatementInterface::class);
134-
$result = $this->createMock(ResultInterface::class);
135-
136-
$this->mockDriver->method('createStatement')
137-
->willReturn($statement);
138-
$this->mockStatement->method('execute')
139-
->willReturn($result);
140-
$result->method('isQueryResult')
141-
->willReturn(true);
142-
143-
self::assertNotSame(
144-
$this->adapter->query('SELECT foo', []),
145-
$this->adapter->query('SELECT foo', [])
146-
);
147-
}
148-
149117
/**
150118
* @throws \Exception
151119
*/
@@ -164,101 +132,9 @@ public function testQueryWhenPreparedWithParameterArrayProducesResult(): void
164132
self::assertSame($result, $r);
165133
}
166134

167-
/**
168-
* @throws \Exception
169-
*/
170-
#[TestDox('unit test: Test query() in prepare mode, with ParameterContainer, produces a result object')]
171-
public function testQueryWhenPreparedWithParameterContainerProducesResult(): void
172-
{
173-
$sql = 'SELECT foo';
174-
$parameterContainer = $this->getMockBuilder(ParameterContainer::class)->getMock();
175-
$result = $this->getMockBuilder(ResultInterface::class)->getMock();
176-
$this->mockDriver->expects($this->any())->method('createStatement')
177-
->with($sql)->willReturn($this->mockStatement);
178-
$this->mockStatement->expects($this->any())->method('execute')->willReturn($result);
179-
$result->expects($this->any())->method('isQueryResult')->willReturn(true);
180-
181-
$r = $this->adapter->query($sql, $parameterContainer);
182-
self::assertInstanceOf(ResultSet::class, $r);
183-
}
184-
185-
/**
186-
* @throws \Exception
187-
*/
188-
#[TestDox('unit test: Test query() in execute mode produces a driver result object')]
189-
public function testQueryWhenExecutedProducesAResult(): void
190-
{
191-
$sql = 'SELECT foo';
192-
$result = $this->getMockBuilder(ResultInterface::class)->getMock();
193-
$this->mockConnection->expects($this->any())->method('execute')->with($sql)->willReturn($result);
194-
195-
$r = $this->adapter->query($sql, AdapterInterface::QUERY_MODE_EXECUTE);
196-
self::assertSame($result, $r);
197-
}
198-
199-
/**
200-
* @throws \Exception
201-
*/
202-
#[TestDox('unit test: Test query() in execute mode produces a resultset object')]
203-
public function testQueryWhenExecutedProducesAResultSetObjectWhenResultIsQuery(): void
204-
{
205-
$sql = 'SELECT foo';
206-
207-
$result = $this->getMockBuilder(ResultInterface::class)->getMock();
208-
$this->mockConnection->expects($this->any())->method('execute')->with($sql)->willReturn($result);
209-
$result->expects($this->any())->method('isQueryResult')->willReturn(true);
210-
211-
$r = $this->adapter->query($sql, AdapterInterface::QUERY_MODE_EXECUTE);
212-
self::assertInstanceOf(ResultSet::class, $r);
213-
214-
$r = $this->adapter->query($sql, AdapterInterface::QUERY_MODE_EXECUTE, new TemporaryResultSet());
215-
self::assertInstanceOf(TemporaryResultSet::class, $r);
216-
}
217-
218135
#[TestDox('unit test: Test createStatement() produces a statement object')]
219136
public function testCreateStatement(): void
220137
{
221138
self::assertSame($this->mockStatement, $this->adapter->createStatement());
222139
}
223-
224-
public function testMagicGet(): void
225-
{
226-
// @codingStandardsIgnoreEnd
227-
self::assertSame($this->mockDriver, $this->adapter->driver);
228-
/** @psalm-suppress UndefinedMagicPropertyFetch */
229-
self::assertSame($this->mockDriver, $this->adapter->DrivER);
230-
/** @psalm-suppress UndefinedMagicPropertyFetch */
231-
self::assertSame($this->mockPlatform, $this->adapter->PlatForm);
232-
self::assertSame($this->mockPlatform, $this->adapter->platform);
233-
234-
$this->expectException(InvalidArgumentException::class);
235-
$this->expectExceptionMessage('Invalid magic');
236-
$this->adapter->foo;
237-
}
238-
239-
// @codingStandardsIgnoreStart
240-
241-
/**
242-
* @throws Exception
243-
*/
244-
#[Override]
245-
protected function setUp(): void
246-
{
247-
$this->mockConnection = $this->createMock(ConnectionInterface::class);
248-
$this->mockPlatform = new SqlitePlatform($this->createMock(PdoDriverInterface::class));
249-
$this->mockStatement = $this->getMockBuilder(Statement::class)->getMock();
250-
$this->mockDriver = $this->getMockBuilder(Pdo::class)
251-
->setConstructorArgs([
252-
$this->mockConnection,
253-
$this->mockStatement,
254-
])
255-
->getMock();
256-
257-
$this->mockDriver->method('getDatabasePlatformName')->willReturn('Sqlite');
258-
$this->mockDriver->method('checkEnvironment')->willReturn(true);
259-
$this->mockDriver->method('getConnection')->willReturn($this->mockConnection);
260-
$this->mockDriver->method('createStatement')->willReturn($this->mockStatement);
261-
262-
$this->adapter = new Adapter($this->mockDriver, $this->mockPlatform);
263-
}
264140
}

0 commit comments

Comments
 (0)