Skip to content

Commit

Permalink
ModelTest is improved to handle and assert error traces during the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Raaghu committed Mar 17, 2018
1 parent 7017759 commit 081cef3
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Tests/PersistUnit/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ abstract class ModelTest extends DBUnitTestcase{
private static $_databaseName = "";
protected static $_pdo = null;
private static $_connectionParams = null;
private static $errorLogDir = null;

protected static $enablePersistenceTrace = true;
protected static $enableApplicationTrace = true;
protected static $enableHttpTrace = true;
protected static $enableSystemTrace = true;


/**
* @return string[] of schema sql files to run before tests
Expand Down Expand Up @@ -130,6 +137,13 @@ public static function setUpBeforeClass(){

// start error handling
ErrorHandler::handleError();

// create a temporary error log directory
$errorLogDir = sys_get_temp_dir().'/icircle/accounts/errors/'.microtime(true);
mkdir($errorLogDir,0777,true);
chmod($errorLogDir, 0777);

self::$errorLogDir = $errorLogDir;
}

public static function tearDownAfterClass(){
Expand All @@ -145,6 +159,51 @@ public static function tearDownAfterClass(){
}
unset($result);
unset($_pdo);

// delete error log directory
rmdir(self::$errorLogDir);
}

public function setUp(){
parent::setUp();

$errorlogFile = self::$errorLogDir.'/'. md5($this->getName());

$traces = [];
if(self::$enablePersistenceTrace){$traces['Persistence'] = $errorlogFile;}
if(self::$enableApplicationTrace){$traces['Application'] = $errorlogFile;}
if(self::$enableHttpTrace){$traces['Http'] = $errorlogFile;}
if(self::$enableSystemTrace){$traces['System'] = $errorlogFile;}

// create an temporary error log
MockSettings::setSettings('php-platform/errors', 'traces', $traces);
}

public function tearDown(){
parent::tearDown();
// display error log if any
$errorlogFile = self::$errorLogDir.'/'. md5($this->getName());
if(file_exists($errorlogFile)){
echo PHP_EOL.file_get_contents($errorlogFile).PHP_EOL;
unlink($errorlogFile);
}
}

function clearErrorLog(){
$errorlogFile = self::$errorLogDir.'/'. md5($this->getName());
if(file_exists($errorlogFile)){
unlink($errorlogFile);
}
}

function assertContainsAndClearLog($message){
$errorlogFile= self::$errorLogDir.'/'. md5($this->getName());
$log = "";
if(file_exists($errorlogFile)){
$log = file_get_contents($errorlogFile);
}
$this->assertContains($message, $log);
unlink($errorlogFile);
}

public function getSetUpOperation()
Expand Down

0 comments on commit 081cef3

Please sign in to comment.