Skip to content

Commit

Permalink
Cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
8ctopus committed Mar 20, 2023
1 parent 71455ee commit 15f3e59
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 77 deletions.
16 changes: 8 additions & 8 deletions tests/FunctionalTest.php
Expand Up @@ -42,7 +42,7 @@ public function getLogs(Logger\Runtime $logger, bool $deferred = false) : array
);
}

return TestCase::normalizeLogs($lines);
return $lines;
}

public function testUsages() : void
Expand Down Expand Up @@ -90,22 +90,22 @@ public function testUsages() : void

$urgent_logs = $this->getLogs($urgent_logger);

static::assertSame('alert Running out of beers 5 left, recharge: true [type: resource]' . PHP_EOL, $urgent_logs[0]);
$date = date('[Y-m-d H:i:s]');

static::assertSame($date . ' ALERT Running out of beers 5 left, recharge: true [type: resource]' . PHP_EOL, $urgent_logs[0]);

$prefixException = 'Exception: Boo! in ';

static::assertStringStartsWith('critical OMG saw ' . $prefixException, $urgent_logs[1]);
static::assertStringStartsWith($date . ' CRITICAL OMG saw ' . $prefixException, $urgent_logs[1]);

$app_logger->flushDeferredLogs();

$app_logs = $this->getLogs($app_logger, true);

var_dump($app_logs);

static::assertStringStartsWith('critical OMG saw ' . $prefixException, $app_logs[0]);
static::assertStringStartsWith($date . ' CRITICAL OMG saw ' . $prefixException, $app_logs[0]);

static::assertStringStartsWith('error ' . $prefixException, $app_logs[1]);
static::assertStringStartsWith($date . ' ERROR ' . $prefixException, $app_logs[1]);

static::assertSame(['info Something happened -> ["xyz"]' . PHP_EOL], $this->getLogs($debug_logger));
static::assertSame([$date . ' INFO Something happened -> ["xyz"]' . PHP_EOL], $this->getLogs($debug_logger));
}
}
69 changes: 0 additions & 69 deletions tests/Logger/TestCase.php
Expand Up @@ -11,38 +11,13 @@
namespace Apix\Log\tests\Logger;

use Apix\Log\Logger\LoggerInterface;
use Exception;
use stdClass;

abstract class TestCase extends \PHPUnit\Framework\TestCase implements LoggerInterface
{
protected string $dest = 'build/apix-unit-test-logger.log';
protected $logger;

public static function normalizeLogs(array $logs) : array
{
return array_map(function ($line) {
return preg_replace_callback(
'{^\[.+\] (\w+) (.+)?}',
function ($match) {
return strtolower($match[1]) . ' ' . (isset($match[2]) ? $match[2] : null);
},
$line);
}, $logs);
}

/**
* This must return the log messages in order with a simple formatting: "<LOG LEVEL> <MESSAGE>".
*
* Example ->error('Foo') would yield "error Foo"
*
* @return array
*/
public function getLogs() : array
{
return self::normalizeLogs(file($this->dest, FILE_IGNORE_NEW_LINES));
}

public function providerMessagesAndContextes() : array
{
$obj = new stdClass();
Expand Down Expand Up @@ -72,50 +47,6 @@ public function providerMessagesAndContextes() : array
];
}

/**
* @dataProvider providerMessagesAndContextes
*
* @param string $msg
* @param mixed $context
* @param string $exp
*/
public function testMessageWithContext(string $msg, mixed $context, string $exp) : void
{
$this->getLogger()->alert('{' . $msg . '}', [$msg => $context]);

static::assertSame(['alert ' . $exp], $this->getLogs());
}

/**
* @dataProvider providerMessagesAndContextes
*
* @param string $msg
* @param mixed $context
* @param string $exp
*/
public function testContextIsPermutted(string $msg, mixed $context, string $exp) : void
{
$this->getLogger()->notice($context);

static::assertSame(['notice ' . $exp], $this->getLogs());
}

public function testContextIsAnException() : void
{
$this->getLogger()->critical(new Exception('Boo!'));

$logs = $this->getLogs();

$prefix = version_compare(PHP_VERSION, '7.0.0-dev', '>=')
? 'critical Exception: Boo! in '
: "critical exception 'Exception' with message 'Boo!' in ";

static::assertStringStartsWith(
$prefix,
$logs[0]
);
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit 15f3e59

Please sign in to comment.