Skip to content

Commit

Permalink
Cache - add indent and line ending to cache signature
Browse files Browse the repository at this point in the history
  • Loading branch information
dmvdbrugge authored and SpacePossum committed Mar 18, 2019
1 parent efa97df commit 739adcf
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 61 deletions.
6 changes: 6 additions & 0 deletions src/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function toJson()
$json = json_encode([
'php' => $this->getSignature()->getPhpVersion(),
'version' => $this->getSignature()->getFixerVersion(),
'indent' => $this->getSignature()->getIndent(),
'lineEnding' => $this->getSignature()->getLineEnding(),
'rules' => $this->getSignature()->getRules(),
'hashes' => $this->hashes,
]);
Expand Down Expand Up @@ -111,6 +113,8 @@ public static function fromJson($json)
$requiredKeys = [
'php',
'version',
'indent',
'lineEnding',
'rules',
'hashes',
];
Expand All @@ -127,6 +131,8 @@ public static function fromJson($json)
$signature = new Signature(
$data['php'],
$data['version'],
$data['indent'],
$data['lineEnding'],
$data['rules']
);

Expand Down
28 changes: 27 additions & 1 deletion src/Cache/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ final class Signature implements SignatureInterface
*/
private $fixerVersion;

/**
* @var string
*/
private $indent;

/**
* @var string
*/
private $lineEnding;

/**
* @var array
*/
Expand All @@ -37,12 +47,16 @@ final class Signature implements SignatureInterface
/**
* @param string $phpVersion
* @param string $fixerVersion
* @param string $indent
* @param string $lineEnding
* @param array $rules
*/
public function __construct($phpVersion, $fixerVersion, array $rules)
public function __construct($phpVersion, $fixerVersion, $indent, $lineEnding, array $rules)
{
$this->phpVersion = $phpVersion;
$this->fixerVersion = $fixerVersion;
$this->indent = $indent;
$this->lineEnding = $lineEnding;
$this->rules = self::utf8Encode($rules);
}

Expand All @@ -56,6 +70,16 @@ public function getFixerVersion()
return $this->fixerVersion;
}

public function getIndent()
{
return $this->indent;
}

public function getLineEnding()
{
return $this->lineEnding;
}

public function getRules()
{
return $this->rules;
Expand All @@ -65,6 +89,8 @@ public function equals(SignatureInterface $signature)
{
return $this->phpVersion === $signature->getPhpVersion()
&& $this->fixerVersion === $signature->getFixerVersion()
&& $this->indent === $signature->getIndent()
&& $this->lineEnding === $signature->getLineEnding()
&& $this->rules === $signature->getRules();
}

Expand Down
10 changes: 10 additions & 0 deletions src/Cache/SignatureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public function getPhpVersion();
*/
public function getFixerVersion();

/**
* @return string
*/
public function getIndent();

/**
* @return string
*/
public function getLineEnding();

/**
* @return array
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Console/ConfigurationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ public function getCacheManager()
new Signature(
PHP_VERSION,
$this->toolInfo->getVersion(),
$this->getConfig()->getIndent(),
$this->getConfig()->getLineEnding(),
$this->getRules()
),
$this->isDryRun(),
Expand Down
8 changes: 8 additions & 0 deletions tests/Cache/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpCsFixer\Cache\Cache;
use PhpCsFixer\Cache\Signature;
use PhpCsFixer\Cache\SignatureInterface;
use PhpCsFixer\Config;
use PhpCsFixer\Tests\TestCase;
use PhpCsFixer\ToolInfo;

Expand Down Expand Up @@ -173,11 +174,14 @@ public function testCanConvertToAndFromJson(SignatureInterface $signature)
public function provideCanConvertToAndFromJsonCases()
{
$toolInfo = new ToolInfo();
$config = new Config();

return [
[new Signature(
PHP_VERSION,
'2.0',
' ',
"\r\n",
[
'foo' => true,
'bar' => true,
Expand All @@ -186,6 +190,8 @@ public function provideCanConvertToAndFromJsonCases()
[new Signature(
PHP_VERSION,
$toolInfo->getVersion(),
$config->getIndent(),
$config->getLineEnding(),
[
// value encoded in ANSI, not UTF
'header_comment' => ['header' => 'Dariusz '.base64_decode('UnVtafFza2k=', true)],
Expand All @@ -201,6 +207,8 @@ public function testToJsonThrowsExceptionOnInvalid()
$signature = $this->prophesize(\PhpCsFixer\Cache\SignatureInterface::class);
$signature->getPhpVersion()->willReturn('7.1.0');
$signature->getFixerVersion()->willReturn('2.2.0');
$signature->getIndent()->willReturn(' ');
$signature->getLineEnding()->willReturn(PHP_EOL);
$signature->getRules()->willReturn([
$invalidUtf8Sequence => true,
]);
Expand Down
72 changes: 24 additions & 48 deletions tests/Cache/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpCsFixer\Cache\Cache;
use PhpCsFixer\Cache\FileHandler;
use PhpCsFixer\Cache\Signature;
use PhpCsFixer\Cache\SignatureInterface;
use PhpCsFixer\Tests\TestCase;

/**
Expand Down Expand Up @@ -79,14 +80,7 @@ public function testReadReturnsCache()
{
$file = $this->getFile();

$signature = new Signature(
PHP_VERSION,
'2.0',
[
'foo',
'bar',
]
);
$signature = $this->createSignature();

$cache = new Cache($signature);

Expand All @@ -110,14 +104,7 @@ public function testWriteThrowsIOExceptionIfFileCanNotBeWritten()
preg_quote($file, '#')
));

$cache = new Cache(new Signature(
PHP_VERSION,
'2.0',
[
'foo',
'bar',
]
));
$cache = new Cache($this->createSignature());

$handler = new FileHandler($file);

Expand All @@ -128,14 +115,7 @@ public function testWriteWritesCache()
{
$file = $this->getFile();

$cache = new Cache(new Signature(
PHP_VERSION,
'2.0',
[
'foo',
'bar',
]
));
$cache = new Cache($this->createSignature());

$handler = new FileHandler($file);

Expand All @@ -160,14 +140,7 @@ public function testWriteCacheToDirectory()
preg_quote('Cannot write cache file "'.realpath($dir).'" as the location exists as directory.', '#')
));

$handler->write(new Cache(new Signature(
PHP_VERSION,
'2.0',
[
'foo',
'bar',
]
)));
$handler->write(new Cache($this->createSignature()));
}

public function testWriteCacheToNonWriteableFile()
Expand All @@ -187,14 +160,7 @@ public function testWriteCacheToNonWriteableFile()
preg_quote('Cannot write to file "'.realpath($file).'" as it is not writable.', '#')
));

$handler->write(new Cache(new Signature(
PHP_VERSION,
'2.0',
[
'foo',
'bar',
]
)));
$handler->write(new Cache($this->createSignature()));
}

public function testWriteCacheFilePermissions()
Expand All @@ -205,14 +171,7 @@ public function testWriteCacheFilePermissions()
$this->assertFileNotExists($file);

$handler = new FileHandler($file);
$handler->write(new Cache(new Signature(
PHP_VERSION,
'2.0',
[
'foo',
'bar',
]
)));
$handler->write(new Cache($this->createSignature()));

$this->assertFileExists($file);
$this->assertTrue(@is_file($file), sprintf('Failed cache "%s" `is_file`.', $file));
Expand All @@ -229,4 +188,21 @@ private function getFile()
{
return __DIR__.'/.php_cs.cache';
}

/**
* @return SignatureInterface
*/
private function createSignature()
{
return new Signature(
PHP_VERSION,
'2.0',
' ',
PHP_EOL,
[
'foo',
'bar',
]
);
}
}
62 changes: 50 additions & 12 deletions tests/Cache/SignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function testConstructorSetsValues()
{
$php = PHP_VERSION;
$version = '2.0';
$indent = ' ';
$lineEnding = PHP_EOL;
$rules = [
'foo',
'bar',
Expand All @@ -50,42 +52,74 @@ public function testConstructorSetsValues()
$signature = new Signature(
$php,
$version,
$indent,
$lineEnding,
$rules
);

$this->assertSame($php, $signature->getPhpVersion());
$this->assertSame($version, $signature->getFixerVersion());
$this->assertSame($indent, $signature->getIndent());
$this->assertSame($lineEnding, $signature->getLineEnding());
$this->assertSame($rules, $signature->getRules());
}

public function testEqualsReturnsFalseIfValuesAreNotIdentical()
/**
* @dataProvider provideEqualsReturnsFalseIfValuesAreNotIdenticalCases
*
* @param Signature $signature
* @param Signature $anotherSignature
*/
public function testEqualsReturnsFalseIfValuesAreNotIdentical($signature, $anotherSignature)
{
$this->assertFalse($signature->equals($anotherSignature));
}

public function provideEqualsReturnsFalseIfValuesAreNotIdenticalCases()
{
$php = PHP_VERSION;
$version = '2.0';
$indent = ' ';
$lineEnding = "\n";
$rules = [
'foo',
'bar',
];

$signature = new Signature(
$php,
$version,
$rules
);
$base = new Signature($php, $version, $indent, $lineEnding, $rules);

$anotherSignature = new Signature(
$php,
$version.'.1',
$rules
);
yield 'php' => [
$base,
new Signature('50400', $version, $indent, $lineEnding, $rules),
];

$this->assertFalse($signature->equals($anotherSignature));
yield 'version' => [
$base,
new Signature($php, '2.12', $indent, $lineEnding, $rules),
];

yield 'indent' => [
$base,
new Signature($php, $version, "\t", $lineEnding, $rules),
];

yield 'lineEnding' => [
$base,
new Signature($php, $version, $indent, "\r\n", $rules),
];

yield 'rules' => [
$base,
new Signature($php, $version, $indent, $lineEnding, ['foo']),
];
}

public function testEqualsReturnsTrueIfValuesAreIdentical()
{
$php = PHP_VERSION;
$version = '2.0';
$indent = ' ';
$lineEnding = PHP_EOL;
$rules = [
'foo',
'bar',
Expand All @@ -94,12 +128,16 @@ public function testEqualsReturnsTrueIfValuesAreIdentical()
$signature = new Signature(
$php,
$version,
$indent,
$lineEnding,
$rules
);

$anotherSignature = new Signature(
$php,
$version,
$indent,
$lineEnding,
$rules
);

Expand Down

0 comments on commit 739adcf

Please sign in to comment.