From 08b5fe26e3a34e8229e9edcf624fafa8f2763ec3 Mon Sep 17 00:00:00 2001 From: jelhan Date: Wed, 18 May 2016 15:47:08 -0700 Subject: [PATCH] cleanup test data dir before each test --- api/codeception.yml | 3 ++- api/tests/_bootstrap.php | 24 ------------------ api/tests/_support/CleanUpExtension.php | 33 +++++++++++++++++++++++++ api/tests/functional/CronCept.php | 9 +++---- 4 files changed, 39 insertions(+), 30 deletions(-) create mode 100644 api/tests/_support/CleanUpExtension.php diff --git a/api/codeception.yml b/api/codeception.yml index 9815a1f..fe2d0e1 100644 --- a/api/codeception.yml +++ b/api/codeception.yml @@ -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 diff --git a/api/tests/_bootstrap.php b/api/tests/_bootstrap.php index 22c5bd4..d6eacad 100644 --- a/api/tests/_bootstrap.php +++ b/api/tests/_bootstrap.php @@ -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); diff --git a/api/tests/_support/CleanUpExtension.php b/api/tests/_support/CleanUpExtension.php new file mode 100644 index 0000000..ce6e839 --- /dev/null +++ b/api/tests/_support/CleanUpExtension.php @@ -0,0 +1,33 @@ + '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); + } +} +?> diff --git a/api/tests/functional/CronCept.php b/api/tests/functional/CronCept.php index 0f6774c..5ab3f8e 100644 --- a/api/tests/functional/CronCept.php +++ b/api/tests/functional/CronCept.php @@ -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' );