|
1 | 1 | # PHPUnit Tools to ease cross operating system Testing
|
2 | 2 |
|
3 |
| -## make `assertEquals*` comparisons end-of-line (aka `PHP_EOL`) character agnostic |
| 3 | +## non-invasive method |
| 4 | + |
| 5 | +A method which makes cross-os assertions obvious. |
| 6 | + |
| 7 | +### make `assertEquals*` comparisons end-of-line (aka `PHP_EOL`) character agnostic |
| 8 | + |
| 9 | +Make use of [`CrossOsAgnosticComparator`](https://github.com/staabm/phpunit-cross-os/blob/main/lib/Comparator/CrossOsAgnosticComparator.php) to make your regular `assert*`-calls aware of [`EolAgnosticString`](https://github.com/staabm/phpunit-cross-os/blob/main/lib/Comparator/EolAgnosticString.php) as a expected type. |
| 10 | + |
| 11 | +```php |
| 12 | +use SebastianBergmann\Comparator\Factory; |
| 13 | +use staabm\PHPUnitCrossOs\Comparator\CrossOsAgnosticComparator; |
| 14 | +use staabm\PHPUnitCrossOs\Comparator\EolAgnosticString; |
| 15 | + |
| 16 | +final class MyTestCase extends TestCase { |
| 17 | + |
| 18 | + /** |
| 19 | + * @var CrossOsAgnosticComparator |
| 20 | + */ |
| 21 | + private $comparator; |
| 22 | + |
| 23 | + public function setUp(): void |
| 24 | + { |
| 25 | + $this->comparator = new CrossOsAgnosticComparator(); |
| 26 | + |
| 27 | + $factory = Factory::getInstance(); |
| 28 | + $factory->register($this->comparator); |
| 29 | + } |
| 30 | + |
| 31 | + public function tearDown(): void |
| 32 | + { |
| 33 | + $factory = Factory::getInstance(); |
| 34 | + $factory->unregister($this->comparator); |
| 35 | + } |
| 36 | + |
| 37 | + public function testStringsAreEqual() { |
| 38 | + // this assertion will be considered successfull |
| 39 | + self::assertEquals(new EolAgnosticString("hello\nworld"), "hello\r\nworld"); |
| 40 | + // works also for assertEquals* variants |
| 41 | + self::assertEqualsIgnoringCase(new EolAgnosticString("hello\nworld"), "hello\r\nWORLD"); |
| 42 | + } |
| 43 | + |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### make `assertEquals*` comparisons directory-separator (aka `DIRECTORY_SEPARATOR`) character agnostic |
| 48 | + |
| 49 | +Make use of [`CrossOsAgnosticComparator`](https://github.com/staabm/phpunit-cross-os/blob/main/lib/Comparator/CrossOsAgnosticComparator.php) to make your regular `assert*`-calls aware of [`DirSeparatorAgnosticString`](https://github.com/staabm/phpunit-cross-os/blob/main/lib/Comparator/DirSeparatorAgnosticString.php) as a expected type. |
| 50 | + |
| 51 | +```php |
| 52 | +use SebastianBergmann\Comparator\Factory; |
| 53 | +use staabm\PHPUnitCrossOs\Comparator\CrossOsAgnosticComparator; |
| 54 | +use staabm\PHPUnitCrossOs\Comparator\DirSeparatorAgnosticString; |
| 55 | + |
| 56 | +final class MyTestCase extends TestCase { |
| 57 | + |
| 58 | + /** |
| 59 | + * @var CrossOsAgnosticComparator |
| 60 | + */ |
| 61 | + private $comparator; |
| 62 | + |
| 63 | + public function setUp(): void |
| 64 | + { |
| 65 | + $this->comparator = new CrossOsAgnosticComparator(); |
| 66 | + |
| 67 | + $factory = Factory::getInstance(); |
| 68 | + $factory->register($this->comparator); |
| 69 | + } |
| 70 | + |
| 71 | + public function tearDown(): void |
| 72 | + { |
| 73 | + $factory = Factory::getInstance(); |
| 74 | + $factory->unregister($this->comparator); |
| 75 | + } |
| 76 | + |
| 77 | + public function testStringsAreEqual() { |
| 78 | + // this assertion will be considered successfull |
| 79 | + self::assertEquals(new DirSeparatorAgnosticString("hello\\world"), "hello/world"); |
| 80 | + // works also for assertEquals* variants |
| 81 | + self::assertEqualsIgnoringCase(new DirSeparatorAgnosticString("hello\\world"), "hello/WORLD"); |
| 82 | + } |
| 83 | + |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | +### make `assertEquals*` comparisons cross os agnostic |
| 89 | + |
| 90 | +Make use of [`CrossOsAgnosticComparator`](https://github.com/staabm/phpunit-cross-os/blob/main/lib/Comparator/CrossOsAgnosticComparator.php) to make your regular `assert*`-calls aware of [`CrossOsAgnosticString`](https://github.com/staabm/phpunit-cross-os/blob/main/lib/Comparator/CrossOsAgnosticString.php) as a expected type. |
| 91 | + |
| 92 | +```php |
| 93 | +use SebastianBergmann\Comparator\Factory; |
| 94 | +use staabm\PHPUnitCrossOs\Comparator\CrossOsAgnosticComparator; |
| 95 | +use staabm\PHPUnitCrossOs\Comparator\CrossOsAgnosticString; |
| 96 | + |
| 97 | +final class MyTestCase extends TestCase { |
| 98 | + |
| 99 | + /** |
| 100 | + * @var CrossOsAgnosticComparator |
| 101 | + */ |
| 102 | + private $comparator; |
| 103 | + |
| 104 | + public function setUp(): void |
| 105 | + { |
| 106 | + $this->comparator = new CrossOsAgnosticComparator(); |
| 107 | + |
| 108 | + $factory = Factory::getInstance(); |
| 109 | + $factory->register($this->comparator); |
| 110 | + } |
| 111 | + |
| 112 | + public function tearDown(): void |
| 113 | + { |
| 114 | + $factory = Factory::getInstance(); |
| 115 | + $factory->unregister($this->comparator); |
| 116 | + } |
| 117 | + |
| 118 | + public function testStringsAreEqual() { |
| 119 | + // this assertion will be considered successfull |
| 120 | + self::assertEquals(new CrossOsAgnosticString("hello\\world"), "hello/world"); |
| 121 | + // works also for assertEquals* variants |
| 122 | + self::assertEqualsIgnoringCase(new CrossOsAgnosticString("hello\\world"), "hello/WORLD"); |
| 123 | + } |
| 124 | + |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | + |
| 129 | +## invasive method |
| 130 | + |
| 131 | +A method to test cross-os related stuff using php builtin assertions. |
| 132 | +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. |
| 133 | + |
| 134 | +### make `assertEquals*` comparisons end-of-line (aka `PHP_EOL`) character agnostic |
4 | 135 |
|
5 | 136 | Make use of [`EolAgnosticStringComparator`](https://github.com/staabm/phpunit-cross-os/blob/main/lib/Comparator/EolAgnosticStringComparator.php) to make your regular `assert*`-calls succeed even if the compared string differ in end-of-line characters:
|
6 | 137 |
|
@@ -39,7 +170,7 @@ final class MyTestCase extends TestCase {
|
39 | 170 | }
|
40 | 171 | ```
|
41 | 172 |
|
42 |
| -## make `assertEquals*` comparisons directory-separator (aka `DIRECTORY_SEPARATOR`) character agnostic |
| 173 | +### make `assertEquals*` comparisons directory-separator (aka `DIRECTORY_SEPARATOR`) character agnostic |
43 | 174 |
|
44 | 175 | Make use of [`DirSeparatorAgnosticStringComparator.php`](https://github.com/staabm/phpunit-cross-os/blob/main/lib/Comparator/DirSeparatorAgnosticStringComparator.php.php) to make your regular `assert*`-calls succeed even if the compared string differ in directory-separation characters:
|
45 | 176 |
|
@@ -79,7 +210,7 @@ final class MyTestCase extends TestCase {
|
79 | 210 | ```
|
80 | 211 |
|
81 | 212 |
|
82 |
| -## make `assertEquals*` comparisons cross os agnostic |
| 213 | +### make `assertEquals*` comparisons cross os agnostic |
83 | 214 |
|
84 | 215 | Make use of [`CrossOsAgnosticStringComparatorFunctionalTest.php`](https://github.com/staabm/phpunit-cross-os/blob/main/lib/Comparator/CrossOsAgnosticStringComparatorFunctionalTest.php.php) to make your regular `assert*`-calls succeed even if the compared string differ in directory-separation and/or end-of-line characters:
|
85 | 216 |
|
|
0 commit comments