diff --git a/tests/PHPStan/Type/MySQLi/MySQLiTypeInferenceTest.php b/tests/PHPStan/Type/MySQLi/MySQLiTypeInferenceTest.php index c5e377a..b8fe28e 100644 --- a/tests/PHPStan/Type/MySQLi/MySQLiTypeInferenceTest.php +++ b/tests/PHPStan/Type/MySQLi/MySQLiTypeInferenceTest.php @@ -8,16 +8,31 @@ use MariaStan\TestCaseHelper; use PHPStan\Testing\TypeInferenceTestCase; +use const PHP_VERSION_ID; + class MySQLiTypeInferenceTest extends TypeInferenceTestCase { + /** @return list */ + public static function getTestFiles(): array + { + $result = [__DIR__ . '/data/MySQLiTypeInferenceDataTest.php']; + + if (PHP_VERSION_ID >= 80200) { + $result[] = __DIR__ . '/data/MySQLiExecuteQueryTypeInferenceDataTest.php'; + } + + return $result; + } + /** @return iterable */ - public function dataFileAsserts(): iterable + public static function dataFileAsserts(): iterable { $mysqli = TestCaseHelper::getDefaultSharedConnection(); MySQLiTypeInferenceDataTest::initData($mysqli); - // path to a file with actual asserts of expected types: - yield from $this->gatherAssertTypes(__DIR__ . '/data/MySQLiTypeInferenceDataTest.php'); + foreach (self::getTestFiles() as $testFile) { + yield from self::gatherAssertTypes($testFile); + } } /** @dataProvider dataFileAsserts */ diff --git a/tests/PHPStan/Type/MySQLi/MySQLiTypeInferenceWithFileReflectionTest.php b/tests/PHPStan/Type/MySQLi/MySQLiTypeInferenceWithFileReflectionTest.php index ec312ba..13903f2 100644 --- a/tests/PHPStan/Type/MySQLi/MySQLiTypeInferenceWithFileReflectionTest.php +++ b/tests/PHPStan/Type/MySQLi/MySQLiTypeInferenceWithFileReflectionTest.php @@ -15,7 +15,7 @@ class MySQLiTypeInferenceWithFileReflectionTest extends TypeInferenceTestCase { /** @return iterable */ - public function dataFileAsserts(): iterable + public static function dataFileAsserts(): iterable { $mysqli = TestCaseHelper::getDefaultSharedConnection(); MySQLiTypeInferenceDataTest::initData($mysqli); @@ -24,8 +24,9 @@ public function dataFileAsserts(): iterable MariaDbFileDbReflection::dumpSchema($mysqli, MysqliUtil::getDatabaseName($mysqli)), ); - // path to a file with actual asserts of expected types: - yield from $this->gatherAssertTypes(__DIR__ . '/data/MySQLiTypeInferenceDataTest.php'); + foreach (MySQLiTypeInferenceTest::getTestFiles() as $testFile) { + yield from self::gatherAssertTypes($testFile); + } } /** @dataProvider dataFileAsserts */ diff --git a/tests/PHPStan/Type/MySQLi/data/MySQLiExecuteQueryTypeInferenceDataTest.php b/tests/PHPStan/Type/MySQLi/data/MySQLiExecuteQueryTypeInferenceDataTest.php new file mode 100644 index 0000000..169ef14 --- /dev/null +++ b/tests/PHPStan/Type/MySQLi/data/MySQLiExecuteQueryTypeInferenceDataTest.php @@ -0,0 +1,83 @@ +markTestSkipped('This test needs PHP 8.2'); + } + + $db = TestCaseHelper::getDefaultSharedConnection(); + + $rows = $db->execute_query('SELECT 1 id', [])->fetch_all(MYSQLI_ASSOC); + + foreach ($rows as $row) { + if (function_exists('assertType')) { + assertType('int', $row['id']); + assertType('*ERROR*', $row[0]); + } + + $this->assertSame(['id'], array_keys($row)); + $this->assertIsInt($row['id']); + } + + $rows = $db->execute_query('SELECT 1 id HAVING id = ?', [1])->fetch_all(MYSQLI_ASSOC); + + foreach ($rows as $row) { + if (function_exists('assertType')) { + assertType('int', $row['id']); + assertType('*ERROR*', $row[0]); + } + + $this->assertSame(['id'], array_keys($row)); + $this->assertIsInt($row['id']); + } + } + + // This is not executed. + public function checkExecuteQueryPlaceholderTypeProvider(mysqli $db): void + { + $row = $db->execute_query('SELECT ? val', [rand() ? 1 : 2])->fetch_assoc(); + + if (function_exists('assertType')) { + assertType("'1'|'2'", $row['val']); + } + + $row = $db->execute_query('SELECT ? val', rand() ? [1] : ['a'])->fetch_assoc(); + + if (function_exists('assertType')) { + assertType("'1'|'a'", $row['val']); + } + + $row = $db->execute_query('SELECT ? val', rand() ? [1] : [null])->fetch_assoc(); + + if (function_exists('assertType')) { + assertType("'1'|null", $row['val']); + } + + $row = $db->execute_query('SELECT ? val', [null])->fetch_assoc(); + + if (function_exists('assertType')) { + assertType("null", $row['val']); + } + + $this->expectNotToPerformAssertions(); + } +} diff --git a/tests/PHPStan/Type/MySQLi/data/MySQLiTypeInferenceDataTest.php b/tests/PHPStan/Type/MySQLi/data/MySQLiTypeInferenceDataTest.php index 81a3306..ce1d3cc 100644 --- a/tests/PHPStan/Type/MySQLi/data/MySQLiTypeInferenceDataTest.php +++ b/tests/PHPStan/Type/MySQLi/data/MySQLiTypeInferenceDataTest.php @@ -22,12 +22,10 @@ use function in_array; use function is_string; use function PHPStan\Testing\assertType; -use function rand; use const MYSQLI_ASSOC; use const MYSQLI_BOTH; use const MYSQLI_NUM; -use const PHP_VERSION_ID; class MySQLiTypeInferenceDataTest extends TestCase { @@ -1042,69 +1040,6 @@ public function testTypeOverrides(): void } } - public function testExecuteQuery(): void - { - if (PHP_VERSION_ID < 80200) { - $this->markTestSkipped('This test needs PHP 8.2'); - } - - $db = TestCaseHelper::getDefaultSharedConnection(); - - $rows = $db->execute_query('SELECT id FROM mysqli_test', [])->fetch_all(MYSQLI_ASSOC); - - foreach ($rows as $row) { - if (function_exists('assertType')) { - assertType('int', $row['id']); - assertType('*ERROR*', $row[0]); - } - - $this->assertSame(['id'], array_keys($row)); - $this->assertIsInt($row['id']); - } - - $rows = $db->execute_query('SELECT id FROM mysqli_test WHERE id = ?', [1])->fetch_all(MYSQLI_ASSOC); - - foreach ($rows as $row) { - if (function_exists('assertType')) { - assertType('int', $row['id']); - assertType('*ERROR*', $row[0]); - } - - $this->assertSame(['id'], array_keys($row)); - $this->assertIsInt($row['id']); - } - } - - // This is not executed. - public function checkExecuteQueryPlaceholderTypeProvider(mysqli $db): void - { - $row = $db->execute_query('SELECT ? val', [rand() ? 1 : 2])->fetch_assoc(); - - if (function_exists('assertType')) { - assertType("'1'|'2'", $row['val']); - } - - $row = $db->execute_query('SELECT ? val', rand() ? [1] : ['a'])->fetch_assoc(); - - if (function_exists('assertType')) { - assertType("'1'|'a'", $row['val']); - } - - $row = $db->execute_query('SELECT ? val', rand() ? [1] : [null])->fetch_assoc(); - - if (function_exists('assertType')) { - assertType("'1'|null", $row['val']); - } - - $row = $db->execute_query('SELECT ? val', [null])->fetch_assoc(); - - if (function_exists('assertType')) { - assertType("null", $row['val']); - } - - $this->expectNotToPerformAssertions(); - } - /** @param string|array $allowedTypes */ private function assertGettype(string|array $allowedTypes, mixed $value): void {