Skip to content

Commit

Permalink
tests: Add more tests for the compactors serializability
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Nov 23, 2023
1 parent 096b332 commit f46a33b
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 69 deletions.
23 changes: 0 additions & 23 deletions fixtures/Compactor/DummyCompactor.php

This file was deleted.

10 changes: 10 additions & 0 deletions fixtures/Compactor/FakeCompactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ public function compact(string $file, string $contents): string
{
throw UnsupportedMethodCall::forMethod(__CLASS__, __METHOD__);
}

public static function unserialize(string $serializedCompactor): static
{
throw UnsupportedMethodCall::forMethod(__CLASS__, __METHOD__);
}

public function serialize(): string
{
throw UnsupportedMethodCall::forMethod(__CLASS__, __METHOD__);
}
}
41 changes: 41 additions & 0 deletions src/Compactor/NullCompactor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* 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 function serialize;
use function unserialize;

/**
* 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
{
return $contents;
}

public function serialize(): string
{
return serialize($this);
}

public static function unserialize(string $serializedCompactor): static
{
return unserialize($serializedCompactor, ['allowed_classes' => [self::class]]);
}
}
37 changes: 37 additions & 0 deletions tests/Compactor/CompactorTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* 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;
}
30 changes: 30 additions & 0 deletions tests/Compactor/CompactorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down
13 changes: 3 additions & 10 deletions tests/Compactor/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
16 changes: 5 additions & 11 deletions tests/Compactor/PhpScoperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()),
];
}
}
32 changes: 19 additions & 13 deletions tests/Compactor/PhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions tests/Compactor/PlaceholderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/Configuration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit f46a33b

Please sign in to comment.