view.test.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. class ViewTest extends PHPUnit_Framework_TestCase {
  3. /**
  4. * Tear down the testing environment.
  5. */
  6. public function tearDown()
  7. {
  8. View::$shared = array();
  9. unset(Event::$events['composing: test.basic']);
  10. }
  11. /**
  12. * Test the View::make method.
  13. *
  14. * @group laravel
  15. */
  16. public function testMakeMethodReturnsAViewInstance()
  17. {
  18. $this->assertInstanceOf('Laravel\\View', View::make('home.index'));
  19. }
  20. /**
  21. * Test the View class constructor.
  22. *
  23. * @group laravel
  24. */
  25. public function testViewNameIsSetByConstrutor()
  26. {
  27. $view = new View('home.index');
  28. $this->assertEquals('home.index', $view->view);
  29. }
  30. /**
  31. * Test the View class constructor.
  32. *
  33. * @group laravel
  34. */
  35. public function testViewIsCreatedWithCorrectPath()
  36. {
  37. $view = new View('home.index');
  38. $this->assertEquals(
  39. str_replace(DS, '/', path('app')).'views/home/index.php',
  40. str_replace(DS, '/', $view->path)
  41. );
  42. }
  43. /**
  44. * Test the View class constructor for bundles.
  45. *
  46. * @group laravel
  47. */
  48. public function testBundleViewIsCreatedWithCorrectPath()
  49. {
  50. $view = new View('home.index');
  51. $this->assertEquals(
  52. str_replace(DS, '/', Bundle::path(DEFAULT_BUNDLE)).'views/home/index.php',
  53. str_replace(DS, '/', $view->path)
  54. );
  55. }
  56. /**
  57. * Test the View class constructor.
  58. *
  59. * @group laravel
  60. */
  61. public function testDataIsSetOnViewByConstructor()
  62. {
  63. $view = new View('home.index', array('name' => 'Taylor'));
  64. $this->assertEquals('Taylor', $view->data['name']);
  65. }
  66. /**
  67. * Test the View::name method.
  68. *
  69. * @group laravel
  70. */
  71. public function testNameMethodRegistersAViewName()
  72. {
  73. View::name('home.index', 'home');
  74. $this->assertEquals('home.index', View::$names['home']);
  75. }
  76. /**
  77. * Test the View::shared method.
  78. *
  79. * @group laravel
  80. */
  81. public function testSharedMethodAddsDataToSharedArray()
  82. {
  83. View::share('comment', 'Taylor');
  84. $this->assertEquals('Taylor', View::$shared['comment']);
  85. }
  86. /**
  87. * Test the View::with method.
  88. *
  89. * @group laravel
  90. */
  91. public function testViewDataCanBeSetUsingWithMethod()
  92. {
  93. $view = View::make('home.index')->with('comment', 'Taylor');
  94. $this->assertEquals('Taylor', $view->data['comment']);
  95. }
  96. /**
  97. * Test the View class constructor.
  98. *
  99. * @group laravel
  100. */
  101. public function testEmptyMessageContainerSetOnViewWhenNoErrorsInSession()
  102. {
  103. $view = new View('home.index');
  104. $this->assertInstanceOf('Laravel\\Messages', $view->data['errors']);
  105. }
  106. /**
  107. * Test the View __set method.
  108. *
  109. * @group laravel
  110. */
  111. public function testDataCanBeSetOnViewsThroughMagicMethods()
  112. {
  113. $view = new View('home.index');
  114. $view->comment = 'Taylor';
  115. $this->assertEquals('Taylor', $view->data['comment']);
  116. }
  117. /**
  118. * Test the View __get method.
  119. *
  120. * @group laravel
  121. */
  122. public function testDataCanBeRetrievedFromViewsThroughMagicMethods()
  123. {
  124. $view = new View('home.index');
  125. $view->comment = 'Taylor';
  126. $this->assertEquals('Taylor', $view->comment);
  127. }
  128. /**
  129. * Test the View's ArrayAccess implementation.
  130. *
  131. * @group laravel
  132. */
  133. public function testDataCanBeSetOnTheViewThroughArrayAccess()
  134. {
  135. $view = new View('home.index');
  136. $view['comment'] = 'Taylor';
  137. $this->assertEquals('Taylor', $view->data['comment']);
  138. }
  139. /**
  140. * Test the View's ArrayAccess implementation.
  141. *
  142. * @group laravel
  143. */
  144. public function testDataCanBeRetrievedThroughArrayAccess()
  145. {
  146. $view = new View('home.index');
  147. $view['comment'] = 'Taylor';
  148. $this->assertEquals('Taylor', $view['comment']);
  149. }
  150. /**
  151. * Test the View::nest method.
  152. *
  153. * @group laravel
  154. */
  155. public function testNestMethodSetsViewInstanceInData()
  156. {
  157. $view = View::make('home.index')->nest('partial', 'tests.basic');
  158. $this->assertEquals('tests.basic', $view->data['partial']->view);
  159. $this->assertInstanceOf('Laravel\\View', $view->data['partial']);
  160. }
  161. /**
  162. * Test that the registered data is passed to the view correctly.
  163. *
  164. * @group laravel
  165. */
  166. public function testDataIsPassedToViewCorrectly()
  167. {
  168. View::share('name', 'Taylor');
  169. $view = View::make('tests.basic')->with('age', 25)->render();
  170. $this->assertEquals('Taylor is 25', $view);
  171. }
  172. /**
  173. * Test that the View class renders nested views.
  174. *
  175. * @group laravel
  176. */
  177. public function testNestedViewsAreRendered()
  178. {
  179. $view = View::make('tests.basic')
  180. ->with('age', 25)
  181. ->nest('name', 'tests.nested');
  182. $this->assertEquals('Taylor is 25', $view->render());
  183. }
  184. /**
  185. * Test that the View class renders nested responses.
  186. *
  187. * @group laravel
  188. */
  189. public function testNestedResponsesAreRendered()
  190. {
  191. $view = View::make('tests.basic')
  192. ->with('age', 25)
  193. ->with('name', Response::view('tests.nested'));
  194. $this->assertEquals('Taylor is 25', $view->render());
  195. }
  196. /**
  197. * Test the View class raises a composer event.
  198. *
  199. * @group laravel
  200. */
  201. public function testComposerEventIsCalledWhenViewIsRendering()
  202. {
  203. View::composer('tests.basic', function($view)
  204. {
  205. $view->data = array('name' => 'Taylor', 'age' => 25);
  206. });
  207. $view = View::make('tests.basic')->render();
  208. $this->assertEquals('Taylor is 25', $view);
  209. }
  210. }