Skip to content

Commit

Permalink
db now supports the .env instead of hardcoded db connections
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWasimAbbasi committed Mar 16, 2024
1 parent beee7a3 commit 6563d94
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions tests/DBTest.php
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");

Expand All @@ -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
Expand All @@ -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
}

0 comments on commit 6563d94

Please sign in to comment.