controller.test.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. Request::$foundation->setMethod('GET');
  81. Controller::call('filter@index');
  82. $this->assertFalse($_SERVER['test-on-post']);
  83. Request::$foundation->setMethod('POST');
  84. Controller::call('filter@index');
  85. $this->assertTrue($_SERVER['test-on-post']);
  86. $_SERVER['test-on-get-put'] = false;
  87. Request::$foundation->setMethod('POST');
  88. Controller::call('filter@index');
  89. $this->assertFalse($_SERVER['test-on-get-put']);
  90. Request::$foundation->setMethod('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. Request::$foundation->setMethod('GET');
  150. //$_SERVER['REQUEST_METHOD'] = 'GET';
  151. $this->assertEquals('get_index', Controller::call('restful@index')->content);
  152. //$_SERVER['REQUEST_METHOD'] = 'PUT';
  153. Request::$foundation->setMethod('PUT');
  154. $this->assertEquals(404, Controller::call('restful@index')->status());
  155. //$_SERVER['REQUEST_METHOD'] = 'POST';
  156. Request::$foundation->setMethod('POST');
  157. $this->assertEquals('post_index', Controller::call('restful@index')->content);
  158. }
  159. /**
  160. * Test that the template is returned by template controllers.
  161. *
  162. * @group laravel
  163. */
  164. public function testTemplateControllersReturnTheTemplate()
  165. {
  166. $response = Controller::call('template.basic@index');
  167. $home = file_get_contents(path('app').'views/home/index.php');
  168. $this->assertEquals($home, $response->content);
  169. }
  170. /**
  171. * Test that controller templates can be named views.
  172. *
  173. * @group laravel
  174. */
  175. public function testControllerTemplatesCanBeNamedViews()
  176. {
  177. View::name('home.index', 'home');
  178. $response = Controller::call('template.named@index');
  179. $home = file_get_contents(path('app').'views/home/index.php');
  180. $this->assertEquals($home, $response->content);
  181. View::$names = array();
  182. }
  183. /**
  184. * Test that the "layout" method is called on the controller.
  185. *
  186. * @group laravel
  187. */
  188. public function testTheTemplateCanBeOverriden()
  189. {
  190. $this->assertEquals('Layout', Controller::call('template.override@index')->content);
  191. }
  192. /**
  193. * Test the Controller::resolve method.
  194. *
  195. * @group laravel
  196. */
  197. public function testResolveMethodChecksTheIoCContainer()
  198. {
  199. IoC::register('controller: home', function()
  200. {
  201. require_once path('app').'controllers/home.php';
  202. $controller = new Home_Controller;
  203. $controller->foo = 'bar';
  204. return $controller;
  205. });
  206. $controller = Controller::resolve(DEFAULT_BUNDLE, 'home');
  207. $this->assertEquals('bar', $controller->foo);
  208. }
  209. }