Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cspray
Last active April 5, 2023 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cspray/7559eeedcc3de0e13ae518cf997170a8 to your computer and use it in GitHub Desktop.
Save cspray/7559eeedcc3de0e13ae518cf997170a8 to your computer and use it in GitHub Desktop.
PHPUnit DatabaseTest Example
<?php
use PHPUnit\Framework\TestCase;
class DatabaseTest extends TestCase {
private static $dbConnection;
public static function setUpBeforeClass() : void {
self::$dbConnection = DbConnectionFactory::create();
}
public function setUp() : void {
self::$dbConnection->query('START TRANSACTION');
// load databse records here
}
public function tearDown() : void {
self::$dbConnection->query('ROLLBACK');
}
public static function tearDownAfterClass() : void {
self::$dbConnection->close();
}
public function testSomethingWithTheDatabase() : void {
$subject = new SystemUnderTest(self::$dbConnection);
$subject->doSomething();
// assert database records expected
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment