A method which makes cross-os assertions obvious.
Make use of CrossOsAgnosticComparator
to make your regular assert*
-calls aware of EolAgnosticString
as a expected type.
use SebastianBergmann\Comparator\Factory;
use staabm\PHPUnitCrossOs\Comparator\CrossOsAgnosticComparator;
use staabm\PHPUnitCrossOs\Comparator\EolAgnosticString;
final class MyTestCase extends TestCase {
/**
* @var CrossOsAgnosticComparator
*/
private $comparator;
public function setUp(): void
{
$this->comparator = new CrossOsAgnosticComparator();
$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(new EolAgnosticString("hello\nworld"), "hello\r\nworld");
// works also for assertEquals* variants
self::assertEqualsIgnoringCase(new EolAgnosticString("hello\nworld"), "hello\r\nWORLD");
}
}
Make use of CrossOsAgnosticComparator
to make your regular assert*
-calls aware of DirSeparatorAgnosticString
as a expected type.
use SebastianBergmann\Comparator\Factory;
use staabm\PHPUnitCrossOs\Comparator\CrossOsAgnosticComparator;
use staabm\PHPUnitCrossOs\Comparator\DirSeparatorAgnosticString;
final class MyTestCase extends TestCase {
/**
* @var CrossOsAgnosticComparator
*/
private $comparator;
public function setUp(): void
{
$this->comparator = new CrossOsAgnosticComparator();
$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(new DirSeparatorAgnosticString("hello\\world"), "hello/world");
// works also for assertEquals* variants
self::assertEqualsIgnoringCase(new DirSeparatorAgnosticString("hello\\world"), "hello/WORLD");
}
}
Make use of CrossOsAgnosticComparator
to make your regular assert*
-calls aware of CrossOsAgnosticString
as a expected type.
use SebastianBergmann\Comparator\Factory;
use staabm\PHPUnitCrossOs\Comparator\CrossOsAgnosticComparator;
use staabm\PHPUnitCrossOs\Comparator\CrossOsAgnosticString;
final class MyTestCase extends TestCase {
/**
* @var CrossOsAgnosticComparator
*/
private $comparator;
public function setUp(): void
{
$this->comparator = new CrossOsAgnosticComparator();
$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(new CrossOsAgnosticString("hello\\world"), "hello/world");
// works also for assertEquals* variants
self::assertEqualsIgnoringCase(new CrossOsAgnosticString("hello\\world"), "hello/WORLD");
}
}
A method to test cross-os related stuff using php builtin assertions.
You may not like it, because it makes your test feels kind of magical as your assertEquals()
calls will behave differently based on the given parameters.
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");
}
}
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");
}
}
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");
}
}