Commit 880729b1 authored by Igor Wiedler's avatar Igor Wiedler

[MonologExtension] initial test case, Monolog submodule, minor adjustments to ease testability

parent 29242300
......@@ -31,3 +31,6 @@
[submodule "vendor/twig"]
path = vendor/twig
url = https://github.com/fabpot/Twig
[submodule "vendor/monolog"]
path = vendor/monolog
url = https://github.com/Seldaek/monolog.git
......@@ -32,10 +32,19 @@ class MonologExtension implements ExtensionInterface
});
$app['monolog.configure'] = $app->protect(function ($log) use ($app) {
$level = isset($app['monolog.level']) ? $app['monolog.level'] : Logger::DEBUG;
$log->pushHandler(new StreamHandler($app['monolog.logfile'], $level));
$log->pushHandler($app['monolog.handler']);
});
$app['monolog.handler'] = function() use ($app) {
return new StreamHandler($app['monolog.logfile'], $app['monolog.level']);
};
if (!isset($app['monolog.level'])) {
$app['monolog.level'] = function() {
return Logger::DEBUG;
};
}
if (isset($app['monolog.class_path'])) {
$app['autoloader']->registerNamespace('Monolog', $app['monolog.class_path']);
}
......
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests;
use Monolog\Handler\TestHandler;
use Silex\Application;
use Silex\Extension\MonologExtension;
use Symfony\Component\HttpFoundation\Request;
/**
* MonologExtension test cases.
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class MonologExtensionTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!is_dir(__DIR__.'/../../../../vendor/monolog/src')) {
$this->markTestSkipped('Monolog submodule was not installed.');
}
}
public function testRegisterAndRender()
{
$app = new Application();
$app->register(new MonologExtension(), array(
'monolog.class_path' => __DIR__.'/../../../../vendor/monolog/src',
));
$app['monolog.handler'] = $app->share(function() use ($app) {
return new TestHandler($app['monolog.level']);
});
$app->get('/log', function() use ($app) {
$app['monolog']->addDebug('logging a message');
});
$app->get('/error', function() {
throw new \RuntimeException('very bad error');
});
$app->error(function(\Exception $e) {
return 'error handled';
});
$this->assertFalse($app['monolog.handler']->hasDebugRecords());
$this->assertFalse($app['monolog.handler']->hasErrorRecords());
$request = Request::create('/log');
$app->handle($request);
$request = Request::create('/error');
$app->handle($request);
$this->assertTrue($app['monolog.handler']->hasDebugRecords());
$this->assertTrue($app['monolog.handler']->hasErrorRecords());
}
}
Subproject commit b90eddd5130c4ca5238cb497a4a743a5145a509d
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment