From e9b7068004e2f85a5e786d4547a274aaea84037c Mon Sep 17 00:00:00 2001 From: Oleg Kasyanov Date: Sun, 17 Nov 2024 16:08:36 +0400 Subject: [PATCH] PhpStan warnings have been resolved --- src/SqlDumpLoader.php | 14 ++++++++++++-- tests/unit/ConfigLoaderTest.php | 20 ++++++++++---------- tests/unit/MysqlTestCaseTest.php | 11 ++--------- tests/unit/SqlDumpLoaderTest.php | 19 +++++++++---------- 4 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/SqlDumpLoader.php b/src/SqlDumpLoader.php index fb615ae..67bc9e2 100644 --- a/src/SqlDumpLoader.php +++ b/src/SqlDumpLoader.php @@ -11,6 +11,10 @@ class SqlDumpLoader { private PDO $pdo; + + /** + * @var array + */ private array $errors = []; public function __construct(PDO $pdo) @@ -32,14 +36,20 @@ public function loadFile(string $file): void public function hasErrors(): bool { - return ! empty($this->errors); + return $this->errors !== []; } + /** + * @return array + */ public function getErrors(): array { return $this->errors; } + /** + * @return array + */ private function splitSql(string $sql): array { return preg_split('~\([^)]*\)(*SKIP)(*F)|;~', $sql); @@ -47,7 +57,7 @@ private function splitSql(string $sql): array private function queryDb(string $query): void { - if (! empty($query)) { + if ($query !== '') { try { $this->pdo->query($query); } catch (PDOException $e) { diff --git a/tests/unit/ConfigLoaderTest.php b/tests/unit/ConfigLoaderTest.php index 7d124c3..1403db1 100644 --- a/tests/unit/ConfigLoaderTest.php +++ b/tests/unit/ConfigLoaderTest.php @@ -13,69 +13,69 @@ public function testHostReturnsGlobalVariable(): void { $GLOBALS['test_db_host'] = 'test_host'; $config = new ConfigLoader(); - $this->assertSame('test_host', $config->host()); + self::assertSame('test_host', $config->host()); } public function testHostReturnsDefaultVariable(): void { unset($GLOBALS['test_db_host']); $config = new ConfigLoader(); - $this->assertSame('localhost', $config->host()); + self::assertSame('localhost', $config->host()); } public function testPortReturnsGlobalVariable(): void { $GLOBALS['test_db_port'] = 9999; $config = new ConfigLoader(); - $this->assertSame(9999, $config->port()); + self::assertSame(9999, $config->port()); } public function testPortReturnsDefaultVariable(): void { unset($GLOBALS['test_db_port']); $config = new ConfigLoader(); - $this->assertSame(3306, $config->port()); + self::assertSame(3306, $config->port()); } public function testUserReturnsGlobalVariable(): void { $GLOBALS['test_db_user'] = 'test_user'; $config = new ConfigLoader(); - $this->assertSame('test_user', $config->user()); + self::assertSame('test_user', $config->user()); } public function testUserReturnsDefaultVariable(): void { unset($GLOBALS['test_db_user']); $config = new ConfigLoader(); - $this->assertSame('root', $config->user()); + self::assertSame('root', $config->user()); } public function testPasswordReturnsGlobalVariable(): void { $GLOBALS['test_db_pass'] = 'test_pass'; $config = new ConfigLoader(); - $this->assertSame('test_pass', $config->password()); + self::assertSame('test_pass', $config->password()); } public function testPasswordReturnsDefaultVariable(): void { unset($GLOBALS['test_db_pass']); $config = new ConfigLoader(); - $this->assertSame('root', $config->password()); + self::assertSame('root', $config->password()); } public function testDbNameReturnsGlobalVariable(): void { $GLOBALS['test_db_name'] = 'test_name'; $config = new ConfigLoader(); - $this->assertSame('test_name', $config->dbName()); + self::assertSame('test_name', $config->dbName()); } public function testDbNameReturnsDefaultVariable(): void { unset($GLOBALS['test_db_name']); $config = new ConfigLoader(); - $this->assertSame('tmp_test_database', $config->dbName()); + self::assertSame('tmp_test_database', $config->dbName()); } } diff --git a/tests/unit/MysqlTestCaseTest.php b/tests/unit/MysqlTestCaseTest.php index 1b7077f..cfa69ba 100644 --- a/tests/unit/MysqlTestCaseTest.php +++ b/tests/unit/MysqlTestCaseTest.php @@ -5,7 +5,6 @@ namespace MobicmsTest; use Mobicms\Testutils\MysqlTestCase; -use PDO; use PHPUnit\Framework\TestCase; class MysqlTestCaseTest extends TestCase @@ -18,19 +17,13 @@ public function setUp(): void MysqlTestCase::setUpBeforeClass(); } - public function testCanGetPdoInstance(): void - { - $pdo = MysqlTestCase::getPdo(); - $this->assertInstanceOf(PDO::class, $pdo); - } - public function testCanCreateTemporaryDatabase(): void { $pdo = MysqlTestCase::getPdo(); $result = $pdo ->query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$this->dbName'") ->rowCount(); - $this->assertEquals(1, $result); + self::assertEquals(1, $result); } public function testCanDestroyTemporaryDatabase(): void @@ -40,6 +33,6 @@ public function testCanDestroyTemporaryDatabase(): void $result = $pdo ->query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$this->dbName'") ->rowCount(); - $this->assertEquals(0, $result); + self::assertEquals(0, $result); } } diff --git a/tests/unit/SqlDumpLoaderTest.php b/tests/unit/SqlDumpLoaderTest.php index 93f9cda..e1531dc 100644 --- a/tests/unit/SqlDumpLoaderTest.php +++ b/tests/unit/SqlDumpLoaderTest.php @@ -34,10 +34,10 @@ public function testLoadSqlFile(): void $this->loader->loadFile(__DIR__ . '/../stubs/test.sql'); $result = $this->pdo->query('SELECT * FROM `test`')->fetchAll(); - $this->assertFalse($this->loader->hasErrors()); - $this->assertEquals('foo;', $result[0][1]); - $this->assertEquals('bar', $result[1][1]); - $this->assertEquals('baz', $result[2][1]); + self::assertFalse($this->loader->hasErrors()); + self::assertEquals('foo;', $result[0][1]); + self::assertEquals('bar', $result[1][1]); + self::assertEquals('baz', $result[2][1]); } public function testThrowExceptionOnMissingSqlFile(): void @@ -46,21 +46,20 @@ public function testThrowExceptionOnMissingSqlFile(): void $this->loader->loadFile('invalid_file'); } - public function testHasErrors() + public function testHasErrors(): void { $this->loader->loadFile(__DIR__ . '/../stubs/test.sql'); - $this->assertFalse($this->loader->hasErrors()); + self::assertFalse($this->loader->hasErrors()); $this->loader->loadFile(__DIR__ . '/../stubs/test_errors.sql'); - $this->assertTrue($this->loader->hasErrors()); + self::assertTrue($this->loader->hasErrors()); } - public function testGetErrors() + public function testGetErrors(): void { $this->loader->loadFile(__DIR__ . '/../stubs/test_errors.sql'); $errors = $this->loader->getErrors(); - $this->assertIsArray($errors); - $this->assertStringContainsString('SQLSTATE[42S02]', $errors[0]); + self::assertStringContainsString('SQLSTATE[42S02]', $errors[0]); } private function connect(): PDO