cleanup test data dir before each test

This commit is contained in:
jelhan 2016-05-18 15:47:08 -07:00
parent a7bc09c9e9
commit 08b5fe26e3
4 changed files with 39 additions and 30 deletions

View file

@ -13,10 +13,11 @@ extensions:
enabled: enabled:
- Codeception\Extension\RunFailed - Codeception\Extension\RunFailed
- Codeception\Extension\PhpBuiltinServer - Codeception\Extension\PhpBuiltinServer
- CleanUpExtension
config: config:
Codeception\Extension\PhpBuiltinServer: Codeception\Extension\PhpBuiltinServer:
hostname: localhost hostname: localhost
port: 8000 port: 8000
documentRoot: documentRoot:
startDelay: 1 startDelay: 1
phpIni: /etc/php5/apache2/php.ini phpIni: /etc/php5/apache2/php.ini

View file

@ -3,30 +3,6 @@
define('TEST_DATA_DIR', 'tests/_tmp/data/'); define('TEST_DATA_DIR', 'tests/_tmp/data/');
function deleteDirRecursively($dir) {
$handle = opendir($dir);
while(false !== ($entry = readdir($handle))) {
if($entry === '.' || $entry === '..') {
continue;
}
if(is_dir($dir . '/' . $entry)) {
$function = __FUNCTION__;
$function($dir .'/' . $entry);
}
else {
unlink($dir . '/' . $entry);
}
}
rmdir($dir);
}
if(!is_dir('tests/_tmp')) { if(!is_dir('tests/_tmp')) {
mkdir('tests/_tmp'); mkdir('tests/_tmp');
} }
if(is_dir(TEST_DATA_DIR)) {
deleteDirRecursively(TEST_DATA_DIR);
}
mkdir(TEST_DATA_DIR);

View file

@ -0,0 +1,33 @@
<?php
class CleanUpExtension extends \Codeception\Extension
{
public static $events = array(
'test.before' => 'beforeTest',
);
public function beforeTest(\Codeception\Event\TestEvent $e) {
if (is_dir(TEST_DATA_DIR)) {
self::deleteDirRecursively(TEST_DATA_DIR);
}
mkdir(TEST_DATA_DIR);
}
private static function deleteDirRecursively($dir) {
$handle = opendir($dir);
while(false !== ($entry = readdir($handle))) {
if($entry === '.' || $entry === '..') {
continue;
}
if(is_dir($dir . '/' . $entry)) {
$method = __METHOD__;
$method($dir .'/' . $entry);
}
else {
unlink($dir . '/' . $entry);
}
}
rmdir($dir);
}
}
?>

View file

@ -28,20 +28,19 @@ $polls = [
"serverExpirationDate" => (clone $date)->add(new DateInterval('P1D'))->format($jsISO8601Format) "serverExpirationDate" => (clone $date)->add(new DateInterval('P1D'))->format($jsISO8601Format)
)) ))
]; ];
$dataDir = 'tests/_tmp/data/';
foreach ($polls as $id => $data) { foreach ($polls as $id => $data) {
mkdir($dataDir . $id); mkdir(TEST_DATA_DIR . $id);
file_put_contents($dataDir . $id . '/poll_data', json_encode($data)); file_put_contents(TEST_DATA_DIR . $id . '/poll_data', json_encode($data));
} }
$I = new FunctionalTester($scenario); $I = new FunctionalTester($scenario);
$I->wantTo('run cron and see expired polls being deleted'); $I->wantTo('run cron and see expired polls being deleted');
$I->runShellCommand('php cron.php tests/_tmp/data/'); $I->runShellCommand('php cron.php tests/_tmp/data/');
\PHPUnit_Framework_Assert::assertFalse( \PHPUnit_Framework_Assert::assertFalse(
is_dir($dataDir . 'expired000'), is_dir(TEST_DATA_DIR . 'expired000'),
'expired poll got deleted' 'expired poll got deleted'
); );
\PHPUnit_Framework_Assert::assertTrue( \PHPUnit_Framework_Assert::assertTrue(
is_dir($dataDir . 'notExpired'), is_dir(TEST_DATA_DIR . 'notExpired'),
'not yet expired poll is still there' 'not yet expired poll is still there'
); );