Skip to content

Commit

Permalink
support assertFileEquals() normalizing
Browse files Browse the repository at this point in the history
  • Loading branch information
clxmstaab authored and TomasVotruba committed Jul 19, 2021
1 parent 60ebb34 commit 94d3b1d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 56 deletions.
19 changes: 0 additions & 19 deletions packages/FileSystemRector/ValueObject/AddedFileWithContent.php
Expand Up @@ -18,7 +18,6 @@ public function __construct(
}
}

<<<<<<< HEAD
public function getRealPath(): string
{
$realpath = realpath($this->filePath);
Expand All @@ -27,24 +26,6 @@ public function getRealPath(): string
}

return $realpath;
=======
/**
* @return string
*/
public function getRealPath()
{
<<<<<<< HEAD
return realpath($this->filePath);
>>>>>>> d5ea8a664 (apply PlatformAgnosticAssertions on MoveValueObjectsToValueObjectDirectoryRectorTest)
=======
$realpath = realpath($this->filePath);

if ($realpath === false) {
throw new ShouldNotHappenException();
}

return $realpath;
>>>>>>> c169f9af3 (eleminate false-return type from AddedFileWithContent::getRealPath())
}

public function getFilePath(): string
Expand Down
27 changes: 13 additions & 14 deletions packages/Testing/PHPUnit/AbstractRectorTestCase.php
Expand Up @@ -5,7 +5,6 @@
namespace Rector\Testing\PHPUnit;

use Iterator;
use Nette\Utils\Strings;
use PHPStan\Analyser\NodeScopeResolver;
use PHPUnit\Framework\ExpectationFailedException;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -71,6 +70,18 @@ protected function setUp(): void
$bootstrapFilesIncluder->includeBootstrapFiles();
}

protected function tearDown(): void
{
unset(
$this->applicationFileProcessor,
$this->parameterProvider,
$this->dynamicSourceLocatorProvider,
$this->removedAndAddedFilesCollector,
$this->originalTempFileInfo,
);
gc_collect_cycles();
}

/**
* @return Iterator<SmartFileInfo>
*/
Expand Down Expand Up @@ -139,7 +150,7 @@ private function doTestFileMatchesExpectedContent(

private function normalizeNewlines(string $string): string
{
return Strings::replace($string, '#\r\n|\r|\n#', "\n");
return str_replace("\r\n", "\n", $string);
}

private function processFileInfo(SmartFileInfo $fileInfo): string
Expand All @@ -160,16 +171,4 @@ private function processFileInfo(SmartFileInfo $fileInfo): string

return $file->getFileContent();
}

protected function tearDown(): void
{
unset(
$this->applicationFileProcessor,
$this->parameterProvider,
$this->dynamicSourceLocatorProvider,
$this->removedAndAddedFilesCollector,
$this->originalTempFileInfo,
);
gc_collect_cycles();
}
}
53 changes: 36 additions & 17 deletions packages/Testing/PHPUnit/PlatformAgnosticAssertions.php
@@ -1,22 +1,22 @@
<?php

declare(strict_types=1);

namespace Rector\Testing\PHPUnit;

use Nette\Utils\FileSystem;
use PHPUnit\Framework\Constraint\IsEqual;
use PHPUnit\Framework\ExpectationFailedException;

/**
* Relaxes phpunit assertions to be forgiving about platform issues, like directory-separators or newlines.
*/
trait PlatformAgnosticAssertions {
trait PlatformAgnosticAssertions
{
/**
* Asserts that two variables have the same type and value.
* Used on objects, it asserts that two variables reference
* the same object.
*
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
* @throws ExpectationFailedException
*
* @psalm-template ExpectedType
* @psalm-param ExpectedType $expected
* @psalm-assert =ExpectedType $actual
Expand All @@ -37,27 +37,46 @@ public static function assertSame($expected, $actual, string $message = ''): voi
/**
* Asserts that the contents of a string is equal
* to the contents of a file.
*
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
* @throws ExpectationFailedException
*/
public static function assertStringEqualsFile(string $expectedFile, string $actualString, string $message = ''): void
{
public static function assertStringEqualsFile(
string $expectedFile,
string $actualString,
string $message = ''
): void {
parent::assertFileExists($expectedFile, $message);

$expectedString = file_get_contents($expectedFile);
$expectedString = self::normalize($expectedString);
$constraint = new IsEqual($expectedString);
$expectedString = self::getNormalizedFileContents($expectedFile);
$isEqual = new IsEqual($expectedString);

$actualString = self::normalize($actualString);

parent::assertThat($actualString, $constraint, $message);
parent::assertThat($actualString, $isEqual, $message);
}

private static function normalize(string $string) {
/**
* Asserts that the contents of one file is equal to the contents of another
* file.
*/
public static function assertFileEquals(string $expected, string $actual, string $message = ''): void
{
static::assertFileExists($expected, $message);
static::assertFileExists($actual, $message);

$isEqual = new IsEqual(self::getNormalizedFileContents($expected));

static::assertThat(self::getNormalizedFileContents($actual), $isEqual, $message);
}

private static function normalize(string $string): array | string
{
$string = str_replace("\r\n", "\n", $string);
$string = str_replace(DIRECTORY_SEPARATOR, "/", $string);

return $string;
return str_replace(DIRECTORY_SEPARATOR, '/', $string);
}

private static function getNormalizedFileContents(string $filePath): string
{
$expectedString = FileSystem::read($filePath);
return self::normalize($expectedString);
}
}
Expand Up @@ -8,6 +8,9 @@
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

/**
* Minor differences on windows, see https://github.com/rectorphp/rector/issues/6571
*/
final class SensitiveHereNowDocRectorTest extends AbstractRectorTestCase
{
/**
Expand Down
10 changes: 5 additions & 5 deletions rules-tests/TypeDeclaration/TypeNormalizerTest.php
Expand Up @@ -23,6 +23,11 @@ protected function setUp(): void
$this->typeNormalizer = $this->getService(TypeNormalizer::class);
}

protected function tearDown(): void
{
unset($this->typeNormalizer);
}

/**
* @dataProvider provideDataNormalizeArrayOfUnionToUnionArray()
*/
Expand All @@ -48,9 +53,4 @@ public function provideDataNormalizeArrayOfUnionToUnionArray(): Iterator
$evenMoreNestedArrayType = new ArrayType(new MixedType(), $moreNestedArrayType);
yield [$evenMoreNestedArrayType, 'int[][][]|string[][][]'];
}

protected function tearDown(): void
{
unset($this->typeNormalizer);
}
}
3 changes: 2 additions & 1 deletion rules/PSR4/FileRelocationResolver.php
Expand Up @@ -180,7 +180,8 @@ private function replaceRelativeFilePathsWithBeforeAfter(
return implode(DIRECTORY_SEPARATOR, $relativeFilePathParts);
}

private function normalizeDirectorySeparator(string $path):string {
private function normalizeDirectorySeparator(string $path): string
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
}
Expand Up @@ -34,6 +34,8 @@ protected function setUp(): void
public function test(SmartFileInfo $fixtureFileInfo): void
{
$contents = $fixtureFileInfo->getContents();
// normalize for windows compat
$contents = str_replace("\r\n", "\n", $contents);
[$content, $expected] = explode("-----\n", $contents, 2);

$classSettings = Json::decode($content, Json::FORCE_ARRAY);
Expand Down

0 comments on commit 94d3b1d

Please sign in to comment.