diff --git a/fixtures/Compactor/DummyCompactor.php b/src/Compactor/NullCompactor.php similarity index 86% rename from fixtures/Compactor/DummyCompactor.php rename to src/Compactor/NullCompactor.php index 5578aba03..a02a7c334 100644 --- a/fixtures/Compactor/DummyCompactor.php +++ b/src/Compactor/NullCompactor.php @@ -14,7 +14,10 @@ namespace KevinGH\Box\Compactor; -class DummyCompactor implements Compactor +/** + * @private + */ +final class NullCompactor implements Compactor { public function compact(string $file, string $contents): string { diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index 9c1bf3c56..ddc5b051c 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -19,10 +19,7 @@ use DateTimeZone; use Fidry\FileSystem\FS; use Humbug\PhpScoper\Configuration\Configuration as PhpScoperConfiguration; -use Humbug\PhpScoper\Container; use InvalidArgumentException; -use KevinGH\Box\Annotation\CompactedFormatter; -use KevinGH\Box\Annotation\DocblockAnnotationParser; use KevinGH\Box\Compactor\Compactor; use KevinGH\Box\Compactor\Compactors; use KevinGH\Box\Compactor\Php as PhpCompactor; @@ -34,9 +31,9 @@ use KevinGH\Box\MapFile; use KevinGH\Box\Phar\CompressionAlgorithm; use KevinGH\Box\Phar\SigningAlgorithm; +use KevinGH\Box\PhpScoper\ConfigurationFactory as PhpScoperConfigurationFactory; use KevinGH\Box\PhpScoper\SerializableScoper; use Phar; -use phpDocumentor\Reflection\DocBlockFactory; use RuntimeException; use Seld\JsonLint\ParsingException; use SplFileInfo; @@ -45,7 +42,6 @@ use Symfony\Component\Finder\Finder; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; -use Throwable; use Webmozart\Assert\Assert; use function array_diff; use function array_filter; @@ -2546,7 +2542,7 @@ private static function retrievePhpScoperConfig(stdClass $raw, string $basePath, $configFilePath = Path::makeAbsolute(self::PHP_SCOPER_CONFIG, $basePath); $configFilePath = file_exists($configFilePath) ? $configFilePath : null; - return self::createPhpScoperConfig($configFilePath); + return PhpScoperConfigurationFactory::create($configFilePath); } $configFile = $raw->{self::PHP_SCOPER_KEY}; @@ -2558,26 +2554,7 @@ private static function retrievePhpScoperConfig(stdClass $raw, string $basePath, Assert::file($configFilePath); Assert::readable($configFilePath); - return self::createPhpScoperConfig($configFilePath); - } - - public static function createPhpScoperConfig(?string $filePath): PhpScoperConfiguration - { - $configFactory = (new Container())->getConfigurationFactory(); - - try { - return $configFactory->create($filePath); - } catch (Throwable $throwable) { - throw new InvalidArgumentException( - sprintf( - 'Could not create a PHP-Scoper config from the file "%s": %s', - $filePath, - $throwable->getMessage(), - ), - $throwable->getCode(), - $throwable, - ); - } + return PhpScoperConfigurationFactory::create($configFilePath); } /** @@ -2686,13 +2663,7 @@ private static function createPhpCompactor(?array $ignoredAnnotations): Compacto ), ); - return new PhpCompactor( - new DocblockAnnotationParser( - DocBlockFactory::createInstance(), - new CompactedFormatter(), - $ignoredAnnotations, - ), - ); + return PhpCompactor::create($ignoredAnnotations); } private static function createPhpScoperCompactor( diff --git a/src/PhpScoper/ConfigurationFactory.php b/src/PhpScoper/ConfigurationFactory.php new file mode 100644 index 000000000..35428a04f --- /dev/null +++ b/src/PhpScoper/ConfigurationFactory.php @@ -0,0 +1,46 @@ + + * Théo Fidry + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace KevinGH\Box\PhpScoper; + +use Humbug\PhpScoper\Configuration\Configuration as PhpScoperConfiguration; +use Humbug\PhpScoper\Container; +use InvalidArgumentException; +use KevinGH\Box\NotInstantiable; +use Throwable; +use function sprintf; + +final class ConfigurationFactory +{ + use NotInstantiable; + + public static function create(?string $filePath): PhpScoperConfiguration + { + $configFactory = (new Container())->getConfigurationFactory(); + + try { + return $configFactory->create($filePath); + } catch (Throwable $throwable) { + throw new InvalidArgumentException( + sprintf( + 'Could not create a PHP-Scoper config from the file "%s": %s', + $filePath, + $throwable->getMessage(), + ), + $throwable->getCode(), + $throwable, + ); + } + } +} diff --git a/tests/Compactor/CompactorTestCase.php b/tests/Compactor/CompactorTestCase.php new file mode 100644 index 000000000..0703e1cb9 --- /dev/null +++ b/tests/Compactor/CompactorTestCase.php @@ -0,0 +1,37 @@ + + * Théo Fidry + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace KevinGH\Box\Compactor; + +use PHPUnit\Framework\TestCase; +use function serialize; +use function unserialize; + +abstract class CompactorTestCase extends TestCase +{ + /** + * @dataProvider compactorProvider + */ + public function test_it_is_serializable(Compactor $compactor): void + { + $unserializedCompactor = unserialize(serialize($compactor)); + + self::assertEquals( + $compactor, + $unserializedCompactor, + ); + } + + abstract public static function compactorProvider(): iterable; +} diff --git a/tests/Compactor/CompactorsTest.php b/tests/Compactor/CompactorsTest.php index e5db31c77..b404154e9 100644 --- a/tests/Compactor/CompactorsTest.php +++ b/tests/Compactor/CompactorsTest.php @@ -22,6 +22,8 @@ use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophet; +use function serialize; +use function unserialize; /** * @covers \KevinGH\Box\Compactor\Compactors @@ -205,6 +207,34 @@ public static function compactorsForFirstSymbolsRegistryChangeProvider(): iterab ]; } + /** + * @dataProvider compactorsProvider + */ + public function test_it_can_be_serialized_and_deserialized(Compactors $compactors): void + { + $unserializedCompactors = unserialize(serialize($compactors)); + + self::assertEquals( + $compactors, + $unserializedCompactors, + ); + } + + public static function compactorsProvider(): iterable + { + yield 'empty compactor' => [ + new Compactors(), + ]; + + yield 'compactor with PHP-Scoper' => [ + new Compactors( + self::createScoperCompactor( + new SymbolsRegistry(), + ), + ), + ]; + } + private static function createScoperCompactor(SymbolsRegistry $symbolsRegistry): PhpScoper { return new PhpScoper(new NullScoper($symbolsRegistry)); diff --git a/tests/Compactor/JsonTest.php b/tests/Compactor/JsonTest.php index 870ba05f5..3505ffd9e 100644 --- a/tests/Compactor/JsonTest.php +++ b/tests/Compactor/JsonTest.php @@ -14,16 +14,12 @@ namespace KevinGH\Box\Compactor; -use PHPUnit\Framework\TestCase; -use function serialize; -use function unserialize; - /** * @covers \KevinGH\Box\Compactor\Json * * @internal */ -class JsonTest extends TestCase +class JsonTest extends CompactorTestCase { private Compactor $compactor; @@ -72,12 +68,9 @@ public function test_it_compacts__composer_lock_files(string $content, string $e self::assertSame($expected, $actual); } - public function test_it_is_serializable(): void + public static function compactorProvider(): iterable { - self::assertEquals( - $this->compactor, - unserialize(serialize($this->compactor)), - ); + yield [new Json()]; } public static function filesProvider(): iterable diff --git a/tests/Compactor/PhpScoperTest.php b/tests/Compactor/PhpScoperTest.php index fc0a43bd4..ce0292199 100644 --- a/tests/Compactor/PhpScoperTest.php +++ b/tests/Compactor/PhpScoperTest.php @@ -17,19 +17,16 @@ use Error; use KevinGH\Box\PhpScoper\FakeScoper; use KevinGH\Box\PhpScoper\Scoper; -use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; -use function serialize; -use function unserialize; /** * @covers \KevinGH\Box\Compactor\PhpScoper * * @internal */ -class PhpScoperTest extends TestCase +class PhpScoperTest extends CompactorTestCase { use ProphecyTrait; @@ -86,13 +83,10 @@ public function test_it_exposes_the_scoper(): void self::assertSame($scoper, $compactor->getScoper()); } - public function test_it_is_serializable(): void + public static function compactorProvider(): iterable { - $compactor = new PhpScoper(new FakeScoper()); - - self::assertEquals( - $compactor, - unserialize(serialize($compactor)), - ); + yield [ + new PhpScoper(new FakeScoper()), + ]; } } diff --git a/tests/Compactor/PhpTest.php b/tests/Compactor/PhpTest.php index 850a7dff8..020a6eb33 100644 --- a/tests/Compactor/PhpTest.php +++ b/tests/Compactor/PhpTest.php @@ -18,14 +18,13 @@ use KevinGH\Box\Annotation\CompactedFormatter; use KevinGH\Box\Annotation\DocblockAnnotationParser; use phpDocumentor\Reflection\DocBlockFactory; -use PHPUnit\Framework\TestCase; /** * @covers \KevinGH\Box\Compactor\Php * * @internal */ -class PhpTest extends TestCase +class PhpTest extends CompactorTestCase { /** * @dataProvider filesProvider @@ -58,8 +57,11 @@ public function test_it_supports_php_files(string $file, bool $supports): void /** * @dataProvider phpContentProvider */ - public function test_it_compacts_php_files(DocblockAnnotationParser $annotationParser, string $content, string $expected): void - { + public function test_it_compacts_php_files( + DocblockAnnotationParser $annotationParser, + string $content, + string $expected + ): void { $file = 'foo.php'; $actual = (new Php($annotationParser))->compact($file, $content); @@ -69,20 +71,27 @@ public function test_it_compacts_php_files(DocblockAnnotationParser $annotationP self::assertSame($expected, $actual); } - public function test_it_is_serializable(): void + public static function compactorProvider(): iterable { - $compactor = new Php( - new DocblockAnnotationParser( - DocBlockFactory::createInstance(), - new CompactedFormatter(), - [], + yield 'empty' => [ + new Php( + new DocblockAnnotationParser( + DocBlockFactory::createInstance(), + new CompactedFormatter(), + [], + ), ), - ); + ]; - self::assertEquals( - $compactor, - unserialize(serialize($compactor)), - ); + yield 'nominal' => [ + new Php( + new DocblockAnnotationParser( + DocBlockFactory::createInstance(), + new CompactedFormatter(), + ['@author'], + ), + ), + ]; } public static function filesProvider(): iterable diff --git a/tests/Compactor/PlaceholderTest.php b/tests/Compactor/PlaceholderTest.php index 1bc058969..10b5e5975 100644 --- a/tests/Compactor/PlaceholderTest.php +++ b/tests/Compactor/PlaceholderTest.php @@ -14,14 +14,12 @@ namespace KevinGH\Box\Compactor; -use PHPUnit\Framework\TestCase; - /** * @covers \KevinGH\Box\Compactor\Placeholder * * @internal */ -class PlaceholderTest extends TestCase +class PlaceholderTest extends CompactorTestCase { /** * @dataProvider filesContentsProvider @@ -33,14 +31,15 @@ public function test_it_replaces_the_placeholders(array $placeholders, string $c self::assertSame($expected, $actual); } - public function test_it_is_serializable(): void + public static function compactorProvider(): iterable { - $compactor = new Placeholder([]); + yield 'empty' => [ + new Placeholder([]), + ]; - self::assertEquals( - $compactor, - unserialize(serialize($compactor)), - ); + yield 'nominal' => [ + new Placeholder(['@foo@' => 'bar']), + ]; } public static function filesContentsProvider(): iterable diff --git a/tests/Configuration/ConfigurationTest.php b/tests/Configuration/ConfigurationTest.php index 811486203..7d6107350 100644 --- a/tests/Configuration/ConfigurationTest.php +++ b/tests/Configuration/ConfigurationTest.php @@ -18,8 +18,8 @@ use DateTimeInterface; use Fidry\FileSystem\FS; use InvalidArgumentException; -use KevinGH\Box\Compactor\DummyCompactor; use KevinGH\Box\Compactor\InvalidCompactor; +use KevinGH\Box\Compactor\NullCompactor; use KevinGH\Box\Compactor\Php; use KevinGH\Box\Compactor\PhpScoper; use KevinGH\Box\Json\JsonValidationException; @@ -565,14 +565,14 @@ public function test_configure_the_compactors(): void 'files' => [self::DEFAULT_FILE], 'compactors' => [ Php::class, - DummyCompactor::class, + NullCompactor::class, ], ]); $compactors = $this->config->getCompactors()->toArray(); self::assertInstanceOf(Php::class, $compactors[0]); - self::assertInstanceOf(DummyCompactor::class, $compactors[1]); + self::assertInstanceOf(NullCompactor::class, $compactors[1]); self::assertCount(2, $compactors); self::assertSame([], $this->config->getRecommendations());