SessionHelperTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\Helper;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpFoundation\Session\Session;
  16. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  17. class SessionHelperTest extends TestCase
  18. {
  19. protected $requestStack;
  20. protected function setUp()
  21. {
  22. $request = new Request();
  23. $session = new Session(new MockArraySessionStorage());
  24. $session->set('foobar', 'bar');
  25. $session->getFlashBag()->set('notice', 'bar');
  26. $request->setSession($session);
  27. $this->requestStack = new RequestStack();
  28. $this->requestStack->push($request);
  29. }
  30. protected function tearDown()
  31. {
  32. $this->requestStack = null;
  33. }
  34. public function testFlash()
  35. {
  36. $helper = new SessionHelper($this->requestStack);
  37. $this->assertTrue($helper->hasFlash('notice'));
  38. $this->assertEquals(array('bar'), $helper->getFlash('notice'));
  39. }
  40. public function testGetFlashes()
  41. {
  42. $helper = new SessionHelper($this->requestStack);
  43. $this->assertEquals(array('notice' => array('bar')), $helper->getFlashes());
  44. }
  45. public function testGet()
  46. {
  47. $helper = new SessionHelper($this->requestStack);
  48. $this->assertEquals('bar', $helper->get('foobar'));
  49. $this->assertEquals('foo', $helper->get('bar', 'foo'));
  50. $this->assertNull($helper->get('foo'));
  51. }
  52. public function testGetName()
  53. {
  54. $helper = new SessionHelper($this->requestStack);
  55. $this->assertEquals('session', $helper->getName());
  56. }
  57. }