Skip to content

Commit 38bb85b

Browse files
committed
Fix not found activation strategy and add tests
1 parent dccac21 commit 38bb85b

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

NotFoundActivationStrategy.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,18 @@ public function __construct(array $excludedUrls, $actionLevel)
4444

4545
public function isHandlerActivated(array $record)
4646
{
47-
if (parent::isHandlerActivated($record) && $this->request && isset($record['context']['exception']) && $record['context']['exception'] instanceof HttpException && $record['context']['exception']->getStatusCode() == 404) {
47+
$isActivated = parent::isHandlerActivated($record);
48+
if (
49+
$isActivated
50+
&& $this->request
51+
&& isset($record['context']['exception'])
52+
&& $record['context']['exception'] instanceof HttpException
53+
&& $record['context']['exception']->getStatusCode() == 404
54+
) {
4855
return !preg_match($this->blacklist, $this->request->getPathInfo());
4956
}
5057

51-
return false;
58+
return $isActivated;
5259
}
5360

5461
public function setRequest(Request $req = null)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MonologBundle\Tests;
13+
14+
use Symfony\Bundle\MonologBundle\NotFoundActivationStrategy;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpKernel\Exception\HttpException;
17+
use Monolog\Logger;
18+
19+
class NotFoundActivationStrategyTest extends TestCase
20+
{
21+
/**
22+
* @dataProvider isActivatedProvider
23+
*/
24+
public function testIsActivated($url, $record, $expected)
25+
{
26+
$strategy = new NotFoundActivationStrategy(array('^/foo', 'bar'), Logger::WARNING);
27+
$strategy->setRequest(Request::create($url));
28+
29+
$this->assertEquals($expected, $strategy->isHandlerActivated($record));
30+
}
31+
32+
public function isActivatedProvider()
33+
{
34+
return array(
35+
array('/test', array('level' => Logger::DEBUG), false),
36+
array('/foo', array('level' => Logger::DEBUG, 'context' => $this->getContextException(404)), false),
37+
array('/baz/bar', array('level' => Logger::ERROR, 'context' => $this->getContextException(404)), false),
38+
array('/foo', array('level' => Logger::ERROR, 'context' => $this->getContextException(404)), false),
39+
array('/foo', array('level' => Logger::ERROR, 'context' => $this->getContextException(500)), true),
40+
41+
array('/test', array('level' => Logger::ERROR), true),
42+
array('/baz', array('level' => Logger::ERROR, 'context' => $this->getContextException(404)), true),
43+
array('/baz', array('level' => Logger::ERROR, 'context' => $this->getContextException(500)), true),
44+
);
45+
}
46+
47+
protected function getContextException($code)
48+
{
49+
return array('exception' => new HttpException($code));
50+
}
51+
}

0 commit comments

Comments
 (0)