diff --git a/analyzer-baseline.toml b/analyzer-baseline.toml index 1914077..265a5a8 100644 --- a/analyzer-baseline.toml +++ b/analyzer-baseline.toml @@ -422,32 +422,32 @@ count = 1 [[issues]] file = "src/Metadata/Source.php" -code = "property-type-coercion" -message = "A value of a less specific type `array{'columns': non-empty-array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': mixed}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'sqlite_columns': non-empty-list, 'table_names'?: array>, 'triggers'?: array>}` is being assigned to property `$data` (array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>})." +code = "invalid-property-assignment-value" +message = "Invalid type for property `$data`: expected `array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>}`, but got `array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names': non-empty-array}|array{'check_option'?: null|string, 'is_updatable'?: bool|null, 'table_type': string, 'view_definition'?: null|string}>>, 'triggers'?: array>}`." count = 1 [[issues]] file = "src/Metadata/Source.php" -code = "property-type-coercion" -message = "A value of a less specific type `array{'columns': non-empty-array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': mixed}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>}` is being assigned to property `$data` (array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>})." +code = "invalid-property-assignment-value" +message = "Invalid type for property `$data`: expected `array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>}`, but got `array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers': non-empty-array}|array{'action_condition': null, 'action_order': int(0), 'action_orientation': string('ROW'), 'action_reference_new_row': string('NEW'), 'action_reference_new_table': null, 'action_reference_old_row': string('OLD'), 'action_reference_old_table': null, 'action_statement': null, 'action_timing': null, 'created': null, 'event_manipulation': null, 'event_object_catalog': null, 'event_object_schema': string, 'event_object_table': mixed, 'trigger_name': mixed}|array{'action_condition': null|string, 'action_order': string, 'action_orientation': string, 'action_reference_new_row': string, 'action_reference_new_table': null|string, 'action_reference_old_row': string, 'action_reference_old_table': null|string, 'action_statement': string, 'action_timing': string, 'created': DateTime|null, 'event_manipulation': string, 'event_object_catalog': string, 'event_object_schema': string, 'event_object_table': string}>>}`." count = 1 [[issues]] file = "src/Metadata/Source.php" -code = "property-type-coercion" -message = "A value of a less specific type `array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints': non-empty-array, 'constraint_name'?: mixed, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: mixed, 'referenced_table_schema'?: string, 'table_name'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>}` is being assigned to property `$data` (array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>})." +code = "mixed-property-type-coercion" +message = "A value with a less specific type `array{'columns': non-empty-array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': mixed}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'sqlite_columns': non-empty-list, 'table_names'?: array>, 'triggers'?: array>}` is being assigned to property `$data` (array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>})." count = 1 [[issues]] file = "src/Metadata/Source.php" -code = "property-type-coercion" -message = "A value of a less specific type `array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names': non-empty-array}|array{'check_option'?: null|string, 'is_updatable'?: bool|null, 'table_type': string, 'view_definition'?: null|string}>>, 'triggers'?: array>}` is being assigned to property `$data` (array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>})." +code = "mixed-property-type-coercion" +message = "A value with a less specific type `array{'columns': non-empty-array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': mixed}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>}` is being assigned to property `$data` (array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>})." count = 1 [[issues]] file = "src/Metadata/Source.php" -code = "property-type-coercion" -message = "A value of a less specific type `array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers': non-empty-array}|array{'action_condition': null, 'action_order': int(0), 'action_orientation': string('ROW'), 'action_reference_new_row': string('NEW'), 'action_reference_new_table': null, 'action_reference_old_row': string('OLD'), 'action_reference_old_table': null, 'action_statement': null, 'action_timing': null, 'created': null, 'event_manipulation': null, 'event_object_catalog': null, 'event_object_schema': string, 'event_object_table': mixed, 'trigger_name': mixed}|array{'action_condition': null|string, 'action_order': string, 'action_orientation': string, 'action_reference_new_row': string, 'action_reference_new_table': null|string, 'action_reference_old_row': string, 'action_reference_old_table': null|string, 'action_statement': string, 'action_timing': string, 'created': DateTime|null, 'event_manipulation': string, 'event_object_catalog': string, 'event_object_schema': string, 'event_object_table': string}>>}` is being assigned to property `$data` (array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>})." +code = "mixed-property-type-coercion" +message = "A value with a less specific type `array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints': non-empty-array, 'constraint_name'?: mixed, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: mixed, 'referenced_table_schema'?: string, 'table_name'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>}` is being assigned to property `$data` (array{'columns'?: array, 'is_nullable': bool|null, 'numeric_precision': int|null|string, 'numeric_scale': int|null|string, 'numeric_unsigned': bool|null, 'ordinal_position': int|null|string}>>>, 'constraint_keys'?: array>, 'constraint_references'?: array>, 'constraints'?: array, 'constraint_type'?: string, 'delete_rule'?: string, 'match_option'?: string, 'referenced_columns'?: list, 'referenced_table_name'?: string, 'referenced_table_schema'?: string, 'update_rule'?: string}>>>, 'schemas'?: list, 'table_names'?: array>, 'triggers'?: array>})." count = 1 [[issues]] @@ -628,13 +628,13 @@ count = 1 file = "src/Pdo/Feature/SqliteRowCounter.php" code = "mixed-array-access" message = "Unsafe array access on type `mixed`." -count = 2 +count = 1 [[issues]] file = "src/Pdo/Feature/SqliteRowCounter.php" code = "mixed-assignment" message = "Assigning `mixed` type to a variable may lead to unexpected behavior." -count = 2 +count = 1 [[issues]] file = "src/Pdo/Feature/SqliteRowCounter.php" @@ -654,24 +654,12 @@ code = "possibly-null-argument" message = 'Argument #1 of method `PhpDb\Sqlite\Pdo\Feature\SqliteRowCounter::getCountForSql` is possibly `null`, but parameter type `string` does not accept it.' count = 1 -[[issues]] -file = "src/Pdo/Feature/SqliteRowCounter.php" -code = "unhandled-thrown-type" -message = 'Potentially unhandled exception `PDOException` in `PhpDb\Sqlite\Pdo\Feature\SqliteRowCounter::getCountForSql`.' -count = 1 - [[issues]] file = "src/Pdo/Feature/SqliteRowCounter.php" code = "unhandled-thrown-type" message = 'Potentially unhandled exception `PhpDb\Adapter\Exception\InvalidQueryException` in `PhpDb\Sqlite\Pdo\Feature\SqliteRowCounter::getCountForStatement`.' count = 1 -[[issues]] -file = "src/Pdo/Feature/SqliteRowCounter.php" -code = "unhandled-thrown-type" -message = 'Potentially unhandled exception `PhpDb\Adapter\Exception\RuntimeException` in `PhpDb\Sqlite\Pdo\Feature\SqliteRowCounter::getCountForStatement`.' -count = 1 - [[issues]] file = "src/Sql/SelectDecorator.php" code = "imprecise-type" diff --git a/lint-baseline.toml b/lint-baseline.toml index 1c6dc88..858a77d 100644 --- a/lint-baseline.toml +++ b/lint-baseline.toml @@ -136,7 +136,7 @@ count = 2 file = "src/Pdo/Feature/SqliteRowCounter.php" code = "no-fully-qualified-global-class-like" message = "Fully-qualified class-like reference detected." -count = 2 +count = 1 [[issues]] file = "src/Pdo/Feature/SqliteRowCounter.php" @@ -618,18 +618,6 @@ code = "prefer-test-attribute" message = "Use `#[Test]` attribute instead of `test` prefix on method `testFormatParameterName`." count = 1 -[[issues]] -file = "test/unit/Pdo/Feature/SqliteRowCounterTest.php" -code = "assertion-style" -message = "Inconsistent assertions style." -count = 1 - -[[issues]] -file = "test/unit/Pdo/Feature/SqliteRowCounterTest.php" -code = "prefer-test-attribute" -message = "Use `#[Test]` attribute instead of `test` prefix on method `testRowCounterExists`." -count = 1 - [[issues]] file = "test/unit/Pdo/ResultTest.php" code = "assert-description" diff --git a/mago.toml b/mago.toml index ad23c4d..fdce9e8 100644 --- a/mago.toml +++ b/mago.toml @@ -5,6 +5,9 @@ php-version = "8.3.0" paths = ["src", "test"] includes = ["vendor"] +[formatter] +parentheses-around-new-in-member-access = true + [linter] baseline = "lint-baseline.toml" diff --git a/src/Metadata/Source.php b/src/Metadata/Source.php index 2f16c1a..4c895cf 100644 --- a/src/Metadata/Source.php +++ b/src/Metadata/Source.php @@ -107,7 +107,7 @@ protected function loadColumnData(string $table, string $schema): void $columns = []; foreach ($results as $row) { - $columns[$row['name']] = [ + $columns[(string) $row['name']] = [ // cid appears to be zero-based, ordinal position needs to be one-based 'ordinal_position' => $row['cid'] + 1, 'column_default' => $row['dflt_value'], @@ -171,7 +171,7 @@ protected function loadConstraintData(string $table, string $schema): void $constraint['constraint_type'] = 'PRIMARY KEY'; $primaryKey = null; } - $constraints[$constraint['constraint_name']] = $constraint; + $constraints[(string) $constraint['constraint_name']] = $constraint; } if (null !== $primaryKey) { @@ -268,7 +268,7 @@ protected function loadTableNameData(string $schema): void $table = array_merge($table, $data); } } - $tables[$row['name']] = $table; + $tables[(string) $row['name']] = $table; } $this->data['table_names'][$schema] = $tables; } @@ -314,7 +314,7 @@ protected function loadTriggerData(string $schema): void if (null !== ($data = $this->parseTrigger($row['sql']))) { $trigger = array_merge($trigger, $data); } - $triggers[$trigger['trigger_name']] = $trigger; + $triggers[(string) $trigger['trigger_name']] = $trigger; } $this->data['triggers'][$schema] = $triggers; diff --git a/src/Pdo/Driver.php b/src/Pdo/Driver.php index 793dfd6..6746cc8 100644 --- a/src/Pdo/Driver.php +++ b/src/Pdo/Driver.php @@ -55,11 +55,11 @@ public function createResult($resource, Statement|string|null $context = null): $sqliteRowCounter = $this->getFeature(Feature\SqliteRowCounter::class); $rowCount = 0; - if ($sqliteRowCounter && $resource->columnCount() > 0) { + if ($sqliteRowCounter && $resource instanceof PDOStatement && $resource->columnCount() > 0) { $rowCount = $sqliteRowCounter->getRowCountClosure($context); } - $result->initialize($resource, $this->connection->getLastGeneratedValue(), $rowCount); + $result->initialize($resource, $this->getLastGeneratedValue(), $rowCount); return $result; } diff --git a/src/Pdo/Feature/SqliteRowCounter.php b/src/Pdo/Feature/SqliteRowCounter.php index ce2ee98..39604b5 100644 --- a/src/Pdo/Feature/SqliteRowCounter.php +++ b/src/Pdo/Feature/SqliteRowCounter.php @@ -5,9 +5,12 @@ namespace PhpDb\Sqlite\Pdo\Feature; use Closure; +use PDOException; +use PDOStatement; use PhpDb\Adapter\Driver\Feature\AbstractFeature; use PhpDb\Adapter\Driver\Pdo; use PhpDb\Adapter\Driver\Pdo\Statement; +use PhpDb\Adapter\Exception\RuntimeException; use function str_contains; use function strtolower; @@ -17,6 +20,10 @@ */ class SqliteRowCounter extends AbstractFeature { + /** + * @throws PDOException + * @throws RuntimeException + */ public function getCountForSql(string $sql): int { if (empty($sql) || ! str_contains(strtolower($sql), 'select')) { @@ -24,13 +31,15 @@ public function getCountForSql(string $sql): int } $countSql = 'SELECT COUNT(*) as count FROM (' . $sql . ')'; /** @var \PDO $pdo */ - $pdo = $this->driver->getConnection()->getResource(); - $result = $pdo->query($countSql); - $countRow = $result->fetch(\PDO::FETCH_ASSOC); + $pdo = $this->driver->getConnection()->getResource(); - return (int) $countRow['count']; + return $this->fetchCount($pdo->query($countSql)); } + /** + * @throws PDOException + * @throws RuntimeException + */ public function getCountForStatement(Pdo\Statement $statement): int { $countStmt = clone $statement; @@ -40,19 +49,39 @@ public function getCountForStatement(Pdo\Statement $statement): int } $countSql = 'SELECT COUNT(*) as "count" FROM (' . $sql . ')'; $countStmt->prepare($countSql); - $result = $countStmt->execute(); - $countRow = $result->getResource()->fetch(\PDO::FETCH_ASSOC); + $result = $countStmt->execute(); + $count = $this->fetchCount($result->getResource()); unset($statement, $result); - return (int) $countRow['count']; + return $count; } public function getRowCountClosure(Statement|string|null $context): Closure { - return function () use ($context): int { - return $context instanceof Pdo\Statement - ? $this->getCountForStatement($context) - : $this->getCountForSql($context); - }; + return ( + /** + * @throws PDOException + * @throws RuntimeException + */ + function () use ($context): int { + return $context instanceof Pdo\Statement + ? $this->getCountForStatement($context) + : $this->getCountForSql($context); + } + ); + } + + /** + * @throws PDOException + * @throws RuntimeException + */ + private function fetchCount(mixed $statement): int + { + if (! $statement instanceof PDOStatement) { + throw new RuntimeException('Unable to fetch the row count: the count query did not produce a statement'); + } + $countRow = $statement->fetch(\PDO::FETCH_ASSOC); + + return (int) $countRow['count']; } } diff --git a/test/unit/AdapterPlatformTest.php b/test/unit/AdapterPlatformTest.php index d23469d..4a3496b 100644 --- a/test/unit/AdapterPlatformTest.php +++ b/test/unit/AdapterPlatformTest.php @@ -6,7 +6,7 @@ use PDO; use PhpDb\Adapter\Driver\PdoDriverInterface; -use PhpDb\Adapter\Exception\VunerablePlatformQuoteException; +use PhpDb\Adapter\Exception\RuntimeException; use PhpDb\Sqlite\AdapterPlatform; use PhpDb\Sqlite\Sql\Platform as SqlPlatformDecorator; use PHPUnit\Framework\Attributes\CoversClass; @@ -152,7 +152,8 @@ public function testQuoteValueList(): void { $expected = "'Foo O\\'Bar'"; $platform = new AdapterPlatform(); - $this->expectException(VunerablePlatformQuoteException::class); + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('without extension/driver support can introduce security vulnerabilities'); $actual = $platform->quoteValueList("Foo O'Bar"); self::assertEquals($expected, $actual); } @@ -161,7 +162,8 @@ public function testQuoteValueThrowsExeceptionWithoutDriverSupport(): void { $platform = new AdapterPlatform(); //$this->expectNotToPerformAssertions(); - $this->expectException(VunerablePlatformQuoteException::class); + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('without extension/driver support can introduce security vulnerabilities'); $platform->quoteValue('value'); } diff --git a/test/unit/Pdo/DriverTest.php b/test/unit/Pdo/DriverTest.php index f953583..5121539 100644 --- a/test/unit/Pdo/DriverTest.php +++ b/test/unit/Pdo/DriverTest.php @@ -5,6 +5,9 @@ namespace PhpDbTest\Sqlite\Pdo; use Override; +use PDO; +use PDOStatement; +use PhpDb\Adapter\Driver\Pdo\Result; use PhpDb\Adapter\Driver\PdoDriverAwareInterface; use PhpDb\Adapter\Driver\PdoDriverInterface; use PhpDb\Adapter\Driver\ResultInterface; @@ -12,12 +15,16 @@ use PhpDb\Exception\RuntimeException; use PhpDb\Sqlite\Pdo\Connection; use PhpDb\Sqlite\Pdo\Driver; +use PhpDb\Sqlite\Pdo\Feature\SqliteRowCounter; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; #[CoversClass(Driver::class)] +#[Group('unit')] final class DriverTest extends TestCase { protected Driver $pdo; @@ -51,6 +58,42 @@ public static function getParamsAndType(): array ]; } + #[Test] + public function createResultCarriesTheConnectionsLastGeneratedValue(): void + { + $result = $this->createDriverWithRowCounter()->createResult($this->createStub(PDOStatement::class)); + + static::assertSame('42', $result->getGeneratedValue()); + } + + #[Test] + public function createResultCountsRowsWithTheRowCounterWhenTheStatementReturnsColumns(): void + { + $countStatement = $this->createStub(PDOStatement::class); + $countStatement->method('fetch')->willReturn(['count' => '5']); + $pdo = $this->createStub(PDO::class); + $pdo->method('query')->willReturn($countStatement); + + $resource = $this->createStub(PDOStatement::class); + $resource->method('columnCount')->willReturn(1); + + $result = $this->createDriverWithRowCounter($pdo)->createResult($resource, 'SELECT * FROM foo'); + + static::assertSame(5, $result->count()); + } + + #[Test] + public function createResultSkipsTheRowCounterWhenTheStatementReturnsNoColumns(): void + { + $resource = $this->createStub(PDOStatement::class); + $resource->method('columnCount')->willReturn(0); + $resource->method('rowCount')->willReturn(3); + + $result = $this->createDriverWithRowCounter()->createResult($resource, 'DELETE FROM foo'); + + static::assertSame(0, $result->count()); + } + #[DataProvider('getParamsAndType')] public function testFormatParameterName(int|string $name, ?string $type, string $expected): void { @@ -86,4 +129,19 @@ protected function setUp(): void $this->createMock(ResultInterface::class), ); } + + private function createDriverWithRowCounter(?PDO $pdo = null): Driver + { + $connection = $this->createStub(Connection::class); + $connection->method('getLastGeneratedValue')->willReturn('42'); + $connection->method('getResource')->willReturn($pdo ?? $this->createStub(PDO::class)); + + /** @var StatementInterface&PdoDriverAwareInterface&MockObject $statementPrototype */ + $statementPrototype = $this->createMockForIntersectionOfInterfaces([ + StatementInterface::class, + PdoDriverAwareInterface::class, + ]); + + return new Driver($connection, $statementPrototype, new Result(), [new SqliteRowCounter()]); + } } diff --git a/test/unit/Pdo/Feature/SqliteRowCounterTest.php b/test/unit/Pdo/Feature/SqliteRowCounterTest.php index ac332d8..aa3a4f1 100644 --- a/test/unit/Pdo/Feature/SqliteRowCounterTest.php +++ b/test/unit/Pdo/Feature/SqliteRowCounterTest.php @@ -4,22 +4,159 @@ namespace PhpDbTest\Sqlite\Pdo\Feature; +use Override; +use PDO; +use PDOStatement; +use PhpDb\Adapter\Driver\ConnectionInterface; +use PhpDb\Adapter\Driver\DriverInterface; +use PhpDb\Adapter\Driver\Pdo\Statement; +use PhpDb\Adapter\Driver\ResultInterface; +use PhpDb\Adapter\Exception\RuntimeException; use PhpDb\Sqlite\Pdo\Feature\SqliteRowCounter; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; #[CoversClass(SqliteRowCounter::class)] +#[Group('unit')] final class SqliteRowCounterTest extends TestCase { private SqliteRowCounter $rowCounter; - public function testRowCounterExists(): void + /** @return array */ + public static function nonSelectSqlProvider(): array { - self::assertInstanceOf(SqliteRowCounter::class, $this->rowCounter); + return [ + 'empty string' => [''], + 'insert statement' => ['INSERT INTO foo VALUES (1)'], + ]; } + /** @return array */ + public static function nonSelectStatementSqlProvider(): array + { + return [ + 'no sql' => [null], + 'insert statement' => ['INSERT INTO foo VALUES (1)'], + ]; + } + + #[DataProvider('nonSelectSqlProvider')] + #[Test] + public function countForSqlIsZeroWhenSqlIsNotASelect(string $sql): void + { + static::assertSame(0, $this->rowCounter->getCountForSql($sql)); + } + + #[Test] + public function countForSqlThrowsWhenTheCountQueryFails(): void + { + $pdo = $this->createStub(PDO::class); + $pdo->method('query')->willReturn(false); + $this->rowCounter->setDriver($this->createDriverForPdo($pdo)); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('the count query did not produce a statement'); + + $this->rowCounter->getCountForSql('SELECT * FROM foo'); + } + + #[Test] + public function countForSqlWrapsTheSelectInACountQuery(): void + { + $pdo = $this->createMock(PDO::class); + $pdo->expects(self::once()) + ->method('query') + ->with('SELECT COUNT(*) as count FROM (SELECT * FROM foo)') + ->willReturn($this->createCountStatement(4)); + $this->rowCounter->setDriver($this->createDriverForPdo($pdo)); + + static::assertSame(4, $this->rowCounter->getCountForSql('SELECT * FROM foo')); + } + + #[Test] + public function countForStatementFetchesTheCountFromTheExecutedResult(): void + { + $statement = $this->createStatementReturningResource($this->createCountStatement(7)); + + static::assertSame(7, $this->rowCounter->getCountForStatement($statement)); + } + + #[DataProvider('nonSelectStatementSqlProvider')] + #[Test] + public function countForStatementIsZeroWhenSqlIsNotASelect(?string $sql): void + { + $statement = $this->createStub(Statement::class); + $statement->method('getSql')->willReturn($sql); + + static::assertSame(0, $this->rowCounter->getCountForStatement($statement)); + } + + #[Test] + public function countForStatementThrowsWhenTheResultHasNoPdoStatement(): void + { + $statement = $this->createStatementReturningResource(null); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('the count query did not produce a statement'); + + $this->rowCounter->getCountForStatement($statement); + } + + #[Test] + public function rowCountClosureCountsAnSqlContext(): void + { + $pdo = $this->createStub(PDO::class); + $pdo->method('query')->willReturn($this->createCountStatement(3)); + $this->rowCounter->setDriver($this->createDriverForPdo($pdo)); + + static::assertSame(3, $this->rowCounter->getRowCountClosure('SELECT * FROM foo')()); + } + + #[Test] + public function rowCountClosureCountsAStatementContext(): void + { + $statement = $this->createStatementReturningResource($this->createCountStatement(2)); + + static::assertSame(2, $this->rowCounter->getRowCountClosure($statement)()); + } + + #[Override] protected function setUp(): void { $this->rowCounter = new SqliteRowCounter(); } + + private function createCountStatement(int $count): PDOStatement + { + $countStatement = $this->createStub(PDOStatement::class); + $countStatement->method('fetch')->willReturn(['count' => (string) $count]); + + return $countStatement; + } + + private function createDriverForPdo(PDO $pdo): DriverInterface + { + $connection = $this->createStub(ConnectionInterface::class); + $connection->method('getResource')->willReturn($pdo); + + $driver = $this->createStub(DriverInterface::class); + $driver->method('getConnection')->willReturn($connection); + + return $driver; + } + + private function createStatementReturningResource(mixed $resource): Statement + { + $result = $this->createStub(ResultInterface::class); + $result->method('getResource')->willReturn($resource); + + $statement = $this->createStub(Statement::class); + $statement->method('getSql')->willReturn('SELECT * FROM foo'); + $statement->method('execute')->willReturn($result); + + return $statement; + } }