Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude committed Jan 23, 2023
1 parent 33ccee1 commit 54cf0ec
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 239 deletions.
2 changes: 2 additions & 0 deletions lib/Doctrine/ORM/Tools/Pagination/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
use function array_key_exists;
use function array_map;
use function array_sum;
use function assert;
use function count;
use function is_string;

/**
* The paginator can handle various complex scenarios with DQL.
Expand Down
30 changes: 30 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH7820Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPUnit\Framework\Assert;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\CacheItem;

use function array_map;
use function is_string;
Expand Down Expand Up @@ -112,6 +113,35 @@ public function save(CacheItemInterface $item): bool
$this->fetchSongLinesWithPaginator();
}

public function testPaginatorQueriesWillBeCached(): void
{
$cache = new class extends ArrayAdapter {
/** @var bool */
private $failOnCacheMiss = false;

public function failOnCacheMiss(): void
{
$this->failOnCacheMiss = true;
}

public function getItem(mixed $key): CacheItem
{
$item = parent::getItem($key);
Assert::assertTrue(! $this->failOnCacheMiss || $item->isHit(), 'cache was missed');

return $item;
}
};
$this->_em->getConfiguration()->setQueryCache($cache);

// Prime the cache
$this->fetchSongLinesWithPaginator();

$cache->failOnCacheMiss();

$this->fetchSongLinesWithPaginator();
}

private function fetchSongLinesWithPaginator(): array
{
$query = $this->_em->getRepository(GH7820Line::class)
Expand Down
54 changes: 54 additions & 0 deletions tests/Doctrine/Tests/ORM/Tools/Pagination/RootTypeWalkerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Tools\Pagination;

use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Query;
use Doctrine\ORM\Tools\Pagination\RootTypeWalker;
use Doctrine\Tests\DbalTypes\Rot13Type;
use Doctrine\Tests\Models\ValueConversionType\AuxiliaryEntity;
use Doctrine\Tests\Models\ValueConversionType\OwningManyToOneIdForeignKeyEntity;
use Generator;

class RootTypeWalkerTest extends PaginationTestCase
{
protected function setUp(): void
{
parent::setUp();

if (! Type::hasType('rot13')) {
Type::addType('rot13', Rot13Type::class);
}
}

/**
* @dataProvider exampleQueries
*/
public function testResolveTypeMapping(string $dqlQuery, string $expectedType): void
{
$query = $this->entityManager->createQuery($dqlQuery);
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, RootTypeWalker::class);

self::assertSame($expectedType, $query->getSQL());
}

public function exampleQueries(): Generator
{
yield 'Entity with #Id column of special type' => [
'SELECT e.id4 FROM ' . AuxiliaryEntity::class . ' e',
'rot13',
];

yield 'Entity where #Id is a to-one relation with special type identifier' => [
'SELECT e FROM ' . OwningManyToOneIdForeignKeyEntity::class . ' e',
'rot13',
];

yield 'Simple integer ID in a query with a JOIN' => [
'SELECT u, g FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g',
'integer',
];
}
}
Loading

0 comments on commit 54cf0ec

Please sign in to comment.