Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<exclude>./test/unit/Adapter/AdapterServiceFactoryTest.php</exclude>
<exclude>./test/unit/Adapter/Driver/Pdo/ConnectionIntegrationTest.php</exclude>
<exclude>./test/unit/Adapter/Driver/Pdo/StatementIntegrationTest.php</exclude>
<exclude>./test/unit/Adapter/AdapterAwareTraitTest.php</exclude>
</testsuite>
<testsuite name="integration test">
<directory>./test/integration</directory>
Expand Down
33 changes: 18 additions & 15 deletions test/unit/Adapter/Driver/AbstractConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

use PhpDb\Adapter\Driver\AbstractConnection;
use PhpDb\Adapter\Profiler\ProfilerInterface;
use PhpDbTest\TestAsset\ConnectionWrapper;
use PhpDbTest\TestAsset\PdoStubDriver;
use PhpDbTest\Adapter\Driver\TestAsset\TestConnection;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
Expand All @@ -25,7 +24,8 @@ final class AbstractConnectionTest extends TestCase
{
public function testDisconnectNullsResourceWhenConnected(): void
{
$connection = new ConnectionWrapper(new PdoStubDriver());
$connection = new TestConnection();
$connection->connect();

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

Expand All @@ -36,8 +36,7 @@ public function testDisconnectNullsResourceWhenConnected(): void

public function testDisconnectIsNoOpWhenNotConnected(): void
{
$connection = new ConnectionWrapper();
$connection->disconnect();
$connection = new TestConnection();

$result = $connection->disconnect();

Expand All @@ -46,28 +45,29 @@ public function testDisconnectIsNoOpWhenNotConnected(): void

public function testGetConnectionParametersReturnsEmptyByDefault(): void
{
$connection = new ConnectionWrapper();
$connection = new TestConnection();

self::assertSame([], $connection->getConnectionParameters());
}

public function testGetDriverNameReturnsDriverAttribute(): void
public function testGetDriverNameReturnsValueWhenSet(): void
{
$connection = new ConnectionWrapper(new PdoStubDriver());
$connection = new TestConnection();
$connection->setConnectionParameters(['driver' => 'sqlite']);

self::assertSame('sqlite', $connection->getDriverName());
self::assertNull($connection->getDriverName());
}

public function testGetProfilerReturnsNullByDefault(): void
{
$connection = new ConnectionWrapper();
$connection = new TestConnection();

self::assertNull($connection->getProfiler());
}

public function testSetProfilerStoresAndReturnsProfiler(): void
{
$connection = new ConnectionWrapper();
$connection = new TestConnection();
$profiler = $this->createMock(ProfilerInterface::class);

$result = $connection->setProfiler($profiler);
Expand All @@ -78,16 +78,19 @@ public function testSetProfilerStoresAndReturnsProfiler(): void

public function testGetResourceAutoConnectsWhenNotConnected(): void
{
$connection = new ConnectionWrapper();
$connection = new TestConnection();

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

$resource = $connection->getResource();

self::assertNotNull($resource);
self::assertTrue($connection->isConnected());
self::assertSame('fake-resource', $resource);
}

public function testSetConnectionParametersStoresAndReturnsConnection(): void
{
$connection = new ConnectionWrapper();
$connection = new TestConnection();
$params = ['host' => 'localhost', 'port' => 3306];

$result = $connection->setConnectionParameters($params);
Expand All @@ -98,7 +101,7 @@ public function testSetConnectionParametersStoresAndReturnsConnection(): void

public function testInTransactionReturnsFalseByDefault(): void
{
$connection = new ConnectionWrapper();
$connection = new TestConnection();

self::assertFalse($connection->inTransaction());
}
Expand Down
2 changes: 2 additions & 0 deletions test/unit/Adapter/Driver/Pdo/PdoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#[CoversMethod(AbstractPdo::class, 'getLastGeneratedValue')]
#[CoversMethod(AbstractPdo::class, 'setProfiler')]
#[CoversMethod(AbstractPdo::class, 'getProfiler')]
#[CoversMethod(AbstractPdo::class, 'formatParameterName')]
#[Group('unit')]
final class PdoTest extends TestCase
{
Expand Down Expand Up @@ -181,6 +182,7 @@ public function testGetProfilerThrowsWhenNotInitialized(): void
$pdo = new TestPdo([]);

$this->expectException(Error::class);
/** @phpstan-ignore method.resultUnused */
$pdo->getProfiler();
}

Expand Down
26 changes: 26 additions & 0 deletions test/unit/Adapter/Driver/Pdo/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,30 @@ public function testExecuteAutoPrepares(): void
self::assertInstanceOf(Result::class, $result);
self::assertTrue($this->statement->isPrepared());
}

public function testSecondExecuteSkipsBindingWhenAlreadyBound(): void
{
$pdo = new SqliteMemoryPdo();
$this->statement->setDriver(new TestPdo(new TestConnection($pdo)));
$this->statement->initialize($pdo);
$this->statement->setSql('SELECT :val');
$this->statement->setParameterContainer(new ParameterContainer(['val' => 'test']));

$result1 = $this->statement->execute();
self::assertInstanceOf(Result::class, $result1);

$result2 = $this->statement->execute();
self::assertInstanceOf(Result::class, $result2);
}

public function testCloneClonesParameterContainerWhenSet(): void
{
$container = new ParameterContainer(['key' => 'value']);
$statement = new Statement($container);

$clone = clone $statement;

self::assertNotSame($container, $clone->getParameterContainer());
self::assertSame('value', $clone->getParameterContainer()->offsetGet('key'));
}
}
56 changes: 56 additions & 0 deletions test/unit/Adapter/Driver/TestAsset/TestConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace PhpDbTest\Adapter\Driver\TestAsset;

use PhpDb\Adapter\Driver\AbstractConnection;
use PhpDb\Adapter\Driver\ConnectionInterface;
use PhpDb\Adapter\Driver\ResultInterface;

final class TestConnection extends AbstractConnection
{
protected ?string $driverName = null;

public function beginTransaction(): ConnectionInterface
{
return $this;
}

public function commit(): ConnectionInterface
{
return $this;
}

public function connect(): ConnectionInterface
{
$this->resource = 'fake-resource';

return $this;
}

public function execute(string $sql): ?ResultInterface
{
return null;
}

public function getCurrentSchema(): string|false
{
return false;
}

public function getLastGeneratedValue(?string $name = null): string|int|false|null
{
return false;
}

public function isConnected(): bool
{
return $this->resource !== null;
}

public function rollback(): ConnectionInterface
{
return $this;
}
}
1 change: 0 additions & 1 deletion test/unit/Adapter/ParameterContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ public function testOffsetSetThrowsOnInvalidKeyType(): void
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Keys must be string, integer or null');

/** @phpstan-ignore argument.type */
$container->offsetSet(1.5, 'value');
}

Expand Down
16 changes: 16 additions & 0 deletions test/unit/Adapter/Platform/Sql92Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpDb\Adapter\Exception\VunerablePlatformQuoteException;
use PhpDb\Adapter\Platform\AbstractPlatform;
use PhpDb\Adapter\Platform\Sql92;
use PhpDbTest\Adapter\Platform\TestAsset\TestPlatform;
use PhpDbTest\TestAsset\TestSql92Platform;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\Group;
Expand Down Expand Up @@ -177,4 +178,19 @@ public function testQuoteValueEscapesSpecialCharacters(): void
self::assertStringStartsWith("'", $quoted);
self::assertStringEndsWith("'", $quoted);
}

public function testAbstractPlatformQuoteValueThrowsWithoutDriver(): void
{
$platform = new TestPlatform();

$this->expectException(VunerablePlatformQuoteException::class);
$platform->quoteValue('value');
}

public function testAbstractPlatformQuoteValueEscapesWithDriver(): void
{
$platform = new TestPlatform($this->createStub(DriverInterface::class));

self::assertSame("'test\\'value'", $platform->quoteValue("test'value"));
}
}
31 changes: 31 additions & 0 deletions test/unit/Adapter/Platform/TestAsset/TestPlatform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace PhpDbTest\Adapter\Platform\TestAsset;

use Override;
use PhpDb\Adapter\Driver\DriverInterface;
use PhpDb\Adapter\Platform\AbstractPlatform;
use PhpDb\Sql\Platform\Platform;
use PhpDb\Sql\Platform\PlatformDecoratorInterface;

final class TestPlatform extends AbstractPlatform
{
public function __construct(
protected ?DriverInterface $driver = null,
) {
}

#[Override]
public function getName(): string
{
return 'Test';
}

#[Override]
public function getSqlPlatformDecorator(): PlatformDecoratorInterface
{
return new Platform($this);
}
}
10 changes: 5 additions & 5 deletions test/unit/Adapter/Profiler/ProfilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use TypeError;

#[CoversMethod(Profiler::class, 'profilerStart')]
#[CoversMethod(Profiler::class, 'profilerFinish')]
Expand All @@ -33,15 +32,16 @@ protected function setUp(): void
$this->profiler = new Profiler();
}

public function testProfilerStart(): void
public function testProfilerStartWithString(): void
{
$ret = $this->profiler->profilerStart('SELECT * FROM FOO');
self::assertSame($this->profiler, $ret);
}

public function testProfilerStartWithStatementContainer(): void
{
$ret = $this->profiler->profilerStart(new StatementContainer());
self::assertSame($this->profiler, $ret);

$this->expectException(TypeError::class);
$this->profiler->profilerStart(5);
}

public function testProfilerFinish(): void
Expand Down
Loading