123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Cache;
- use PhpCsFixer\Cache\Cache;
- use PhpCsFixer\Cache\CacheInterface;
- use PhpCsFixer\Cache\Signature;
- use PhpCsFixer\Cache\SignatureInterface;
- use PhpCsFixer\Config;
- use PhpCsFixer\Tests\TestCase;
- use PhpCsFixer\ToolInfo;
- /**
- * @author Andreas Möller <am@localheinz.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Cache\Cache
- */
- final class CacheTest extends TestCase
- {
- public function testIsFinal(): void
- {
- $reflection = new \ReflectionClass(Cache::class);
- self::assertTrue($reflection->isFinal());
- }
- public function testImplementsCacheInterface(): void
- {
- $reflection = new \ReflectionClass(Cache::class);
- self::assertTrue($reflection->implementsInterface(CacheInterface::class));
- }
- public function testConstructorSetsValues(): void
- {
- $signature = $this->createSignatureDouble();
- $cache = new Cache($signature);
- self::assertSame($signature, $cache->getSignature());
- }
- public function testDefaults(): void
- {
- $signature = $this->createSignatureDouble();
- $cache = new Cache($signature);
- $file = 'test.php';
- self::assertFalse($cache->has($file));
- self::assertNull($cache->get($file));
- }
- public function testCanSetAndGetValue(): void
- {
- $signature = $this->createSignatureDouble();
- $cache = new Cache($signature);
- $file = 'test.php';
- $hash = md5('hello');
- $cache->set($file, $hash);
- self::assertTrue($cache->has($file));
- self::assertSame($hash, $cache->get($file));
- }
- public function testCanClearValue(): void
- {
- $signature = $this->createSignatureDouble();
- $cache = new Cache($signature);
- $file = 'test.php';
- $hash = md5('hello');
- $cache->set($file, $hash);
- $cache->clear($file);
- self::assertNull($cache->get($file));
- }
- public function testFromJsonThrowsInvalidArgumentExceptionIfJsonIsInvalid(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $json = '{"foo';
- Cache::fromJson($json);
- }
- /**
- * @param array<string, mixed> $data
- *
- * @dataProvider provideFromJsonThrowsInvalidArgumentExceptionIfJsonIsMissingKeyCases
- */
- public function testFromJsonThrowsInvalidArgumentExceptionIfJsonIsMissingKey(array $data): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $json = json_encode($data, JSON_THROW_ON_ERROR);
- Cache::fromJson($json);
- }
- public static function provideFromJsonThrowsInvalidArgumentExceptionIfJsonIsMissingKeyCases(): iterable
- {
- $data = [
- 'php' => '7.1.2',
- 'version' => '2.0',
- 'rules' => [
- 'foo' => true,
- 'bar' => false,
- ],
- 'hashes' => [],
- ];
- return array_map(static function (string $missingKey) use ($data): array {
- unset($data[$missingKey]);
- return [
- $data,
- ];
- }, array_keys($data));
- }
- /**
- * @dataProvider provideCanConvertToAndFromJsonCases
- */
- public function testCanConvertToAndFromJson(SignatureInterface $signature): void
- {
- $cache = new Cache($signature);
- $file = 'test.php';
- $hash = md5('hello');
- $cache->set($file, $hash);
- $cached = Cache::fromJson($cache->toJson());
- self::assertTrue($cached->getSignature()->equals($signature));
- self::assertTrue($cached->has($file));
- self::assertSame($hash, $cached->get($file));
- }
- /**
- * @return iterable<array{Signature}>
- */
- public static function provideCanConvertToAndFromJsonCases(): iterable
- {
- $toolInfo = new ToolInfo();
- $config = new Config();
- yield [new Signature(
- PHP_VERSION,
- '2.0',
- ' ',
- "\r\n",
- [
- 'foo' => true,
- 'bar' => true,
- ]
- )];
- yield [new Signature(
- PHP_VERSION,
- $toolInfo->getVersion(),
- $config->getIndent(),
- $config->getLineEnding(),
- [
- // value encoded in ANSI, not UTF
- 'header_comment' => ['header' => 'Dariusz '.base64_decode('UnVtafFza2k=', true)],
- ]
- )];
- }
- public function testToJsonThrowsExceptionOnInvalid(): void
- {
- $signature = $this->createSignatureDouble();
- $cache = new Cache($signature);
- $this->expectException(
- \UnexpectedValueException::class
- );
- $this->expectExceptionMessage(
- 'Cannot encode cache signature to JSON, error: "Malformed UTF-8 characters, possibly incorrectly encoded". If you have non-UTF8 chars in your signature, like in license for `header_comment`, consider enabling `ext-mbstring` or install `symfony/polyfill-mbstring`.'
- );
- $cache->toJson();
- }
- private function createSignatureDouble(): SignatureInterface
- {
- return new class implements SignatureInterface {
- public function getPhpVersion(): string
- {
- return '7.1.0';
- }
- public function getFixerVersion(): string
- {
- return '2.2.0';
- }
- public function getIndent(): string
- {
- return ' ';
- }
- public function getLineEnding(): string
- {
- return PHP_EOL;
- }
- public function getRules(): array
- {
- return [
- "\xB1\x31" => true, // invalid UTF8 sequence
- ];
- }
- public function equals(SignatureInterface $signature): bool
- {
- throw new \LogicException('Not implemented.');
- }
- };
- }
- }
|