Skip to content

Commit

Permalink
fix PHP 8.1 tests - skip mysqli::execute_query
Browse files Browse the repository at this point in the history
  • Loading branch information
schlndh committed Jun 15, 2024
1 parent aefdfa7 commit 92bb88d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 71 deletions.
21 changes: 18 additions & 3 deletions tests/PHPStan/Type/MySQLi/MySQLiTypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,31 @@
use MariaStan\TestCaseHelper;
use PHPStan\Testing\TypeInferenceTestCase;

use const PHP_VERSION_ID;

class MySQLiTypeInferenceTest extends TypeInferenceTestCase
{
/** @return list<string> */
public static function getTestFiles(): array
{
$result = [__DIR__ . '/data/MySQLiTypeInferenceDataTest.php'];

if (PHP_VERSION_ID >= 80200) {
$result[] = __DIR__ . '/data/MySQLiExecuteQueryTypeInferenceDataTest.php';
}

return $result;
}

/** @return iterable<mixed> */
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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class MySQLiTypeInferenceWithFileReflectionTest extends TypeInferenceTestCase
{
/** @return iterable<mixed> */
public function dataFileAsserts(): iterable
public static function dataFileAsserts(): iterable
{
$mysqli = TestCaseHelper::getDefaultSharedConnection();
MySQLiTypeInferenceDataTest::initData($mysqli);
Expand All @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace MariaStan\PHPStan\Type\MySQLi\data;

use MariaStan\TestCaseHelper;
use mysqli;
use PHPUnit\Framework\TestCase;

use function array_keys;
use function function_exists;
use function PHPStan\Testing\assertType;
use function rand;

use const MYSQLI_ASSOC;
use const PHP_VERSION_ID;

class MySQLiExecuteQueryTypeInferenceDataTest extends TestCase
{
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 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();
}
}
65 changes: 0 additions & 65 deletions tests/PHPStan/Type/MySQLi/data/MySQLiTypeInferenceDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<string> $allowedTypes */
private function assertGettype(string|array $allowedTypes, mixed $value): void
{
Expand Down

0 comments on commit 92bb88d

Please sign in to comment.