Skip to content

Commit

Permalink
added PlatformAgnosticAssertions::assertStringEqualsFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Aug 16, 2021
1 parent 9a9d9aa commit de33551
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
2 changes: 0 additions & 2 deletions packages-tests/FileFormatter/ValueObject/NewLineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
namespace Rector\Tests\FileFormatter\ValueObject;

use Iterator;
use PHPUnit\Framework\TestCase;
use Rector\FileFormatter\Exception\InvalidNewLineStringException;
use Rector\FileFormatter\ValueObject\NewLine;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\PHPUnit\PlatformAgnosticAssertions;
use Symplify\SmartFileSystem\SmartFileInfo;

final class NewLineTest extends AbstractTestCase
Expand Down
12 changes: 11 additions & 1 deletion packages/FileSystemRector/ValueObject/AddedFileWithContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ public function getRealPath(): string
return $realpath;
=======
/**
* @return false|string
* @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
39 changes: 35 additions & 4 deletions packages/Testing/PHPUnit/PlatformAgnosticAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Rector\Testing\PHPUnit;

use PHPUnit\Framework\Constraint\IsEqual;
use PHPUnit\Framework\ExpectationFailedException;

/**
* Relaxes phpunit assertions to be forgiving about platform issues, like directory-separators or newlines.
*/
Expand All @@ -11,22 +14,50 @@ trait PlatformAgnosticAssertions {
* 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
*/
public static function assertSame($expected, $actual, string $message = ''): void
{
if (is_string($expected)) {
$expected = str_replace("\r\n", "\n", $expected);
$expected = str_replace(DIRECTORY_SEPARATOR, "/", $expected);
$expected = self::normalize($expected);
}

if (is_string($actual)) {
$actual = str_replace("\r\n", "\n", $actual);
$actual = str_replace(DIRECTORY_SEPARATOR, "/", $actual);
$actual = self::normalize($actual);
}

parent::assertSame($expected, $actual, $message);
}

/**
* 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
{
parent::assertFileExists($expectedFile, $message);

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

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

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

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

return $string;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Iterator;
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Testing\PHPUnit\PlatformAgnosticAssertions;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;

Expand Down
3 changes: 0 additions & 3 deletions rules-tests/PSR4/FileRelocationResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
use Iterator;
use Rector\PSR4\FileRelocationResolver;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Testing\PHPUnit\PlatformAgnosticAssertions;
use Rector\Tests\PSR4\Source\SomeFile;
use Symplify\SmartFileSystem\SmartFileInfo;

final class FileRelocationResolverTest extends AbstractTestCase
{
use PlatformAgnosticAssertions;

private FileRelocationResolver $fileRelocationResolver;

protected function setUp(): void
Expand Down

0 comments on commit de33551

Please sign in to comment.