Skip to content

Commit

Permalink
Normalize line endings in test assertions for Windows platform
Browse files Browse the repository at this point in the history
Refactor test assertions to use a helper method for normalizing line endings. This ensures consistency across different operating systems and prevents test failures due to line ending discrepancies.
  • Loading branch information
koriym committed Nov 27, 2024
1 parent 0d0db43 commit 1bbb135
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions tests/DependencyCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testInstanceCompileString(): void
return 'bear';
EOT;
$this->assertSame($expected, (string) $code);
$this->assertSame($this->normalizeLineEndings($expected), (string) $code);
}

public function testInstanceCompileInt(): void
Expand All @@ -45,7 +45,7 @@ public function testInstanceCompileInt(): void
return 1;
EOT;
$this->assertSame($expected, (string) $code);
$this->assertSame($this->normalizeLineEndings($expected), (string) $code);
}

public function testInstanceCompileArray(): void
Expand All @@ -58,8 +58,8 @@ public function testInstanceCompileArray(): void
return array(1, 2, 3);
EOT;
$this->assertContains((string) $code, [
str_replace('array(1, 2, 3)', '[1, 2, 3]', $expected),
$expected,
str_replace('array(1, 2, 3)', '[1, 2, 3]', $this->normalizeLineEndings($expected)),
$this->normalizeLineEndings($expected),
]);
}

Expand Down Expand Up @@ -88,9 +88,9 @@ public function testDependencyCompile(): void
str_replace(
'array(\'Ray\\Compiler\\FakeCar\', \'setHandle\', \'handle\')',
'[\'Ray\\Compiler\\FakeCar\', \'setHandle\', \'handle\']',
str_replace('\\\\', '\\', $expected)
str_replace('\\\\', '\\', $this->normalizeLineEndings($expected))
),
$expected,
$this->normalizeLineEndings($expected),
]);
}

Expand All @@ -108,7 +108,7 @@ public function testDependencyProviderCompile(): void
$isSingleton = false;
return $instance->get();
EOT;
$this->assertSame($expected, (string) $code);
$this->assertSame($this->normalizeLineEndings($expected), (string) $code);
}

public function testDependencyInstanceCompile(): void
Expand All @@ -121,7 +121,7 @@ public function testDependencyInstanceCompile(): void
return 'momo';
EOT;
$this->assertSame($expected, (string) $code);
$this->assertSame($this->normalizeLineEndings($expected), (string) $code);
}

public function testDependencyObjectInstanceCompile(): void
Expand All @@ -135,8 +135,8 @@ public function testDependencyObjectInstanceCompile(): void
return unserialize('O:23:"Ray\\Compiler\\FakeEngine":0:{}');
EOT;
$this->assertContains((string) $code, [
str_replace('\\\\', '\\', $expected),
$expected,
str_replace('\\\\', '\\', $this->normalizeLineEndings($expected)),
$this->normalizeLineEndings($expected),
]);
}

Expand Down Expand Up @@ -165,6 +165,12 @@ public function testContextualProviderCompile(): void
$isSingleton = false;
return $instance->get();
EOT;
$this->assertSame($expected, (string) $code);
$this->assertSame($this->normalizeLineEndings($expected), (string) $code);
}

private function normalizeLineEndings($content)

Check failure on line 171 in tests/DependencyCompilerTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Method \Ray\Compiler\DependencyCompilerTest::normalizeLineEndings() does not have parameter type hint nor @param annotation for its parameter $content.

Check failure on line 171 in tests/DependencyCompilerTest.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Method \Ray\Compiler\DependencyCompilerTest::normalizeLineEndings() does not have return type hint nor @return annotation for its return value.

Check failure on line 171 in tests/DependencyCompilerTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Method Ray\Compiler\DependencyCompilerTest::normalizeLineEndings() has no return type specified.

Check failure on line 171 in tests/DependencyCompilerTest.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Method Ray\Compiler\DependencyCompilerTest::normalizeLineEndings() has parameter $content with no type specified.
{
// Convert Windows (CRLF: \r\n) and old Mac (CR: \r) to Unix (LF: \n)
return str_replace(["\r\n", "\r"], "\n", $content);
}
}

0 comments on commit 1bbb135

Please sign in to comment.