File: NotFoundActivationStrategyTest.php

package info (click to toggle)
symfony 7.3.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 148,424 kB
  • sloc: php: 1,510,651; xml: 7,039; javascript: 979; sh: 586; makefile: 242; pascal: 70
file content (56 lines) | stat: -rw-r--r-- 2,323 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?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\Bridge\Monolog\Tests\Handler\FingersCrossed;

use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Level;
use Monolog\LogRecord;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy;
use Symfony\Bridge\Monolog\Tests\RecordFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\HttpException;

class NotFoundActivationStrategyTest extends TestCase
{
    #[\PHPUnit\Framework\Attributes\DataProvider('isActivatedProvider')]
    public function testIsActivated(string $url, array|LogRecord $record, bool $expected)
    {
        $requestStack = new RequestStack();
        $requestStack->push(Request::create($url));

        $strategy = new NotFoundActivationStrategy($requestStack, ['^/foo', 'bar'], new ErrorLevelActivationStrategy(Level::Warning));

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

    public static function isActivatedProvider(): array
    {
        return [
            ['/test',      RecordFactory::create(Level::Debug), false],
            ['/foo',       RecordFactory::create(Level::Debug, context: self::getContextException(404)), false],
            ['/baz/bar',   RecordFactory::create(Level::Error, context: self::getContextException(404)), false],
            ['/foo',       RecordFactory::create(Level::Error, context: self::getContextException(404)), false],
            ['/foo',       RecordFactory::create(Level::Error, context: self::getContextException(500)), true],

            ['/test',      RecordFactory::create(Level::Error), true],
            ['/baz',       RecordFactory::create(Level::Error, context: self::getContextException(404)), true],
            ['/baz',       RecordFactory::create(Level::Error, context: self::getContextException(500)), true],
        ];
    }

    protected static function getContextException(int $code): array
    {
        return ['exception' => new HttpException($code)];
    }
}