Skip to content

Commit 017335e

Browse files
committed
test: eliminate phpunit-8 deprecation warnings
After enabling phpunit-7, phpunit-8 starts to work too, but gives several deprecation warnings: * Using of `assertContains()` for search in a string. * Using of `@expectedException` and related annotations. Functionality that is deprecated in phpunit-8 will be disabled in phpunit-9. Since we plan to use phpunit-9 for testing on php-7.3 and php-7.4, it worth to fix the warnings now. This commit updates the test suite to use the new constructions and provides the compatibility layer for phpunit-6, which does not contain `assertStringContainsString()` method. The commit also disables caching of testing results, which was enabled by default in phpunit-8 (see [1]). The test suite is quite small and the feature does not add any value. The reason for disabling is to protect ourself from side effects of this caching: to be honest I don't know how it may change testing results. [1]: https://phpunit.de/announcements/phpunit-8.html
1 parent b3846f6 commit 017335e

File tree

6 files changed

+105
-74
lines changed

6 files changed

+105
-74
lines changed

test/AssertTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ function assert_f()
3636
$this->assertFalse(True);
3737
} catch (TarantoolException $e) {
3838
// print($e->getMessage());
39-
$this->assertContains("Failed to read", $e->getMessage());
39+
$this->assertStringContainsString(
40+
"Failed to read", $e->getMessage());
4041
}
4142

4243
/* We can reconnect and everything will be ok */

test/CreateTest.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,29 @@ public function test_01_01_double_disconnect() {
4747
$a->disconnect();
4848
}
4949

50-
/**
51-
* @expectedException TarantoolException
52-
* @expectedExceptionMessageRegExp /Name or service not known|nodename nor servname provided|Temporary failure in name resolution/
53-
*/
5450
public function test_02_create_error_host() {
55-
(new Tarantool('very_bad_host'))->connect();
51+
$c = new Tarantool('very_bad_host');
52+
53+
$this->expectException(TarantoolException::class);
54+
$this->expectExceptionMessageRegExp(
55+
'/Name or service not known' .
56+
'|nodename nor servname provided' .
57+
'|Temporary failure in name resolution/');
58+
$c->connect();
5659
}
5760

58-
/**
59-
* @expectedException TarantoolException
60-
* @expectedExceptionMessageRegExp /Connection refused|Network is unreachable/
61-
*/
6261
public function test_03_00_create_error_port() {
63-
(new Tarantool('127.0.0.1', 65500))->connect();
62+
$c = new Tarantool('127.0.0.1', 65500);
63+
64+
$this->expectException(TarantoolException::class);
65+
$this->expectExceptionMessageRegExp(
66+
'/Connection refused|Network is unreachable/');
67+
$c->connect();
6468
}
6569

66-
/**
67-
* @expectedException TarantoolException
68-
* @expectedExceptionMessage Invalid primary port value
69-
*/
7070
public function test_03_01_create_error_port() {
71+
$this->expectException(TarantoolException::class);
72+
$this->expectExceptionMessage('Invalid primary port value');
7173
new Tarantool('localhost', 123456);
7274
}
7375

@@ -99,38 +101,38 @@ public function test_05_flush_construct() {
99101
$c->flush_schema();
100102
}
101103

102-
/**
103-
* @expectedException TarantoolClientError
104-
* @expectedExceptionMessage Incorrect password supplied for user
105-
*/
106104
public function test_06_bad_credentials() {
107105
$c = new Tarantool('localhost', self::$port);
108106
$c->connect();
109107
$this->assertTrue($c->ping());
108+
109+
$this->expectException(TarantoolClientError::class);
110+
$this->expectExceptionMessage(
111+
'Incorrect password supplied for user');
110112
$c->authenticate('test', 'bad_password');
111113
}
112114

113-
/**
114-
* @expectedException TarantoolClientError
115-
* @expectedExceptionMessage Incorrect password supplied for user
116-
*/
117115
public function test_07_bad_guest_credentials() {
118116
$c = new Tarantool('localhost', self::$port);
119117
$c->connect();
120118
$this->assertTrue($c->ping());
119+
120+
$this->expectException(TarantoolClientError::class);
121+
$this->expectExceptionMessage(
122+
'Incorrect password supplied for user');
121123
$c->authenticate('guest', 'guest');
122124
}
123125

124-
/**
125-
* @expectedException TarantoolClientError
126-
* @expectedExceptionMessage Incorrect password supplied for user
127-
*/
128126
/**
129127
* Comment this, since behaviour of authentication with 'empty password' has changed
130128
public function test_07_01_bad_guest_credentials() {
131129
$c = new Tarantool('localhost', self::$port);
132130
$c->connect();
133131
$this->assertTrue($c->ping());
132+
133+
$this->expectException(TarantoolClientError::class);
134+
$this->expectExceptionMessage(
135+
'Incorrect password supplied for user');
134136
$c->authenticate('guest', '');
135137
}
136138
*/

test/DMLTest.php

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,11 @@ public function test_02_select_diff() {
7878
$this->devastate(100);
7979
}
8080

81-
/**
82-
* @expectedException TarantoolClientError
83-
* @expectedExceptionMessage Duplicate key exists
84-
**/
8581
public function test_03_insert_error() {
8682
self::$tarantool->insert("test", array(1, 2, "smth"));
83+
84+
$this->expectException(TarantoolClientError::class);
85+
$this->expectExceptionMessage('Duplicate key exists');
8786
self::$tarantool->insert("test", array(1, 2, "smth"));
8887
}
8988

@@ -170,11 +169,9 @@ public function test_07_update_no_error() {
170169
self::$tarantool->update("test", 0, array());
171170
}
172171

173-
/**
174-
* @expectedException TarantoolException
175-
* @expectedExceptionMessage No field 'nosuchfield' defined
176-
*/
177172
public function test_08_update_error_nosuchfield() {
173+
$this->expectException(TarantoolException::class);
174+
$this->expectExceptionMessage("No field 'nosuchfield' defined");
178175
self::$tarantool->update("test", 0, array(
179176
array(
180177
"field" => "nosuchfield",
@@ -183,11 +180,9 @@ public function test_08_update_error_nosuchfield() {
183180
));
184181
}
185182

186-
/**
187-
* @expectedException TarantoolException
188-
* @expectedExceptionMessage Five fields
189-
*/
190183
public function test_08_update_error() {
184+
$this->expectException(TarantoolException::class);
185+
$this->expectExceptionMessage('Five fields');
191186
self::$tarantool->update("test", 0, array(
192187
array(
193188
"field" => 2,
@@ -198,11 +193,9 @@ public function test_08_update_error() {
198193
));
199194
}
200195

201-
/**
202-
* @expectedException TarantoolException
203-
* @expectedExceptionMessage Field OP must be provided
204-
*/
205196
public function test_09_update_error() {
197+
$this->expectException(TarantoolException::class);
198+
$this->expectExceptionMessage('Field OP must be provided');
206199
self::$tarantool->update("test", 0, array(
207200
array(
208201
"field" => 2,
@@ -212,11 +205,9 @@ public function test_09_update_error() {
212205
));
213206
}
214207

215-
/**
216-
* @expectedException TarantoolException
217-
* @expectedExceptionMessage Field OP must be provided
218-
*/
219208
public function test_10_update_error() {
209+
$this->expectException(TarantoolException::class);
210+
$this->expectExceptionMessage('Field OP must be provided');
220211
self::$tarantool->update("test", 0, array(
221212
array(
222213
"field" => 2,
@@ -225,11 +216,9 @@ public function test_10_update_error() {
225216
));
226217
}
227218

228-
/**
229-
* @expectedException TarantoolException
230-
* @expectedExceptionMessage Three fields must be provided
231-
*/
232219
public function test_11_update_error() {
220+
$this->expectException(TarantoolException::class);
221+
$this->expectExceptionMessage('Three fields must be provided');
233222
self::$tarantool->update("test", 0,
234223
array(
235224
array(
@@ -374,7 +363,7 @@ public function test_17_02_it_exception($spc, $itype, $xcmsg) {
374363
self::$tarantool->select($spc, null, null, null, null, $itype);
375364
$this->assertFalse(True);
376365
} catch (TarantoolException $e) {
377-
$this->assertContains($xcmsg, $e->getMessage());
366+
$this->assertStringContainsString($xcmsg, $e->getMessage());
378367
}
379368
}
380369

@@ -386,7 +375,7 @@ public function test_17_03_it_good($spc, $itype) {
386375
self::$tarantool->select($spc, null, null, null, null, $itype);
387376
$this->assertTrue(True);
388377
} catch (Exception $e) {
389-
$this->assertContains($xcmsg, $e->getMessage());
378+
$this->assertStringContainsString($xcmsg, $e->getMessage());
390379
}
391380
}
392381

test/MsgPackTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,21 @@ public function test_00_msgpack_call() {
2929
$this->assertEquals($resp[0][0], 2);
3030
}
3131

32-
/**
33-
* @expectedException TarantoolException
34-
* @expectedExceptionMessage Bad key type for PHP Array
35-
**/
3632
public function test_01_msgpack_array_key() {
33+
$this->expectException(TarantoolException::class);
34+
$this->expectExceptionMessage('Bad key type for PHP Array');
3735
self::$tarantool->select("msgpack", array(2));
3836
}
3937

40-
/**
41-
* @expectedException TarantoolException
42-
* @expectedExceptionMessage Bad key type for PHP Array
43-
**/
4438
public function test_02_msgpack_float_key() {
39+
$this->expectException(TarantoolException::class);
40+
$this->expectExceptionMessage('Bad key type for PHP Array');
4541
self::$tarantool->select("msgpack", array(1));
4642
}
4743

48-
/**
49-
* @expectedException TarantoolException
50-
* @expectedExceptionMessage Bad key type for PHP Array
51-
**/
5244
public function test_03_msgpack_array_of_float_as_key() {
45+
$this->expectException(TarantoolException::class);
46+
$this->expectExceptionMessage('Bad key type for PHP Array');
5347
self::$tarantool->select("msgpack", array(3));
5448
}
5549

test/PhpUnitCompat.php

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,30 @@
77
* different phpunit versions (phpunit-6 and phpunit-7 at the
88
* moment).
99
*
10-
* Now it implements the workaround for phpunit-7 requirement to
11-
* use `void` return type declarations for the following methods
12-
* (see [1]).
10+
* Now it contains workarounds for two problems:
11+
*
12+
* - phpunit-7 requirement to use `void` return type declaration
13+
* for several methods.
14+
* - phpunit-8 deprecation warning re using assertContains() for
15+
* search a substring in a string.
16+
*
17+
* Usage: add `use TestCaseCompat;` in a TestCase derived class
18+
* and follow instructions and examples below.
19+
*
20+
* `void` return type declaration
21+
* ------------------------------
22+
*
23+
* phpunit-7 requires to use `void` return type declaration for
24+
* the following methods (and several others, which we don't use):
1325
*
1426
* - setUpBeforeClass()
1527
* - tearDownAfterClass()
1628
* - setUp()
1729
* - tearDown()
1830
*
31+
* See the announcement [1] for more information about changes in
32+
* this phpunit version.
33+
*
1934
* The problem is that php-7.0 does not support `void` return type
2035
* declaration, so a trick is necessary to support both php-7.0
2136
* and phpunit-7+ for, say, php-7.3+.
@@ -24,9 +39,6 @@
2439
* test case class and specify do*() methods instead of ones
2540
* listed above (see the example below).
2641
*
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-
*
3042
* Example:
3143
*
3244
* | use PHPUnit\Framework\TestCase;
@@ -53,10 +65,15 @@
5365
* | }
5466
* | }
5567
*
56-
* Use `TestCaseCompat` trait and consider others as
57-
* implementation details.
58-
*
5968
* [1]: https://phpunit.de/announcements/phpunit-7.html
69+
*
70+
* assertContains() for strings
71+
* ----------------------------
72+
*
73+
* phpunit-8 warns about using of assertContains() for search a
74+
* substring in a string. Add `use TestCaseCompat;` to a TestCase
75+
* derived class and use assertStringContainsString() on any
76+
* phpunit-6+ version.
6077
*/
6178

6279
use PHPUnit\Framework\TestCase;
@@ -157,6 +174,32 @@ protected function tearDown()
157174
}
158175
}
159176

177+
/*
178+
* AssertStringContainsStringTrait (private).
179+
*
180+
* phpunit-6 does not contain assertStringContainsString() method,
181+
* while phpunit-8 warns about using assertContains() for search a
182+
* substring in a string.
183+
*
184+
* This trait adds assertStringContainsString() method for
185+
* phpunit-6.
186+
*/
187+
if ($testCaseRef->hasMethod('assertStringContainsString')) {
188+
trait AssertStringContainsStringTrait
189+
{
190+
/* Nothing to define. */
191+
}
192+
} else {
193+
trait AssertStringContainsStringTrait
194+
{
195+
public static function assertStringContainsString(
196+
$needle, $haystack, $message = '')
197+
{
198+
self::assertContains($needle, $haystack, $message);
199+
}
200+
}
201+
}
202+
160203
/*
161204
* TestCaseCompat (public).
162205
*
@@ -165,4 +208,5 @@ protected function tearDown()
165208
trait TestCaseCompat
166209
{
167210
use SetUpTearDownTrait;
211+
use AssertStringContainsStringTrait;
168212
}

test/shared/phpunit.xml

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

0 commit comments

Comments
 (0)