-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
db now supports the .env instead of hardcoded db connections
- Loading branch information
1 parent
beee7a3
commit 6563d94
Showing
1 changed file
with
13 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
<?php | ||
|
||
namespace DB\Tests; | ||
|
||
use DB\DB; | ||
use PDO; | ||
use Dotenv\Dotenv; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DBTest extends TestCase | ||
{ | ||
private DB $db; | ||
private PDO $pdo; | ||
|
||
protected function setUp(): void | ||
{ | ||
// Connect to MySQL server (replace with your actual credentials) | ||
$this->pdo = new PDO("mysql:host=localhost", "root", ""); | ||
// Load environment variables from .env file | ||
$dotenv = Dotenv::createImmutable(__DIR__ . '/..'); // Adjust the path as needed | ||
$dotenv->load(); | ||
|
||
$this->pdo = new \PDO("mysql:host=". $_ENV['DB_HOST'], $_ENV['DB_USERNAME'], $_ENV['DB_PASSWORD']); | ||
|
||
// Create database | ||
$this->pdo->exec("CREATE DATABASE IF NOT EXISTS test_database"); | ||
$this->pdo->exec("USE test_database"); | ||
|
||
|
@@ -32,14 +31,17 @@ protected function setUp(): void | |
$stmt->execute(["name" => "John Doe", "email" => "[email protected]"]); | ||
$stmt->execute(["name" => "Jane Doe", "email" => "[email protected]"]); | ||
|
||
|
||
// Instantiate DB object | ||
$this->db = new DB('localhost', 'root', '', 'test_database'); | ||
$this->db = new DB($_ENV['DB_HOST'], $_ENV['DB_USERNAME'], $_ENV['DB_PASSWORD'], $_ENV['DB_DATABASE']); | ||
|
||
|
||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
// Drop the test database after each test | ||
$this->pdo->exec("DROP DATABASE IF EXISTS test_database"); | ||
// Close DB connection after each test | ||
$this->db->close(); | ||
} | ||
|
||
public function testQuery(): void | ||
|
@@ -61,8 +63,7 @@ public function testAll(): void | |
$result = $this->db->query("SELECT * FROM users"); | ||
$rows = $result->all(); | ||
$this->assertIsArray($rows); | ||
$this->assertCount(2, $rows); // Assuming 2 rows were inserted | ||
$this->assertNotEmpty($rows); | ||
} | ||
|
||
// Add more test cases as needed | ||
} |