Skip to content

Commit

Permalink
Merge pull request #38 from jingu/1.x
Browse files Browse the repository at this point in the history
Add support parameter injection with pagination
  • Loading branch information
koriym authored Sep 20, 2022
2 parents 65247a3 + 1087a9b commit c82cee1
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 31 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ on:

jobs:
ci:
uses: ray-di/.github/.github/workflows/continuous-integration.yml@next_stable
uses: ray-di/.github/.github/workflows/continuous-integration.yml@v1
with:
old_stable: '["7.4", "8.0"]'
current_stable: 8.1
next_stable: 8.2
old_stable: '["7.4", "8.0", "8.1"]'
current_stable: 8.2
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
"ray/aura-sql-module": "^1.12.0",
"ray/di": "^2.12",
"roave/better-reflection": "^4.12 || ^5.6",
"symfony/polyfill-php81": "^1.24"
"symfony/polyfill-php81": "^1.24",
"nikic/php-parser": "^4.15"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^9.5.5",
"doctrine/coding-standard": "^9",
"phpmd/phpmd": "^2.9",
"phpmetrics/phpmetrics": "^2.7",
Expand Down Expand Up @@ -88,7 +89,8 @@
"config": {
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
"dealerdirect/phpcodesniffer-composer-installer": true,
"bamarni/composer-bin-plugin": true
}
}
}
2 changes: 2 additions & 0 deletions src/SqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ public function getStatement(): PDOStatement
*/
public function getPages(string $sqlId, array $values, int $perPage, string $queryTemplate = '/{?page}', ?string $entity = null): PagesInterface
{
($this->paramConverter)($values);

/** @var array<array<array-key, int|string>|int|string> $values */
$pager = $this->pagerFactory->newInstance($this->pdo, $this->getSql($sqlId), $values, $perPage, $queryTemplate, $entity);

Expand Down
34 changes: 30 additions & 4 deletions tests/DbQueryModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function testEntity(): void
{
/** @var TodoEntityInterface $todoList */
$todoList = $this->injector->getInstance(TodoEntityInterface::class);
$list = $todoList->getlist();
$list = $todoList->getList();
$this->assertInstanceOf(Todo::class, $list[0]);
$this->assertSame('run', $list[0]->title);
$item = $todoList->getItem('1');
Expand All @@ -149,7 +149,7 @@ public function testEntityWithConstructor(): void
{
/** @var TodoEntityInterface $todoList */
$todoList = $this->injector->getInstance(TodoConstcuctEntityInterface::class);
$list = $todoList->getlist();
$list = $todoList->getList();
$this->assertInstanceOf(TodoConstruct::class, $list[0]);
$this->assertSame('run', $list[0]->title);
$item = $todoList->getItem('1');
Expand All @@ -160,7 +160,7 @@ public function testDynamicPerPage(): void
{
$todoList = $this->injector->getInstance(DynamicPerPageInterface::class);
assert($todoList instanceof DynamicPerPageInterface);
$list = ($todoList)(2);
$list = $todoList->get(2);
/** @var Page $page */
$page = $list[1];
$this->assertSame([['id' => '1', 'title' => 'run']], $page->data);
Expand All @@ -169,6 +169,32 @@ public function testDynamicPerPage(): void
$this->assertStringContainsString('query: todo_list', $log);
}

public function testDynamicPerPageWithParameterInjection(): void
{
$todoList = $this->injector->getInstance(DynamicPerPageInterface::class);
assert($todoList instanceof DynamicPerPageInterface);

$list = $todoList->getWithScalarParam(2);
/** @var Page $page */
$page = $list[1];
$this->assertSame([['id' => '1', 'title' => 'run']], $page->data);
$this->assertSame(2, $page->maxPerPage);
$log = (string) $this->logger;
$this->assertStringContainsString('query: todo_list', $log);

$list = $todoList->getWithFakeStringParam(2);
/** @var Page $page */
$page = $list[1];
$this->assertSame([['id' => '1', 'title' => 'run']], $page->data);
$this->assertSame(2, $page->maxPerPage);

$list = $todoList->getWithFakeBoolParam(2);
/** @var Page $page */
$page = $list[1];
$this->assertSame([['id' => '1', 'title' => 'run']], $page->data);
$this->assertSame(2, $page->maxPerPage);
}

public function testDynamicPerPageVariableNameNotGiven(): void
{
$this->expectException(InvalidPerPageVarNameException::class);
Expand All @@ -181,7 +207,7 @@ public function testGivenPerPageShouldBeInt(): void
{
$this->expectException(PerPageNotIntTypeException::class);
$todoList = $this->injector->getInstance(DynamicPerPageInvalidType::class);
$todoList('1'); // @phpstan-ignore-line
$todoList('1');
}

public function testSelectPagerEntity(): void
Expand Down
16 changes: 0 additions & 16 deletions tests/Fake/IdentityValue/IdentityValueItemInterface.php

This file was deleted.

25 changes: 24 additions & 1 deletion tests/Fake/Queries/DynamicPerPageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Ray\MediaQuery\Annotation\DbQuery;
use Ray\MediaQuery\Annotation\Pager;
use Ray\MediaQuery\FakeBool;
use Ray\MediaQuery\FakeString;
use Ray\MediaQuery\Pages;

interface DynamicPerPageInterface
Expand All @@ -15,5 +17,26 @@ interface DynamicPerPageInterface
* @Pager(perPage="perPage", template="/{?page}")
*/
#[DbQuery('todo_list'), Pager(perPage: 'perPage', template: '/{?page}')]
public function __invoke(int $perPage): Pages;
public function get(int $perPage): Pages;

/**
* @DbQuery("todo_list_scalar_param")
* @Pager(perPage="perPage", template="/{?page}")
*/
#[DbQuery('todo_list_scalar_param'), Pager(perPage: 'perPage', template: '/{?page}')]
public function getWithScalarParam(int $perPage, int $scalar = 1): Pages;

/**
* @DbQuery("todo_list_fake_string_param")
* @Pager(perPage="perPage", template="/{?page}")
*/
#[DbQuery('todo_list_fake_string_param'), Pager(perPage: 'perPage', template: '/{?page}')]
public function getWithFakeStringParam(int $perPage, ?FakeString $fakeString = null): Pages;

/**
* @DbQuery("todo_list_fake_bool_param")
* @Pager(perPage="perPage", template="/{?page}")
*/
#[DbQuery('todo_list_fake_bool_param'), Pager(perPage: 'perPage', template: '/{?page}')]
public function getWithFakeBoolParam(int $perPage, ?FakeBool $fakeBool = null): Pages;
}
3 changes: 1 addition & 2 deletions tests/Fake/Queries/TodoConstcuctEntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Ray\MediaQuery\Queries;

use Ray\MediaQuery\Annotation\DbQuery;
use Ray\MediaQuery\Entity\Todo;
use Ray\MediaQuery\Entity\TodoConstruct;

interface TodoConstcuctEntityInterface
Expand All @@ -20,5 +19,5 @@ public function getItem(string $id): TodoConstruct;
* @DbQuery(id="todo_list", entity=TodoConstruct::class)
*/
#[DbQuery('todo_list', entity: TodoConstruct::class)]
public function getlist(): array;
public function getList(): array;
}
2 changes: 1 addition & 1 deletion tests/Fake/Queries/TodoEntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ public function getItem(string $id): Todo;
* @return list<Todo>
*/
#[DbQuery('todo_list', entity: Todo::class)]
public function getlist(): array;
public function getList(): array;
}
3 changes: 3 additions & 0 deletions tests/sql/todo_list_fake_bool_param.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT *
FROM todo
WHERE 1 = :fakeBool;
3 changes: 3 additions & 0 deletions tests/sql/todo_list_fake_string_param.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT *
FROM todo
WHERE 'a' = :fakeString;
3 changes: 3 additions & 0 deletions tests/sql/todo_list_scalar_param.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT *
FROM todo
WHERE 1 = :scalar;

0 comments on commit c82cee1

Please sign in to comment.