cleanup test data dir before each test
This commit is contained in:
parent
a7bc09c9e9
commit
08b5fe26e3
4 changed files with 39 additions and 30 deletions
|
@ -13,10 +13,11 @@ extensions:
|
|||
enabled:
|
||||
- Codeception\Extension\RunFailed
|
||||
- Codeception\Extension\PhpBuiltinServer
|
||||
- CleanUpExtension
|
||||
config:
|
||||
Codeception\Extension\PhpBuiltinServer:
|
||||
hostname: localhost
|
||||
port: 8000
|
||||
documentRoot:
|
||||
documentRoot:
|
||||
startDelay: 1
|
||||
phpIni: /etc/php5/apache2/php.ini
|
||||
|
|
|
@ -3,30 +3,6 @@
|
|||
|
||||
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')) {
|
||||
mkdir('tests/_tmp');
|
||||
}
|
||||
|
||||
if(is_dir(TEST_DATA_DIR)) {
|
||||
deleteDirRecursively(TEST_DATA_DIR);
|
||||
}
|
||||
|
||||
mkdir(TEST_DATA_DIR);
|
||||
|
|
33
api/tests/_support/CleanUpExtension.php
Normal file
33
api/tests/_support/CleanUpExtension.php
Normal 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);
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -28,20 +28,19 @@ $polls = [
|
|||
"serverExpirationDate" => (clone $date)->add(new DateInterval('P1D'))->format($jsISO8601Format)
|
||||
))
|
||||
];
|
||||
$dataDir = 'tests/_tmp/data/';
|
||||
foreach ($polls as $id => $data) {
|
||||
mkdir($dataDir . $id);
|
||||
file_put_contents($dataDir . $id . '/poll_data', json_encode($data));
|
||||
mkdir(TEST_DATA_DIR . $id);
|
||||
file_put_contents(TEST_DATA_DIR . $id . '/poll_data', json_encode($data));
|
||||
}
|
||||
|
||||
$I = new FunctionalTester($scenario);
|
||||
$I->wantTo('run cron and see expired polls being deleted');
|
||||
$I->runShellCommand('php cron.php tests/_tmp/data/');
|
||||
\PHPUnit_Framework_Assert::assertFalse(
|
||||
is_dir($dataDir . 'expired000'),
|
||||
is_dir(TEST_DATA_DIR . 'expired000'),
|
||||
'expired poll got deleted'
|
||||
);
|
||||
\PHPUnit_Framework_Assert::assertTrue(
|
||||
is_dir($dataDir . 'notExpired'),
|
||||
is_dir(TEST_DATA_DIR . 'notExpired'),
|
||||
'not yet expired poll is still there'
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue