Skip to content

Commit 4835886

Browse files
committed
test: support phpunit-7
Added the compatibility layer for phpunit-6 and phpunit-7. The problem, which is solved here, is that PHP 7.0 does not support `void` return value declaration, but phpunit-7 (which supports php-7.[1-3]) requires it for some methods (see [1]). The compatibility layer is necessary to run the same testing code on different php/phpunit versions. The implementation idea is borrowed from Symfony's [PHPUnit Bridge][2]. There is also broad discussion about this problem in the Drupal's [issue 3063887][3]. In fact the test suite works on phpunit-8 too, but phpunit produces several warnings about using of deprecated functionality. It will be fixed in the following commits. [1]: https://phpunit.de/announcements/phpunit-7.html [2]: https://symfony.com/doc/current/components/phpunit_bridge.html#removing-the-void-return-type [3]: https://www.drupal.org/project/drupal/issues/3063887
1 parent 8ce61e5 commit 4835886

File tree

8 files changed

+191
-9
lines changed

8 files changed

+191
-9
lines changed

test/AssertTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44

55
class AssertTest extends TestCase
66
{
7+
use TestCaseCompat;
8+
79
protected static $tarantool, $tm;
810

9-
public static function setUpBeforeClass() {
11+
public static function doSetUpBeforeClass() {
1012
self::$tm = ini_get("tarantool.request_timeout");
1113
ini_set("tarantool.request_timeout", "0.1");
1214
self::$tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'));
1315
self::$tarantool->authenticate('test', 'test');
1416
}
1517

16-
public static function tearDownAfterClass() {
18+
public static function doTearDownAfterClass() {
1719
ini_set("tarantool.request_timeout", self::$tm);
1820
}
1921

20-
protected function tearDown() {
22+
protected function doTearDown() {
2123
$tuples = self::$tarantool->select("test");
2224
foreach($tuples as $value)
2325
self::$tarantool->delete("test", Array($value[0]));

test/CreateTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44

55
class CreateTest extends TestCase
66
{
7+
use TestCaseCompat;
8+
79
protected static $port, $tm;
810

9-
public static function setUpBeforeClass() {
11+
public static function doSetUpBeforeClass() {
1012
self::$port = getenv('PRIMARY_PORT');
1113
self::$tm = ini_get("tarantool.timeout");
1214
// error_log("before setting tarantool timeout");
1315
ini_set("tarantool.timeout", "0.1");
1416
// error_log("after setting tarantool timeout");
1517
}
1618

17-
public static function tearDownAfterClass() {
19+
public static function doTearDownAfterClass() {
1820
ini_set("tarantool.timeout", self::$tm);
1921
}
2022

test/DMLTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
class DMLTest extends TestCase
66
{
7+
use TestCaseCompat;
8+
79
protected static $tarantool;
810

9-
public static function setUpBeforeClass()
11+
public static function doSetUpBeforeClass()
1012
{
1113
self::$tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'), 'test', 'test');
1214
}
1315

14-
protected function tearDown()
16+
protected function doTearDown()
1517
{
1618
$tuples = self::$tarantool->select("test");
1719
foreach($tuples as $value)

test/MsgPackTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
class MsgPackTest extends TestCase
66
{
7+
use TestCaseCompat;
8+
79
protected static $tarantool;
810

9-
public static function setUpBeforeClass()
11+
public static function doSetUpBeforeClass()
1012
{
1113
self::$tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'), 'test', 'test');
1214
self::$tarantool->ping();

test/PhpUnitCompat.php

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
}

test/RandomTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ function generateRandomString($length = 10) {
1414

1515
class RandomTest extends TestCase
1616
{
17+
use TestCaseCompat;
18+
1719
protected static $tarantool;
1820

19-
public static function setUpBeforeClass() {
21+
public static function doSetUpBeforeClass() {
2022
self::$tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'), 'test', 'test');
2123
self::$tarantool->ping();
2224
}

test/bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require __DIR__ . '/PhpUnitCompat.php';

test/shared/phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<phpunit
22
colors ="true"
33
verbose="true"
4+
bootstrap="test/bootstrap.php"
45
>
56
<testsuites>
67
<testsuite name="Basic PHP Tests">

0 commit comments

Comments
 (0)