PHPUnit Tools to ease cross operating system Testing
assertEquals*
comparisons end-of-line (aka PHP_EOL
) character agnostic
make Make use of EolAgnosticStringComparator
to make your regular assert*
-calls succeed even if the compared string differ in end-of-line characters:
use SebastianBergmann\Comparator\Factory;
use staabm\PHPUnitCrossOs\Comparator\EolAgnosticStringComparator;
final class MyTestCase extends TestCase {
/**
* @var EolAgnosticStringComparator
*/
private $comparator;
public function setUp(): void
{
$this->comparator = new EolAgnosticStringComparator();
$factory = Factory::getInstance();
$factory->register($this->comparator);
}
public function tearDown(): void
{
$factory = Factory::getInstance();
$factory->unregister($this->comparator);
}
public function testStringsAreEqual() {
// this assertion will be considered successfull
self::assertEquals("hello\nworld", "hello\r\nworld");
// works also for assertEquals* variants
self::assertEqualsIgnoringCase("hello\nworld", "hello\r\nWORLD");
}
}
assertEquals*
comparisons directory-separator (aka DIRECTORY_SEPARATOR
) character agnostic
make Make use of DirSeparatorAgnosticStringComparator.php
to make your regular assert*
-calls succeed even if the compared string differ in directory-separation characters:
use SebastianBergmann\Comparator\Factory;
use staabm\PHPUnitCrossOs\Comparator\DirSeparatorAgnosticStringComparator;
final class MyTestCase extends TestCase {
/**
* @var DirSeparatorAgnosticStringComparator
*/
private $comparator;
public function setUp(): void
{
$this->comparator = new DirSeparatorAgnosticStringComparator();
$factory = Factory::getInstance();
$factory->register($this->comparator);
}
public function tearDown(): void
{
$factory = Factory::getInstance();
$factory->unregister($this->comparator);
}
public function testStringsAreEqual() {
// this assertion will be considered successfull
self::assertEquals("hello\\world", "hello/world");
// works also for assertEquals* variants
self::assertEqualsIgnoringCase("hello\\world", "hello/WORLD");
}
}
assertEquals*
comparisons cross os agnostic
make Make use of CrossOsAgnosticStringComparatorFunctionalTest.php
to make your regular assert*
-calls succeed even if the compared string differ in directory-separation and/or end-of-line characters:
CrossOsAgnosticStringComparatorFunctionalTest
essentially provides all features of DirSeparatorAgnosticStringComparator
and EolAgnosticStringComparator
combined in a single class.
use SebastianBergmann\Comparator\Factory;
use staabm\PHPUnitCrossOs\Comparator\CrossOsAgnosticStringComparatorFunctionalTest;
final class MyTestCase extends TestCase {
/**
* @var CrossOsAgnosticStringComparatorFunctionalTest
*/
private $comparator;
public function setUp(): void
{
$this->comparator = new CrossOsAgnosticStringComparatorFunctionalTest();
$factory = Factory::getInstance();
$factory->register($this->comparator);
}
public function tearDown(): void
{
$factory = Factory::getInstance();
$factory->unregister($this->comparator);
}
public function testStringsAreEqual() {
// this assertion will be considered successfull
self::assertEquals("hello\\world\n", "hello/world\r\n");
// works also for assertEquals* variants
self::assertEqualsIgnoringCase("hello\\world\r\n", "hello/WORLD\n");
}
}