From 2142d716c2ff0541e16409339b2087948ad5f785 Mon Sep 17 00:00:00 2001 From: Simon Mundy Date: Tue, 17 Nov 2026 19:59:12 +1100 Subject: [PATCH 1/5] Use named arguments and plain adapter service names in tests Pass optional literal arguments by name and replace namespace-shaped adapter service names with plain names, matching the documented configuration style. --- .../AbstractAdapterInterfaceFactoryTest.php | 18 +++++++++--------- .../ResultSet/ResultSetIntegrationTest.php | 6 +++--- test/unit/Sql/Ddl/AlterTableTest.php | 4 ++-- test/unit/Sql/TableIdentifierFactoryTest.php | 16 ++++++++-------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/test/unit/Adapter/Container/AbstractAdapterInterfaceFactoryTest.php b/test/unit/Adapter/Container/AbstractAdapterInterfaceFactoryTest.php index 298529267..5b0019329 100644 --- a/test/unit/Adapter/Container/AbstractAdapterInterfaceFactoryTest.php +++ b/test/unit/Adapter/Container/AbstractAdapterInterfaceFactoryTest.php @@ -37,15 +37,15 @@ final class AbstractAdapterInterfaceFactoryTest extends TestCase public static function providerInvalidService(): array { return [ - ['PhpDb\Adapter\Unknown'], + 'unconfigured adapter' => ['UnknownAdapter'], ]; } public static function providerValidService(): array { return [ - ['PhpDb\Adapter\Writer'], - ['PhpDb\Adapter\Reader'], + 'write adapter' => ['WriteAdapter'], + 'read adapter' => ['ReadAdapter'], ]; } @@ -57,7 +57,7 @@ public function canCreateReturnsFalseForEmptyConfig(): void $factory = new AbstractAdapterInterfaceFactory(); - static::assertFalse($factory->canCreate($container, 'PhpDb\Adapter\Writer')); + static::assertFalse($factory->canCreate($container, 'WriteAdapter')); } #[Test] @@ -109,17 +109,17 @@ public function invokeThrowsWhenDriverNotConfigured(): void $container->setService('config', [ AdapterInterface::class => [ 'adapters' => [ - 'PhpDb\Adapter\NoDriver' => [], + 'NoDriverAdapter' => [], ], ], ]); $factory = new AbstractAdapterInterfaceFactory(); - $factory->canCreate($container, 'PhpDb\Adapter\NoDriver'); + $factory->canCreate($container, 'NoDriverAdapter'); self::expectException(ContainerException::class); self::expectExceptionMessage(ContainerException::MISSING_DRIVER); - $factory($container, 'PhpDb\Adapter\NoDriver'); + $factory($container, requestedName: 'NoDriverAdapter'); } #[Test] @@ -197,10 +197,10 @@ protected function setUp(): void $this->serviceManager->setService('config', [ AdapterInterface::class => [ 'adapters' => [ - 'PhpDb\Adapter\Writer' => [ + 'WriteAdapter' => [ 'driver' => PdoStubDriver::class, ], - 'PhpDb\Adapter\Reader' => [ + 'ReadAdapter' => [ 'driver' => PdoStubDriver::class, ], ], diff --git a/test/unit/ResultSet/ResultSetIntegrationTest.php b/test/unit/ResultSet/ResultSetIntegrationTest.php index 814a3a852..90f2ce89c 100644 --- a/test/unit/ResultSet/ResultSetIntegrationTest.php +++ b/test/unit/ResultSet/ResultSetIntegrationTest.php @@ -231,7 +231,7 @@ public function canProvideIteratorAsDataSource(): void #[Test] public function countReturnsCountOfRows(): void { - $count = random_int(3, 75); + $count = random_int(3, max: 75); $dataSource = $this->getArrayDataSource($count); // Verify count() returns correct number of rows $this->resultSet->initialize($dataSource); @@ -476,12 +476,12 @@ public function settingInvalidReturnTypeRaisesException(mixed $type): void #[Test] public function toArrayCreatesArrayOfArraysRepresentingRows(): void { - $count = random_int(3, 75); + $count = random_int(3, max: 75); $dataSource = $this->getArrayDataSource($count); // Verify toArray() returns array representation of all rows $this->resultSet->initialize($dataSource); $test = $this->resultSet->toArray(); - static::assertEquals($dataSource->getArrayCopy(), $test, var_export($test, true)); + static::assertEquals($dataSource->getArrayCopy(), $test, var_export($test, return: true)); } /** diff --git a/test/unit/Sql/Ddl/AlterTableTest.php b/test/unit/Sql/Ddl/AlterTableTest.php index 1200b3657..00402e0f1 100644 --- a/test/unit/Sql/Ddl/AlterTableTest.php +++ b/test/unit/Sql/Ddl/AlterTableTest.php @@ -282,8 +282,8 @@ public function getSqlString(): void $actual = $at->getSqlString(); static::assertEquals( - str_replace(["\r", "\n"], '', $expected), - str_replace(["\r", "\n"], '', $actual), + str_replace(["\r", "\n"], replace: '', subject: $expected), + str_replace(["\r", "\n"], replace: '', subject: $actual), ); $at = new AlterTable(new TableIdentifier('foo')); diff --git a/test/unit/Sql/TableIdentifierFactoryTest.php b/test/unit/Sql/TableIdentifierFactoryTest.php index 0fcf8a108..dc84eacc5 100644 --- a/test/unit/Sql/TableIdentifierFactoryTest.php +++ b/test/unit/Sql/TableIdentifierFactoryTest.php @@ -19,7 +19,7 @@ final class TableIdentifierFactoryTest extends TestCase public function callTimePrefixIsAppliedWhenNonePreconfigured(): void { $factory = new TableIdentifierFactory(); - $tableIdentifier = $factory('users', null, 'archive'); + $tableIdentifier = $factory('users', prefix: 'archive'); static::assertSame('archive', $tableIdentifier->getPrefix()); static::assertSame('archive_users', $tableIdentifier->getTable()); @@ -29,7 +29,7 @@ public function callTimePrefixIsAppliedWhenNonePreconfigured(): void public function callTimePrefixOverridesConfiguredPrefix(): void { $factory = new TableIdentifierFactory('backup'); - $tableIdentifier = $factory('users', null, 'archive'); + $tableIdentifier = $factory('users', prefix: 'archive'); static::assertSame('archive', $tableIdentifier->getPrefix()); static::assertSame('archive_users', $tableIdentifier->getTable()); @@ -39,7 +39,7 @@ public function callTimePrefixOverridesConfiguredPrefix(): void public function callTimeSeparatorIsAppliedWhenNonePreconfigured(): void { $factory = new TableIdentifierFactory(); - $tableIdentifier = $factory('users', null, 'archive', '__'); + $tableIdentifier = $factory('users', prefix: 'archive', separator: '__'); static::assertSame('__', $tableIdentifier->getSeparator()); static::assertSame('archive__users', $tableIdentifier->getTable()); @@ -49,7 +49,7 @@ public function callTimeSeparatorIsAppliedWhenNonePreconfigured(): void public function callTimeSeparatorOverridesConfiguredSeparator(): void { $factory = new TableIdentifierFactory('backup', '__'); - $tableIdentifier = $factory('users', null, null, '_'); + $tableIdentifier = $factory('users', separator: '_'); static::assertSame('_', $tableIdentifier->getSeparator()); static::assertSame('backup_users', $tableIdentifier->getTable()); @@ -91,7 +91,7 @@ public function createsIdentifierWithConfiguredSeparator(): void public function createsIdentifierWithoutPrefixWhenNoneConfigured(): void { $factory = new TableIdentifierFactory(); - $tableIdentifier = $factory('users', 'public'); + $tableIdentifier = $factory('users', schema: 'public'); static::assertNull($tableIdentifier->getPrefix()); static::assertSame('users', $tableIdentifier->getTable()); @@ -101,7 +101,7 @@ public function createsIdentifierWithoutPrefixWhenNoneConfigured(): void public function createsIdentifierWithSchema(): void { $factory = new TableIdentifierFactory('backup'); - $tableIdentifier = $factory('users', 'public'); + $tableIdentifier = $factory('users', schema: 'public'); static::assertSame(['backup_users', 'public'], $tableIdentifier->getTableAndSchema()); } @@ -121,7 +121,7 @@ public function rejectsEmptyStringCallTimePrefix(): void self::expectException(InvalidArgumentException::class); self::expectExceptionMessage(InvalidArgumentException::EMPTY_PREFIX); - $factory('users', null, ''); + $factory('users', prefix: ''); } #[Test] @@ -131,7 +131,7 @@ public function rejectsEmptyStringCallTimeSeparator(): void self::expectException(InvalidArgumentException::class); self::expectExceptionMessage(InvalidArgumentException::EMPTY_SEPARATOR); - $factory('users', null, null, ''); + $factory('users', separator: ''); } #[Test] From 4bd4d0bb6ae892d3829aa5ade5779b95b08190b5 Mon Sep 17 00:00:00 2001 From: Simon Mundy Date: Tue, 17 Nov 2026 19:59:12 +1100 Subject: [PATCH 2/5] Remove error suppression and empty catch from adapter tests Use PDO::ERRMODE_SILENT so the failing query takes the same error path without emitting a warning, drop the unreachable suppressed assertions in Sql92Test in favour of asserting the exception message, connect directly instead of swallowing exceptions, and drop unused credentials from the SQLite fixture. --- .../Adapter/Driver/Pdo/ConnectionTest.php | 26 ++++++------------- test/unit/Adapter/Platform/Sql92Test.php | 18 ++++++------- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/test/unit/Adapter/Driver/Pdo/ConnectionTest.php b/test/unit/Adapter/Driver/Pdo/ConnectionTest.php index 35f0e5f64..3a668c467 100644 --- a/test/unit/Adapter/Driver/Pdo/ConnectionTest.php +++ b/test/unit/Adapter/Driver/Pdo/ConnectionTest.php @@ -5,7 +5,6 @@ namespace PhpDbTest\Adapter\Driver\Pdo; use Closure; -use Exception; use Override; use PDO; use PhpDb\Adapter\Driver\Pdo\AbstractPdoConnection; @@ -101,7 +100,7 @@ public function constructorWithPdoResourceSetsConnected(): void public function executeAutoConnectsWhenNotConnected(): void { $connection = new TestConnection(['dsn' => 'sqlite::memory:']); - $driver = new TestPdo($connection); + new TestPdo($connection); static::assertFalse($connection->isConnected()); @@ -118,13 +117,13 @@ public function executeCallsProfilerFinishBeforeThrowingOnError(): void $profiler->expects($this->once())->method('profilerFinish')->willReturnSelf(); $pdo = new SqliteMemoryPdo(); - $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); $connection = new TestConnection($pdo); - $driver = new TestPdo($connection); + new TestPdo($connection); $connection->setProfiler($profiler); self::expectException(InvalidQueryException::class); - @$connection->execute('INVALID SQL STATEMENT HERE %%%'); + $connection->execute('INVALID SQL STATEMENT HERE %%%'); } #[Test] @@ -136,7 +135,7 @@ public function executeCallsProfilerStartAndFinish(): void $pdo = new SqliteMemoryPdo(); $connection = new TestConnection($pdo); - $driver = new TestPdo($connection); + new TestPdo($connection); $connection->setProfiler($profiler); $connection->execute('SELECT 1'); @@ -171,10 +170,7 @@ public function getDsnReturnsDsnAfterConnect(): void { $dsn = 'sqlite::memory:'; $this->connection->setConnectionParameters(['dsn' => $dsn]); - try { - $this->connection->connect(); - } catch (Exception) { - } + $this->connection->connect(); $responseString = $this->connection->getDsn(); static::assertEquals($dsn, $responseString); @@ -199,7 +195,7 @@ public function getDsnThrowsWhenDsnIsNull(): void public function prepareAutoConnectsAndReturnsStatement(): void { $connection = new TestConnection(['dsn' => 'sqlite::memory:']); - $driver = new TestPdo($connection); + new TestPdo($connection); static::assertFalse($connection->isConnected()); @@ -292,12 +288,6 @@ public function throwsWhenConnectingDoesNotProduceAPdoResource(Closure $operatio #[Override] protected function setUp(): void { - $this->connection = new TestConnection( - [ - 'dsn' => 'sqlite::memory:', - 'username' => 'bar', - 'password' => 'baz', - ], - ); + $this->connection = new TestConnection(['dsn' => 'sqlite::memory:']); } } diff --git a/test/unit/Adapter/Platform/Sql92Test.php b/test/unit/Adapter/Platform/Sql92Test.php index cb3c92762..189336b0a 100644 --- a/test/unit/Adapter/Platform/Sql92Test.php +++ b/test/unit/Adapter/Platform/Sql92Test.php @@ -17,6 +17,8 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use function sprintf; + #[CoversMethod(Sql92::class, 'getName')] #[CoversMethod(Sql92::class, 'getQuoteIdentifierSymbol')] #[CoversMethod(Sql92::class, 'quoteIdentifier')] @@ -247,16 +249,12 @@ public function quoteValueRaisesNoticeWithoutPlatformSupport(): void public function quoteValueThrowsWithoutDriver(): void { $this->expectException(RuntimeException::class); - static::assertSame("'value'", @$this->platform->quoteValue('value')); - static::assertSame("'Foo O\\'Bar'", @$this->platform->quoteValue("Foo O'Bar")); - static::assertSame( - '\'\\\'; DELETE FROM some_table; -- \'', - @$this->platform->quoteValue("'; DELETE FROM some_table; -- "), - ); - static::assertSame( - "'\\\\\\'; DELETE FROM some_table; -- '", - @$this->platform->quoteValue('\\\'; DELETE FROM some_table; -- '), - ); + $this->expectExceptionMessage(sprintf( + RuntimeException::VULNERABLE_PLATFORM_QUOTE, + Sql92::class, + Sql92::class . '::quoteValue', + )); + $this->platform->quoteValue("Foo O'Bar"); } /** From cc5e5e814506af19c013444d69438a362576b741 Mon Sep 17 00:00:00 2001 From: Simon Mundy Date: Tue, 17 Nov 2026 19:59:12 +1100 Subject: [PATCH 3/5] Remove redundant variables and assignments in arguments from adapter tests --- test/unit/Adapter/AdapterTest.php | 3 ++- .../Adapter/Driver/Pdo/ConnectionTransactionsTest.php | 4 ++-- test/unit/Adapter/Driver/Pdo/PdoTest.php | 2 +- .../Adapter/Driver/Pdo/StatementIntegrationTest.php | 10 +++++----- test/unit/Adapter/Driver/Pdo/StatementTest.php | 6 ++++-- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/test/unit/Adapter/AdapterTest.php b/test/unit/Adapter/AdapterTest.php index ac5cfe4f9..bae4395ba 100644 --- a/test/unit/Adapter/AdapterTest.php +++ b/test/unit/Adapter/AdapterTest.php @@ -280,7 +280,8 @@ public function getPlatformReturnsPlatform(): void #[TestDox('unit test: Test getProfiler() will store profiler')] public function getProfilerReturnsProfiler(): void { - $this->adapter->setProfiler($profiler = new Profiler\Profiler()); + $profiler = new Profiler\Profiler(); + $this->adapter->setProfiler($profiler); static::assertSame($profiler, $this->adapter->getProfiler()); $adapter = new Adapter( diff --git a/test/unit/Adapter/Driver/Pdo/ConnectionTransactionsTest.php b/test/unit/Adapter/Driver/Pdo/ConnectionTransactionsTest.php index d719a0714..822ecbbdf 100644 --- a/test/unit/Adapter/Driver/Pdo/ConnectionTransactionsTest.php +++ b/test/unit/Adapter/Driver/Pdo/ConnectionTransactionsTest.php @@ -114,7 +114,7 @@ public function nestedTransactionsCommit(): void // 2nd commit $this->wrapper->commit(); static::assertFalse($this->wrapper->inTransaction()); - static::assertSame(--$nested, $this->wrapper->getNestedTransactionsCount()); + static::assertSame($nested - 1, $this->wrapper->getNestedTransactionsCount()); } #[Test] @@ -132,7 +132,7 @@ public function nestedTransactionsRollback(): void // 2nd transaction $this->wrapper->beginTransaction(); static::assertTrue($this->wrapper->inTransaction()); - static::assertSame(++$nested, $this->wrapper->getNestedTransactionsCount()); + static::assertSame($nested + 1, $this->wrapper->getNestedTransactionsCount()); // Rollback $this->wrapper->rollback(); diff --git a/test/unit/Adapter/Driver/Pdo/PdoTest.php b/test/unit/Adapter/Driver/Pdo/PdoTest.php index 3a448073d..56f99b6dc 100644 --- a/test/unit/Adapter/Driver/Pdo/PdoTest.php +++ b/test/unit/Adapter/Driver/Pdo/PdoTest.php @@ -238,7 +238,7 @@ public function getProfilerThrowsWhenNotInitialized(): void $this->expectException(Error::class); - $unused = $pdo->getProfiler(); + $pdo->getProfiler(); } #[Test] diff --git a/test/unit/Adapter/Driver/Pdo/StatementIntegrationTest.php b/test/unit/Adapter/Driver/Pdo/StatementIntegrationTest.php index 033fc3e39..4a02ec286 100644 --- a/test/unit/Adapter/Driver/Pdo/StatementIntegrationTest.php +++ b/test/unit/Adapter/Driver/Pdo/StatementIntegrationTest.php @@ -87,13 +87,13 @@ protected function setUp(): void ->disableOriginalConstructor() ->getMock(); + $this->pdoStatementMock = $this->getMockBuilder('PDOStatement') + ->onlyMethods(['execute', 'bindParam']) + ->getMock(); + $this->statement = new Statement(); $this->statement->setDriver($driver); - $this->statement->initialize(new TestAsset\CtorlessPdo( - $this->pdoStatementMock = $this->getMockBuilder('PDOStatement') - ->onlyMethods(['execute', 'bindParam']) - ->getMock(), - )); + $this->statement->initialize(new TestAsset\CtorlessPdo($this->pdoStatementMock)); } /** diff --git a/test/unit/Adapter/Driver/Pdo/StatementTest.php b/test/unit/Adapter/Driver/Pdo/StatementTest.php index 4db3cedd7..6e4fa33b8 100644 --- a/test/unit/Adapter/Driver/Pdo/StatementTest.php +++ b/test/unit/Adapter/Driver/Pdo/StatementTest.php @@ -414,7 +414,8 @@ public function executeCastsNonIntErrorCodeToZero(): void #[Test] public function executeReturnsResult(): void { - $this->statement->setDriver(new TestPdo(new TestConnection($pdo = new SqliteMemoryPdo()))); + $pdo = new SqliteMemoryPdo(); + $this->statement->setDriver(new TestPdo(new TestConnection($pdo))); $this->statement->initialize($pdo); $this->statement->prepare('SELECT 1'); static::assertInstanceOf(Result::class, $this->statement->execute()); @@ -444,7 +445,8 @@ public function executeThrowsInvalidQueryExceptionOnPdoException(): void #[DataProvider('invalidParameterNameProvider')] public function executeThrowsOnInvalidParameterName(string $name): void { - $this->statement->setDriver(new TestPdo(new TestConnection($pdo = new SqliteMemoryPdo()))); + $pdo = new SqliteMemoryPdo(); + $this->statement->setDriver(new TestPdo(new TestConnection($pdo))); $this->statement->initialize($pdo); $this->statement->prepare('SELECT 1'); From c88ad7b7857f92e1e32100d8634836a60ba1dbe3 Mon Sep 17 00:00:00 2001 From: Simon Mundy Date: Tue, 17 Nov 2026 19:59:12 +1100 Subject: [PATCH 4/5] Give snake-case Sql tests and variables behaviour-describing camelCase names Rename the _get/_isset/_set/_unset/_construct tests, drop their coding standards ignore markers, camel-case the SelectTest data provider variables, and remove a redundant variable and an assignment in an argument. --- test/unit/Sql/InsertIgnoreTest.php | 102 +++++++++++------------- test/unit/Sql/InsertTest.php | 122 ++++++++++++++--------------- test/unit/Sql/SelectTest.php | 43 +++++----- test/unit/Sql/SqlTest.php | 32 ++++---- 4 files changed, 141 insertions(+), 158 deletions(-) diff --git a/test/unit/Sql/InsertIgnoreTest.php b/test/unit/Sql/InsertIgnoreTest.php index 6b3a5461c..e80793ee8 100644 --- a/test/unit/Sql/InsertIgnoreTest.php +++ b/test/unit/Sql/InsertIgnoreTest.php @@ -36,61 +36,6 @@ final class InsertIgnoreTest extends TestCase protected InsertIgnore $insert; - // @codingStandardsIgnoreStart - #[Test] - public function _get(): void - { - // @codingStandardsIgnoreEnd - $this->insert->foo = 'bar'; - static::assertSame('bar', $this->insert->foo); - - $this->insert->foo = null; - static::assertNull($this->insert->foo); - } - - // @codingStandardsIgnoreStart - #[Test] - public function _isset(): void - { - // @codingStandardsIgnoreEnd - $this->insert->foo = 'bar'; - static::assertTrue(isset($this->insert->foo)); - - $this->insert->foo = null; - static::assertTrue(isset($this->insert->foo)); - } - - // @codingStandardsIgnoreStart - #[Test] - public function _set(): void - { - // @codingStandardsIgnoreEnd - $this->insert->foo = 'bar'; - static::assertEquals(['foo'], $this->insert->getRawState('columns')); - static::assertEquals(['bar'], $this->insert->getRawState('values')); - } - - // @codingStandardsIgnoreStart - #[Test] - public function _unset(): void - { - // @codingStandardsIgnoreEnd - $this->insert->foo = 'bar'; - static::assertEquals(['foo'], $this->insert->getRawState('columns')); - static::assertEquals(['bar'], $this->insert->getRawState('values')); - unset($this->insert->foo); - static::assertEquals([], $this->insert->getRawState('columns')); - static::assertEquals([], $this->insert->getRawState('values')); - - $this->insert->foo = null; - static::assertEquals(['foo'], $this->insert->getRawState('columns')); - static::assertEquals([null], $this->insert->getRawState('values')); - - unset($this->insert->foo); - static::assertEquals([], $this->insert->getRawState('columns')); - static::assertEquals([], $this->insert->getRawState('values')); - } - #[Test] public function columns(): void { @@ -174,6 +119,53 @@ public function into(): void static::assertEquals($tableIdentifier, $this->insert->getRawState('table')); } + #[Test] + public function magicGetReturnsAssignedValue(): void + { + $this->insert->foo = 'bar'; + static::assertSame('bar', $this->insert->foo); + + $this->insert->foo = null; + static::assertNull($this->insert->foo); + } + + #[Test] + public function magicIssetIsTrueForAssignedColumnEvenWhenNull(): void + { + $this->insert->foo = 'bar'; + static::assertTrue(isset($this->insert->foo)); + + $this->insert->foo = null; + static::assertTrue(isset($this->insert->foo)); + } + + #[Test] + public function magicSetAddsColumnAndValue(): void + { + $this->insert->foo = 'bar'; + static::assertEquals(['foo'], $this->insert->getRawState('columns')); + static::assertEquals(['bar'], $this->insert->getRawState('values')); + } + + #[Test] + public function magicUnsetRemovesColumnAndValue(): void + { + $this->insert->foo = 'bar'; + static::assertEquals(['foo'], $this->insert->getRawState('columns')); + static::assertEquals(['bar'], $this->insert->getRawState('values')); + unset($this->insert->foo); + static::assertEquals([], $this->insert->getRawState('columns')); + static::assertEquals([], $this->insert->getRawState('values')); + + $this->insert->foo = null; + static::assertEquals(['foo'], $this->insert->getRawState('columns')); + static::assertEquals([null], $this->insert->getRawState('values')); + + unset($this->insert->foo); + static::assertEquals([], $this->insert->getRawState('columns')); + static::assertEquals([], $this->insert->getRawState('values')); + } + #[Test] public function prepareStatement(): void { diff --git a/test/unit/Sql/InsertTest.php b/test/unit/Sql/InsertTest.php index 4d6b8d421..be0080642 100644 --- a/test/unit/Sql/InsertTest.php +++ b/test/unit/Sql/InsertTest.php @@ -66,70 +66,6 @@ public static function aliasedTableProvider(): array ]; } - // @codingStandardsIgnoreStart - #[Test] - public function _get(): void - { - // @codingStandardsIgnoreEnd - /** @psalm-suppress UndefinedMagicPropertyAssignment */ - $this->insert->foo = 'bar'; - static::assertSame('bar', $this->insert->foo); - - /** @psalm-suppress UndefinedMagicPropertyAssignment */ - $this->insert->foo = null; - static::assertNull($this->insert->foo); - } - - // @codingStandardsIgnoreStart - #[Test] - public function _isset(): void - { - // @codingStandardsIgnoreEnd - /** @psalm-suppress UndefinedMagicPropertyAssignment */ - $this->insert->foo = 'bar'; - /** @psalm-suppress RedundantCondition */ - static::assertTrue(isset($this->insert->foo)); - - /** @psalm-suppress UndefinedMagicPropertyAssignment */ - $this->insert->foo = null; - /** @psalm-suppress TypeDoesNotContainType */ - static::assertTrue(isset($this->insert->foo)); - } - - // @codingStandardsIgnoreStart - #[Test] - public function _set(): void - { - // @codingStandardsIgnoreEnd - /** @psalm-suppress UndefinedMagicPropertyAssignment */ - $this->insert->foo = 'bar'; - static::assertEquals(['foo'], $this->insert->getRawState('columns')); - static::assertEquals(['bar'], $this->insert->getRawState('values')); - } - - // @codingStandardsIgnoreStart - #[Test] - public function _unset(): void - { - // @codingStandardsIgnoreEnd - /** @psalm-suppress UndefinedMagicPropertyAssignment */ - $this->insert->foo = 'bar'; - static::assertEquals(['foo'], $this->insert->getRawState('columns')); - static::assertEquals(['bar'], $this->insert->getRawState('values')); - unset($this->insert->foo); - static::assertEquals([], $this->insert->getRawState('columns')); - static::assertEquals([], $this->insert->getRawState('values')); - - /** @psalm-suppress UndefinedMagicPropertyAssignment */ - $this->insert->foo = null; - static::assertEquals(['foo'], $this->insert->getRawState('columns')); - static::assertEquals([null], $this->insert->getRawState('values')); - - unset($this->insert->foo); - static::assertEquals([], $this->insert->getRawState('columns')); - static::assertEquals([], $this->insert->getRawState('values')); - } - #[Test] public function columns(): void { @@ -232,7 +168,7 @@ public function getThrowsExceptionForNonExistentColumn(): void { self::expectException(InvalidArgumentException::class); self::expectExceptionMessage(sprintf(InvalidArgumentException::UNKNOWN_COLUMN_KEY, 'nonexistent')); - $value = $this->insert->nonexistent; + $this->insert->nonexistent; } #[Test] @@ -246,6 +182,62 @@ public function into(): void static::assertEquals($tableIdentifier, $this->insert->getRawState('table')); } + #[Test] + public function magicGetReturnsAssignedValue(): void + { + /** @psalm-suppress UndefinedMagicPropertyAssignment */ + $this->insert->foo = 'bar'; + static::assertSame('bar', $this->insert->foo); + + /** @psalm-suppress UndefinedMagicPropertyAssignment */ + $this->insert->foo = null; + static::assertNull($this->insert->foo); + } + + #[Test] + public function magicIssetIsTrueForAssignedColumnEvenWhenNull(): void + { + /** @psalm-suppress UndefinedMagicPropertyAssignment */ + $this->insert->foo = 'bar'; + /** @psalm-suppress RedundantCondition */ + static::assertTrue(isset($this->insert->foo)); + + /** @psalm-suppress UndefinedMagicPropertyAssignment */ + $this->insert->foo = null; + /** @psalm-suppress TypeDoesNotContainType */ + static::assertTrue(isset($this->insert->foo)); + } + + #[Test] + public function magicSetAddsColumnAndValue(): void + { + /** @psalm-suppress UndefinedMagicPropertyAssignment */ + $this->insert->foo = 'bar'; + static::assertEquals(['foo'], $this->insert->getRawState('columns')); + static::assertEquals(['bar'], $this->insert->getRawState('values')); + } + + #[Test] + public function magicUnsetRemovesColumnAndValue(): void + { + /** @psalm-suppress UndefinedMagicPropertyAssignment */ + $this->insert->foo = 'bar'; + static::assertEquals(['foo'], $this->insert->getRawState('columns')); + static::assertEquals(['bar'], $this->insert->getRawState('values')); + unset($this->insert->foo); + static::assertEquals([], $this->insert->getRawState('columns')); + static::assertEquals([], $this->insert->getRawState('values')); + + /** @psalm-suppress UndefinedMagicPropertyAssignment */ + $this->insert->foo = null; + static::assertEquals(['foo'], $this->insert->getRawState('columns')); + static::assertEquals([null], $this->insert->getRawState('values')); + + unset($this->insert->foo); + static::assertEquals([], $this->insert->getRawState('columns')); + static::assertEquals([], $this->insert->getRawState('values')); + } + #[Test] public function prepareStatement(): void { diff --git a/test/unit/Sql/SelectTest.php b/test/unit/Sql/SelectTest.php index f4185085d..ad74ce0bb 100644 --- a/test/unit/Sql/SelectTest.php +++ b/test/unit/Sql/SelectTest.php @@ -524,10 +524,10 @@ public static function providerData(): array 'processJoins' => [[['INNER', '"zac"', '(m = n AND c.x) BETWEEN x AND y.z']]], ]; - $select32subselect = new Select(); - $select32subselect->from('bar')->where->like('y', '%Foo%'); + $select32Subselect = new Select(); + $select32Subselect->from('bar')->where->like('y', '%Foo%'); $select32 = new Select(); - $select32->from(['x' => $select32subselect]); + $select32->from(['x' => $select32Subselect]); $sqlPrep32 = 'SELECT "x".* FROM (SELECT "bar".* FROM "bar" WHERE "y" LIKE ?) AS "x"'; $sqlStr32 = 'SELECT "x".* FROM (SELECT "bar".* FROM "bar" WHERE "y" LIKE \'%Foo%\') AS "x"'; @@ -621,10 +621,10 @@ public static function providerData(): array ]; // subselect in join - $select39subselect = new Select(); - $select39subselect->from('bar')->where->like('y', '%Foo%'); + $select39Subselect = new Select(); + $select39Subselect->from('bar')->where->like('y', '%Foo%'); $select39 = new Select(); - $select39->from('foo')->join(['z' => $select39subselect], 'z.foo = bar.id'); + $select39->from('foo')->join(['z' => $select39Subselect], 'z.foo = bar.id'); $sqlPrep39 = 'SELECT "foo".*, "z".* FROM "foo" INNER JOIN (SELECT "bar".* FROM "bar" WHERE "y" LIKE ?) AS "z" ON "z"."foo" = "bar"."id"'; $sqlStr39 = 'SELECT "foo".*, "z".* FROM "foo" INNER JOIN (SELECT "bar".* FROM "bar" WHERE "y" LIKE \'%Foo%\') AS "z" ON "z"."foo" = "bar"."id"'; $internalTests39 = [ @@ -683,9 +683,9 @@ public static function providerData(): array $select44 = new Select(); $select44->from('foo')->where('a = b'); - $select44b = new Select(); - $select44b->from('bar')->where('c = d'); - $select44->combine($select44b, Select::COMBINE_UNION, 'ALL'); + $select44Other = new Select(); + $select44Other->from('bar')->where('c = d'); + $select44->combine($select44Other, Select::COMBINE_UNION, 'ALL'); $sqlPrep44 = '( SELECT "foo".* FROM "foo" WHERE a = b ) UNION ALL ( SELECT "bar".* FROM "bar" WHERE c = d )'; $sqlStr44 = '( SELECT "foo".* FROM "foo" WHERE a = b ) UNION ALL ( SELECT "bar".* FROM "bar" WHERE c = d )'; $internalTests44 = [ @@ -730,15 +730,15 @@ public static function providerData(): array //combine and union with order at the end $select48 = new Select(); $select48->from('foo')->where('a = b'); - $select48b = new Select(); - $select48b->from('bar')->where('c = d'); - $select48->combine($select48b); - - $select48combined = new Select(); - $select48 = $select48combined->from(['sub' => $select48])->order('id DESC'); - $sqlPrep48 = 'SELECT "sub".* FROM (( SELECT "foo".* FROM "foo" WHERE a = b ) UNION ( SELECT "bar".* FROM "bar" WHERE c = d )) AS "sub" ORDER BY "id" DESC'; - $sqlStr48 = 'SELECT "sub".* FROM (( SELECT "foo".* FROM "foo" WHERE a = b ) UNION ( SELECT "bar".* FROM "bar" WHERE c = d )) AS "sub" ORDER BY "id" DESC'; - $internalTests48 = [ + $select48Other = new Select(); + $select48Other->from('bar')->where('c = d'); + $select48->combine($select48Other); + + $select48Wrapper = new Select(); + $select48 = $select48Wrapper->from(['sub' => $select48])->order('id DESC'); + $sqlPrep48 = 'SELECT "sub".* FROM (( SELECT "foo".* FROM "foo" WHERE a = b ) UNION ( SELECT "bar".* FROM "bar" WHERE c = d )) AS "sub" ORDER BY "id" DESC'; + $sqlStr48 = 'SELECT "sub".* FROM (( SELECT "foo".* FROM "foo" WHERE a = b ) UNION ( SELECT "bar".* FROM "bar" WHERE c = d )) AS "sub" ORDER BY "id" DESC'; + $internalTests48 = [ 'processCombine' => null, ]; @@ -1197,7 +1197,7 @@ public function getThrowsExceptionForInvalidProperty(): void self::expectException(InvalidArgumentException::class); self::expectExceptionMessage(InvalidArgumentException::INVALID_MAGIC_PROPERTY); /** @noinspection ALL */ - $value = $select->invalidProperty; /** @phpstan-ignore-line */ + $select->invalidProperty; /** @phpstan-ignore-line */ } #[Test] @@ -2012,8 +2012,9 @@ public function whereArgument1IsString(): void #[TestDox('unit test: Test where() will accept a Where object')] public function whereArgument1IsWhereObject(): void { - $select = new Select(); - $select->where($newWhere = new Where()); + $select = new Select(); + $newWhere = new Where(); + $select->where($newWhere); static::assertSame($newWhere, $select->getRawState('where')); } diff --git a/test/unit/Sql/SqlTest.php b/test/unit/Sql/SqlTest.php index 19c878ed6..2c61837ce 100644 --- a/test/unit/Sql/SqlTest.php +++ b/test/unit/Sql/SqlTest.php @@ -70,23 +70,6 @@ public static function foreignTableProvider(): array ]; } - // @codingStandardsIgnoreStart - #[Test] - public function _construct(): void - { - // @codingStandardsIgnoreEnd - $sql = new Sql($this->mockAdapter); - - static::assertFalse($sql->hasTable()); - - $sql->setTable('foo'); - static::assertSame('foo', $sql->getTable()); - - self::expectException(TypeError::class); - /** @noinspection PhpStrictTypeCheckingInspection */ - $sql->setTable(null); - } - #[Test] public function buildSqlString(): void { @@ -256,6 +239,21 @@ public function selectThrowsWhenTableConflicts(): void $this->sql->select(new TableIdentifier('bar')); } + #[Test] + public function tableIsUnsetByDefaultAndRejectsNull(): void + { + $sql = new Sql($this->mockAdapter); + + static::assertFalse($sql->hasTable()); + + $sql->setTable('foo'); + static::assertSame('foo', $sql->getTable()); + + self::expectException(TypeError::class); + /** @noinspection PhpStrictTypeCheckingInspection */ + $sql->setTable(null); + } + #[Test] public function update(): void { From e3d87ab827ad9ad30eff072dbe085d490cb9d1fb Mon Sep 17 00:00:00 2001 From: Simon Mundy Date: Tue, 17 Nov 2026 19:59:12 +1100 Subject: [PATCH 5/5] Clear remaining test lint findings and prune the lint baseline Replace isset/empty/elseif in test assets and helpers, describe assertions, use a deterministic consecutive return in ResultTest, import the predicate Expression class, interpolate quoted identifiers, and drop a stale commented-out TODO fixture. Remove the lint-baseline entries these commits fixed. --- lint-baseline.toml | 300 ------------------ test/unit/Adapter/Driver/Pdo/ResultTest.php | 5 +- .../Driver/Pdo/TestAsset/SqliteMemoryPdo.php | 5 +- .../Driver/Pdo/TestAsset/TestConnection.php | 13 +- test/unit/ResultSet/AbstractResultSetTest.php | 2 +- .../Sql/AbstractSqlFunctionalTestCase.php | 22 +- .../Sql/Platform/AbstractPlatformTest.php | 4 +- test/unit/Sql/Predicate/PredicateTest.php | 10 +- test/unit/Sql/UpdateTest.php | 3 +- 9 files changed, 21 insertions(+), 343 deletions(-) diff --git a/lint-baseline.toml b/lint-baseline.toml index 19faa6c46..829393e8d 100644 --- a/lint-baseline.toml +++ b/lint-baseline.toml @@ -246,132 +246,12 @@ code = "no-shorthand-ternary" message = "Use of the shorthand ternary operator." count = 1 -[[issues]] -file = "test/unit/Adapter/AdapterTest.php" -code = "no-assign-in-argument" -message = "Avoid assignments in function call arguments." -count = 1 - -[[issues]] -file = "test/unit/Adapter/Container/AbstractAdapterInterfaceFactoryTest.php" -code = "literal-named-argument" -message = '''Literal argument `'PhpDb\Adapter\NoDriver'` should be passed as a named argument for clarity.''' -count = 1 - -[[issues]] -file = "test/unit/Adapter/Container/AbstractAdapterInterfaceFactoryTest.php" -code = "no-literal-namespace-string" -message = "Use `::class` notation instead of a hardcoded namespace string." -count = 9 - -[[issues]] -file = "test/unit/Adapter/Driver/Pdo/ConnectionTest.php" -code = "no-empty-catch-clause" -message = "Do not use empty `catch` blocks." -count = 1 - -[[issues]] -file = "test/unit/Adapter/Driver/Pdo/ConnectionTest.php" -code = "no-error-control-operator" -message = "Unsafe use of error control operator `@`." -count = 1 - -[[issues]] -file = "test/unit/Adapter/Driver/Pdo/ConnectionTest.php" -code = "no-literal-password" -message = "Literal passwords or sensitive data should not be stored in code." -count = 1 - -[[issues]] -file = "test/unit/Adapter/Driver/Pdo/ConnectionTest.php" -code = "no-redundant-variable" -message = "Variable `$driver` is assigned but never used." -count = 4 - -[[issues]] -file = "test/unit/Adapter/Driver/Pdo/ConnectionTransactionsTest.php" -code = "no-redundant-variable" -message = "Variable `$nested` is assigned but never used." -count = 2 - -[[issues]] -file = "test/unit/Adapter/Driver/Pdo/PdoTest.php" -code = "no-redundant-variable" -message = "Variable `$unused` is assigned but never used." -count = 1 - -[[issues]] -file = "test/unit/Adapter/Driver/Pdo/ResultTest.php" -code = "assert-description" -message = "Missing description in assert function." -count = 1 - -[[issues]] -file = "test/unit/Adapter/Driver/Pdo/ResultTest.php" -code = "prefer-first-class-callable" -message = "Use first-class callable syntax `...` instead of a arrow function." -count = 1 - -[[issues]] -file = "test/unit/Adapter/Driver/Pdo/StatementIntegrationTest.php" -code = "no-assign-in-argument" -message = "Avoid assignments in function call arguments." -count = 1 - -[[issues]] -file = "test/unit/Adapter/Driver/Pdo/StatementTest.php" -code = "no-assign-in-argument" -message = "Avoid assignments in function call arguments." -count = 2 - -[[issues]] -file = "test/unit/Adapter/Driver/Pdo/TestAsset/SqliteMemoryPdo.php" -code = "no-empty" -message = "Use of the `empty` construct." -count = 1 - -[[issues]] -file = "test/unit/Adapter/Driver/Pdo/TestAsset/TestConnection.php" -code = "no-else-clause" -message = "Avoid `elseif` clauses." -count = 1 - -[[issues]] -file = "test/unit/Adapter/Driver/Pdo/TestAsset/TestConnection.php" -code = "no-isset" -message = "Use of the `isset` construct." -count = 1 - [[issues]] file = "test/unit/Adapter/ParameterContainerTest.php" code = "no-isset" message = "Use of the `isset` construct." count = 2 -[[issues]] -file = "test/unit/Adapter/Platform/Sql92Test.php" -code = "no-error-control-operator" -message = "Unsafe use of error control operator `@`." -count = 4 - -[[issues]] -file = "test/unit/ResultSet/AbstractResultSetTest.php" -code = "assert-description" -message = "Missing description in assert function." -count = 1 - -[[issues]] -file = "test/unit/ResultSet/ResultSetIntegrationTest.php" -code = "literal-named-argument" -message = "Literal argument `75` should be passed as a named argument for clarity." -count = 2 - -[[issues]] -file = "test/unit/ResultSet/ResultSetIntegrationTest.php" -code = "literal-named-argument" -message = "Literal argument `true` should be passed as a named argument for clarity." -count = 1 - [[issues]] file = "test/unit/RowGateway/AbstractRowGatewayTest.php" code = "no-isset" @@ -384,108 +264,18 @@ code = "halstead" message = "Method has a high halstead volume" count = 1 -[[issues]] -file = "test/unit/Sql/AbstractSqlFunctionalTestCase.php" -code = "no-isset" -message = "Use of the `isset` construct." -count = 3 - -[[issues]] -file = "test/unit/Sql/AbstractSqlFunctionalTestCase.php" -code = "tagged-todo" -message = "TODO should be tagged with (@username) or (#issue)." -count = 1 - -[[issues]] -file = "test/unit/Sql/Ddl/AlterTableTest.php" -code = "literal-named-argument" -message = "Literal argument `''` should be passed as a named argument for clarity." -count = 2 - -[[issues]] -file = "test/unit/Sql/InsertIgnoreTest.php" -code = "method-name" -message = "Method name `_get` should be in camel case." -count = 1 - -[[issues]] -file = "test/unit/Sql/InsertIgnoreTest.php" -code = "method-name" -message = "Method name `_isset` should be in camel case." -count = 1 - -[[issues]] -file = "test/unit/Sql/InsertIgnoreTest.php" -code = "method-name" -message = "Method name `_set` should be in camel case." -count = 1 - -[[issues]] -file = "test/unit/Sql/InsertIgnoreTest.php" -code = "method-name" -message = "Method name `_unset` should be in camel case." -count = 1 - [[issues]] file = "test/unit/Sql/InsertIgnoreTest.php" code = "no-isset" message = "Use of the `isset` construct." count = 2 -[[issues]] -file = "test/unit/Sql/InsertTest.php" -code = "method-name" -message = "Method name `_get` should be in camel case." -count = 1 - -[[issues]] -file = "test/unit/Sql/InsertTest.php" -code = "method-name" -message = "Method name `_isset` should be in camel case." -count = 1 - -[[issues]] -file = "test/unit/Sql/InsertTest.php" -code = "method-name" -message = "Method name `_set` should be in camel case." -count = 1 - -[[issues]] -file = "test/unit/Sql/InsertTest.php" -code = "method-name" -message = "Method name `_unset` should be in camel case." -count = 1 - [[issues]] file = "test/unit/Sql/InsertTest.php" code = "no-isset" message = "Use of the `isset` construct." count = 2 -[[issues]] -file = "test/unit/Sql/InsertTest.php" -code = "no-redundant-variable" -message = "Variable `$value` is assigned but never used." -count = 1 - -[[issues]] -file = "test/unit/Sql/Platform/AbstractPlatformTest.php" -code = "string-style" -message = "String concatenation can be replaced with interpolation." -count = 2 - -[[issues]] -file = "test/unit/Sql/Predicate/PredicateTest.php" -code = "no-else-clause" -message = "Avoid `else` clauses." -count = 1 - -[[issues]] -file = "test/unit/Sql/Predicate/PredicateTest.php" -code = "no-isset" -message = "Use of the `isset` construct." -count = 1 - [[issues]] file = "test/unit/Sql/SelectTest.php" code = "excessive-parameter-list" @@ -498,102 +288,12 @@ code = "halstead" message = "Method has a high halstead volume and effort" count = 1 -[[issues]] -file = "test/unit/Sql/SelectTest.php" -code = "no-assign-in-argument" -message = "Avoid assignments in function call arguments." -count = 1 - [[issues]] file = "test/unit/Sql/SelectTest.php" code = "no-boolean-flag-parameter" message = "Avoid boolean flag parameters." count = 1 -[[issues]] -file = "test/unit/Sql/SelectTest.php" -code = "no-redundant-variable" -message = "Variable `$value` is assigned but never used." -count = 1 - -[[issues]] -file = "test/unit/Sql/SelectTest.php" -code = "variable-name" -message = "Variable name `$select32subselect` should be in camel case." -count = 1 - -[[issues]] -file = "test/unit/Sql/SelectTest.php" -code = "variable-name" -message = "Variable name `$select39subselect` should be in camel case." -count = 1 - -[[issues]] -file = "test/unit/Sql/SelectTest.php" -code = "variable-name" -message = "Variable name `$select44b` should be in camel case." -count = 1 - -[[issues]] -file = "test/unit/Sql/SelectTest.php" -code = "variable-name" -message = "Variable name `$select48b` should be in camel case." -count = 1 - -[[issues]] -file = "test/unit/Sql/SelectTest.php" -code = "variable-name" -message = "Variable name `$select48combined` should be in camel case." -count = 1 - -[[issues]] -file = "test/unit/Sql/SqlTest.php" -code = "method-name" -message = "Method name `_construct` should be in camel case." -count = 1 - -[[issues]] -file = "test/unit/Sql/TableIdentifierFactoryTest.php" -code = "literal-named-argument" -message = "Literal argument `''` should be passed as a named argument for clarity." -count = 2 - -[[issues]] -file = "test/unit/Sql/TableIdentifierFactoryTest.php" -code = "literal-named-argument" -message = "Literal argument `'_'` should be passed as a named argument for clarity." -count = 1 - -[[issues]] -file = "test/unit/Sql/TableIdentifierFactoryTest.php" -code = "literal-named-argument" -message = "Literal argument `'__'` should be passed as a named argument for clarity." -count = 1 - -[[issues]] -file = "test/unit/Sql/TableIdentifierFactoryTest.php" -code = "literal-named-argument" -message = "Literal argument `'archive'` should be passed as a named argument for clarity." -count = 3 - -[[issues]] -file = "test/unit/Sql/TableIdentifierFactoryTest.php" -code = "literal-named-argument" -message = "Literal argument `'public'` should be passed as a named argument for clarity." -count = 2 - -[[issues]] -file = "test/unit/Sql/TableIdentifierFactoryTest.php" -code = "literal-named-argument" -message = "Literal argument `null` should be passed as a named argument for clarity." -count = 8 - -[[issues]] -file = "test/unit/Sql/UpdateTest.php" -code = "no-fully-qualified-global-class-like" -message = "Fully-qualified class-like reference detected." -count = 1 - [[issues]] file = "test/unit/TableGateway/TableGatewayTest.php" code = "no-assign-in-argument" diff --git a/test/unit/Adapter/Driver/Pdo/ResultTest.php b/test/unit/Adapter/Driver/Pdo/ResultTest.php index 11cde0e9d..94d02884b 100644 --- a/test/unit/Adapter/Driver/Pdo/ResultTest.php +++ b/test/unit/Adapter/Driver/Pdo/ResultTest.php @@ -18,7 +18,6 @@ use stdClass; use function assert; -use function uniqid; #[CoversMethod(Result::class, 'current')] #[CoversMethod(Result::class, 'count')] @@ -137,7 +136,7 @@ public function currentReturnsSameDataOnConsecutiveCalls(): void $stub = $this->getMockBuilder('PDOStatement')->getMock(); $stub->expects($this->any()) ->method('fetch') - ->willReturnCallback(static fn() => uniqid()); + ->willReturn('first row', 'second row'); $result = new Result(); $result->initialize($stub, null); @@ -364,7 +363,7 @@ public function rewindResetsIterationToStart(): void $position = 0; $stub = $this->getMockBuilder('PDOStatement')->getMock(); - assert($stub instanceof PDOStatement); // to suppress IDE type warnings + assert($stub instanceof PDOStatement, description: 'Mock must be a PDOStatement'); $stub->expects($this->any()) ->method('fetch') ->willReturnCallback(static function () use ($data, &$position) { diff --git a/test/unit/Adapter/Driver/Pdo/TestAsset/SqliteMemoryPdo.php b/test/unit/Adapter/Driver/Pdo/TestAsset/SqliteMemoryPdo.php index d95f77c14..1baa2a15f 100644 --- a/test/unit/Adapter/Driver/Pdo/TestAsset/SqliteMemoryPdo.php +++ b/test/unit/Adapter/Driver/Pdo/TestAsset/SqliteMemoryPdo.php @@ -17,14 +17,13 @@ class SqliteMemoryPdo extends PDO protected MockObject&PDOStatement $mockStatement; /** - * @param null $sql * @throws Exception */ - public function __construct($sql = null) + public function __construct(?string $sql = null) { parent::__construct('sqlite::memory:'); - if (empty($sql)) { + if (null === $sql || '' === $sql) { return; } diff --git a/test/unit/Adapter/Driver/Pdo/TestAsset/TestConnection.php b/test/unit/Adapter/Driver/Pdo/TestAsset/TestConnection.php index 170e47e90..9c784ecf8 100644 --- a/test/unit/Adapter/Driver/Pdo/TestAsset/TestConnection.php +++ b/test/unit/Adapter/Driver/Pdo/TestAsset/TestConnection.php @@ -9,7 +9,6 @@ use PhpDb\Adapter\Driver\ConnectionInterface; use PhpDb\Adapter\Driver\Pdo\AbstractPdoConnection; -use function is_array; use function sprintf; /** @@ -19,11 +18,12 @@ final class TestConnection extends AbstractPdoConnection { public function __construct(PDO|array $connectionParameters) { - if (is_array($connectionParameters)) { - $this->setConnectionParameters($connectionParameters); - } elseif ($connectionParameters instanceof PDO) { + if ($connectionParameters instanceof PDO) { $this->setResource($connectionParameters); + return; } + + $this->setConnectionParameters($connectionParameters); } #[Override] @@ -33,10 +33,7 @@ public function connect(): ConnectionInterface return $this; } - // Build DSN if not already set - if (! isset($this->dsn)) { - $this->dsn = $this->buildDsn(); - } + $this->dsn ??= $this->buildDsn(); $this->resource = new PDO( $this->getDsn(), diff --git a/test/unit/ResultSet/AbstractResultSetTest.php b/test/unit/ResultSet/AbstractResultSetTest.php index cc5af842e..d9eb0e257 100644 --- a/test/unit/ResultSet/AbstractResultSetTest.php +++ b/test/unit/ResultSet/AbstractResultSetTest.php @@ -570,7 +570,7 @@ public function multipleRewindBufferIterations(): void ['id' => 2, 'name' => 'two'], ['id' => 3, 'name' => 'three'], ]); - assert($stub instanceof PDOStatement); // to suppress IDE type warnings + assert($stub instanceof PDOStatement, description: 'Mock must be a PDOStatement'); $stub->expects($this->any()) ->method('fetch') ->willReturnCallback(static function () use ($data) { diff --git a/test/unit/Sql/AbstractSqlFunctionalTestCase.php b/test/unit/Sql/AbstractSqlFunctionalTestCase.php index cbd9ec000..b9fa11841 100644 --- a/test/unit/Sql/AbstractSqlFunctionalTestCase.php +++ b/test/unit/Sql/AbstractSqlFunctionalTestCase.php @@ -26,6 +26,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use function array_key_exists; use function is_array; use function is_string; @@ -252,21 +253,6 @@ protected static function dataProviderDecorators(): array ], ], ], - // phpcs:disable Generic.Files.LineLength.TooLong - /* TODO - should be implemented - * 'RootDecorators::Insert' => array( - * 'sqlObject' => self::insert('foo')->select(self::select()), - * 'expected' => array( - * 'sql92' => array( - * 'decorators' => array( - * 'PhpDb\Sql\Insert' => new TestAsset\InsertDecorator, // Decorator for root sqlObject - * 'PhpDb\Sql\Select' => array('PhpDb\Sql\Platform\Mysql\SelectDecorator', '{=SELECT_Sql92=}') - * ), - * 'string' => 'INSERT INTO "foo" {=SELECT_Sql92=}', - * ), - * ), - * ),*/ - // phpcs:enable Generic.Files.LineLength.TooLong ]; } @@ -296,7 +282,7 @@ public function test(PreparableSqlInterface|SqlInterface $sqlObject, string $pla { $sql = new Sql\Sql($this->resolveAdapter($platform)); - if (is_array($expected) && isset($expected['decorators'])) { + if (is_array($expected) && array_key_exists('decorators', $expected)) { /** @var PlatformDecoratorInterface|array $decorator */ foreach ($expected['decorators'] as $type => $decorator) { self::assertIsString($type); @@ -317,11 +303,11 @@ public function test(PreparableSqlInterface|SqlInterface $sqlObject, string $pla self::assertEquals($expectedString, $actual, 'getSqlString()'); } - if (is_array($expected) && isset($expected['prepare'])) { + if (is_array($expected) && array_key_exists('prepare', $expected)) { self::assertInstanceOf(PreparableSqlInterface::class, $sqlObject); $actual = $sql->prepareStatementForSqlObject($sqlObject); self::assertEquals($expected['prepare'], $actual->getSql(), 'prepareStatement()'); - if (isset($expected['parameters'])) { + if (array_key_exists('parameters', $expected)) { $parametersContainer = $actual->getParameterContainer(); self::assertInstanceOf(ParameterContainer::class, $parametersContainer); $actual = $parametersContainer->getNamedArray(); diff --git a/test/unit/Sql/Platform/AbstractPlatformTest.php b/test/unit/Sql/Platform/AbstractPlatformTest.php index e58e08904..8a8f9ed54 100644 --- a/test/unit/Sql/Platform/AbstractPlatformTest.php +++ b/test/unit/Sql/Platform/AbstractPlatformTest.php @@ -91,8 +91,8 @@ public function prepareStatementDelegatesToDecoratorSubject(): void $this->platform->setSubject($select); $mockPlatform = $this->createMock(PlatformInterface::class); - $mockPlatform->method('quoteIdentifier')->willReturnCallback(static fn($v) => '"' . $v . '"'); - $mockPlatform->method('quoteIdentifierInFragment')->willReturnCallback(static fn($v) => '"' . $v . '"'); + $mockPlatform->method('quoteIdentifier')->willReturnCallback(static fn($v) => "\"{$v}\""); + $mockPlatform->method('quoteIdentifierInFragment')->willReturnCallback(static fn($v) => "\"{$v}\""); $mockPlatform->method('getIdentifierSeparator')->willReturn('.'); $mockPlatform->method('getSqlPlatformDecorator')->willReturn($this->platform); diff --git a/test/unit/Sql/Predicate/PredicateTest.php b/test/unit/Sql/Predicate/PredicateTest.php index f392440df..c1d7ac68f 100644 --- a/test/unit/Sql/Predicate/PredicateTest.php +++ b/test/unit/Sql/Predicate/PredicateTest.php @@ -235,13 +235,9 @@ public function expressionNullParameters(): void $predicates = $predicate->getPredicates(); - if (isset($predicates[0][1])) { - $expression = $predicates[0][1]; - static::assertInstanceOf(Expression::class, $expression); - static::assertEquals([], $expression->getParameters()); - } else { - static::fail('Expression not found'); - } + $expression = $predicates[0][1] ?? null; + static::assertInstanceOf(Expression::class, $expression); + static::assertEquals([], $expression->getParameters()); } /** diff --git a/test/unit/Sql/UpdateTest.php b/test/unit/Sql/UpdateTest.php index d608d8715..ba4030b07 100644 --- a/test/unit/Sql/UpdateTest.php +++ b/test/unit/Sql/UpdateTest.php @@ -16,6 +16,7 @@ use PhpDb\Sql\Exception\InvalidArgumentException; use PhpDb\Sql\Expression; use PhpDb\Sql\Join; +use PhpDb\Sql\Predicate\Expression as PredicateExpression; use PhpDb\Sql\Predicate\In; use PhpDb\Sql\Predicate\IsNotNull; use PhpDb\Sql\Predicate\IsNull; @@ -592,7 +593,7 @@ public function where(): void static::assertInstanceOf(Literal::class, $predicates[0][1] ?? null); static::assertSame('AND', $predicates[1][0] ?? ''); - static::assertInstanceOf(\PhpDb\Sql\Predicate\Expression::class, $predicates[1][1] ?? null); + static::assertInstanceOf(PredicateExpression::class, $predicates[1][1] ?? null); static::assertSame('AND', $predicates[2][0] ?? ''); static::assertInstanceOf(Operator::class, $predicates[2][1] ?? null);