|
| 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\Component\HttpKernel\Tests\EventListener; |
| 13 | + |
| 14 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 15 | +use Symfony\Component\DependencyInjection\Definition; |
| 16 | +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
| 17 | +use Symfony\Component\HttpKernel\EventListener\DumpListener; |
| 18 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 19 | +use Symfony\Component\VarDumper\VarDumper; |
| 20 | + |
| 21 | +/** |
| 22 | + * DumpListenerTest |
| 23 | + * |
| 24 | + * @author Nicolas Grekas <p@tchwork.com> |
| 25 | + */ |
| 26 | +class DumpListenerTest extends \PHPUnit_Framework_TestCase |
| 27 | +{ |
| 28 | + public function testSubscribedEvents() |
| 29 | + { |
| 30 | + $this->assertSame( |
| 31 | + array(KernelEvents::REQUEST => array('configure', 1024)), |
| 32 | + DumpListener::getSubscribedEvents() |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public function testConfigure() |
| 37 | + { |
| 38 | + $prevDumper = $this->getDumpHandler(); |
| 39 | + |
| 40 | + $container = new ContainerBuilder(); |
| 41 | + $container->setDefinition('var_dumper.cloner', new Definition('Symfony\Component\HttpKernel\Tests\EventListener\MockCloner')); |
| 42 | + $container->setDefinition('mock_dumper', new Definition('Symfony\Component\HttpKernel\Tests\EventListener\MockDumper')); |
| 43 | + |
| 44 | + ob_start(); |
| 45 | + $exception = null; |
| 46 | + $listener = new DumpListener($container, 'mock_dumper'); |
| 47 | + |
| 48 | + try { |
| 49 | + $listener->configure(); |
| 50 | + |
| 51 | + $lazyDumper = $this->getDumpHandler(); |
| 52 | + VarDumper::dump('foo'); |
| 53 | + |
| 54 | + $loadedDumper = $this->getDumpHandler(); |
| 55 | + VarDumper::dump('bar'); |
| 56 | + |
| 57 | + $this->assertSame('+foo-+bar-', ob_get_clean()); |
| 58 | + |
| 59 | + $listenerReflector = new \ReflectionClass($listener); |
| 60 | + $lazyReflector = new \ReflectionFunction($lazyDumper); |
| 61 | + $loadedReflector = new \ReflectionFunction($loadedDumper); |
| 62 | + |
| 63 | + $this->assertSame($listenerReflector->getFilename(), $lazyReflector->getFilename()); |
| 64 | + $this->assertSame($listenerReflector->getFilename(), $loadedReflector->getFilename()); |
| 65 | + $this->assertGreaterThan($lazyReflector->getStartLine(), $loadedReflector->getStartLine()); |
| 66 | + } catch (\Exception $exception) { |
| 67 | + } |
| 68 | + |
| 69 | + VarDumper::setHandler($prevDumper); |
| 70 | + |
| 71 | + if (null !== $exception) { |
| 72 | + throw $exception; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private function getDumpHandler() |
| 77 | + { |
| 78 | + $prevDumper = VarDumper::setHandler('var_dump'); |
| 79 | + VarDumper::setHandler($prevDumper ); |
| 80 | + |
| 81 | + return $prevDumper; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +class MockCloner |
| 86 | +{ |
| 87 | + public function cloneVar($var) |
| 88 | + { |
| 89 | + return $var.'-'; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +class MockDumper |
| 94 | +{ |
| 95 | + public function dump($var) |
| 96 | + { |
| 97 | + echo '+'.$var; |
| 98 | + } |
| 99 | +} |
0 commit comments