SignpostMarv /
daft-interface-collector
| 1 | <?php |
||||||||
| 2 | |||||||||
| 3 | declare(strict_types=1); |
||||||||
| 4 | /** |
||||||||
| 5 | * @author SignpostMarv |
||||||||
| 6 | */ |
||||||||
| 7 | |||||||||
| 8 | namespace SignpostMarv\DaftInterfaceCollector\Tests; |
||||||||
| 9 | |||||||||
| 10 | use Generator; |
||||||||
| 11 | use InvalidArgumentException; |
||||||||
| 12 | use PHPUnit\Framework\TestCase as Base; |
||||||||
| 13 | use SignpostMarv\DaftInterfaceCollector\StaticMethodCollector; |
||||||||
| 14 | |||||||||
| 15 | class StaticMethodCollectorTest extends Base |
||||||||
| 16 | { |
||||||||
| 17 | /** |
||||||||
| 18 | * @var bool |
||||||||
| 19 | */ |
||||||||
| 20 | protected $backupGlobals = false; |
||||||||
| 21 | |||||||||
| 22 | /** |
||||||||
| 23 | * @var bool |
||||||||
| 24 | */ |
||||||||
| 25 | protected $backupStaticAttributes = false; |
||||||||
| 26 | |||||||||
| 27 | /** |
||||||||
| 28 | * @var bool |
||||||||
| 29 | */ |
||||||||
| 30 | protected $runTestInSeparateProcess = false; |
||||||||
| 31 | |||||||||
| 32 | /** |
||||||||
| 33 | * @return Generator<int, array{0:array<class-string, array<string, array<int, class-string>>>, 1:array<int, class-string>, 2:array<int, class-string>, 3:class-string}, mixed, void> |
||||||||
| 34 | */ |
||||||||
| 35 | public function DataProviderCollection() : Generator |
||||||||
| 36 | { |
||||||||
| 37 | yield from [ |
||||||||
| 38 | [ |
||||||||
| 39 | [ |
||||||||
| 40 | Fixtures\DaftSource::class => [ |
||||||||
| 41 | 'DaftRouterRouteAndMiddlewareSources' => [ |
||||||||
| 42 | Fixtures\DaftMiddleware::class, |
||||||||
| 43 | Fixtures\DaftRoute::class, |
||||||||
| 44 | Fixtures\DaftSource::class, |
||||||||
| 45 | ], |
||||||||
| 46 | ], |
||||||||
| 47 | ], |
||||||||
| 48 | [ |
||||||||
| 49 | Fixtures\DaftMiddleware::class, |
||||||||
| 50 | Fixtures\DaftRoute::class, |
||||||||
| 51 | ], |
||||||||
| 52 | [ |
||||||||
| 53 | Fixtures\Home::class, |
||||||||
| 54 | Fixtures\Login::class, |
||||||||
| 55 | Fixtures\NotLoggedIn::class, |
||||||||
| 56 | ], |
||||||||
| 57 | Fixtures\Config::class, |
||||||||
| 58 | ], |
||||||||
| 59 | ]; |
||||||||
| 60 | } |
||||||||
| 61 | |||||||||
| 62 | /** |
||||||||
| 63 | * @return Generator<int, array{0:bool, 1:array<class-string, array<string, array<int, class-string>>>, 2:array<int, class-string>, 3:array<int, class-string>, 4:class-string}, mixed, void> |
||||||||
| 64 | */ |
||||||||
| 65 | public function DataProviderCollectionToggle() : Generator |
||||||||
| 66 | { |
||||||||
| 67 | /** |
||||||||
| 68 | * @var iterable<array<int, scalar|array>> |
||||||||
| 69 | */ |
||||||||
| 70 | $sources = $this->DataProviderCollection(); |
||||||||
| 71 | |||||||||
| 72 | foreach ($sources as $args) { |
||||||||
| 73 | /** |
||||||||
| 74 | * @var array{0:true, 1:array<class-string, array<string, array<int, class-string>>>, 2:array<int, class-string>, 3:array<int, class-string>, 4:class-string} |
||||||||
| 75 | */ |
||||||||
| 76 | $out = array_merge([true], $args); |
||||||||
| 77 | |||||||||
| 78 | yield $out; |
||||||||
| 79 | |||||||||
| 80 | /** |
||||||||
| 81 | * @var array{0:false, 1:array<class-string, array<string, array<int, class-string>>>, 2:array<int, class-string>, 3:array<int, class-string>, 4:class-string} |
||||||||
| 82 | */ |
||||||||
| 83 | $out = array_merge([false], $args); |
||||||||
| 84 | |||||||||
| 85 | yield $out; |
||||||||
| 86 | } |
||||||||
| 87 | } |
||||||||
| 88 | |||||||||
| 89 | /** |
||||||||
| 90 | * @param array<class-string, array<string, array<int, class-string>>> $staticMethods |
||||||||
| 91 | * @param array<int, class-string> $interfaces |
||||||||
| 92 | * @param class-string ...$implementations |
||||||||
| 93 | * |
||||||||
| 94 | * @dataProvider DataProviderCollectionToggle |
||||||||
| 95 | */ |
||||||||
| 96 | public function testCollection( |
||||||||
| 97 | bool $semiResetting, |
||||||||
| 98 | array $staticMethods, |
||||||||
| 99 | array $interfaces, |
||||||||
| 100 | array $expectedResult, |
||||||||
| 101 | string ...$implementations |
||||||||
| 102 | ) : void { |
||||||||
| 103 | $collector = |
||||||||
| 104 | $semiResetting |
||||||||
| 105 | ? new StaticMethodCollector($staticMethods, $interfaces) |
||||||||
| 106 | : new Fixtures\SemiResettingStaticMethodCollector($staticMethods, $interfaces); |
||||||||
| 107 | |||||||||
| 108 | $collection = $collector->Collect(...$implementations); |
||||||||
| 109 | |||||||||
| 110 | static::assertSame($expectedResult, iterator_to_array($collection)); |
||||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
The method
PHPUnit\Framework\Assert::assertSame() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 111 | |||||||||
| 112 | $collection = $collector->Collect(...$implementations); |
||||||||
| 113 | |||||||||
| 114 | if ( ! ($collector instanceof Fixtures\SemiResettingStaticMethodCollector)) { |
||||||||
| 115 | static::assertSame($expectedResult, iterator_to_array($collection)); |
||||||||
| 116 | } else { |
||||||||
| 117 | static::assertSame([], iterator_to_array($collection)); |
||||||||
|
0 ignored issues
–
show
array() of type array is incompatible with the type PHPUnit\Framework\T expected by parameter $expected of PHPUnit\Framework\Assert::assertSame().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 118 | } |
||||||||
| 119 | } |
||||||||
| 120 | |||||||||
| 121 | /** |
||||||||
| 122 | * @param array<class-string, array<string, array<int, class-string>>> $staticMethods |
||||||||
| 123 | * @param array<int, class-string> $interfaces |
||||||||
| 124 | * @param array<int, class-string> $expectedResult |
||||||||
| 125 | * @param string ...$implementations |
||||||||
| 126 | * |
||||||||
| 127 | * @psalm-param class-string ...$implementations |
||||||||
| 128 | * |
||||||||
| 129 | * @dataProvider DataProviderCollectionToggle |
||||||||
| 130 | */ |
||||||||
| 131 | public function testCollectionWithoutResettingProcessedSources( |
||||||||
| 132 | bool $semiResetting, |
||||||||
| 133 | array $staticMethods, |
||||||||
| 134 | array $interfaces, |
||||||||
| 135 | array $expectedResult, |
||||||||
| 136 | string ...$implementations |
||||||||
| 137 | ) : void { |
||||||||
| 138 | $collector = |
||||||||
| 139 | $semiResetting |
||||||||
| 140 | ? new StaticMethodCollector($staticMethods, $interfaces, false) |
||||||||
| 141 | : new Fixtures\SemiResettingStaticMethodCollector( |
||||||||
| 142 | $staticMethods, |
||||||||
| 143 | $interfaces, |
||||||||
| 144 | false |
||||||||
| 145 | ); |
||||||||
| 146 | |||||||||
| 147 | $collection = $collector->Collect(...$implementations); |
||||||||
| 148 | |||||||||
| 149 | static::assertSame($expectedResult, iterator_to_array($collection)); |
||||||||
|
0 ignored issues
–
show
$expectedResult of type array is incompatible with the type PHPUnit\Framework\T expected by parameter $expected of PHPUnit\Framework\Assert::assertSame().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The method
PHPUnit\Framework\Assert::assertSame() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 150 | |||||||||
| 151 | $collection = $collector->Collect(...$implementations); |
||||||||
| 152 | |||||||||
| 153 | static::assertSame([], iterator_to_array($collection)); |
||||||||
|
0 ignored issues
–
show
array() of type array is incompatible with the type PHPUnit\Framework\T expected by parameter $expected of PHPUnit\Framework\Assert::assertSame().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 154 | } |
||||||||
| 155 | |||||||||
| 156 | public function testCollectInterfacesFromImplementationTypesFails() : void |
||||||||
| 157 | { |
||||||||
| 158 | $collector = new Fixtures\StaticMethodCollector\PublicCollectInterfacesFromImplementationTypes( |
||||||||
| 159 | [], |
||||||||
| 160 | [] |
||||||||
| 161 | ); |
||||||||
| 162 | |||||||||
| 163 | static::expectException(InvalidArgumentException::class); |
||||||||
|
0 ignored issues
–
show
The method
PHPUnit\Framework\TestCase::expectException() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 164 | |||||||||
| 165 | $collector->PublicCollectInterfacesFromImplementationTypes( |
||||||||
| 166 | Generator::class, |
||||||||
| 167 | 'foo', |
||||||||
| 168 | [] |
||||||||
| 169 | )->valid(); |
||||||||
| 170 | } |
||||||||
| 171 | } |
||||||||
| 172 |