Skip to content

Commit

Permalink
PhpStan warnings have been resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
batumibiz committed Nov 26, 2024
1 parent cf6bc7e commit e9b7068
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
14 changes: 12 additions & 2 deletions src/SqlDumpLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
class SqlDumpLoader
{
private PDO $pdo;

/**
* @var array<string>
*/
private array $errors = [];

public function __construct(PDO $pdo)
Expand All @@ -32,22 +36,28 @@ public function loadFile(string $file): void

public function hasErrors(): bool
{
return ! empty($this->errors);
return $this->errors !== [];
}

/**
* @return array<string>
*/
public function getErrors(): array
{
return $this->errors;
}

/**
* @return array<string>
*/
private function splitSql(string $sql): array
{
return preg_split('~\([^)]*\)(*SKIP)(*F)|;~', $sql);
}

private function queryDb(string $query): void
{
if (! empty($query)) {
if ($query !== '') {
try {
$this->pdo->query($query);
} catch (PDOException $e) {
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/ConfigLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
11 changes: 2 additions & 9 deletions tests/unit/MysqlTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace MobicmsTest;

use Mobicms\Testutils\MysqlTestCase;
use PDO;
use PHPUnit\Framework\TestCase;

class MysqlTestCaseTest extends TestCase
Expand All @@ -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
Expand All @@ -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);
}
}
19 changes: 9 additions & 10 deletions tests/unit/SqlDumpLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e9b7068

Please sign in to comment.