diff --git a/analyzer-baseline.toml b/analyzer-baseline.toml index 6525bc725..d42d08e8a 100644 --- a/analyzer-baseline.toml +++ b/analyzer-baseline.toml @@ -126,216 +126,18 @@ code = "missing-property-type" message = "Property `$profiles` is missing a type hint." count = 1 -[[issues]] -file = "src/ConfigProvider.php" -code = "imprecise-type" -message = "Type `array` in return type of `__invoke` is imprecise, equivalent to `array`." -count = 1 - -[[issues]] -file = "src/ConfigProvider.php" -code = "imprecise-type" -message = "Type `array` in return type of `getDependencies` is imprecise, equivalent to `array`." -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "imprecise-type" -message = "Type `array` in parameter `$options` is imprecise, equivalent to `array`." -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "imprecise-type" -message = "Type `array` in property `$config` is imprecise, equivalent to `array`." -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "imprecise-type" -message = "Type `array` in return type of `getConfig` is imprecise, equivalent to `array`." -count = 1 - [[issues]] file = "src/Container/AbstractAdapterInterfaceFactory.php" code = "incompatible-parameter-type" message = 'Parameter `$container` of `PhpDb\Container\AbstractAdapterInterfaceFactory::__invoke()` expects type `Psr\Container\ContainerInterface&Laminas\ServiceManager\ServiceManager` but parent `Laminas\ServiceManager\Factory\FactoryInterface::__invoke()` expects type `Psr\Container\ContainerInterface`' count = 1 -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "invalid-return-statement" -message = 'Invalid return type for function `PhpDb\Container\AbstractAdapterInterfaceFactory::getConfig`: expected `array`, but found `array|null`.' -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "mixed-argument" -message = 'Invalid argument type for argument #2 of `Laminas\ServiceManager\ServiceManager::build`: expected `array|null`, but found `mixed`.' -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "mixed-assignment" -message = "Assigning `mixed` type to a variable may lead to unexpected behavior." -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "mixed-property-type-coercion" -message = "A value with a less specific type `nonnull` is being assigned to property `$config` (array|null)." -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "nullable-return-statement" -message = 'Function `PhpDb\Container\AbstractAdapterInterfaceFactory::getConfig` is declared to return `array` but possibly returns a nullable value (inferred as `array|null`).' -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "possibly-null-argument" -message = 'Argument #3 of method `PhpDb\Adapter\Adapter::__construct` is possibly `null`, but parameter type `PhpDb\ResultSet\ResultSetInterface` does not accept it.' -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "possibly-null-array-access" -message = "Cannot perform array access on possibly `null` value." -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "possibly-undefined-string-array-index" -message = "Possibly undefined array key `string` accessed on `array`." -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "redundant-docblock-type" -message = "Redundant docblock type for variable `$driver`." -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "redundant-docblock-type" -message = "Redundant docblock type for variable `$platform`." -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "redundant-docblock-type" -message = "Redundant docblock type for variable `$profiler`." -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "unhandled-thrown-type" -message = 'Potentially unhandled exception `Psr\Container\ContainerExceptionInterface` in `PhpDb\Container\AbstractAdapterInterfaceFactory::getConfig`.' -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "unhandled-thrown-type" -message = 'Potentially unhandled exception `Psr\Container\NotFoundExceptionInterface` in `PhpDb\Container\AbstractAdapterInterfaceFactory::getConfig`.' -count = 1 - [[issues]] file = "src/Container/AdapterInterfaceDelegator.php" code = "class-must-be-final" message = 'Class `PhpDb\Container\AdapterInterfaceDelegator` should be declared `final`.' count = 1 -[[issues]] -file = "src/Container/AdapterInterfaceDelegator.php" -code = "imprecise-type" -message = "Type `array` in parameter `$options` is imprecise, equivalent to `array`." -count = 1 - -[[issues]] -file = "src/Container/AdapterInterfaceDelegator.php" -code = "imprecise-type" -message = "Type `array` in parameter `$state` is imprecise, equivalent to `array`." -count = 1 - -[[issues]] -file = "src/Container/AdapterInterfaceDelegator.php" -code = "mixed-argument" -message = 'Invalid argument type for argument #1 of `PhpDb\Container\AdapterInterfaceDelegator::__construct`: expected `string`, but found `nonnull`.' -count = 1 - -[[issues]] -file = "src/Container/AdapterInterfaceDelegator.php" -code = "mixed-assignment" -message = "Assigning `mixed` type to a variable may lead to unexpected behavior." -count = 2 - -[[issues]] -file = "src/Container/AdapterInterfaceDelegator.php" -code = "unused-parameter" -message = "Parameter `$options` is never used." -count = 1 - -[[issues]] -file = "src/Container/AdapterInterfaceFactory.php" -code = "mixed-argument" -message = 'Invalid argument type for argument #2 of `Laminas\ServiceManager\ServiceManager::build`: expected `array|null`, but found `nonnull`.' -count = 1 - -[[issues]] -file = "src/Container/AdapterInterfaceFactory.php" -code = "mixed-assignment" -message = "Assigning `nonnull` type to a variable may lead to unexpected behavior." -count = 2 - -[[issues]] -file = "src/Container/AdapterInterfaceFactory.php" -code = "possibly-null-argument" -message = 'Argument #1 of method `Laminas\ServiceManager\ServiceManager::build` is possibly `null`, but parameter type `class-string|class-string|string` does not accept it.' -count = 1 - -[[issues]] -file = "src/Container/AdapterInterfaceFactory.php" -code = "possibly-null-argument" -message = 'Argument #3 of method `PhpDb\Adapter\Adapter::__construct` is possibly `null`, but parameter type `PhpDb\ResultSet\ResultSetInterface` does not accept it.' -count = 1 - -[[issues]] -file = "src/Container/AdapterInterfaceFactory.php" -code = "redundant-docblock-type" -message = "Redundant docblock type for variable `$adapterPlatform`." -count = 1 - -[[issues]] -file = "src/Container/AdapterInterfaceFactory.php" -code = "redundant-docblock-type" -message = "Redundant docblock type for variable `$profilerInterface`." -count = 1 - -[[issues]] -file = "src/Container/AdapterInterfaceFactory.php" -code = "unhandled-thrown-type" -message = 'Potentially unhandled exception `Laminas\ServiceManager\Exception\ServiceNotCreatedException` in `PhpDb\Container\AdapterInterfaceFactory::__invoke`.' -count = 1 - -[[issues]] -file = "src/Container/AdapterInterfaceFactory.php" -code = "unhandled-thrown-type" -message = 'Potentially unhandled exception `PhpDb\Exception\ContainerException` in `PhpDb\Container\AdapterInterfaceFactory::__invoke`.' -count = 1 - -[[issues]] -file = "src/Container/AdapterInterfaceFactory.php" -code = "unhandled-thrown-type" -message = 'Potentially unhandled exception `Psr\Container\ContainerExceptionInterface` in `PhpDb\Container\AdapterInterfaceFactory::__invoke`.' -count = 1 - -[[issues]] -file = "src/Container/AdapterInterfaceFactory.php" -code = "unhandled-thrown-type" -message = 'Potentially unhandled exception `Psr\Container\NotFoundExceptionInterface` in `PhpDb\Container\AdapterInterfaceFactory::__invoke`.' -count = 1 - [[issues]] file = "src/ResultSet/AbstractResultSet.php" code = "incompatible-return-type" @@ -348,12 +150,6 @@ code = "invalid-type-cast" message = "Casting `mixed` to `array`." count = 1 -[[issues]] -file = "src/ResultSet/AbstractResultSet.php" -code = "mixed-assignment" -message = "Assigning `mixed` type to a variable may lead to unexpected behavior." -count = 2 - [[issues]] file = "src/ResultSet/Exception/InvalidArgumentException.php" code = "class-must-be-final" @@ -366,24 +162,6 @@ code = "class-must-be-final" message = 'Class `PhpDb\ResultSet\Exception\RuntimeException` should be declared `final`.' count = 1 -[[issues]] -file = "src/ResultSet/HydratingResultSet.php" -code = "redundant-docblock-type" -message = "Redundant docblock type for variable `$row`." -count = 1 - -[[issues]] -file = "src/ResultSet/ObjectResultSet.php" -code = "redundant-docblock-type" -message = "Redundant docblock type for variable `$row`." -count = 1 - -[[issues]] -file = "src/ResultSet/RowPrototypeResultSet.php" -code = "redundant-condition" -message = "Redundant ternary operator: condition is always truthy." -count = 1 - [[issues]] file = "src/RowGateway/AbstractRowGateway.php" code = "imprecise-type" diff --git a/lint-baseline.toml b/lint-baseline.toml index 19faa6c46..3320b8736 100644 --- a/lint-baseline.toml +++ b/lint-baseline.toml @@ -6,18 +6,6 @@ code = "cyclomatic-complexity" message = "Class has high complexity." count = 1 -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "no-empty" -message = "Use of the `empty` construct." -count = 1 - -[[issues]] -file = "src/Container/AbstractAdapterInterfaceFactory.php" -code = "no-isset" -message = "Use of the `isset` construct." -count = 1 - [[issues]] file = "src/ResultSet/AbstractResultSet.php" code = "cyclomatic-complexity" diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 05942a274..b16fdec91 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -8,6 +8,13 @@ { public const string NAMED_ADAPTER_KEY = 'adapters'; + /** + * @return array{ + * abstract_factories: list, + * aliases: array, + * factories: array, + * } + */ public function getDependencies(): array { return [ @@ -24,6 +31,13 @@ public function getDependencies(): array ]; } + /** + * @return array{dependencies: array{ + * abstract_factories: list, + * aliases: array, + * factories: array, + * }} + */ public function __invoke(): array { return [ diff --git a/src/Container/AbstractAdapterInterfaceFactory.php b/src/Container/AbstractAdapterInterfaceFactory.php index b596b0252..154ed947a 100644 --- a/src/Container/AbstractAdapterInterfaceFactory.php +++ b/src/Container/AbstractAdapterInterfaceFactory.php @@ -10,14 +10,15 @@ use PhpDb\Adapter\Adapter; use PhpDb\Adapter\AdapterInterface; use PhpDb\Adapter\Driver\DriverInterface; -use PhpDb\Adapter\Driver\PdoDriverInterface; use PhpDb\Adapter\Platform\PlatformInterface; use PhpDb\Adapter\Profiler\ProfilerInterface; use PhpDb\ConfigProvider; use PhpDb\Exception\ContainerException; use PhpDb\ResultSet\ResultSet; use PhpDb\ResultSet\ResultSetInterface; +use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; use function is_array; @@ -30,12 +31,16 @@ */ final class AbstractAdapterInterfaceFactory implements AbstractFactoryInterface { + /** @var array|null */ protected ?array $config = null; /** * Can we create an adapter by the requested name? * * @param string $requestedName + * + * @throws ContainerExceptionInterface If retrieving the config service fails. + * @throws NotFoundExceptionInterface If the config service cannot be resolved. */ #[Override] public function canCreate(ContainerInterface $container, $requestedName): bool @@ -46,15 +51,16 @@ public function canCreate(ContainerInterface $container, $requestedName): bool return false; } - return ( - isset($config[$requestedName]) - && is_array($config[$requestedName]) - && ! empty($config[$requestedName]) - ); + return is_array($config[$requestedName] ?? null) && [] !== $config[$requestedName]; } /** * Get db configuration, if any + * + * @return array + * + * @throws ContainerExceptionInterface If retrieving the config service fails. + * @throws NotFoundExceptionInterface If the config service cannot be resolved. */ protected function getConfig(ContainerInterface $container): array { @@ -67,6 +73,7 @@ protected function getConfig(ContainerInterface $container): array return $this->config; } + /** @var array{'PhpDb\Adapter\AdapterInterface'?: array{adapters?: array, ...}, ...} $config */ $config = $container->get('config'); $this->config = $config[AdapterInterface::class][ConfigProvider::NAMED_ADAPTER_KEY] ?? []; return $this->config; @@ -77,6 +84,10 @@ protected function getConfig(ContainerInterface $container): array * * @phpstan-param ContainerInterface&ServiceManager $container * @param string $requestedName + * @param array|null $options + * + * @throws ContainerException If the named adapter has no driver configured. + * @throws ContainerExceptionInterface If a dependency cannot be built. */ #[Override] public function __invoke( @@ -84,25 +95,22 @@ public function __invoke( $requestedName, ?array $options = null, ): AdapterInterface&Adapter { - /** @var class-string|class-string|null $driverClass */ - $driverClass = $this->config[$requestedName]['driver'] ?? null; + /** @var array{driver?: class-string|null, ...} $adapterConfig */ + $adapterConfig = $this->config[$requestedName] ?? []; + $driverClass = $adapterConfig['driver'] ?? null; if (null === $driverClass) { throw ContainerException::forMissingDriver($requestedName, self::class); } - /** @var DriverInterface|PdoDriverInterface $driver */ - $driver = $container->build($driverClass, $this->config[$requestedName]); + $driver = $container->build($driverClass, $adapterConfig); - /** @var PlatformInterface $platform */ $platform = $container->build(PlatformInterface::class, ['driver' => $driver]); - /** @var ResultSetInterface|null $resultSet */ $resultSet = $container->has(ResultSetInterface::class) ? $container->build(ResultSetInterface::class) : new ResultSet(); - /** @var ProfilerInterface|null $profiler */ $profiler = $container->has(ProfilerInterface::class) ? $container->build(ProfilerInterface::class) : null; diff --git a/src/Container/AdapterInterfaceDelegator.php b/src/Container/AdapterInterfaceDelegator.php index 8213006e2..8921e94d2 100644 --- a/src/Container/AdapterInterfaceDelegator.php +++ b/src/Container/AdapterInterfaceDelegator.php @@ -21,8 +21,12 @@ public function __construct( ) {} /** + * @param array|null $options + * * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface + * + * @mago-expect analysis:unused-parameter */ public function __invoke( ContainerInterface $container, @@ -30,6 +34,7 @@ public function __invoke( callable $callback, ?array $options = null, ): AdapterAwareInterface { + /** @var mixed $instance */ $instance = $callback(); if (! $instance instanceof AdapterAwareInterface) { @@ -40,6 +45,7 @@ public function __invoke( throw new ServiceNotFoundException(sprintf(ContainerException::MISSING_SERVICE, $this->adapterName)); } + /** @var mixed $databaseAdapter */ $databaseAdapter = $container->get($this->adapterName); if (! $databaseAdapter instanceof AdapterInterface) { @@ -51,6 +57,9 @@ public function __invoke( return $instance; } + /** + * @param array{adapterName?: string} $state + */ public static function __set_state(array $state): self { return new self($state['adapterName'] ?? AdapterInterface::class); diff --git a/src/Container/AdapterInterfaceFactory.php b/src/Container/AdapterInterfaceFactory.php index b4777cea6..49bd22a93 100644 --- a/src/Container/AdapterInterfaceFactory.php +++ b/src/Container/AdapterInterfaceFactory.php @@ -8,16 +8,22 @@ use PhpDb\Adapter\Adapter; use PhpDb\Adapter\AdapterInterface; use PhpDb\Adapter\Driver\DriverInterface; -use PhpDb\Adapter\Driver\PdoDriverInterface; use PhpDb\Adapter\Platform\PlatformInterface; use PhpDb\Adapter\Profiler\ProfilerInterface; use PhpDb\Exception\ContainerException; use PhpDb\ResultSet\ResultSet; use PhpDb\ResultSet\ResultSetInterface; +use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; final class AdapterInterfaceFactory { + /** + * @throws ContainerException If the config service, the adapter configuration or its driver is missing. + * @throws ContainerExceptionInterface If a dependency cannot be built. + * @throws NotFoundExceptionInterface If a dependency cannot be resolved. + */ public function __invoke( ContainerInterface&ServiceManager $container, string $requestedName, @@ -26,28 +32,30 @@ public function __invoke( throw ContainerException::forMissingConfigService($requestedName, self::class); } - $config = $container->get('config') ?? []; + /** @var array $config */ + $config = $container->get('config') ?? []; + + /** @var array{driver?: class-string, ...} $adapterConfig */ $adapterConfig = $config[AdapterInterface::class] ?? $config[Adapter::class] ?? []; if ([] === $adapterConfig) { throw ContainerException::forMissingConfiguration(AdapterInterface::class, self::class, $requestedName); } - /** @var class-string|class-string|null $driverClass */ $driverClass = $adapterConfig['driver'] ?? null; - /** @var DriverInterface|PdoDriverInterface $driver */ + if (null === $driverClass) { + throw ContainerException::forMissingDriver($requestedName, self::class); + } + $driver = $container->build($driverClass, $adapterConfig); - /** @var PlatformInterface $adapterPlatform */ $adapterPlatform = $container->build(PlatformInterface::class, ['driver' => $driver]); - /** @var ProfilerInterface|null $profilerInterface */ $profilerInterface = $container->has(ProfilerInterface::class) ? $container->build(ProfilerInterface::class) : null; - /** @var ResultSetInterface|null $queryResultSetPrototype */ $queryResultSetPrototype = $container->has(ResultSetInterface::class) ? $container->get(ResultSetInterface::class) : new ResultSet(); diff --git a/src/ResultSet/AbstractResultSet.php b/src/ResultSet/AbstractResultSet.php index 23ce8e53d..c3f555eb0 100644 --- a/src/ResultSet/AbstractResultSet.php +++ b/src/ResultSet/AbstractResultSet.php @@ -202,6 +202,7 @@ public function getFieldCount(): int return 0; } + /** @var mixed $row */ $row = $dataSource->current(); if ($row instanceof Countable) { $this->fieldCount = $row->count(); @@ -238,6 +239,7 @@ public function initialize(iterable $dataSource): ResultSetInterface return $this; } + /** @var mixed $first */ $first = current($dataSource); $this->fieldCount = is_array($first) || $first instanceof Countable ? count($first) : 0; reset($dataSource); diff --git a/src/ResultSet/HydratingResultSet.php b/src/ResultSet/HydratingResultSet.php index 3ea4ae43b..8f6bfde5b 100644 --- a/src/ResultSet/HydratingResultSet.php +++ b/src/ResultSet/HydratingResultSet.php @@ -108,7 +108,6 @@ public function toArray(): array { $return = []; - /** @var object $row Every row this set yields is hydrated, or mapRow() throws. */ foreach ($this as $row) { $return[] = $this->getHydrator()->extract($row); } diff --git a/src/ResultSet/ObjectResultSet.php b/src/ResultSet/ObjectResultSet.php index 667afef97..838d5837e 100644 --- a/src/ResultSet/ObjectResultSet.php +++ b/src/ResultSet/ObjectResultSet.php @@ -47,7 +47,6 @@ public function toArray(): array { $return = []; - /** @var object $row Every row this set yields is an object, or mapRow() throws. */ foreach ($this as $row) { $return[] = $this->rowToArray($row); } diff --git a/src/ResultSet/RowPrototypeResultSet.php b/src/ResultSet/RowPrototypeResultSet.php index a08401bb1..992d0cf19 100644 --- a/src/ResultSet/RowPrototypeResultSet.php +++ b/src/ResultSet/RowPrototypeResultSet.php @@ -53,13 +53,18 @@ public function setRowPrototype( return $this; } - /** {@inheritDoc} */ + /** + * {@inheritDoc} + * + * @throws Exception\RuntimeException + * @throws Exception\UnexpectedValueException If a row is not row data. + */ #[Override] public function toArray(): array { $return = []; - foreach ($this as $row) { - $return[] = $row instanceof RowPrototypeInterface ? $row->toArray() : $row; + for ($this->rewind(); $this->valid(); $this->next()) { + $return[] = $this->current()?->toArray(); } return $return; diff --git a/test/unit/Adapter/Container/AbstractAdapterInterfaceFactoryTest.php b/test/unit/Adapter/Container/AbstractAdapterInterfaceFactoryTest.php index 298529267..e8c59a31f 100644 --- a/test/unit/Adapter/Container/AbstractAdapterInterfaceFactoryTest.php +++ b/test/unit/Adapter/Container/AbstractAdapterInterfaceFactoryTest.php @@ -41,6 +41,16 @@ public static function providerInvalidService(): array ]; } + /** @return array */ + public static function providerUnusableAdapterConfig(): array + { + return [ + 'null' => [null], + 'not an array' => ['writer'], + 'empty array' => [[]], + ]; + } + public static function providerValidService(): array { return [ @@ -60,6 +70,42 @@ public function canCreateReturnsFalseForEmptyConfig(): void static::assertFalse($factory->canCreate($container, 'PhpDb\Adapter\Writer')); } + #[Test] + #[DataProvider('providerUnusableAdapterConfig')] + public function canCreateReturnsFalseForUnusableAdapterConfig(mixed $adapterConfig): void + { + $container = new ServiceManager(); + $container->setService('config', [ + AdapterInterface::class => [ + 'adapters' => [ + 'other' => ['driver' => PdoStubDriver::class], + 'writer' => $adapterConfig, + ], + ], + ]); + + $factory = new AbstractAdapterInterfaceFactory(); + + static::assertFalse($factory->canCreate($container, 'writer')); + } + + #[Test] + public function canCreateReturnsTrueForConfiguredAdapter(): void + { + $container = new ServiceManager(); + $container->setService('config', [ + AdapterInterface::class => [ + 'adapters' => [ + 'writer' => ['driver' => PdoStubDriver::class], + ], + ], + ]); + + $factory = new AbstractAdapterInterfaceFactory(); + + static::assertTrue($factory->canCreate($container, 'writer')); + } + #[Test] public function getConfigCachesResult(): void { diff --git a/test/unit/Container/AdapterInterfaceFactoryTest.php b/test/unit/Container/AdapterInterfaceFactoryTest.php index 3d390e015..2eb1ca76b 100644 --- a/test/unit/Container/AdapterInterfaceFactoryTest.php +++ b/test/unit/Container/AdapterInterfaceFactoryTest.php @@ -15,6 +15,7 @@ use PhpDb\ResultSet\ResultSet; use PhpDb\ResultSet\ResultSetInterface; use PHPUnit\Framework\Attributes\CoversMethod; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; @@ -23,6 +24,18 @@ #[CoversMethod(AdapterInterfaceFactory::class, '__invoke')] final class AdapterInterfaceFactoryTest extends TestCase { + /** @return array>}> */ + public static function adapterConfigKeyProvider(): array + { + return [ + 'adapter class key only' => [[Adapter::class => ['driver' => DriverInterface::class]]], + 'interface key wins over class key' => [[ + AdapterInterface::class => ['driver' => DriverInterface::class], + Adapter::class => ['options' => []], + ]], + ]; + } + #[Test] public function invokeCreatesAdapterWithAllDependencies(): void { @@ -100,6 +113,43 @@ public function invokeCreatesAdapterWithoutOptionalProfiler(): void static::assertNull($adapter->getProfiler()); } + /** @param array> $config */ + #[Test] + #[DataProvider('adapterConfigKeyProvider')] + public function invokeReadsAdapterConfigFromInterfaceKeyThenClassKey(array $config): void + { + $driverMock = $this->createMock(DriverInterface::class); + + $container = new ServiceManager([ + 'factories' => [ + DriverInterface::class => static fn() => $driverMock, + PlatformInterface::class => fn() => $this->createMock(PlatformInterface::class), + ], + ]); + $container->setService('config', $config); + + $adapter = (new AdapterInterfaceFactory())($container, AdapterInterface::class); + + static::assertSame($driverMock, $adapter->getDriver()); + } + + #[Test] + public function invokeThrowsWhenAdapterConfigHasNoDriver(): void + { + $container = new ServiceManager(); + $container->setService('config', [ + AdapterInterface::class => [ + 'options' => [], + ], + ]); + + $factory = new AdapterInterfaceFactory(); + + self::expectException(ContainerException::class); + self::expectExceptionMessage(ContainerException::MISSING_DRIVER); + $factory($container, AdapterInterface::class); + } + #[Test] public function invokeThrowsWhenAdapterConfigIsEmpty(): void { diff --git a/test/unit/ResultSet/RowPrototypeResultSetTest.php b/test/unit/ResultSet/RowPrototypeResultSetTest.php index 06b9cee1a..c95a938f0 100644 --- a/test/unit/ResultSet/RowPrototypeResultSetTest.php +++ b/test/unit/ResultSet/RowPrototypeResultSetTest.php @@ -177,6 +177,18 @@ public function toArrayConvertsPrototypeRowsToArrays(): void ); } + #[Test] + public function toArrayKeepsNullRowsAsNull(): void + { + $resultSet = new RowPrototypeResultSet($this->createRowPrototype()); + $resultSet->initialize(new ArrayIterator([ + ['id' => 1], + null, + ])); + + static::assertSame([['id' => 1], null], $resultSet->toArray()); + } + private function createRowPrototype(): RowPrototypeInterface { return new class implements RowPrototypeInterface {