diff --git a/tdd-project/tests/Unit/PdoDatabaseConnectionTest.php b/tdd-project/tests/Unit/PdoDatabaseConnectionTest.php index 60b5283..fe9f43f 100644 --- a/tdd-project/tests/Unit/PdoDatabaseConnectionTest.php +++ b/tdd-project/tests/Unit/PdoDatabaseConnectionTest.php @@ -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(); } }