diff --git a/src/DbQueryInterceptor.php b/src/DbQueryInterceptor.php index 7ba19240..e5050692 100644 --- a/src/DbQueryInterceptor.php +++ b/src/DbQueryInterceptor.php @@ -12,9 +12,9 @@ use Ray\MediaQuery\Exception\InvalidPerPageVarNameException; use Ray\MediaQuery\Exception\PerPageNotIntTypeException; use ReflectionNamedType; +use ReflectionUnionType; use function assert; -use function class_exists; use function is_callable; use function is_int; use function is_string; @@ -76,9 +76,9 @@ private function getFetchMode(string|null $entity): int * * @return array|object|null */ - private function sqlQuery(ReflectionNamedType|null $returnType, DbQuery $dbQuery, array $values, int $fetchStyle, int|string|callable $fetchArg): array|object|null + private function sqlQuery(ReflectionNamedType|ReflectionUnionType|null $returnType, DbQuery $dbQuery, array $values, int $fetchStyle, int|string|callable $fetchArg): array|object|null { - if ($dbQuery->type === 'row' || ($returnType && class_exists($returnType->getName()))) { + if ($dbQuery->type === 'row' || $returnType instanceof ReflectionUnionType || ($returnType instanceof ReflectionNamedType && $returnType->getName() !== 'array')) { return $this->sqlQuery->getRow($dbQuery->id, $values, $fetchStyle, $fetchArg); } diff --git a/tests/DbQueryModuleTest.php b/tests/DbQueryModuleTest.php index fbc7d22c..458ee0a6 100644 --- a/tests/DbQueryModuleTest.php +++ b/tests/DbQueryModuleTest.php @@ -16,6 +16,7 @@ use Ray\MediaQuery\Exception\InvalidPerPageVarNameException; use Ray\MediaQuery\Exception\PerPageNotIntTypeException; use Ray\MediaQuery\Fake\Queries\TodoFactoryInterface; +use Ray\MediaQuery\Fake\Queries\TodoFactoryUnionInterface; use Ray\MediaQuery\Queries\DynamicPerPageInterface; use Ray\MediaQuery\Queries\DynamicPerPageInvalidInterface; use Ray\MediaQuery\Queries\DynamicPerPageInvalidType; @@ -58,6 +59,7 @@ protected function setUp(): void DynamicPerPageInvalidType::class, PagerEntityInterface::class, TodoFactoryInterface::class, + TodoFactoryUnionInterface::class, ]); $sqlDir = dirname(__DIR__) . '/tests/sql'; $dbQueryConfig = new DbQueryConfig($sqlDir); @@ -222,10 +224,20 @@ public function testSelectPagerEntity(): void $this->assertStringContainsString('query: todo_list', $log); } - public function testFactory(): void + /** @return array> */ + public function queryInterfaceProvider(): array + { + return [ + [TodoFactoryInterface::class], + [TodoFactoryUnionInterface::class], + ]; + } + + /** @dataProvider queryInterfaceProvider */ + public function testFactory(string $queryInterface): void { /** @var TodoEntityInterface $todoList */ - $todoList = $this->injector->getInstance(TodoFactoryInterface::class); + $todoList = $this->injector->getInstance($queryInterface); $list = $todoList->getList(); $this->assertInstanceOf(TodoConstruct::class, $list[0]); $this->assertSame('run', $list[0]->title); diff --git a/tests/Fake/Entity/TodoConstructExtended.php b/tests/Fake/Entity/TodoConstructExtended.php new file mode 100644 index 00000000..2a374b4a --- /dev/null +++ b/tests/Fake/Entity/TodoConstructExtended.php @@ -0,0 +1,9 @@ + + */ + public function getList(): array; +}