|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace RoaveTest\BackwardCompatibility\DetectChanges\BCBreak\MethodBased; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Roave\BackwardCompatibility\Change; |
| 9 | +use Roave\BackwardCompatibility\Changes; |
| 10 | +use Roave\BackwardCompatibility\DetectChanges\BCBreak\MethodBased\MethodBased; |
| 11 | +use Roave\BackwardCompatibility\DetectChanges\BCBreak\MethodBased\MethodParameterAdded; |
| 12 | +use Roave\BetterReflection\BetterReflection; |
| 13 | +use Roave\BetterReflection\Reflection\ReflectionClass; |
| 14 | +use Roave\BetterReflection\Reflection\ReflectionMethod; |
| 15 | +use Roave\BetterReflection\Reflector\DefaultReflector; |
| 16 | +use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator; |
| 17 | + |
| 18 | +use function array_combine; |
| 19 | +use function array_keys; |
| 20 | +use function array_map; |
| 21 | +use function assert; |
| 22 | +use function iterator_to_array; |
| 23 | + |
| 24 | +/** @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\MethodBased\MethodParameterAdded */ |
| 25 | +final class MethodParameterAddedTest extends TestCase |
| 26 | +{ |
| 27 | + private MethodBased $methodCheck; |
| 28 | + |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + $this->methodCheck = new MethodParameterAdded(); |
| 34 | + } |
| 35 | + |
| 36 | + public function testWillSkipCheckingPrivateMethods(): void |
| 37 | + { |
| 38 | + $from = $this->createMock(ReflectionMethod::class); |
| 39 | + $to = $this->createMock(ReflectionMethod::class); |
| 40 | + |
| 41 | + $from |
| 42 | + ->method('isPrivate') |
| 43 | + ->willReturn(true); |
| 44 | + |
| 45 | + self::assertEquals(Changes::empty(), ($this->methodCheck)($from, $to)); |
| 46 | + } |
| 47 | + |
| 48 | + public function testWillSkipCheckingMethodsOnFinalClasses(): void |
| 49 | + { |
| 50 | + $from = $this->createMock(ReflectionMethod::class); |
| 51 | + $to = $this->createMock(ReflectionMethod::class); |
| 52 | + |
| 53 | + $from |
| 54 | + ->method('isPrivate') |
| 55 | + ->willReturn(false); |
| 56 | + |
| 57 | + self::assertEquals(Changes::empty(), ($this->methodCheck)($from, $to)); |
| 58 | + } |
| 59 | + |
| 60 | + public function testWillSkipCheckingPrivateMethodsOnFinalClasses(): void |
| 61 | + { |
| 62 | + $from = $this->createMock(ReflectionMethod::class); |
| 63 | + $to = $this->createMock(ReflectionMethod::class); |
| 64 | + |
| 65 | + $from |
| 66 | + ->method('isPrivate') |
| 67 | + ->willReturn(true); |
| 68 | + |
| 69 | + $from |
| 70 | + ->method('isFinal') |
| 71 | + ->willReturn(true); |
| 72 | + |
| 73 | + self::assertEquals(Changes::empty(), ($this->methodCheck)($from, $to)); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param string[] $expectedMessages |
| 78 | + * |
| 79 | + * @dataProvider methodsToBeTested |
| 80 | + */ |
| 81 | + public function testDiffs( |
| 82 | + ReflectionMethod $fromMethod, |
| 83 | + ReflectionMethod $toMethod, |
| 84 | + array $expectedMessages, |
| 85 | + ): void { |
| 86 | + $changes = (new MethodParameterAdded())($fromMethod, $toMethod); |
| 87 | + |
| 88 | + self::assertSame( |
| 89 | + $expectedMessages, |
| 90 | + array_map(static function (Change $change): string { |
| 91 | + return $change->__toString(); |
| 92 | + }, iterator_to_array($changes)), |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + /** @return array<string, array{0: ReflectionMethod, 1: ReflectionMethod, 2: list<string>}> */ |
| 97 | + public function methodsToBeTested(): array |
| 98 | + { |
| 99 | + $astLocator = (new BetterReflection())->astLocator(); |
| 100 | + |
| 101 | + $fromLocator = new StringSourceLocator( |
| 102 | + <<<'PHP' |
| 103 | +<?php |
| 104 | +
|
| 105 | +class TheClass { |
| 106 | + public function addedParameter() {} |
| 107 | + public function addedTwoParameters() {} |
| 108 | + public function addedAnotherParameter(int $one) {} |
| 109 | + public function noParameters() {} |
| 110 | + public function twoParams(int $one, int $two) {} |
| 111 | + private function privateMethod() {} |
| 112 | + public function removedParameter(int $one, array $options = []) {} |
| 113 | +} |
| 114 | +PHP |
| 115 | + , |
| 116 | + $astLocator, |
| 117 | + ); |
| 118 | + |
| 119 | + $toLocator = new StringSourceLocator( |
| 120 | + <<<'PHP' |
| 121 | +<?php |
| 122 | +
|
| 123 | +class TheClass { |
| 124 | + public function addedParameter(array $options = []) {} |
| 125 | + public function addedTwoParameters(int $one, array $options = []) {} |
| 126 | + public function addedAnotherParameter(int $one, array $options = []) {} |
| 127 | + public function noParameters() {} |
| 128 | + public function twoParams(int $one, int $two) {} |
| 129 | + private function privateMethod(array $options = []) {} |
| 130 | + public function removedParameter(int $one) {} |
| 131 | +} |
| 132 | +PHP |
| 133 | + , |
| 134 | + $astLocator, |
| 135 | + ); |
| 136 | + |
| 137 | + $fromClassReflector = new DefaultReflector($fromLocator); |
| 138 | + $toClassReflector = new DefaultReflector($toLocator); |
| 139 | + $fromClass = $fromClassReflector->reflectClass('TheClass'); |
| 140 | + $toClass = $toClassReflector->reflectClass('TheClass'); |
| 141 | + |
| 142 | + $methods = [ |
| 143 | + 'addedParameter' => ['[BC] ADDED: Parameter options was added to Method addedParameter() of class TheClass'], |
| 144 | + 'addedTwoParameters' => [ |
| 145 | + '[BC] ADDED: Parameter one was added to Method addedTwoParameters() of class TheClass', |
| 146 | + '[BC] ADDED: Parameter options was added to Method addedTwoParameters() of class TheClass', |
| 147 | + ], |
| 148 | + 'addedAnotherParameter' => ['[BC] ADDED: Parameter options was added to Method addedAnotherParameter() of class TheClass'], |
| 149 | + 'noParameters' => [], |
| 150 | + 'privateMethod' => [], |
| 151 | + 'removedParameter' => [], |
| 152 | + 'twoParams' => [], |
| 153 | + ]; |
| 154 | + |
| 155 | + return array_combine( |
| 156 | + array_keys($methods), |
| 157 | + array_map( |
| 158 | + static fn (string $methodName, array $errors): array => [ |
| 159 | + self::getMethod($fromClass, $methodName), |
| 160 | + self::getMethod($toClass, $methodName), |
| 161 | + $errors, |
| 162 | + ], |
| 163 | + array_keys($methods), |
| 164 | + $methods, |
| 165 | + ), |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + /** @param non-empty-string $name */ |
| 170 | + private static function getMethod(ReflectionClass $class, string $name): ReflectionMethod |
| 171 | + { |
| 172 | + $method = $class->getMethod($name); |
| 173 | + |
| 174 | + assert($method !== null); |
| 175 | + |
| 176 | + return $method; |
| 177 | + } |
| 178 | +} |
0 commit comments