|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Packagist. |
| 5 | + * |
| 6 | + * (c) Jordi Boggiano <[email protected]> |
| 7 | + * Nils Adermann <[email protected]> |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the LICENSE |
| 10 | + * file that was distributed with this source code. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace App\Tests\QueryFilter\AuditLog; |
| 14 | + |
| 15 | +use App\Audit\AuditRecordType; |
| 16 | +use App\QueryFilter\AuditLog\AuditRecordTypeFilter; |
| 17 | +use Doctrine\ORM\QueryBuilder; |
| 18 | +use Doctrine\ORM\EntityManagerInterface; |
| 19 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Symfony\Component\HttpFoundation\InputBag; |
| 22 | + |
| 23 | +class AuditRecordTypeFilterTest extends TestCase |
| 24 | +{ |
| 25 | + private EntityManagerInterface $entityManager; |
| 26 | + |
| 27 | + protected function setUp(): void |
| 28 | + { |
| 29 | + $this->entityManager = $this->createMock(EntityManagerInterface::class); |
| 30 | + } |
| 31 | + |
| 32 | + public function testFromQueryWithEmptyInput(): void |
| 33 | + { |
| 34 | + $bag = new InputBag([]); |
| 35 | + $filter = AuditRecordTypeFilter::fromQuery($bag); |
| 36 | + |
| 37 | + $this->assertSame('type', $filter->getKey()); |
| 38 | + $this->assertSame([], $filter->getSelectedValue()); |
| 39 | + } |
| 40 | + |
| 41 | + public function testFromQueryWithMultipleValidAndInvalidTypes(): void |
| 42 | + { |
| 43 | + $types = [ |
| 44 | + AuditRecordType::PackageCreated->value, |
| 45 | + 'invalid_type', |
| 46 | + AuditRecordType::VersionDeleted->value, |
| 47 | + ]; |
| 48 | + |
| 49 | + $bag = new InputBag(['type' => $types]); |
| 50 | + $filter = AuditRecordTypeFilter::fromQuery($bag); |
| 51 | + |
| 52 | + $this->assertSame( |
| 53 | + [AuditRecordType::PackageCreated->value, AuditRecordType::VersionDeleted->value], |
| 54 | + $filter->getSelectedValue() |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + public function testFilterWithEmptyTypes(): void |
| 59 | + { |
| 60 | + $bag = new InputBag([]); |
| 61 | + $filter = AuditRecordTypeFilter::fromQuery($bag); |
| 62 | + |
| 63 | + $qb = new QueryBuilder($this->entityManager); |
| 64 | + $result = $filter->filter($qb); |
| 65 | + |
| 66 | + $this->assertSame($qb, $result); |
| 67 | + $this->assertNull($qb->getDQLPart('where')); |
| 68 | + } |
| 69 | + |
| 70 | + public function testFilterWithTypes(): void |
| 71 | + { |
| 72 | + $types = [ |
| 73 | + AuditRecordType::PackageCreated->value, |
| 74 | + AuditRecordType::VersionReferenceChanged->value, |
| 75 | + ]; |
| 76 | + |
| 77 | + $bag = new InputBag(['type' => $types]); |
| 78 | + $filter = AuditRecordTypeFilter::fromQuery($bag); |
| 79 | + |
| 80 | + $qb = new QueryBuilder($this->entityManager); |
| 81 | + $result = $filter->filter($qb); |
| 82 | + |
| 83 | + $this->assertSame($qb, $result); |
| 84 | + $this->assertNotNull($qb->getDQLPart('where')); |
| 85 | + $this->assertEqualsCanonicalizing( |
| 86 | + $types, |
| 87 | + $qb->getParameter('types')->getValue() |
| 88 | + ); |
| 89 | + } |
| 90 | +} |
0 commit comments