Skip to content

Commit

Permalink
Fix not found activation strategy and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Jul 30, 2013
1 parent dccac21 commit 38bb85b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
11 changes: 9 additions & 2 deletions NotFoundActivationStrategy.php
Expand Up @@ -44,11 +44,18 @@ public function __construct(array $excludedUrls, $actionLevel)

public function isHandlerActivated(array $record)
{
if (parent::isHandlerActivated($record) && $this->request && isset($record['context']['exception']) && $record['context']['exception'] instanceof HttpException && $record['context']['exception']->getStatusCode() == 404) {
$isActivated = parent::isHandlerActivated($record);
if (
$isActivated
&& $this->request
&& isset($record['context']['exception'])
&& $record['context']['exception'] instanceof HttpException
&& $record['context']['exception']->getStatusCode() == 404
) {
return !preg_match($this->blacklist, $this->request->getPathInfo());
}

return false;
return $isActivated;
}

public function setRequest(Request $req = null)
Expand Down
51 changes: 51 additions & 0 deletions Tests/NotFoundActivationStrategyTest.php
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MonologBundle\Tests;

use Symfony\Bundle\MonologBundle\NotFoundActivationStrategy;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Monolog\Logger;

class NotFoundActivationStrategyTest extends TestCase
{
/**
* @dataProvider isActivatedProvider
*/
public function testIsActivated($url, $record, $expected)
{
$strategy = new NotFoundActivationStrategy(array('^/foo', 'bar'), Logger::WARNING);
$strategy->setRequest(Request::create($url));

$this->assertEquals($expected, $strategy->isHandlerActivated($record));
}

public function isActivatedProvider()
{
return array(
array('/test', array('level' => Logger::DEBUG), false),
array('/foo', array('level' => Logger::DEBUG, 'context' => $this->getContextException(404)), false),
array('/baz/bar', array('level' => Logger::ERROR, 'context' => $this->getContextException(404)), false),
array('/foo', array('level' => Logger::ERROR, 'context' => $this->getContextException(404)), false),
array('/foo', array('level' => Logger::ERROR, 'context' => $this->getContextException(500)), true),

array('/test', array('level' => Logger::ERROR), true),
array('/baz', array('level' => Logger::ERROR, 'context' => $this->getContextException(404)), true),
array('/baz', array('level' => Logger::ERROR, 'context' => $this->getContextException(500)), true),
);
}

protected function getContextException($code)
{
return array('exception' => new HttpException($code));
}
}

0 comments on commit 38bb85b

Please sign in to comment.