Skip to content

Commit

Permalink
test: use strict types mode
Browse files Browse the repository at this point in the history
It is the rule of thumb to run tests in the strict types mode, because
it may reveal subtle problems. See #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.
  • Loading branch information
Totktonada committed Mar 28, 2020
1 parent 0a206c4 commit da8ecba
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 6 deletions.
5 changes: 4 additions & 1 deletion test/AssertTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

class AssertTest extends TestCase
Expand All @@ -11,7 +13,8 @@ class AssertTest extends TestCase
public static function doSetUpBeforeClass() {
self::$tm = ini_get("tarantool.request_timeout");
ini_set("tarantool.request_timeout", "0.1");
self::$tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'));
$port = TestHelpers::getTarantoolPort();
self::$tarantool = new Tarantool('localhost', $port);
self::$tarantool->authenticate('test', 'test');
}

Expand Down
2 changes: 1 addition & 1 deletion test/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CreateTest extends TestCase
protected static $port, $tm;

public static function doSetUpBeforeClass() {
self::$port = getenv('PRIMARY_PORT');
self::$port = TestHelpers::getTarantoolPort();
self::$tm = ini_get("tarantool.timeout");
// error_log("before setting tarantool timeout");
ini_set("tarantool.timeout", "0.1");
Expand Down
5 changes: 4 additions & 1 deletion test/DMLTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

class DMLTest extends TestCase
Expand All @@ -10,7 +12,8 @@ class DMLTest extends TestCase

public static function doSetUpBeforeClass()
{
self::$tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'), 'test', 'test');
$port = TestHelpers::getTarantoolPort();
self::$tarantool = new Tarantool('localhost', $port, 'test', 'test');
}

protected function doTearDown()
Expand Down
5 changes: 4 additions & 1 deletion test/MockTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

class MockTest extends TestCase
Expand All @@ -14,7 +16,8 @@ public function testFoo()
public function testDoo()
{
try {
(new Tarantool('localhost', getenv('PRIMARY_PORT')))->select('_vindex', [], 'name');
$port = testHelpers::getTarantoolPort();
(new Tarantool('localhost', $port))->select('_vindex', [], 'name');
$this->assertFalse(True);
} catch (Exception $e) {
$this->assertTrue(True);
Expand Down
5 changes: 4 additions & 1 deletion test/MsgPackTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

class MsgPackTest extends TestCase
Expand All @@ -10,7 +12,8 @@ class MsgPackTest extends TestCase

public static function doSetUpBeforeClass()
{
self::$tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'), 'test', 'test');
$port = TestHelpers::getTarantoolPort();
self::$tarantool = new Tarantool('localhost', $port, 'test', 'test');
self::$tarantool->ping();
}

Expand Down
5 changes: 4 additions & 1 deletion test/RandomTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
Expand All @@ -19,7 +21,8 @@ class RandomTest extends TestCase
protected static $tarantool;

public static function doSetUpBeforeClass() {
self::$tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'), 'test', 'test');
$port = TestHelpers::getTarantoolPort();
self::$tarantool = new Tarantool('localhost', $port, 'test', 'test');
self::$tarantool->ping();
}

Expand Down
24 changes: 24 additions & 0 deletions test/TestHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* Set of utility functions for testing.
*/
final class TestHelpers {
/*
* Get a port number, where tarantool expects binary protocol
* clients.
*
* The source of information is PRIMARY_PORT environment
* variable, which is set by the test-run.py script.
*
* If it is not set for some reason, the function will raise
* an exception.
*/
public static function getTarantoolPort() {
$port = intval(getenv('PRIMARY_PORT'));
if ($port == 0)
throw new Exception(
'Unable to parse PRIMARY_PORT environment variable as int');
return $port;
}
};
1 change: 1 addition & 0 deletions test/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?php

require __DIR__ . '/PhpUnitCompat.php';
require __DIR__ . '/TestHelpers.php';

0 comments on commit da8ecba

Please sign in to comment.