Skip to content

Commit

Permalink
test(KzNUCYHy): ConnectionException tests for Mysqli & Pdo_Mysql drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
misha-rollun committed Jun 22, 2024
1 parent 2293f79 commit 509ccd7
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
18 changes: 18 additions & 0 deletions config/autoload/db.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,23 @@
'password' => getenv('DB_PASS'),
'hostname' => getenv('DB_HOST'),
'port' => getenv('DB_PORT') ?: 3306,
'adapters' => [
'db.pdo.wrong-connection' => [
'driver' => 'Pdo_Mysql',
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USER'),
'password' => 'wrong',
'hostname' => getenv('DB_HOST'),
'port' => getenv('DB_PORT') ?: 3306,
],
'db.mysqli.wrong-connection' => [
'driver' => 'Mysqli',
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USER'),
'password' => 'wrong',
'hostname' => getenv('DB_HOST'),
'port' => getenv('DB_PORT') ?: 3306,
],
]
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace rollun\test\functional\DataStore\DataStore\ConnectionExceptionTest;

use rollun\datastore\DataStore\ConnectionException;
use rollun\datastore\DataStore\Interfaces\DataStoreInterface;
use rollun\test\functional\FunctionalTestCase;
use Xiag\Rql\Parser\Node\Query\ScalarOperator\EqNode;
use Xiag\Rql\Parser\Query;

abstract class BaseTest extends FunctionalTestCase
{
/**
* Should return dataStore that have problems with connection
*/
abstract protected function getDbTableDataStore(): DataStoreInterface;

public function testCreate(): void
{
$this->expectException(ConnectionException::class);

$this->getDbTableDataStore()->create(['id' => 1, 'value' => 'foo']);
}

public function testMultiCreate(): void
{
$this->expectException(ConnectionException::class);

$this->getDbTableDataStore()->multiCreate([
['id' => 1, 'value' => 'foo'],
['id' => 2, 'value' => 'bar'],
]);
}

public function testUpdate(): void
{
$this->expectException(ConnectionException::class);

$this->getDbTableDataStore()->update(['id' => 1, 'value' => 'foo']);
}

public function testMultiUpdate(): void
{
$updatedRecords = $this->getDbTableDataStore()->multiUpdate([
['id' => 1, 'value' => 'foo'],
['id' => 2, 'value' => 'bar'],
]);

// Any exceptions that are thrown while updating records are simply ignored in this method.
// Therefore, the best we can do is to check that nothing has been updated.
self::assertEmpty($updatedRecords);
}

public function testQueriedUpdate(): void
{
$this->expectException(ConnectionException::class);

$this->getDbTableDataStore()->queriedUpdate(['id' => 1, 'value' => 'foo'], $this->getQuery());
}

public function testRewrite(): void
{
$this->expectException(ConnectionException::class);

$this->getDbTableDataStore()->rewrite(['id' => 1, 'value' => 'foo']);
}

public function testDelete(): void
{
$this->expectException(ConnectionException::class);

$this->getDbTableDataStore()->delete(1);
}

public function testQueriedDelete(): void
{
$this->expectException(ConnectionException::class);

$this->getDbTableDataStore()->queriedDelete($this->getQuery());
}

public function testRead(): void
{
$this->expectException(ConnectionException::class);

$this->getDbTableDataStore()->read(1);
}

public function testHas(): void
{
$this->expectException(ConnectionException::class);

$this->getDbTableDataStore()->has(1);
}

public function testQuery(): void
{
$this->expectException(ConnectionException::class);

$this->getDbTableDataStore()->query($this->getQuery());
}

private function getQuery(): Query
{
$query = new Query();
$query->setQuery(new EqNode('value', 'foo'));
return $query;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace rollun\test\functional\DataStore\DataStore\ConnectionExceptionTest;

use Laminas\Db\TableGateway\TableGateway;
use rollun\datastore\DataStore\DbTable;

class DbTableMysqliDriverDataStoreTest extends BaseTest
{
protected function getDbTableDataStore(): DbTable
{
return new DbTable(
new TableGateway(
table: 'pdo-connection-test',
adapter: $this->getContainer()->get('db.mysqli.wrong-connection')
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace rollun\test\functional\DataStore\DataStore\ConnectionExceptionTest;

use Laminas\Db\TableGateway\TableGateway;
use rollun\datastore\DataStore\DbTable;

class DbTablePdoDriverDataStoreTest extends BaseTest
{
protected function getDbTableDataStore(): DbTable
{
return new DbTable(
new TableGateway(
table: 'pdo-connection-test',
adapter: $this->getContainer()->get('db.pdo.wrong-connection')
)
);
}
}

1 comment on commit 509ccd7

@misha-rollun
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.