AppVariableTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace Symfony\Bridge\Twig\Tests;
  3. use PHPUnit\Framework\TestCase;
  4. use Symfony\Bridge\Twig\AppVariable;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  7. use Symfony\Component\HttpFoundation\Session\Session;
  8. class AppVariableTest extends TestCase
  9. {
  10. /**
  11. * @var AppVariable
  12. */
  13. protected $appVariable;
  14. protected function setUp()
  15. {
  16. $this->appVariable = new AppVariable();
  17. }
  18. /**
  19. * @dataProvider debugDataProvider
  20. */
  21. public function testDebug($debugFlag)
  22. {
  23. $this->appVariable->setDebug($debugFlag);
  24. $this->assertEquals($debugFlag, $this->appVariable->getDebug());
  25. }
  26. public function debugDataProvider()
  27. {
  28. return array(
  29. 'debug on' => array(true),
  30. 'debug off' => array(false),
  31. );
  32. }
  33. public function testEnvironment()
  34. {
  35. $this->appVariable->setEnvironment('dev');
  36. $this->assertEquals('dev', $this->appVariable->getEnvironment());
  37. }
  38. public function testGetSession()
  39. {
  40. $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
  41. $request->method('getSession')->willReturn($session = new Session());
  42. $this->setRequestStack($request);
  43. $this->assertEquals($session, $this->appVariable->getSession());
  44. }
  45. public function testGetSessionWithNoRequest()
  46. {
  47. $this->setRequestStack(null);
  48. $this->assertNull($this->appVariable->getSession());
  49. }
  50. public function testGetRequest()
  51. {
  52. $this->setRequestStack($request = new Request());
  53. $this->assertEquals($request, $this->appVariable->getRequest());
  54. }
  55. public function testGetToken()
  56. {
  57. $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
  58. $this->appVariable->setTokenStorage($tokenStorage);
  59. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  60. $tokenStorage->method('getToken')->willReturn($token);
  61. $this->assertEquals($token, $this->appVariable->getToken());
  62. }
  63. public function testGetUser()
  64. {
  65. $this->setTokenStorage($user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock());
  66. $this->assertEquals($user, $this->appVariable->getUser());
  67. }
  68. public function testGetUserWithUsernameAsTokenUser()
  69. {
  70. $this->setTokenStorage($user = 'username');
  71. $this->assertNull($this->appVariable->getUser());
  72. }
  73. public function testGetTokenWithNoToken()
  74. {
  75. $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
  76. $this->appVariable->setTokenStorage($tokenStorage);
  77. $this->assertNull($this->appVariable->getToken());
  78. }
  79. public function testGetUserWithNoToken()
  80. {
  81. $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
  82. $this->appVariable->setTokenStorage($tokenStorage);
  83. $this->assertNull($this->appVariable->getUser());
  84. }
  85. /**
  86. * @expectedException \RuntimeException
  87. */
  88. public function testEnvironmentNotSet()
  89. {
  90. $this->appVariable->getEnvironment();
  91. }
  92. /**
  93. * @expectedException \RuntimeException
  94. */
  95. public function testDebugNotSet()
  96. {
  97. $this->appVariable->getDebug();
  98. }
  99. /**
  100. * @expectedException \RuntimeException
  101. */
  102. public function testGetTokenWithTokenStorageNotSet()
  103. {
  104. $this->appVariable->getToken();
  105. }
  106. /**
  107. * @expectedException \RuntimeException
  108. */
  109. public function testGetUserWithTokenStorageNotSet()
  110. {
  111. $this->appVariable->getUser();
  112. }
  113. /**
  114. * @expectedException \RuntimeException
  115. */
  116. public function testGetRequestWithRequestStackNotSet()
  117. {
  118. $this->appVariable->getRequest();
  119. }
  120. /**
  121. * @expectedException \RuntimeException
  122. */
  123. public function testGetSessionWithRequestStackNotSet()
  124. {
  125. $this->appVariable->getSession();
  126. }
  127. public function testGetFlashesWithNoRequest()
  128. {
  129. $this->setRequestStack(null);
  130. $this->assertEquals(array(), $this->appVariable->getFlashes());
  131. }
  132. public function testGetFlashesWithNoSessionStarted()
  133. {
  134. $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
  135. $request->method('getSession')->willReturn(new Session());
  136. $this->setRequestStack($request);
  137. $this->assertEquals(array(), $this->appVariable->getFlashes());
  138. }
  139. public function testGetFlashes()
  140. {
  141. $flashMessages = $this->setFlashMessages();
  142. $this->assertEquals($flashMessages, $this->appVariable->getFlashes(null));
  143. $flashMessages = $this->setFlashMessages();
  144. $this->assertEquals($flashMessages, $this->appVariable->getFlashes(''));
  145. $flashMessages = $this->setFlashMessages();
  146. $this->assertEquals($flashMessages, $this->appVariable->getFlashes(array()));
  147. $flashMessages = $this->setFlashMessages();
  148. $this->assertEquals(array(), $this->appVariable->getFlashes('this-does-not-exist'));
  149. $flashMessages = $this->setFlashMessages();
  150. $this->assertEquals(
  151. array('this-does-not-exist' => array()),
  152. $this->appVariable->getFlashes(array('this-does-not-exist'))
  153. );
  154. $flashMessages = $this->setFlashMessages();
  155. $this->assertEquals($flashMessages['notice'], $this->appVariable->getFlashes('notice'));
  156. $flashMessages = $this->setFlashMessages();
  157. $this->assertEquals(
  158. array('notice' => $flashMessages['notice']),
  159. $this->appVariable->getFlashes(array('notice'))
  160. );
  161. $flashMessages = $this->setFlashMessages();
  162. $this->assertEquals(
  163. array('notice' => $flashMessages['notice'], 'this-does-not-exist' => array()),
  164. $this->appVariable->getFlashes(array('notice', 'this-does-not-exist'))
  165. );
  166. $flashMessages = $this->setFlashMessages();
  167. $this->assertEquals(
  168. array('notice' => $flashMessages['notice'], 'error' => $flashMessages['error']),
  169. $this->appVariable->getFlashes(array('notice', 'error'))
  170. );
  171. $this->assertEquals(
  172. array('warning' => $flashMessages['warning']),
  173. $this->appVariable->getFlashes(array('warning')),
  174. 'After getting some flash types (e.g. "notice" and "error"), the rest of flash messages must remain (e.g. "warning").'
  175. );
  176. $this->assertEquals(
  177. array('this-does-not-exist' => array()),
  178. $this->appVariable->getFlashes(array('this-does-not-exist'))
  179. );
  180. }
  181. protected function setRequestStack($request)
  182. {
  183. $requestStackMock = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
  184. $requestStackMock->method('getCurrentRequest')->willReturn($request);
  185. $this->appVariable->setRequestStack($requestStackMock);
  186. }
  187. protected function setTokenStorage($user)
  188. {
  189. $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
  190. $this->appVariable->setTokenStorage($tokenStorage);
  191. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  192. $tokenStorage->method('getToken')->willReturn($token);
  193. $token->method('getUser')->willReturn($user);
  194. }
  195. private function setFlashMessages()
  196. {
  197. $flashMessages = array(
  198. 'notice' => array('Notice #1 message'),
  199. 'warning' => array('Warning #1 message'),
  200. 'error' => array('Error #1 message', 'Error #2 message'),
  201. );
  202. $flashBag = new FlashBag();
  203. $flashBag->initialize($flashMessages);
  204. $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->getMock();
  205. $session->method('isStarted')->willReturn(true);
  206. $session->method('getFlashBag')->willReturn($flashBag);
  207. $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
  208. $request->method('getSession')->willReturn($session);
  209. $this->setRequestStack($request);
  210. return $flashMessages;
  211. }
  212. }