Skip to content

Commit

Permalink
Add return union test
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym committed Jan 30, 2023
1 parent 57a22c4 commit 5afb5df
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/DbQueryInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,9 +76,9 @@ private function getFetchMode(string|null $entity): int
*
* @return array<mixed>|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);
}

Expand Down
22 changes: 19 additions & 3 deletions tests/DbQueryModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -222,10 +224,24 @@ public function testSelectPagerEntity(): void
$this->assertStringContainsString('query: todo_list', $log);
}

public function testFactory(): void
/** @return array<array<class-string>> */
public function queryInterfaceProvider(): array
{
/** @var TodoEntityInterface $todoList */
$todoList = $this->injector->getInstance(TodoFactoryInterface::class);
return [
[TodoFactoryInterface::class],
[TodoFactoryUnionInterface::class],
];
}

/**
* @param class-string $queryInterface
*
* @dataProvider queryInterfaceProvider
*/
public function testFactory(string $queryInterface): void
{
/** @var TodoFactoryInterface|TodoFactoryUnionInterface $todoList */
$todoList = $this->injector->getInstance($queryInterface);
$list = $todoList->getList();
$this->assertInstanceOf(TodoConstruct::class, $list[0]);
$this->assertSame('run', $list[0]->title);
Expand Down
9 changes: 9 additions & 0 deletions tests/Fake/Entity/TodoConstructExtended.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Ray\MediaQuery\Entity;

class TodoConstructExtended extends TodoConstruct
{
}
23 changes: 23 additions & 0 deletions tests/Fake/Queries/TodoFactoryUnionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Ray\MediaQuery\Fake\Queries;

use Ray\MediaQuery\Annotation\DbQuery;
use Ray\MediaQuery\Entity\Todo;
use Ray\MediaQuery\Entity\TodoConstruct;
use Ray\MediaQuery\Entity\TodoConstructExtended;
use Ray\MediaQuery\Factory\TodoEntityFactory;

interface TodoFactoryUnionInterface
{
#[DbQuery('todo_item', factory: TodoEntityFactory::class)]
public function getItem(string $id): TodoConstruct|TodoConstructExtended;

#[DbQuery('todo_list', factory: TodoEntityFactory::class)]
/**
* @return array<TodoConstruct|TodoConstructExtended>
*/
public function getList(): array;
}

0 comments on commit 5afb5df

Please sign in to comment.