Skip to content

Commit

Permalink
tests: TEMP_DIR replaced with getTempDir()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 18, 2019
1 parent d97208b commit dd17b8e
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 31 deletions.
4 changes: 2 additions & 2 deletions tests/Tracy/Debugger.log().error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Assert::exception(function () {


// no error
Debugger::$logDirectory = TEMP_DIR;
Debugger::$logDirectory = getTempDir();
Debugger::log('Hello');


Debugger::$logDirectory = TEMP_DIR . '/unknown';
Debugger::$logDirectory = getTempDir() . '/unknown';
Assert::exception(function () {
Debugger::log('Hello');
}, 'RuntimeException', "Logging directory '%a%' is not found or is not directory.");
6 changes: 3 additions & 3 deletions tests/Tracy/Debugger.log().samefile.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require __DIR__ . '/../bootstrap.php';


// Setup environment
Debugger::$logDirectory = TEMP_DIR;
Debugger::$logDirectory = getTempDir();


function foo($fp)
Expand All @@ -24,9 +24,9 @@ function foo($fp)


for ($i = 0; $i < 3; $i++) {
$path = TEMP_DIR . "/$i";
$path = getTempDir() . "/$i";
try {
$files[] = $file = fopen(TEMP_DIR . "/$i", 'w');
$files[] = $file = fopen(getTempDir() . "/$i", 'w');
foo($file);
} catch (Exception $e) {
$name[] = Debugger::log($e);
Expand Down
6 changes: 3 additions & 3 deletions tests/Tracy/Debugger.logSeverity.E_NOTICE.development.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ require __DIR__ . '/../bootstrap.php';


// Setup environment
Debugger::enable(Debugger::DEVELOPMENT, TEMP_DIR);
Debugger::enable(Debugger::DEVELOPMENT, getTempDir());
Debugger::$logSeverity = E_NOTICE;

$variable = $missingVariable;

Assert::count(0, glob(TEMP_DIR . '/exception*.html'));
Assert::count(0, glob(TEMP_DIR . '/error.log'));
Assert::count(0, glob(getTempDir() . '/exception*.html'));
Assert::count(0, glob(getTempDir() . '/error.log'));
6 changes: 3 additions & 3 deletions tests/Tracy/Debugger.logSeverity.E_NOTICE.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ require __DIR__ . '/../bootstrap.php';


// Setup environment
Debugger::enable(Debugger::PRODUCTION, TEMP_DIR);
Debugger::enable(Debugger::PRODUCTION, getTempDir());
Debugger::$logSeverity = E_NOTICE;

$variable = $missingVariable;

Assert::count(1, glob(TEMP_DIR . '/error*.html'));
Assert::count(1, glob(TEMP_DIR . '/error.log'));
Assert::count(1, glob(getTempDir() . '/error*.html'));
Assert::count(1, glob(getTempDir() . '/error.log'));
2 changes: 1 addition & 1 deletion tests/Tracy/Debugger.logging.error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require __DIR__ . '/../bootstrap.php';
// Setup environment
$_SERVER['HTTP_HOST'] = 'nette.org';

Debugger::$logDirectory = TEMP_DIR;
Debugger::$logDirectory = getTempDir();

Debugger::getLogger()->mailer = function () {};

Expand Down
2 changes: 1 addition & 1 deletion tests/Tracy/Debugger.logging.warnings.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require __DIR__ . '/../bootstrap.php';
// Setup environment
$_SERVER['HTTP_HOST'] = 'nette.org';

$logDirectory = TEMP_DIR;
$logDirectory = getTempDir();

Debugger::getLogger()->mailer = function () {};

Expand Down
4 changes: 2 additions & 2 deletions tests/Tracy/Logger.extensible.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CustomLogger extends Logger


test(function () {
$logger = new CustomLogger(TEMP_DIR);
$logger = new CustomLogger(getTempDir());
$logger->log(new Exception('First'), 'a');

Assert::match('a', $logger->collector[0][0]);
Expand All @@ -47,7 +47,7 @@ test(function () {
});

test(function () {
$logger = new CustomLogger(TEMP_DIR);
$logger = new CustomLogger(getTempDir());
$logger->log('message', 'b');

Assert::match('b', $logger->collector[0][0]);
Expand Down
8 changes: 4 additions & 4 deletions tests/Tracy/Logger.log().errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ use Tracy\Logger;
require __DIR__ . '/../bootstrap.php';


$logger = new Logger(TEMP_DIR);
$logger = new Logger(getTempDir());
$logger->log('Hello'); // no error


Assert::exception(function () {
$logger = new Logger(TEMP_DIR . '/unknown');
$logger = new Logger(getTempDir() . '/unknown');
$logger->log('Hello');
}, 'RuntimeException', "Logging directory '%a%' is not found or is not directory.");


Assert::exception(function () {
$logger = new Logger(TEMP_DIR);
mkdir(TEMP_DIR . '/test.log');
$logger = new Logger(getTempDir());
mkdir(getTempDir() . '/test.log');
$logger->log('Hello', 'test');
}, 'RuntimeException', "Unable to write to log file '%a%'. Is directory writable?");
12 changes: 6 additions & 6 deletions tests/Tracy/Logger.log().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require __DIR__ . '/../bootstrap.php';

test(function () {
$e = new Exception('First');
$logger = new Logger(TEMP_DIR);
$logger = new Logger(getTempDir());
$logger->log($e, 'a');
Assert::match('[%a%] Exception: First in %a%:%d% %A%', file_get_contents($logger->directory . '/a.log'));
});
Expand All @@ -24,31 +24,31 @@ test(function () {
$e = new Exception('First');
$e = new InvalidArgumentException('Second', 0, $e);
$e = new RuntimeException('Third', 0, $e);
$logger = new Logger(TEMP_DIR);
$logger = new Logger(getTempDir());
$logger->log($e, 'b');
Assert::match('[%a%] RuntimeException: Third in %a%:%d% caused by InvalidArgumentException: Second in %a%:%d% caused by Exception: First in %a%:%d% %A%', file_get_contents($logger->directory . '/b.log'));
});

test(function () {
$logger = new Logger(TEMP_DIR);
$logger = new Logger(getTempDir());
$logger->log(new ErrorException('Msg', 0, E_ERROR, __FILE__, __LINE__), 'c');
Assert::match('[%a%] Fatal Error: Msg in %a%Logger.log().phpt:%d% @ CLI (PID: %d%): %a% @@ c-%a%.html', file_get_contents($logger->directory . '/c.log'));
});

test(function () {
$logger = new Logger(TEMP_DIR);
$logger = new Logger(getTempDir());
$logger->log(new ErrorException('Msg', 0, E_WARNING, __FILE__, __LINE__), 'd');
Assert::match('[%a%] Warning: Msg in %a%Logger.log().phpt:%d% @ CLI (PID: %d%): %a% @@ d-%a%.html', file_get_contents($logger->directory . '/d.log'));
});

test(function () {
$logger = new Logger(TEMP_DIR);
$logger = new Logger(getTempDir());
$logger->log(new ErrorException('Msg', 0, E_COMPILE_ERROR, __FILE__, __LINE__), 'e');
Assert::match('[%a%] Compile Error: Msg in %a%Logger.log().phpt:%d% @ CLI (PID: %d%): %a% @@ e-%a%.html', file_get_contents($logger->directory . '/e.log'));
});

test(function () {
$logger = new Logger(TEMP_DIR);
$logger = new Logger(getTempDir());
$logger->log(new ErrorException('Msg', 0, E_NOTICE, __FILE__, __LINE__), 'f');
Assert::match('[%a%] Notice: Msg in %a%Logger.log().phpt:%d% @ CLI (PID: %d%): %a% @@ f-%a%.html', file_get_contents($logger->directory . '/f.log'));
});
28 changes: 22 additions & 6 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,35 @@
date_default_timezone_set('Europe/Prague');
ini_set('serialize_precision', '14');
$_GET = $_POST = $_COOKIE = [];


// create temporary directory
define('TEMP_DIR', __DIR__ . '/tmp/' . getmypid());
@mkdir(dirname(TEMP_DIR)); // @ - directory may already exist
Tester\Helpers::purge(TEMP_DIR);
define('USER_CONST', 1);


if (extension_loaded('xdebug')) {
xdebug_disable();
}


function getTempDir(): string
{
$dir = __DIR__ . '/tmp/' . getmypid();

if (empty($GLOBALS['\\lock'])) {
// garbage collector
$GLOBALS['\\lock'] = $lock = fopen(__DIR__ . '/lock', 'w');
if (rand(0, 100)) {
flock($lock, LOCK_SH);
@mkdir(dirname($dir));
} elseif (flock($lock, LOCK_EX)) {
Tester\Helpers::purge(dirname($dir));
}

@mkdir($dir);
}

return $dir;
}


function test(\Closure $function): void
{
$function();
Expand Down

0 comments on commit dd17b8e

Please sign in to comment.