|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Tests\Admin; |
| 6 | + |
| 7 | +use App\Admin\SemverSort; |
| 8 | +use App\Entity\Installation; |
| 9 | +use Doctrine\ORM\EntityManagerInterface; |
| 10 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 11 | + |
| 12 | +class SemverSortTest extends KernelTestCase |
| 13 | +{ |
| 14 | + public function testRewritesMatchingPropertyToSemverNumeric(): void |
| 15 | + { |
| 16 | + $qb = $this->makeQb()->orderBy('entity.frameworkVersion', 'DESC'); |
| 17 | + |
| 18 | + SemverSort::apply($qb, 'frameworkVersion'); |
| 19 | + |
| 20 | + $parts = $qb->getDQLPart('orderBy'); |
| 21 | + self::assertCount(1, $parts); |
| 22 | + self::assertSame('SEMVER_NUMERIC(entity.frameworkVersion) DESC', $parts[0]->getParts()[0]); |
| 23 | + } |
| 24 | + |
| 25 | + public function testKeepsDirectionAndAddsAscDefaultWhenMissing(): void |
| 26 | + { |
| 27 | + $qb = $this->makeQb()->orderBy('entity.frameworkVersion'); |
| 28 | + |
| 29 | + SemverSort::apply($qb, 'frameworkVersion'); |
| 30 | + |
| 31 | + self::assertSame('SEMVER_NUMERIC(entity.frameworkVersion) ASC', $qb->getDQLPart('orderBy')[0]->getParts()[0]); |
| 32 | + } |
| 33 | + |
| 34 | + public function testLeavesNonMatchingPropertiesUntouched(): void |
| 35 | + { |
| 36 | + $qb = $this->makeQb() |
| 37 | + ->orderBy('entity.rootDir', 'ASC') |
| 38 | + ->addOrderBy('entity.frameworkVersion', 'DESC'); |
| 39 | + |
| 40 | + SemverSort::apply($qb, 'frameworkVersion'); |
| 41 | + |
| 42 | + $allParts = []; |
| 43 | + foreach ($qb->getDQLPart('orderBy') as $orderBy) { |
| 44 | + $allParts = [...$allParts, ...$orderBy->getParts()]; |
| 45 | + } |
| 46 | + self::assertSame(['entity.rootDir ASC', 'SEMVER_NUMERIC(entity.frameworkVersion) DESC'], $allParts); |
| 47 | + } |
| 48 | + |
| 49 | + public function testRewritesMultipleListedProperties(): void |
| 50 | + { |
| 51 | + $qb = $this->makeQb() |
| 52 | + ->orderBy('entity.frameworkVersion', 'DESC') |
| 53 | + ->addOrderBy('entity.composerVersion', 'ASC'); |
| 54 | + |
| 55 | + SemverSort::apply($qb, 'frameworkVersion', 'composerVersion'); |
| 56 | + |
| 57 | + $allParts = []; |
| 58 | + foreach ($qb->getDQLPart('orderBy') as $orderBy) { |
| 59 | + $allParts = [...$allParts, ...$orderBy->getParts()]; |
| 60 | + } |
| 61 | + self::assertSame( |
| 62 | + ['SEMVER_NUMERIC(entity.frameworkVersion) DESC', 'SEMVER_NUMERIC(entity.composerVersion) ASC'], |
| 63 | + $allParts, |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + public function testIsNoopWhenNoOrderByPresent(): void |
| 68 | + { |
| 69 | + $qb = $this->makeQb(); |
| 70 | + |
| 71 | + SemverSort::apply($qb, 'frameworkVersion'); |
| 72 | + |
| 73 | + self::assertSame([], $qb->getDQLPart('orderBy')); |
| 74 | + } |
| 75 | + |
| 76 | + private function makeQb(): \Doctrine\ORM\QueryBuilder |
| 77 | + { |
| 78 | + return $this->getEntityManager()->getRepository(Installation::class)->createQueryBuilder('entity'); |
| 79 | + } |
| 80 | + |
| 81 | + private function getEntityManager(): EntityManagerInterface |
| 82 | + { |
| 83 | + return static::getContainer()->get('doctrine')->getManager(); |
| 84 | + } |
| 85 | +} |
0 commit comments