Skip to content

Commit da8ecba

Browse files
committed
test: use strict types mode
It is the rule of thumb to run tests in the strict types mode, because it may reveal subtle problems. See tarantool#154 for example. Updated the test suite to cast tarantool port (acquired from PRIMARY_PORT environment variable) from string to int. Aside of this, raise an error when the port is not set: it'll allow to fail fast and provide a good diagnostic when a configuration problem occurs.
1 parent 0a206c4 commit da8ecba

8 files changed

+46
-6
lines changed

test/AssertTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
use PHPUnit\Framework\TestCase;
46

57
class AssertTest extends TestCase
@@ -11,7 +13,8 @@ class AssertTest extends TestCase
1113
public static function doSetUpBeforeClass() {
1214
self::$tm = ini_get("tarantool.request_timeout");
1315
ini_set("tarantool.request_timeout", "0.1");
14-
self::$tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'));
16+
$port = TestHelpers::getTarantoolPort();
17+
self::$tarantool = new Tarantool('localhost', $port);
1518
self::$tarantool->authenticate('test', 'test');
1619
}
1720

test/CreateTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class CreateTest extends TestCase
99
protected static $port, $tm;
1010

1111
public static function doSetUpBeforeClass() {
12-
self::$port = getenv('PRIMARY_PORT');
12+
self::$port = TestHelpers::getTarantoolPort();
1313
self::$tm = ini_get("tarantool.timeout");
1414
// error_log("before setting tarantool timeout");
1515
ini_set("tarantool.timeout", "0.1");

test/DMLTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
use PHPUnit\Framework\TestCase;
46

57
class DMLTest extends TestCase
@@ -10,7 +12,8 @@ class DMLTest extends TestCase
1012

1113
public static function doSetUpBeforeClass()
1214
{
13-
self::$tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'), 'test', 'test');
15+
$port = TestHelpers::getTarantoolPort();
16+
self::$tarantool = new Tarantool('localhost', $port, 'test', 'test');
1417
}
1518

1619
protected function doTearDown()

test/MockTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
use PHPUnit\Framework\TestCase;
46

57
class MockTest extends TestCase
@@ -14,7 +16,8 @@ public function testFoo()
1416
public function testDoo()
1517
{
1618
try {
17-
(new Tarantool('localhost', getenv('PRIMARY_PORT')))->select('_vindex', [], 'name');
19+
$port = testHelpers::getTarantoolPort();
20+
(new Tarantool('localhost', $port))->select('_vindex', [], 'name');
1821
$this->assertFalse(True);
1922
} catch (Exception $e) {
2023
$this->assertTrue(True);

test/MsgPackTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
use PHPUnit\Framework\TestCase;
46

57
class MsgPackTest extends TestCase
@@ -10,7 +12,8 @@ class MsgPackTest extends TestCase
1012

1113
public static function doSetUpBeforeClass()
1214
{
13-
self::$tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'), 'test', 'test');
15+
$port = TestHelpers::getTarantoolPort();
16+
self::$tarantool = new Tarantool('localhost', $port, 'test', 'test');
1417
self::$tarantool->ping();
1518
}
1619

test/RandomTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
function generateRandomString($length = 10) {
46
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
57
$charactersLength = strlen($characters);
@@ -19,7 +21,8 @@ class RandomTest extends TestCase
1921
protected static $tarantool;
2022

2123
public static function doSetUpBeforeClass() {
22-
self::$tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'), 'test', 'test');
24+
$port = TestHelpers::getTarantoolPort();
25+
self::$tarantool = new Tarantool('localhost', $port, 'test', 'test');
2326
self::$tarantool->ping();
2427
}
2528

test/TestHelpers.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* Set of utility functions for testing.
5+
*/
6+
final class TestHelpers {
7+
/*
8+
* Get a port number, where tarantool expects binary protocol
9+
* clients.
10+
*
11+
* The source of information is PRIMARY_PORT environment
12+
* variable, which is set by the test-run.py script.
13+
*
14+
* If it is not set for some reason, the function will raise
15+
* an exception.
16+
*/
17+
public static function getTarantoolPort() {
18+
$port = intval(getenv('PRIMARY_PORT'));
19+
if ($port == 0)
20+
throw new Exception(
21+
'Unable to parse PRIMARY_PORT environment variable as int');
22+
return $port;
23+
}
24+
};

test/bootstrap.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<?php
22

33
require __DIR__ . '/PhpUnitCompat.php';
4+
require __DIR__ . '/TestHelpers.php';

0 commit comments

Comments
 (0)