Skip to content

Commit 081cef3

Browse files
committed
ModelTest is improved to handle and assert error traces during the tests
1 parent 7017759 commit 081cef3

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/Tests/PersistUnit/ModelTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ abstract class ModelTest extends DBUnitTestcase{
2323
private static $_databaseName = "";
2424
protected static $_pdo = null;
2525
private static $_connectionParams = null;
26+
private static $errorLogDir = null;
27+
28+
protected static $enablePersistenceTrace = true;
29+
protected static $enableApplicationTrace = true;
30+
protected static $enableHttpTrace = true;
31+
protected static $enableSystemTrace = true;
32+
2633

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

131138
// start error handling
132139
ErrorHandler::handleError();
140+
141+
// create a temporary error log directory
142+
$errorLogDir = sys_get_temp_dir().'/icircle/accounts/errors/'.microtime(true);
143+
mkdir($errorLogDir,0777,true);
144+
chmod($errorLogDir, 0777);
145+
146+
self::$errorLogDir = $errorLogDir;
133147
}
134148

135149
public static function tearDownAfterClass(){
@@ -145,6 +159,51 @@ public static function tearDownAfterClass(){
145159
}
146160
unset($result);
147161
unset($_pdo);
162+
163+
// delete error log directory
164+
rmdir(self::$errorLogDir);
165+
}
166+
167+
public function setUp(){
168+
parent::setUp();
169+
170+
$errorlogFile = self::$errorLogDir.'/'. md5($this->getName());
171+
172+
$traces = [];
173+
if(self::$enablePersistenceTrace){$traces['Persistence'] = $errorlogFile;}
174+
if(self::$enableApplicationTrace){$traces['Application'] = $errorlogFile;}
175+
if(self::$enableHttpTrace){$traces['Http'] = $errorlogFile;}
176+
if(self::$enableSystemTrace){$traces['System'] = $errorlogFile;}
177+
178+
// create an temporary error log
179+
MockSettings::setSettings('php-platform/errors', 'traces', $traces);
180+
}
181+
182+
public function tearDown(){
183+
parent::tearDown();
184+
// display error log if any
185+
$errorlogFile = self::$errorLogDir.'/'. md5($this->getName());
186+
if(file_exists($errorlogFile)){
187+
echo PHP_EOL.file_get_contents($errorlogFile).PHP_EOL;
188+
unlink($errorlogFile);
189+
}
190+
}
191+
192+
function clearErrorLog(){
193+
$errorlogFile = self::$errorLogDir.'/'. md5($this->getName());
194+
if(file_exists($errorlogFile)){
195+
unlink($errorlogFile);
196+
}
197+
}
198+
199+
function assertContainsAndClearLog($message){
200+
$errorlogFile= self::$errorLogDir.'/'. md5($this->getName());
201+
$log = "";
202+
if(file_exists($errorlogFile)){
203+
$log = file_get_contents($errorlogFile);
204+
}
205+
$this->assertContains($message, $log);
206+
unlink($errorlogFile);
148207
}
149208

150209
public function getSetUpOperation()

0 commit comments

Comments
 (0)