diff options
author | Davey Shafik <me@daveyshafik.com> | 2016-01-28 16:14:56 +0100 |
---|---|---|
committer | Davey Shafik <me@daveyshafik.com> | 2016-02-02 12:50:51 -0500 |
commit | e2f4aeb61cae100653b61c10a1424771eb6ff25c (patch) | |
tree | 7af810a0babe1d6420c42551bd7044427235616f | |
parent | 3d5a14dbea4538a87ddbcfc19c3ac1dc00c475e1 (diff) | |
download | php7-mysql-shim-e2f4aeb61cae100653b61c10a1424771eb6ff25c.zip php7-mysql-shim-e2f4aeb61cae100653b61c10a1424771eb6ff25c.tar.gz php7-mysql-shim-e2f4aeb61cae100653b61c10a1424771eb6ff25c.tar.bz2 |
Fix return values for `mysql_fetch_*` functions
The methods `mysql_fetch_* and `mysqli_fetch_*` have different return values, if there is no [more] results to fetch. Therefore the **null** return value of `mysqli` needs to be matched to the **false** return value of `mysql`.
Thanks to @muhammedalialat
-rw-r--r-- | lib/mysql.php | 13 | ||||
-rw-r--r-- | tests/MySqlShimTest.php | 36 |
2 files changed, 39 insertions, 10 deletions
diff --git a/lib/mysql.php b/lib/mysql.php index 91d8a29..03bd7e3 100644 --- a/lib/mysql.php +++ b/lib/mysql.php @@ -284,7 +284,7 @@ namespace { return false; // @codeCoverageIgnoreEnd } - return mysqli_fetch_row($result); + return mysqli_fetch_row($result) ?: false; } function mysql_fetch_array($result) @@ -294,7 +294,7 @@ namespace { return false; // @codeCoverageIgnoreEnd } - return mysqli_fetch_array($result); + return mysqli_fetch_array($result) ?: false; } function mysql_fetch_assoc($result) /* : array|null */ @@ -304,7 +304,8 @@ namespace { return false; // @codeCoverageIgnoreEnd } - return mysqli_fetch_assoc($result); + + return mysqli_fetch_assoc($result) ?: false; } function mysql_fetch_object($result, $class = null, array $params = []) /* : object|null */ @@ -321,11 +322,7 @@ namespace { $object = mysqli_fetch_object($result, $class, $params); } - if($object == null) { - return false; - } - - return $object; + return $object ?: false; } function mysql_data_seek($result, $offset) diff --git a/tests/MySqlShimTest.php b/tests/MySqlShimTest.php index 2c8adcf..18b7cd2 100644 --- a/tests/MySqlShimTest.php +++ b/tests/MySqlShimTest.php @@ -489,6 +489,20 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase $this->assertEquals(sizeof($results), $i); } + /** + * @param $function + * @dataProvider mysql_fetch_no_rows_dataProvider + */ + public function test_mysql_fetch_no_rows($function) + { + $this->getConnection("shim_test"); + $result = mysql_query("SELECT * FROM testing WHERE one = 'fail'"); + + $this->assertResult($result); + $this->assertEquals(0, mysql_num_rows($result)); + $this->assertFalse($function($result)); + } + public function test_mysql_num_rows() { $this->getConnection("shim_test"); @@ -654,8 +668,8 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase if (!empty($dm)) { fwrite(STDERR, "=> Starting Docker Machine\n"); - exec($dm . ' create -d virtualbox mysql-shim'); - exec($dm . ' start mysql-shim'); + passthru($dm . ' create -d virtualbox mysql-shim'); + passthru($dm . ' start mysql-shim'); $env = ''; exec($dm . ' env mysql-shim', $env); @@ -778,6 +792,24 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase ]; } + public function mysql_fetch_no_rows_dataProvider() + { + return [ + [ + 'function' => 'mysql_fetch_array', + ], + [ + 'function' => 'mysql_fetch_assoc', + ], + [ + 'function' => 'mysql_fetch_row', + ], + [ + 'function' => 'mysql_fetch_object', + ], + ]; + } + public function mysql_function_invalid_result_DataProvider() { return [ |