Skip to content

Commit

Permalink
Merge pull request #37 from Mr-Basirnia/develop
Browse files Browse the repository at this point in the history
♻️ refactor(pdo connection): refactor connection.
  • Loading branch information
Mr-Basirnia authored Oct 21, 2022
2 parents 0b8390f + 9331652 commit 9fd8c98
Showing 1 changed file with 86 additions and 25 deletions.
111 changes: 86 additions & 25 deletions tdd-project/tests/Unit/PdoDatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,121 @@

use App\Contracts\DatabaseConnectionInterface;
use App\Database\PDODatabaseConnection;
use App\Exceptions\ConfigFileNotValidException;
use App\Exceptions\PdoDatabaseException;
use App\Exceptions\{ConfigFileNotFoundException, ConfigFileNotValidException, PdoDatabaseException};
use App\Helpers\Config;
use PDO;
use PHPUnit\Framework\TestCase;

class PdoDatabaseConnectionTest extends TestCase
{
public function testPDOConnectionImplementsDatabaseConnectionInterface()
/**
*
*
* @return void
*
* @throws ConfigFileNotFoundException
* @throws ConfigFileNotValidException
*/
public function testPDOConnectionImplementsDatabaseConnectionInterface(): void
{
$connection = new PDODatabaseConnection($this->config());
$this->assertInstanceOf(DatabaseConnectionInterface::class, $connection);
$this->assertInstanceOf(DatabaseConnectionInterface::class, $this->connection());
}

private function config()
/**
*
*
* @param array $config
* @param string $operation
*
* @return PDODatabaseConnection
*
* @throws ConfigFileNotValidException|ConfigFileNotFoundException
*/
private function connection(array $config = [], string $operation = ''): PDODatabaseConnection
{
return Config::get('database', 'pdo_testing');
return new PDODatabaseConnection($this->config($config, $operation));
}

/**
*
*
* @param array $data
* @param string $operation
*
* @return array|mixed|null
*
* @throws ConfigFileNotFoundException
*/
private function config(array $data = [], string $operation = '')
{
$config = Config::get('database', 'pdo_testing');

switch ($operation) {
case 'merge':
return array_merge($config, $data);
case 'unset':
foreach ($data as $key) {
unset($config[$key]);
}
return $config;
}

return $config;
}

/**
*
*
* @depends testPdoDatabaseConnectionConnectMethodReturnValid
*
* @param $pdo
*
* @return void
* @throws PdoDatabaseException
*/
public function testContentMethodShouldBeInstanceOfPdo($pdo)
public function testContentMethodShouldBeInstanceOfPdo($pdo): void
{
$this->assertInstanceOf(PDO::class, $pdo->getConnection());
}

/**
*
*
* @return PDODatabaseConnection
*
* @throws ConfigFileNotValidException
* @throws PdoDatabaseException|ConfigFileNotFoundException
*/
public function testPdoDatabaseConnectionConnectMethodReturnValid(): PDODatabaseConnection
{
$pdo = new PDODatabaseConnection($this->config());
$this->assertInstanceOf(PDODatabaseConnection::class, $pdo->connect());
return $pdo->connect();
$this->assertInstanceOf(PDODatabaseConnection::class, $this->connection()->connect());
return $this->connection()->connect();
}

public function testPdoDatabaseReturnValidException()
/**
*
*
* @return void
*
* @throws ConfigFileNotValidException
* @throws PdoDatabaseException|ConfigFileNotFoundException
*/
public function testPdoDatabaseReturnValidException(): void
{
$this->expectException(PdoDatabaseException::class);
$config = $this->config();
$config['database'] = md5(rand(9, 9999));
$pdo = new PDODatabaseConnection($config);
$pdo->connect();
$this->connection(['database' => 'Zrn3g22E3Tp'], 'merge')->connect();
}

public function testConfigDatabaseConnectionIsValidKeys()
/**
*
*
* @return void
*
* @throws ConfigFileNotValidException
* @throws PdoDatabaseException|ConfigFileNotFoundException
*/
public function testConfigDatabaseConnectionIsValidKeys(): void
{
$this->expectException(ConfigFileNotValidException::class);

$config = $this->config();
unset($config['db_username']);
unset($config['db_password']);

$pdo = new PDODatabaseConnection($config);
$pdo->connect();
$this->connection(['db_username', 'db_password'], 'unset')->connect();
}
}

0 comments on commit 9fd8c98

Please sign in to comment.