diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/MySqlShimTest.php | 236 | ||||
-rw-r--r-- | tests/bootstrap.php | 14 |
2 files changed, 65 insertions, 185 deletions
diff --git a/tests/MySqlShimTest.php b/tests/MySqlShimTest.php index a7264be..ac975a2 100644 --- a/tests/MySqlShimTest.php +++ b/tests/MySqlShimTest.php @@ -9,21 +9,16 @@ */ namespace Dshafik\MySQL\Tests; -class MySqlShimTest extends \PHPUnit_Framework_TestCase +class MySqlShimTest extends \PHPUnit\Framework\TestCase { /** * @var string MySQL Host */ - public static $host; + public static $host = 'localhost'; public static $username = 'root'; public static $password = null; /** - * @var string Docker container - */ - protected static $container; - - /** * @var array Location of binaries */ protected static $bin = array(); @@ -40,6 +35,19 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase public function __construct($name = null, array $data = array(), $dataName = '') { + if (getenv('MYSQL_HOST') !== false) { + static::$host = getenv('MYSQL_HOST'); + } + + if (getenv('MYSQL_USERNAME') !== false) { + static::$username = getenv('MYSQL_USERNAME'); + } + + if (getenv('MYSQL_PASSWORD') !== false) { + var_dump(getenv('MYSQL_PASSWORD')); + static::$password = getenv('MYSQL_PASSWORD'); + } + parent::__construct($name, $data, $dataName); $this->runtime = new \SebastianBergmann\Environment\Runtime(); } @@ -55,7 +63,7 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase */ public function test_mysql_connect_defaults() { - ini_set('mysqli.default_host', static::$hostname); + ini_set('mysqli.default_host', static::$host); ini_set('mysqli.default_user', static::$username); ini_set('mysqli.default_pw', (static::$password === null) ? '' : static::$password); @@ -63,12 +71,10 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase $this->assertConnection($mysql); } - /** - * @expectedException \PHPUnit_Framework_Error_Warning - * @expectedExceptionMessageRegExp /^mysql(i?)_connect\(\): (\(HY000\/1045\): )?Access denied for user 'baduser'@'(.*?)' \(using password: YES\)$/ - */ public function test_mysql_connect_fail_warning() { + $this->expectWarning(); + $this->expectWarningMessageMatches("/^mysql(i?)_connect\(\): (\(HY000\/1045\): )?Access denied for user 'baduser'@'(.*?)' \(using password: YES\)$/"); mysql_connect(static::$host, 'baduser', 'badpass'); } @@ -79,13 +85,16 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \PHPUnit_Framework_Error_Warning - * @expectedExceptionMessage Argument $new is no longer supported in PHP > 7 * @requires PHP 7.0.0 */ public function test_mysql_connect_new() { - mysql_connect(static::$host, static::$username, static::$password, true); + $this->expectWarning(); + $this->expectWarningMessage('Argument $new is no longer supported in PHP > 7'); + $conn1 = mysql_connect(static::$host, static::$username, static::$password, true); + $conn2 = mysql_connect(static::$host, static::$username, static::$password, true); + + $this->assertEquals($conn1, $conn2); } public function test_mysql_connect_options() @@ -94,19 +103,15 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase $this->assertConnection($mysql); } - /** - * @expectedException \PHPUnit_Framework_Error_Warning - * @expectedExceptionMessageRegExp /^mysql((i_real)?)_connect\(\): (\(HY000\/1045\): )?Access denied for user 'baduser'@'(.*?)' \(using password: YES\)$/ - */ public function test_mysql_connect_options_fail() { + $this->expectWarning(); + $this->expectWarningMessageMatches("/^mysql((i_real)?)_connect\(\): (\(HY000\/1045\): )?Access denied for user 'baduser'@'(.*?)' \(using password: YES\)$/"); mysql_connect(static::$host, 'baduser', 'badpass', false, MYSQL_CLIENT_COMPRESS); } public function test_mysql_connect_multi() { - $this->skipForHHVM(); - $conn = mysql_connect(static::$host, static::$username, static::$password); $conn2 = mysql_connect(static::$host, static::$username, static::$password); @@ -279,8 +284,6 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase public function test_mysql_db_query() { - $this->skipForHHVM(); - $this->getConnection(); $result = mysql_db_query('shim_test', 'SELECT DATABASE()'); $this->assertResult($result); @@ -294,8 +297,6 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase public function test_mysql_db_query_fail() { - $this->skipForHHVM(); - $this->getConnection(); $result = mysql_db_query('nonexistent', 'SELECT DATABASE()'); $this->assertFalse($result); @@ -340,8 +341,6 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase */ public function test_mysql_list_tables_fail() { - $this->skipForHHVM(); - $this->getConnection(); $result = mysql_list_tables('nonexistent'); $this->assertFalse($result); @@ -366,8 +365,6 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase */ public function test_mysql_list_fields() { - $this->skipForHHVM(); - $mysql = $this->getConnection(); $result = mysql_list_fields('shim_test', 'testing', $mysql); @@ -398,8 +395,6 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase */ public function test_mysql_list_fields_specialchars() { - $this->skipForHHVM(); - $this->getConnection('shim-test'); mysql_query( @@ -450,15 +445,11 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase public function test_mysql_list_fields_fail() { - $this->skipForHHVM(); + $this->expectWarning(); + $this->expectWarningMessage("mysql_list_fields(): Unable to save MySQL query result"); - try { - $this->getConnection(); - mysql_list_fields('shim_test', 'nonexistent'); - } catch (\Exception $e) { - $this->assertInstanceOf('\PHPUnit_Framework_Error_Warning', $e); - $this->assertEquals('mysql_list_fields(): Unable to save MySQL query result', $e->getMessage()); - } + $this->getConnection(); + mysql_list_fields('shim_test', 'nonexistent'); } public function test_mysql_field() @@ -617,60 +608,51 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase $this->assertEquals('blob', mysql_field_flags($result, 11)); } - /** - * @expectedException \PHPUnit_Framework_Error_Warning - * @expectedExceptionMessageRegExp /^(mysql_field_name\(\): )?Field 999 is invalid for MySQL result index .*$/ - */ public function test_mysql_field_name_fail() { + $this->expectWarning(); + $this->expectWarningMessageMatches("/^(mysql_field_name\(\): )?Field 999 is invalid for MySQL result index .*$/"); $this->getConnection('shim_test'); $result = mysql_query('SELECT * FROM testing LIMIT 1'); $this->assertEquals('testing', mysql_field_name($result, 999)); } - /** - * @expectedException \PHPUnit_Framework_Error_Warning - * @expectedExceptionMessageRegExp /^(mysql_field_table\(\): )?Field 999 is invalid for MySQL result index .*$/ - */ public function test_mysql_field_table_fail() { + $this->expectWarning(); + $this->expectWarningMessageMatches("/^(mysql_field_table\(\): )?Field 999 is invalid for MySQL result index .*$/"); $this->getConnection('shim_test'); $result = mysql_query('SELECT * FROM testing LIMIT 1'); $this->assertEquals('testing', mysql_field_table($result, 999)); } - /** - * @expectedException \PHPUnit_Framework_Error_Warning - * @expectedExceptionMessageRegExp /^(mysql_field_type\(\): )?Field 999 is invalid for MySQL result index .*$/ - */ public function test_mysql_field_type_fail() { + $this->expectWarning(); + $this->expectWarningMessageMatches("/^(mysql_field_type\(\): )?Field 999 is invalid for MySQL result index .*$/"); $this->getConnection('shim_test'); $result = mysql_query('SELECT * FROM testing LIMIT 1'); $this->assertEquals('testing', mysql_field_type($result, 999)); } - /** - * @expectedException \PHPUnit_Framework_Error_Warning - * @expectedExceptionMessageRegExp /^(mysql_field_len\(\): )?Field 999 is invalid for MySQL result index .*$/ - */ public function test_mysql_field_len_fail() { + $this->expectWarning(); + $this->expectWarningMessageMatches("/^(mysql_field_len\(\): )?Field 999 is invalid for MySQL result index .*$/"); $this->getConnection('shim_test'); $result = mysql_query('SELECT * FROM testing LIMIT 1'); $this->assertEquals('testing', mysql_field_len($result, 999)); } - /** - * @expectedException \PHPUnit_Framework_Error_Warning - * @expectedExceptionMessageRegExp /^(mysql_field_flags\(\): )?Field 999 is invalid for MySQL result index .*$/ - */ public function test_mysql_field_flags_fail() { + + $this->expectWarning(); + $this->expectWarningMessageMatches("/^(mysql_field_flags\(\): )?Field 999 is invalid for MySQL result index .*$/"); $this->getConnection('shim_test'); $result = mysql_query('SELECT * FROM testing LIMIT 1'); @@ -686,12 +668,10 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase $this->assertEquals(2, mysql_num_fields($result)); } - /** - * @expectedException \PHPUnit_Framework_Error_Warning - * @expectedExceptionMessageRegExp /^mysql_num_fields\(\) expects parameter 1 to be resource, boolean given$/ - */ public function test_mysql_num_fields_fail() { + $this->expectWarning(); + $this->expectWarningMessage("mysql_num_fields() expects parameter 1 to be resource, boolean given"); $this->getConnection('shim_test'); $result = mysql_query('SELECT one, two FROM nonexistent'); @@ -701,20 +681,16 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase /** * @dataProvider mysql_function_invalid_result_DataProvider */ - public function test_mysql_function_invalid_result($function, $error, $args, $skipHHVM = false) + public function test_mysql_function_invalid_result($function, $error, $args) { - $this->skipForHHVM($skipHHVM); + $this->expectWarning(); + $this->expectWarningMessageMatches('@' .$error. '@'); - try { - if ($args !== array()) { - array_unshift($args, null); - call_user_func_array($function, $args); - } - call_user_func($function, null); - } catch (\Exception $e) { - $this->assertInstanceOf('\PHPUnit_Framework_Error_Warning', $e); - $this->assertRegExp('@' . $error . '@', $e->getMessage()); + if ($args !== array()) { + array_unshift($args, null); + call_user_func_array($function, $args); } + call_user_func($function, null); } /** @@ -794,12 +770,10 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase } } - /** - * @expectedException \PHPUnit_Framework_Error_Warning - * @expectedExceptionMessageRegExp /^(mysql_result\(\): )?three not found in MySQL result index (.*?)$/ - */ public function test_mysql_result_fail() { + $this->expectWarning(); + $this->expectWarningMessageMatches("/^(mysql_result\(\): )?three not found in MySQL result index (.*?)$/"); $this->getConnection(); $result = mysql_query('SELECT one, two FROM testing LIMIT 1'); @@ -819,12 +793,10 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase $this->assertEquals(1, mysql_result($result, 0, 'testing.two')); } - /** - * @expectedException \PHPUnit_Framework_Error_Warning - * @expectedExceptionMessageRegExp /^(mysql_result\(\): )?testing.three not found in MySQL result index (.*?)$/ - */ public function test_mysql_result_prefixed_fail() { + $this->expectWarning(); + $this->expectWarningMessageMatches("/^(mysql_result\(\): )?testing.three not found in MySQL result index (.*?)$/"); $this->getConnection(); $result = mysql_query('SELECT one, two FROM testing LIMIT 1'); @@ -833,12 +805,10 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase mysql_result($result, 0, 'testing.three'); } - /** - * @expectedException \PHPUnit_Framework_Error_Warning - * @expectedExceptionMessageRegExp /^(mysql_result\(\): )?Unable to jump to row 1 on MySQL result index (.*?)$/ - */ public function test_mysql_result_invalid_row() { + $this->expectWarning(); + $this->expectWarningMessageMatches("/^(mysql_result\(\): )?Unable to jump to row 1 on MySQL result index (.*?)$/"); $this->getConnection(); $result = mysql_query('SELECT one FROM testing LIMIT 1'); @@ -869,14 +839,10 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase public function test_mysql_close_fail() { - $this->skipForHHVM(); + $this->expectWarning(); + $this->expectWarningMessage("mysql_close(): no MySQL-Link resource supplied"); - try { - mysql_close(); - } catch (\Exception $e) { - $this->assertInstanceOf('\PHPUnit_Framework_Error_Warning', $e); - $this->assertEquals('mysql_close(): no MySQL-Link resource supplied', $e->getMessage()); - } + mysql_close(); } public function test_mysql_error() @@ -914,11 +880,11 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase /** * @requires PHP 7.0.0 - * @expectedException \PHPUnit_Framework_Error_Notice - * @expectedExceptionMessage mysql_escape_string() is insecure; use mysql_real_escape_string() instead! */ public function test_mysql_escape_string_notice() { + $this->expectNotice(); + $this->expectNoticeMessage("mysql_escape_string() is insecure; use mysql_real_escape_string() instead!"); mysql_escape_string("'\0\032\r\n\"\\"); } @@ -950,75 +916,13 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase $this->assertEquals('information_schema', mysql_db_name($dbs, 0)); } - public function tearDown() + public function tearDown(): void { @mysql_close(); } - public static function setUpBeforeClass() + public static function tearDownAfterClass(): void { - error_reporting(E_ALL & ~E_DEPRECATED); - if (getenv('TRAVIS') === false && getenv('APPVEYOR') === false) { - fwrite(STDERR, "=> Finding binaries\n"); - static::$bin['docker'] = $docker = exec('/usr/bin/env which docker'); - if (empty($docker)) { - static::markTestSkipped('Docker is required to run these tests'); - } - - fwrite(STDERR, '=> Running Docker Container: '); - static::$container = exec($docker . ' run -e MYSQL_ALLOW_EMPTY_PASSWORD=1 -P -d mysql/mysql-server:5.7'); - - if (empty(static::$container)) { - static::markTestSkipped('Unable to start docker container'); - } - - fwrite(STDERR, static::$container . "\n"); - - fwrite(STDERR, '=> Finding MySQL Host: '); - static::$host = exec($docker . ' port ' . self::$container . ' 3306'); - fwrite(STDERR, static::$host . "\n"); - - fwrite(STDERR, '=> Waiting on mysqld to start:'); - $out = ''; - while (trim($out) !== 'mysqld') { - $out = exec(static::$bin['docker'] . ' exec ' . static::$container . ' ps ax | awk \'/mysqld/ {print $NF}\''); - } - fwrite(STDERR, " started\n"); - sleep(3); - - fwrite(STDERR, "=> Docker Container Running\n\n"); - - return; - } - - - static::$host = '0.0.0.0'; - if (getenv('APPVEYOR') !== false) { - static::$host = static::$hostname; - static::$password = 'Password12!'; - } - } - - public static function tearDownAfterClass() - { - if (getenv('TRAVIS') === false && getenv('APPVEYOR') === false) { - fwrite(STDERR, "\n\nStopping Docker Container: "); - $output = exec(static::$bin['docker'] . ' stop ' . static::$container); - if (trim($output) !== static::$container) { - fwrite(STDERR, " Failed to stop container!\n"); - return; - } - - $output = exec(static::$bin['docker'] . ' rm ' . static::$container); - if (trim($output) !== static::$container) { - fwrite(STDERR, " Failed to remove container!\n"); - return; - } - fwrite(STDERR, "Done\n"); - - return; - } - mysql_connect(static::$host, static::$username, static::$password); foreach (self::$dbs as $db) { mysql_query("DROP DATABASE IF EXISTS `$db`"); @@ -1126,7 +1030,6 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase 'function' => 'mysql_fetch_row', 'message' => "mysql_fetch_row\(\) expects parameter 1 to be resource, (null|NULL) given", 'args' => array(), - 'skipHHVM' => true ), array( 'function' => 'mysql_fetch_array', @@ -1137,7 +1040,6 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase 'function' => 'mysql_fetch_assoc', 'message' => "mysql_fetch_assoc\(\) expects parameter 1 to be resource, (null|NULL) given", 'args' => array(), - 'skipHHVM' => true ), array( 'function' => 'mysql_fetch_object', @@ -1198,14 +1100,11 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase 'function' => 'mysql_db_name', 'message' => "mysql_db_name\(\) expects parameter 1 to be resource, (null|NULL) given", 'args' => array(0), - 'skipHHVM' => true ), array( 'function' => 'mysql_tablename', 'message' => "mysql_tablename\(\) expects parameter 1 to be resource, (null|NULL) given", - 'args' => array(0), - 'skipHHVM' => true - ), + 'args' => array(0), ), ); } @@ -1301,11 +1200,4 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase 'Not a valid MySQL connection' ); } - - protected function skipForHHVM($condition = true) - { - if ($this->runtime->isHHVM() && $condition) { - $this->markTestSkipped('HHVM Behavior differs from PHP'); - } - } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f8d1391..d5e8404 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -8,16 +8,4 @@ * @link https://github.com/dshafik/php7-mysql-shim */ -require_once __DIR__ . '/../vendor/autoload.php'; - -if (getenv('MYSQL_HOST') !== '') { - Dshafik\MySQL\Tests\MySqlShimTest::$host = getenv('MYSQL_HOST'); -} - -if (getenv('MYSQL_USERNAME') !== '') { - Dshafik\MySQL\Tests\MySqlShimTest::$username = getenv('MYSQL_USERNAME'); -} - -if (getenv('MYSQL_PASSWORD') !== '') { - Dshafik\MySQL\Tests\MySqlShimTest::$password = getenv('MYSQL_PASSWORD'); -}
\ No newline at end of file +require_once __DIR__ . '/../vendor/autoload.php';
\ No newline at end of file |