Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into feat/parallel-proce…
Browse files Browse the repository at this point in the history
…ssing
  • Loading branch information
theofidry committed Nov 23, 2023
2 parents 270c933 + cca9229 commit cf31800
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
37 changes: 4 additions & 33 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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};
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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(
Expand Down
46 changes: 46 additions & 0 deletions src/PhpScoper/ConfigurationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\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,
);
}
}
}
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()),
];
}
}
39 changes: 24 additions & 15 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 @@ -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);
Expand All @@ -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
Expand Down
Loading

0 comments on commit cf31800

Please sign in to comment.