diff --git a/composer.json b/composer.json index 89243dab9..1e55d5854 100644 --- a/composer.json +++ b/composer.json @@ -77,6 +77,7 @@ "matthiasnoback/symfony-config-test": "^4.2 || ^5.1", "matthiasnoback/symfony-dependency-injection-test": "^4.0 || ^5.0", "phpstan/extension-installer": "^1.0", + "phpstan/phpdoc-parser": "^1.0", "phpstan/phpstan": "^1.0", "phpstan/phpstan-phpunit": "^1.0", "phpstan/phpstan-strict-rules": "^1.0", diff --git a/rector.php b/rector.php index b6dac16fc..744632387 100644 --- a/rector.php +++ b/rector.php @@ -19,6 +19,7 @@ use Rector\Config\RectorConfig; use Rector\Php70\Rector\FunctionLike\ExceptionHandlerTypehintRector; +use Rector\PHPUnit\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector; use Rector\PHPUnit\Set\PHPUnitSetList; use Rector\Set\ValueObject\LevelSetList; @@ -40,5 +41,6 @@ $rectorConfig->skip([ ExceptionHandlerTypehintRector::class, PreferPHPUnitThisCallRector::class, + NarrowUnusedSetUpDefinedPropertyRector::class, ]); }; diff --git a/tests/Controller/MediaAdminControllerTest.php b/tests/Controller/MediaAdminControllerTest.php index 97078cef4..831a3f846 100644 --- a/tests/Controller/MediaAdminControllerTest.php +++ b/tests/Controller/MediaAdminControllerTest.php @@ -129,13 +129,28 @@ public function testListAction(): void $this->configureSetCsrfToken('sonata.batch'); $this->configureRender('templateList', 'renderResponse'); + $matcher = static::exactly(3); /** - * @psalm-suppress DeprecatedMethod + * @psalm-suppress MissingClosureParamType */ - $datagrid->expects(static::exactly(3))->method('setValue')->withConsecutive( - ['context', null, 'another_context'], - ['category', null, 1] - ); + $datagrid->expects($matcher)->method('setValue')->willReturnCallback(static function (...$parameters) use ($matcher) { + /** + * @psalm-suppress InternalMethod + */ + if (1 === $matcher->getInvocationCount()) { + self::assertSame('context', $parameters[0]); + self::assertNull($parameters[1]); + self::assertSame('another_context', $parameters[2]); + } + /** + * @psalm-suppress InternalMethod + */ + if (2 === $matcher->getInvocationCount()) { + self::assertSame('category', $parameters[0]); + self::assertNull($parameters[1]); + self::assertSame(1, $parameters[2]); + } + }); $datagrid->method('getForm')->willReturn($form); $contextManager->method('find')->with('another_context')->willReturn($context); $categoryManager->method('getRootCategoriesForContext')->with($context)->willReturn([$category]); diff --git a/tests/Entity/MediaTest.php b/tests/Entity/MediaTest.php index 25a237d2b..7c1aaf71b 100644 --- a/tests/Entity/MediaTest.php +++ b/tests/Entity/MediaTest.php @@ -24,8 +24,8 @@ public function testMetadata(): void $media->setProviderMetadata(['thumbnail_url' => 'http://pasloin.com/thumb.png']); - static::assertSame($media->getMetadataValue('thumbnail_url'), 'http://pasloin.com/thumb.png', '::getMetadataValue() return the good value'); - static::assertSame($media->getMetadataValue('thumbnail_url1', 'default'), 'default', '::getMetadataValue() return the default'); + static::assertSame('http://pasloin.com/thumb.png', $media->getMetadataValue('thumbnail_url'), '::getMetadataValue() return the good value'); + static::assertSame('default', $media->getMetadataValue('thumbnail_url1', 'default'), '::getMetadataValue() return the default'); static::assertNull($media->getMetadataValue('thumbnail_url1'), '::getMetadataValue() return the null value'); } diff --git a/tests/Form/DataTransformer/ProviderDataTransformerTest.php b/tests/Form/DataTransformer/ProviderDataTransformerTest.php index a61135a79..2ae64748b 100644 --- a/tests/Form/DataTransformer/ProviderDataTransformerTest.php +++ b/tests/Form/DataTransformer/ProviderDataTransformerTest.php @@ -129,7 +129,7 @@ public function testReverseTransformWithThrowingProviderThrowTransformationFaile $this->expectException(TransformationFailedException::class); $provider = $this->createMock(MediaProviderInterface::class); - $provider->expects(static::once())->method('transform')->will(static::throwException(new \Exception())); + $provider->expects(static::once())->method('transform')->willThrowException(new \Exception()); $pool = new Pool('default'); $pool->addProvider('default', $provider); diff --git a/tests/Provider/BaseProviderTest.php b/tests/Provider/BaseProviderTest.php index 3f3431410..dcfe521cb 100644 --- a/tests/Provider/BaseProviderTest.php +++ b/tests/Provider/BaseProviderTest.php @@ -44,13 +44,7 @@ public function getProvider(): MediaProviderInterface $cdn = $this->createStub(CDNInterface::class); $cdn->method('flushPaths')->willReturn((string) random_int(0, mt_getrandmax())); - $cdn->method('getFlushStatus') - ->will(static::onConsecutiveCalls( - CDNInterface::STATUS_OK, - CDNInterface::STATUS_TO_FLUSH, - CDNInterface::STATUS_WAITING, - CDNInterface::STATUS_OK - )); + $cdn->method('getFlushStatus')->willReturnOnConsecutiveCalls(CDNInterface::STATUS_OK, CDNInterface::STATUS_TO_FLUSH, CDNInterface::STATUS_WAITING, CDNInterface::STATUS_OK); $cdn->method('getPath')->willReturnCallback(static fn (string $path, bool $isFlushable): string => '/uploads/media/'.$path); $generator = new IdGenerator(); diff --git a/tests/Provider/DailyMotionProviderTest.php b/tests/Provider/DailyMotionProviderTest.php index 70f4456b7..63a67c153 100644 --- a/tests/Provider/DailyMotionProviderTest.php +++ b/tests/Provider/DailyMotionProviderTest.php @@ -230,7 +230,7 @@ public function testGetMetadataException(): void $this->expectExceptionCode(12); $client = $this->createMock(ClientInterface::class); - $client->expects(static::once())->method('sendRequest')->will(static::throwException(new \RuntimeException('First error on get', 12))); + $client->expects(static::once())->method('sendRequest')->willThrowException(new \RuntimeException('First error on get', 12)); $provider = $this->getProvider($client); diff --git a/tests/Provider/ImageProviderTest.php b/tests/Provider/ImageProviderTest.php index 4fbe01c6f..014e876ff 100644 --- a/tests/Provider/ImageProviderTest.php +++ b/tests/Provider/ImageProviderTest.php @@ -46,21 +46,26 @@ public function getProvider(array $allowedExtensions = [], array $allowedMimeTyp $mediumBox = new Box(500, 250); $largeBox = new Box(1000, 500); - $resizer->method('getBox')->will(static::onConsecutiveCalls( - $largeBox, // first properties call + $resizer->method('getBox')->willReturnOnConsecutiveCalls( + $largeBox, + // first properties call $mediumBox, $largeBox, - $mediumBox, // second call $mediumBox, + // second call + $mediumBox, + $largeBox, + $adminBox, + // Third call $largeBox, - $adminBox, // Third call - $largeBox, // Fourth call + // Fourth call $mediumBox, $largeBox, - $largeBox, // Fifth call + $largeBox, + // Fifth call $mediumBox, $largeBox - )); + ); $filesystem = new Filesystem(new Local(sys_get_temp_dir().'/sonata-media-bundle/var/', true)); $cdn = new Server('/uploads/media'); diff --git a/tests/Provider/VimeoProviderTest.php b/tests/Provider/VimeoProviderTest.php index 27cef9938..e77fe0c79 100644 --- a/tests/Provider/VimeoProviderTest.php +++ b/tests/Provider/VimeoProviderTest.php @@ -234,7 +234,7 @@ public function testGetMetadataException(): void $this->expectExceptionCode(12); $client = $this->createMock(ClientInterface::class); - $client->expects(static::once())->method('sendRequest')->will(static::throwException(new \RuntimeException('First error on get', 12))); + $client->expects(static::once())->method('sendRequest')->willThrowException(new \RuntimeException('First error on get', 12)); $provider = $this->getProvider($client); diff --git a/tests/Provider/YouTubeProviderTest.php b/tests/Provider/YouTubeProviderTest.php index a98e94f07..b52a2d201 100644 --- a/tests/Provider/YouTubeProviderTest.php +++ b/tests/Provider/YouTubeProviderTest.php @@ -239,7 +239,7 @@ public function testGetMetadataException(): void $this->expectExceptionCode(12); $client = $this->createMock(ClientInterface::class); - $client->expects(static::once())->method('sendRequest')->will(static::throwException(new \RuntimeException('First error on get', 12))); + $client->expects(static::once())->method('sendRequest')->willThrowException(new \RuntimeException('First error on get', 12)); $provider = $this->getProvider($client);