/lib/vendor/symfony-1.4.14/test/unit/controller/sfWebControllerTest.php

https://github.com/yuya-takeyama/symfony-hackathon-20110924 · PHP · 214 lines · 187 code · 15 blank · 12 comment · 0 complexity · f173e98cf845a9cdd15c920b42284caf MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
  10. require_once($_test_dir.'/unit/sfContextMock.class.php');
  11. require_once($_test_dir.'/unit/sfNoRouting.class.php');
  12. $t = new lime_test(21);
  13. class myWebResponse extends sfWebResponse
  14. {
  15. public function sendHttpHeaders()
  16. {
  17. }
  18. public function send()
  19. {
  20. }
  21. }
  22. $_SERVER['HTTP_HOST'] = 'localhost';
  23. $_SERVER['SCRIPT_NAME'] = '/index.php';
  24. sfConfig::set('sf_max_forwards', 10);
  25. $context = sfContext::getInstance(array(
  26. 'routing' => 'sfNoRouting',
  27. 'request' => 'sfWebRequest',
  28. 'response' => 'myWebResponse',
  29. ));
  30. $controller = new sfFrontWebController($context, null);
  31. $tests = array(
  32. 'module/action' => array(
  33. '',
  34. array(
  35. 'module' => 'module',
  36. 'action' => 'action',
  37. ),
  38. ),
  39. 'module/action?id=12' => array(
  40. '',
  41. array(
  42. 'module' => 'module',
  43. 'action' => 'action',
  44. 'id' => 12,
  45. ),
  46. ),
  47. 'module/action?id=12&' => array(
  48. '',
  49. array(
  50. 'module' => 'module',
  51. 'action' => 'action',
  52. 'id' => '12&',
  53. ),
  54. ),
  55. 'module/action?id=12&test=4&toto=9' => array(
  56. '',
  57. array(
  58. 'module' => 'module',
  59. 'action' => 'action',
  60. 'id' => 12,
  61. 'test' => 4,
  62. 'toto' => 9,
  63. ),
  64. ),
  65. 'module/action?id=12&test=4&5&6&7&&toto=9' => array(
  66. '',
  67. array(
  68. 'module' => 'module',
  69. 'action' => 'action',
  70. 'id' => 12,
  71. 'test' => '4&5&6&7&',
  72. 'toto' => 9,
  73. ),
  74. ),
  75. 'module/action?test=value1&value2&toto=9' => array(
  76. '',
  77. array(
  78. 'module' => 'module',
  79. 'action' => 'action',
  80. 'test' => 'value1&value2',
  81. 'toto' => 9,
  82. ),
  83. ),
  84. 'module/action?test=value1&value2' => array(
  85. '',
  86. array(
  87. 'module' => 'module',
  88. 'action' => 'action',
  89. 'test' => 'value1&value2',
  90. ),
  91. ),
  92. 'module/action?test=value1=value2&toto=9' => array(
  93. '',
  94. array(
  95. 'module' => 'module',
  96. 'action' => 'action',
  97. 'test' => 'value1=value2',
  98. 'toto' => 9,
  99. ),
  100. ),
  101. 'module/action?test=value1=value2' => array(
  102. '',
  103. array(
  104. 'module' => 'module',
  105. 'action' => 'action',
  106. 'test' => 'value1=value2',
  107. ),
  108. ),
  109. 'module/action?test=4&5&6&7&&toto=9&id=' => array(
  110. '',
  111. array(
  112. 'module' => 'module',
  113. 'action' => 'action',
  114. 'test' => '4&5&6&7&',
  115. 'toto' => 9,
  116. 'id' => '',
  117. ),
  118. ),
  119. '@test?test=4' => array(
  120. 'test',
  121. array(
  122. 'test' => 4
  123. ),
  124. ),
  125. '@test' => array(
  126. 'test',
  127. array(
  128. ),
  129. ),
  130. '@test?id=12&foo=bar' => array(
  131. 'test',
  132. array(
  133. 'id' => 12,
  134. 'foo' => 'bar',
  135. ),
  136. ),
  137. '@test?id=foo%26bar&foo=bar%3Dfoo' => array(
  138. 'test',
  139. array(
  140. 'id' => 'foo&bar',
  141. 'foo' => 'bar=foo',
  142. ),
  143. ),
  144. );
  145. // ->convertUrlStringToParameters()
  146. $t->diag('->convertUrlStringToParameters()');
  147. foreach ($tests as $url => $result)
  148. {
  149. $t->is($controller->convertUrlStringToParameters($url), $result, sprintf('->convertUrlStringToParameters() converts a symfony internal URI to an array of parameters (%s)', $url));
  150. }
  151. try
  152. {
  153. $controller->convertUrlStringToParameters('@test?foobar');
  154. $t->fail('->convertUrlStringToParameters() throw a sfParseException if it cannot parse the query string');
  155. }
  156. catch (sfParseException $e)
  157. {
  158. $t->pass('->convertUrlStringToParameters() throw a sfParseException if it cannot parse the query string');
  159. }
  160. // ->redirect()
  161. $t->diag('->redirect()');
  162. $controller->redirect('module/action?id=1#photos');
  163. $response = $context->getResponse();
  164. $t->like($response->getContent(), '~http\://localhost/index.php/\?module=module&amp;action=action&amp;id=1#photos~', '->redirect() adds a refresh meta in the content');
  165. $t->like($response->getHttpHeader('Location'), '~http\://localhost/index.php/\?module=module&action=action&id=1#photos~', '->redirect() adds a Location HTTP header');
  166. // Test null url argument for ->redirect()
  167. try
  168. {
  169. $controller->redirect(null);
  170. $t->fail('->redirect() throw an InvalidArgumentException when the url argument is null');
  171. }
  172. catch (InvalidArgumentException $iae)
  173. {
  174. $t->pass('->redirect() throw an InvalidArgumentException when the url argument is null');
  175. }
  176. catch(Exception $e)
  177. {
  178. $t->fail('->redirect() throw an InvalidArgumentException when the url argument is null. '.get_class($e).' was received');
  179. }
  180. // Test empty string url argument for ->redirect()
  181. try
  182. {
  183. $controller->redirect('');
  184. $t->fail('->redirect() throw an InvalidArgumentException when the url argument is an empty string');
  185. }
  186. catch (InvalidArgumentException $iae)
  187. {
  188. $t->pass('->redirect() throw an InvalidArgumentException when the url argument is an empty string');
  189. }
  190. catch(Exception $e)
  191. {
  192. $t->fail('->redirect() throw an InvalidArgumentException when the url argument is an empty string. '.get_class($e).' was received');
  193. }
  194. // ->genUrl()
  195. $t->diag('->genUrl()');
  196. $t->is($controller->genUrl('module/action?id=4'), $controller->genUrl(array('action' => 'action', 'module' => 'module', 'id' => 4)), '->genUrl() accepts a string or an array as its first argument');
  197. $lastError = error_get_last();
  198. $controller->genUrl('');
  199. $t->is_deeply(error_get_last(), $lastError, '->genUrl() accepts an empty string');