PhpEngineTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
  11. use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
  12. use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine;
  13. use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
  14. use Symfony\Component\DependencyInjection\Container;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\HttpFoundation\Session\Session;
  18. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  19. use Symfony\Component\Templating\TemplateNameParser;
  20. class PhpEngineTest extends TestCase
  21. {
  22. public function testEvaluateAddsAppGlobal()
  23. {
  24. $container = $this->getContainer();
  25. $loader = $this->getMockForAbstractClass('Symfony\Component\Templating\Loader\Loader');
  26. $engine = new PhpEngine(new TemplateNameParser(), $container, $loader, $app = new GlobalVariables($container));
  27. $globals = $engine->getGlobals();
  28. $this->assertSame($app, $globals['app']);
  29. }
  30. public function testEvaluateWithoutAvailableRequest()
  31. {
  32. $container = new Container();
  33. $loader = $this->getMockForAbstractClass('Symfony\Component\Templating\Loader\Loader');
  34. $engine = new PhpEngine(new TemplateNameParser(), $container, $loader, new GlobalVariables($container));
  35. $container->set('request_stack', null);
  36. $globals = $engine->getGlobals();
  37. $this->assertEmpty($globals['app']->getRequest());
  38. }
  39. /**
  40. * @expectedException \InvalidArgumentException
  41. */
  42. public function testGetInvalidHelper()
  43. {
  44. $container = $this->getContainer();
  45. $loader = $this->getMockForAbstractClass('Symfony\Component\Templating\Loader\Loader');
  46. $engine = new PhpEngine(new TemplateNameParser(), $container, $loader);
  47. $engine->get('non-existing-helper');
  48. }
  49. /**
  50. * Creates a Container with a Session-containing Request service.
  51. *
  52. * @return Container
  53. */
  54. protected function getContainer()
  55. {
  56. $container = new Container();
  57. $session = new Session(new MockArraySessionStorage());
  58. $request = new Request();
  59. $stack = new RequestStack();
  60. $stack->push($request);
  61. $request->setSession($session);
  62. $container->set('request_stack', $stack);
  63. return $container;
  64. }
  65. }