|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\Bridge\Monolog\Tests\Processor; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Bridge\Monolog\Processor\RouteProcessor; |
| 16 | +use Symfony\Component\HttpFoundation\ParameterBag; |
| 17 | +use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
| 18 | + |
| 19 | +class RouteProcessorTest extends TestCase |
| 20 | +{ |
| 21 | + const TEST_CONTROLLER = 'App\Controller\SomeController::someMethod'; |
| 22 | + const TEST_ROUTE = 'someRouteName'; |
| 23 | + const TEST_PARAMS = array('param1' => 'value1'); |
| 24 | + |
| 25 | + public function testProcessor() |
| 26 | + { |
| 27 | + $processor = new RouteProcessor(); |
| 28 | + $processor->onKernelRequest($this->getFilledRequestEvent()); |
| 29 | + |
| 30 | + $record = $processor(array('extra' => array())); |
| 31 | + |
| 32 | + $this->assertArrayHasKey('route', $record['extra']); |
| 33 | + $this->assertEquals( |
| 34 | + array('controller' => self::TEST_CONTROLLER, 'route' => self::TEST_ROUTE, 'route_params' => self::TEST_PARAMS), |
| 35 | + $record['extra']['route'] |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + public function testProcessorWithoutParams() |
| 40 | + { |
| 41 | + $processor = new RouteProcessor(false); |
| 42 | + $processor->onKernelRequest($this->getFilledRequestEvent()); |
| 43 | + |
| 44 | + $record = $processor(array('extra' => array())); |
| 45 | + |
| 46 | + $this->assertArrayHasKey('route', $record['extra']); |
| 47 | + $this->assertEquals( |
| 48 | + array('controller' => self::TEST_CONTROLLER, 'route' => self::TEST_ROUTE), |
| 49 | + $record['extra']['route'] |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public function testProcessorWithEmptyRequest() |
| 54 | + { |
| 55 | + $processor = new RouteProcessor(); |
| 56 | + $processor->onKernelRequest($this->getEmptyRequestEvent()); |
| 57 | + |
| 58 | + $record = $processor(array('extra' => array())); |
| 59 | + $this->assertEquals(array('extra' => array()), $record); |
| 60 | + } |
| 61 | + |
| 62 | + public function testProcessorDoesNothingWhenNoRequest() |
| 63 | + { |
| 64 | + $processor = new RouteProcessor(); |
| 65 | + |
| 66 | + $record = $processor(array('extra' => array())); |
| 67 | + $this->assertEquals(array('extra' => array()), $record); |
| 68 | + } |
| 69 | + |
| 70 | + private function getRequestEvent(array $attributes): GetResponseEvent |
| 71 | + { |
| 72 | + $request = new \stdClass(); |
| 73 | + $request->attributes = new ParameterBag($attributes); |
| 74 | + |
| 75 | + $event = $this->getMockBuilder(GetResponseEvent::class)->disableOriginalConstructor()->getMock(); |
| 76 | + $event->method('getRequest')->willReturn($request); |
| 77 | + |
| 78 | + return $event; |
| 79 | + } |
| 80 | + |
| 81 | + private function getEmptyRequestEvent(): GetResponseEvent |
| 82 | + { |
| 83 | + return $this->getRequestEvent(array()); |
| 84 | + } |
| 85 | + |
| 86 | + private function getFilledRequestEvent(): GetResponseEvent |
| 87 | + { |
| 88 | + return $this->getRequestEvent( |
| 89 | + array( |
| 90 | + '_controller' => self::TEST_CONTROLLER, |
| 91 | + '_route' => self::TEST_ROUTE, |
| 92 | + '_route_params' => self::TEST_PARAMS, |
| 93 | + ) |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
0 commit comments