staabm / phpunit-cross-os
Fund package maintenance!
staabm
Requires
- php: ^7.4 || ^8.0
- sebastian/comparator: ^4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: 2.19.3
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.2
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- dev-main
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2
- 0.1
- dev-dependabot/composer/friendsofphp/php-cs-fixer-3.14.4
- dev-dependabot/composer/phpunit/phpunit-tw-9.6
- dev-dependabot/composer/phpstan/phpstan-phpunit-tw-1.3
- dev-dependabot/github_actions/ramsey/composer-install-2
- dev-dependabot/composer/phpstan/phpstan-tw-1.9
- dev-dependabot/composer/phpstan/extension-installer-tw-1.2
This package is auto-updated.
Last update: 2023-03-13 23:06:42 UTC
README
non-invasive method
A method which makes cross-os assertions obvious.
make assertEquals*
comparisons end-of-line (aka PHP_EOL
) character agnostic
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 assertEquals*
comparisons directory-separator (aka DIRECTORY_SEPARATOR
) character agnostic
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 assertEquals*
comparisons cross os agnostic
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"); } }
invasive method
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 assertEquals*
comparisons end-of-line (aka PHP_EOL
) character agnostic
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 assertEquals*
comparisons directory-separator (aka DIRECTORY_SEPARATOR
) character agnostic
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 assertEquals*
comparisons cross os agnostic
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"); } }