|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * PhpUnit compatibility layer. |
| 5 | + * |
| 6 | + * This set of traits allows to run the same test code using |
| 7 | + * different phpunit versions (phpunit-6 and phpunit-7 at the |
| 8 | + * moment). |
| 9 | + * |
| 10 | + * Now it implements the workaround for phpunit-7 requirement to |
| 11 | + * use `void` return type declarations for the following methods |
| 12 | + * (see [1]). |
| 13 | + * |
| 14 | + * - setUpBeforeClass() |
| 15 | + * - tearDownAfterClass() |
| 16 | + * - setUp() |
| 17 | + * - tearDown() |
| 18 | + * |
| 19 | + * The problem is that php-7.0 does not support `void` return type |
| 20 | + * declaration, so a trick is necessary to support both php-7.0 |
| 21 | + * and phpunit-7+ for, say, php-7.3+. |
| 22 | + * |
| 23 | + * The `TestCaseCompat` trait implements the trick. Use it in your |
| 24 | + * test case class and specify do*() methods instead of ones |
| 25 | + * listed above (see the example below). |
| 26 | + * |
| 27 | + * For now it is the only problem, which is solved by the |
| 28 | + * `TestCaseCompat` trait, but it may be expanded in a future. |
| 29 | + * |
| 30 | + * Example: |
| 31 | + * |
| 32 | + * | use PHPUnit\Framework\TestCase; |
| 33 | + * | |
| 34 | + * | final class FooTest extends TestCase |
| 35 | + * | { |
| 36 | + * | use TestCaseCompat; |
| 37 | + * | |
| 38 | + * | public static function doSetUpBeforeClass() |
| 39 | + * | { |
| 40 | + * | <...> |
| 41 | + * | } |
| 42 | + * | |
| 43 | + * | public static function doTearDownAfterClass() { |
| 44 | + * | <...> |
| 45 | + * | } |
| 46 | + * | |
| 47 | + * | protected function doSetUp() { |
| 48 | + * | <...> |
| 49 | + * | } |
| 50 | + * | |
| 51 | + * | protected function doTearDown() { |
| 52 | + * | <...> |
| 53 | + * | } |
| 54 | + * | } |
| 55 | + * |
| 56 | + * Use `TestCaseCompat` trait and consider others as |
| 57 | + * implementation details. |
| 58 | + * |
| 59 | + * [1]: https://phpunit.de/announcements/phpunit-7.html |
| 60 | + */ |
| 61 | + |
| 62 | +use PHPUnit\Framework\TestCase; |
| 63 | + |
| 64 | +$testCaseRef = new ReflectionClass(TestCase::class); |
| 65 | + |
| 66 | +/* |
| 67 | + * SetUpTearDownTraitDefaultDoMethods (internal). |
| 68 | + * |
| 69 | + * The common code to use in SetUpTearDownTrait traits on both |
| 70 | + * phpunit-6 and phpunit-7+. |
| 71 | + */ |
| 72 | +trait SetUpTearDownTraitDefaultDoMethods |
| 73 | +{ |
| 74 | + private static function doSetUpBeforeClass() |
| 75 | + { |
| 76 | + parent::setUpBeforeClass(); |
| 77 | + } |
| 78 | + |
| 79 | + private static function doTearDownAfterClass() |
| 80 | + { |
| 81 | + parent::tearDownAfterClass(); |
| 82 | + } |
| 83 | + |
| 84 | + private function doSetUp() |
| 85 | + { |
| 86 | + parent::setUp(); |
| 87 | + } |
| 88 | + |
| 89 | + private function doTearDown() |
| 90 | + { |
| 91 | + parent::tearDown(); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/* |
| 96 | + * SetUpTearDownTrait (private). |
| 97 | + * |
| 98 | + * This trait allow to overcome the problem that php-7.0 does not |
| 99 | + * support `void` return type declaration, while phpunit-7 (which |
| 100 | + * supports php-7.1+) requires them for certain set of methods. |
| 101 | + * |
| 102 | + * The idea is borrowed from Symfony's PHPUnit Bridge, see [1]. |
| 103 | + * |
| 104 | + * [1]: https://symfony.com/doc/current/components/phpunit_bridge.html#removing-the-void-return-type |
| 105 | + */ |
| 106 | +if ($testCaseRef->getMethod('setUp')->hasReturnType()) { |
| 107 | + /* phpunit-7 and newer */ |
| 108 | + trait SetUpTearDownTrait |
| 109 | + { |
| 110 | + use SetUpTearDownTraitDefaultDoMethods; |
| 111 | + |
| 112 | + public static function setUpBeforeClass(): void |
| 113 | + { |
| 114 | + self::doSetUpBeforeClass(); |
| 115 | + } |
| 116 | + |
| 117 | + public static function tearDownAfterClass(): void |
| 118 | + { |
| 119 | + self::doTearDownAfterClass(); |
| 120 | + } |
| 121 | + |
| 122 | + protected function setUp(): void |
| 123 | + { |
| 124 | + self::doSetUp(); |
| 125 | + } |
| 126 | + |
| 127 | + protected function tearDown(): void |
| 128 | + { |
| 129 | + self::doTearDown(); |
| 130 | + } |
| 131 | + } |
| 132 | +} else { |
| 133 | + /* phpunit-6 */ |
| 134 | + trait SetUpTearDownTrait |
| 135 | + { |
| 136 | + use SetUpTearDownTraitDefaultDoMethods; |
| 137 | + |
| 138 | + public static function setUpBeforeClass() |
| 139 | + { |
| 140 | + self::doSetUpBeforeClass(); |
| 141 | + } |
| 142 | + |
| 143 | + public static function tearDownAfterClass() |
| 144 | + { |
| 145 | + self::doTearDownAfterClass(); |
| 146 | + } |
| 147 | + |
| 148 | + protected function setUp() |
| 149 | + { |
| 150 | + self::doSetUp(); |
| 151 | + } |
| 152 | + |
| 153 | + protected function tearDown() |
| 154 | + { |
| 155 | + self::doTearDown(); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +/* |
| 161 | + * TestCaseCompat (public). |
| 162 | + * |
| 163 | + * This trait accumulates all hacks defined above. |
| 164 | + */ |
| 165 | +trait TestCaseCompat |
| 166 | +{ |
| 167 | + use SetUpTearDownTrait; |
| 168 | +} |
0 commit comments