From 57c3b0a8d25ff1d3b4666e7c49862b19cdfb3c86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Thu, 23 Nov 2023 09:05:35 +0100 Subject: [PATCH] tests: Add more tests for the compactors serializability (#1196) --- .../Compactor/NullCompactor.php | 7 +++- tests/Compactor/CompactorTestCase.php | 37 +++++++++++++++++++ tests/Compactor/CompactorsTest.php | 30 +++++++++++++++ tests/Compactor/JsonTest.php | 13 ++----- tests/Compactor/PhpScoperTest.php | 16 +++----- tests/Compactor/PhpTest.php | 32 +++++++++------- tests/Compactor/PlaceholderTest.php | 17 ++++----- tests/Configuration/ConfigurationTest.php | 6 +-- 8 files changed, 111 insertions(+), 47 deletions(-) rename fixtures/Compactor/DummyCompactor.php => src/Compactor/NullCompactor.php (70%) create mode 100644 tests/Compactor/CompactorTestCase.php diff --git a/fixtures/Compactor/DummyCompactor.php b/src/Compactor/NullCompactor.php similarity index 70% rename from fixtures/Compactor/DummyCompactor.php rename to src/Compactor/NullCompactor.php index 5578aba03..8cd9d1f0b 100644 --- a/fixtures/Compactor/DummyCompactor.php +++ b/src/Compactor/NullCompactor.php @@ -14,7 +14,12 @@ namespace KevinGH\Box\Compactor; -class DummyCompactor implements Compactor +/** + * Base compactor class providing a slightly simpler API to compact the content only if the file is supported. + * + * @private + */ +final class NullCompactor implements Compactor { public function compact(string $file, string $contents): string { 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 5b2ed9871..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 @@ -72,20 +71,27 @@ public function test_it_compacts_php_files( 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());