|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace APITester\Tests\Config; |
| 6 | + |
| 7 | +use APITester\Config\Filters; |
| 8 | +use APITester\Util\Filterable; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Symfony\Component\Yaml\Tag\TaggedValue; |
| 11 | + |
| 12 | +final class FiltersTest extends TestCase |
| 13 | +{ |
| 14 | + private ?string $tempFile = null; |
| 15 | + |
| 16 | + protected function tearDown(): void |
| 17 | + { |
| 18 | + if ($this->tempFile !== null && file_exists($this->tempFile)) { |
| 19 | + unlink($this->tempFile); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public function testDefaultConstructorHasEmptyArrays(): void |
| 24 | + { |
| 25 | + $filters = new Filters(); |
| 26 | + |
| 27 | + static::assertSame([], $filters->getInclude()); |
| 28 | + static::assertSame([], $filters->getExclude()); |
| 29 | + } |
| 30 | + |
| 31 | + public function testAddIncludeAppendsRules(): void |
| 32 | + { |
| 33 | + $filters = new Filters(); |
| 34 | + $filters->addInclude([ |
| 35 | + [ |
| 36 | + 'id' => 'foo', |
| 37 | + ], |
| 38 | + ]); |
| 39 | + $filters->addInclude([ |
| 40 | + [ |
| 41 | + 'id' => 'bar', |
| 42 | + ], |
| 43 | + ]); |
| 44 | + |
| 45 | + static::assertCount(2, $filters->getInclude()); |
| 46 | + static::assertSame('foo', $filters->getInclude()[0]['id']); |
| 47 | + static::assertSame('bar', $filters->getInclude()[1]['id']); |
| 48 | + } |
| 49 | + |
| 50 | + public function testAddExcludeAppendsRules(): void |
| 51 | + { |
| 52 | + $filters = new Filters(); |
| 53 | + $filters->addExclude([ |
| 54 | + [ |
| 55 | + 'method' => 'DELETE', |
| 56 | + ], |
| 57 | + ]); |
| 58 | + |
| 59 | + static::assertCount(1, $filters->getExclude()); |
| 60 | + } |
| 61 | + |
| 62 | + public function testIncludesWithEmptyFiltersReturnsTrue(): void |
| 63 | + { |
| 64 | + $filters = new Filters(); |
| 65 | + $object = $this->createFilterable([ |
| 66 | + 'id' => 'anything', |
| 67 | + ]); |
| 68 | + |
| 69 | + static::assertTrue($filters->includes($object)); |
| 70 | + } |
| 71 | + |
| 72 | + public function testIncludesWithMatchingIncludeRule(): void |
| 73 | + { |
| 74 | + $filters = new Filters([ |
| 75 | + [ |
| 76 | + 'id' => 'foo', |
| 77 | + ], |
| 78 | + ]); |
| 79 | + $matching = $this->createFilterable([ |
| 80 | + 'id' => 'foo', |
| 81 | + ]); |
| 82 | + $nonMatching = $this->createFilterable([ |
| 83 | + 'id' => 'bar', |
| 84 | + ]); |
| 85 | + |
| 86 | + static::assertTrue($filters->includes($matching)); |
| 87 | + static::assertFalse($filters->includes($nonMatching)); |
| 88 | + } |
| 89 | + |
| 90 | + public function testIncludesWithExcludeRule(): void |
| 91 | + { |
| 92 | + $filters = new Filters(null, [ |
| 93 | + [ |
| 94 | + 'id' => 'excluded', |
| 95 | + ], |
| 96 | + ]); |
| 97 | + $excluded = $this->createFilterable([ |
| 98 | + 'id' => 'excluded', |
| 99 | + ]); |
| 100 | + $included = $this->createFilterable([ |
| 101 | + 'id' => 'other', |
| 102 | + ]); |
| 103 | + |
| 104 | + static::assertFalse($filters->includes($excluded)); |
| 105 | + static::assertTrue($filters->includes($included)); |
| 106 | + } |
| 107 | + |
| 108 | + public function testHandleTagsNotProducesNotEqual(): void |
| 109 | + { |
| 110 | + $ref = new \ReflectionMethod(Filters::class, 'handleTags'); |
| 111 | + $ref->setAccessible(true); |
| 112 | + $filters = new Filters(); |
| 113 | + |
| 114 | + /** @var array{0: string, 1: string|int|null} $result */ |
| 115 | + $result = $ref->invoke($filters, new TaggedValue('NOT', 'DELETE')); |
| 116 | + |
| 117 | + static::assertSame('!=', $result[0]); |
| 118 | + static::assertSame('DELETE', $result[1]); |
| 119 | + } |
| 120 | + |
| 121 | + public function testHandleTagsInProducesContains(): void |
| 122 | + { |
| 123 | + $ref = new \ReflectionMethod(Filters::class, 'handleTags'); |
| 124 | + $ref->setAccessible(true); |
| 125 | + $filters = new Filters(); |
| 126 | + |
| 127 | + /** @var array{0: string, 1: string|int|null} $result */ |
| 128 | + $result = $ref->invoke($filters, new TaggedValue('IN', 'pet')); |
| 129 | + |
| 130 | + static::assertSame('contains', $result[0]); |
| 131 | + static::assertSame('pet', $result[1]); |
| 132 | + } |
| 133 | + |
| 134 | + public function testHandleTagsNullStringConvertsToNull(): void |
| 135 | + { |
| 136 | + $ref = new \ReflectionMethod(Filters::class, 'handleTags'); |
| 137 | + $ref->setAccessible(true); |
| 138 | + $filters = new Filters(); |
| 139 | + |
| 140 | + /** @var array{0: string, 1: string|int|null} $result */ |
| 141 | + $result = $ref->invoke($filters, 'null'); |
| 142 | + |
| 143 | + static::assertSame('=', $result[0]); |
| 144 | + static::assertNull($result[1]); |
| 145 | + } |
| 146 | + |
| 147 | + public function testWriteBaselineAndGetBaselineExcludeRoundTrip(): void |
| 148 | + { |
| 149 | + $this->tempFile = tempnam(sys_get_temp_dir(), 'api-tester-test-') . '.yaml'; |
| 150 | + $filters = new Filters(null, null, $this->tempFile); |
| 151 | + |
| 152 | + $exclude = [ |
| 153 | + [ |
| 154 | + 'id' => 'test_1', |
| 155 | + ], |
| 156 | + [ |
| 157 | + 'id' => 'test_2', |
| 158 | + ], |
| 159 | + ]; |
| 160 | + $filters->writeBaseline($exclude); |
| 161 | + |
| 162 | + $result = $filters->getBaseLineExclude(); |
| 163 | + |
| 164 | + static::assertCount(2, $result); |
| 165 | + static::assertSame('test_1', $result[0]['id']); |
| 166 | + static::assertSame('test_2', $result[1]['id']); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @param array<string, mixed> $props |
| 171 | + */ |
| 172 | + private function createFilterable(array $props): Filterable |
| 173 | + { |
| 174 | + return new class($props) implements Filterable { |
| 175 | + /** |
| 176 | + * @param array<string, mixed> $props |
| 177 | + */ |
| 178 | + public function __construct( |
| 179 | + private readonly array $props |
| 180 | + ) { |
| 181 | + } |
| 182 | + |
| 183 | + public function has(string $prop, $value, string $operator = '='): bool |
| 184 | + { |
| 185 | + if (!array_key_exists($prop, $this->props)) { |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | + $propValue = $this->props[$prop]; |
| 190 | + |
| 191 | + return match ($operator) { |
| 192 | + '=' => $propValue === $value, |
| 193 | + '!=' => $propValue !== $value, |
| 194 | + 'contains' => is_string($propValue) && is_string($value) && str_contains($propValue, $value), |
| 195 | + default => false, |
| 196 | + }; |
| 197 | + } |
| 198 | + }; |
| 199 | + } |
| 200 | +} |
0 commit comments