controller.test.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. class ControllerTest extends PHPUnit_Framework_TestCase {
  3. /**
  4. * Setup the testing environment.
  5. */
  6. public function setUp()
  7. {
  8. $_SERVER['REQUEST_METHOD'] = 'GET';
  9. }
  10. /**
  11. * Tear down the testing environment.
  12. */
  13. public function tearDown()
  14. {
  15. unset(Filter::$filters['test-all-before']);
  16. unset(Filter::$filters['test-all-after']);
  17. unset(Filter::$filters['test-profile-before']);
  18. unset($_SERVER['REQUEST_METHOD']);
  19. }
  20. /**
  21. * Test the Controller::call method.
  22. *
  23. * @group laravel
  24. */
  25. public function testBasicControllerActionCanBeCalled()
  26. {
  27. $this->assertEquals('action_index', Controller::call('auth@index')->content);
  28. $this->assertEquals('Admin_Panel_Index', Controller::call('admin.panel@index')->content);
  29. $this->assertEquals('Taylor', Controller::call('auth@profile', array('Taylor'))->content);
  30. $this->assertEquals('Dashboard_Panel_Index', Controller::call('dashboard::panel@index')->content);
  31. }
  32. /**
  33. * Test basic controller filters are called.
  34. *
  35. * @group laravel
  36. */
  37. public function testAssignedBeforeFiltersAreRun()
  38. {
  39. $_SERVER['test-all-after'] = false;
  40. $_SERVER['test-all-before'] = false;
  41. Controller::call('filter@index');
  42. $this->assertTrue($_SERVER['test-all-after']);
  43. $this->assertTrue($_SERVER['test-all-before']);
  44. }
  45. /**
  46. * Test that "only" filters only apply to their assigned methods.
  47. *
  48. * @group laravel
  49. */
  50. public function testOnlyFiltersOnlyApplyToTheirAssignedMethods()
  51. {
  52. $_SERVER['test-profile-before'] = false;
  53. Controller::call('filter@index');
  54. $this->assertFalse($_SERVER['test-profile-before']);
  55. Controller::call('filter@profile');
  56. $this->assertTrue($_SERVER['test-profile-before']);
  57. }
  58. /**
  59. * Test that "except" filters only apply to the excluded methods.
  60. *
  61. * @group laravel
  62. */
  63. public function testExceptFiltersOnlyApplyToTheExlucdedMethods()
  64. {
  65. $_SERVER['test-except'] = false;
  66. Controller::call('filter@index');
  67. Controller::call('filter@profile');
  68. $this->assertFalse($_SERVER['test-except']);
  69. Controller::call('filter@show');
  70. $this->assertTrue($_SERVER['test-except']);
  71. }
  72. /**
  73. * Test that filters can be constrained by the request method.
  74. *
  75. * @group laravel
  76. */
  77. public function testFiltersCanBeConstrainedByRequestMethod()
  78. {
  79. $_SERVER['test-on-post'] = false;
  80. $_SERVER['REQUEST_METHOD'] = 'GET';
  81. Controller::call('filter@index');
  82. $this->assertFalse($_SERVER['test-on-post']);
  83. $_SERVER['REQUEST_METHOD'] = 'POST';
  84. Controller::call('filter@index');
  85. $this->assertTrue($_SERVER['test-on-post']);
  86. $_SERVER['test-on-get-put'] = false;
  87. $_SERVER['REQUEST_METHOD'] = 'POST';
  88. Controller::call('filter@index');
  89. $this->assertFalse($_SERVER['test-on-get-put']);
  90. $_SERVER['REQUEST_METHOD'] = 'PUT';
  91. Controller::call('filter@index');
  92. $this->assertTrue($_SERVER['test-on-get-put']);
  93. }
  94. public function testGlobalBeforeFilterIsNotCalledByController()
  95. {
  96. $_SERVER['before'] = false;
  97. $_SERVER['after'] = false;
  98. Controller::call('auth@index');
  99. $this->assertFalse($_SERVER['before']);
  100. $this->assertFalse($_SERVER['after']);
  101. }
  102. /**
  103. * Test that before filters can override the controller response.
  104. *
  105. * @group laravel
  106. */
  107. public function testBeforeFiltersCanOverrideResponses()
  108. {
  109. $this->assertEquals('Filtered!', Controller::call('filter@login')->content);
  110. }
  111. /**
  112. * Test that after filters do not affect the response.
  113. *
  114. * @group laravel
  115. */
  116. public function testAfterFiltersDoNotAffectControllerResponse()
  117. {
  118. $this->assertEquals('action_logout', Controller::call('filter@logout')->content);
  119. }
  120. /**
  121. * Test that filter parameters are passed to the filter.
  122. *
  123. * @group laravel
  124. */
  125. public function testFilterParametersArePassedToTheFilter()
  126. {
  127. $this->assertEquals('12', Controller::call('filter@edit')->content);
  128. }
  129. /**
  130. * Test that multiple filters can be assigned to a single method.
  131. *
  132. * @group laravel
  133. */
  134. public function testMultipleFiltersCanBeAssignedToAnAction()
  135. {
  136. $_SERVER['test-multi-1'] = false;
  137. $_SERVER['test-multi-2'] = false;
  138. Controller::call('filter@save');
  139. $this->assertTrue($_SERVER['test-multi-1']);
  140. $this->assertTrue($_SERVER['test-multi-2']);
  141. }
  142. /**
  143. * Test Restful controllers respond by request method.
  144. *
  145. * @group laravel
  146. */
  147. public function testRestfulControllersRespondWithRestfulMethods()
  148. {
  149. $_SERVER['REQUEST_METHOD'] = 'GET';
  150. $this->assertEquals('get_index', Controller::call('restful@index')->content);
  151. $_SERVER['REQUEST_METHOD'] = 'PUT';
  152. $this->assertEquals(404, Controller::call('restful@index')->status);
  153. $_SERVER['REQUEST_METHOD'] = 'POST';
  154. $this->assertEquals('post_index', Controller::call('restful@index')->content);
  155. }
  156. /**
  157. * Test that the template is returned by template controllers.
  158. *
  159. * @group laravel
  160. */
  161. public function testTemplateControllersReturnTheTemplate()
  162. {
  163. $response = Controller::call('template.basic@index');
  164. $home = file_get_contents(path('app').'views/home/index.php');
  165. $this->assertEquals($home, $response->content);
  166. }
  167. /**
  168. * Test that controller templates can be named views.
  169. *
  170. * @group laravel
  171. */
  172. public function testControllerTemplatesCanBeNamedViews()
  173. {
  174. View::name('home.index', 'home');
  175. $response = Controller::call('template.named@index');
  176. $home = file_get_contents(path('app').'views/home/index.php');
  177. $this->assertEquals($home, $response->content);
  178. View::$names = array();
  179. }
  180. /**
  181. * Test that the "layout" method is called on the controller.
  182. *
  183. * @group laravel
  184. */
  185. public function testTheTemplateCanBeOverriden()
  186. {
  187. $this->assertEquals('Layout', Controller::call('template.override@index')->content);
  188. }
  189. /**
  190. * Test the Controller::resolve method.
  191. *
  192. * @group laravel
  193. */
  194. public function testResolveMethodChecksTheIoCContainer()
  195. {
  196. IoC::controller('home', function()
  197. {
  198. require_once path('app').'controllers/home.php';
  199. $controller = new Home_Controller;
  200. $controller->foo = 'bar';
  201. return $controller;
  202. });
  203. $controller = Controller::resolve(DEFAULT_BUNDLE, 'home');
  204. $this->assertEquals('bar', $controller->foo);
  205. }
  206. }