view.test.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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(path('app').'views/home/index.php', $view->path);
  39. }
  40. /**
  41. * Test the View class constructor.
  42. *
  43. * @group laravel
  44. */
  45. public function testDataIsSetOnViewByConstructor()
  46. {
  47. $view = new View('home.index', array('name' => 'Taylor'));
  48. $this->assertEquals('Taylor', $view->data['name']);
  49. }
  50. /**
  51. * Test the View::name method.
  52. *
  53. * @group laravel
  54. */
  55. public function testNameMethodRegistersAViewName()
  56. {
  57. View::name('home.index', 'home');
  58. $this->assertEquals('home.index', View::$names['home']);
  59. }
  60. /**
  61. * Test the View::shared method.
  62. *
  63. * @group laravel
  64. */
  65. public function testSharedMethodAddsDataToSharedArray()
  66. {
  67. View::share('comment', 'Taylor');
  68. $this->assertEquals('Taylor', View::$shared['comment']);
  69. }
  70. /**
  71. * Test the View::with method.
  72. *
  73. * @group laravel
  74. */
  75. public function testViewDataCanBeSetUsingWithMethod()
  76. {
  77. $view = View::make('home.index')->with('comment', 'Taylor');
  78. $this->assertEquals('Taylor', $view->data['comment']);
  79. }
  80. /**
  81. * Test the View class constructor.
  82. *
  83. * @group laravel
  84. */
  85. public function testEmptyMessageContainerSetOnViewWhenNoErrorsInSession()
  86. {
  87. $view = new View('home.index');
  88. $this->assertInstanceOf('Laravel\\Messages', $view->data['errors']);
  89. }
  90. /**
  91. * Test the View __set method.
  92. *
  93. * @group laravel
  94. */
  95. public function testDataCanBeSetOnViewsThroughMagicMethods()
  96. {
  97. $view = new View('home.index');
  98. $view->comment = 'Taylor';
  99. $this->assertEquals('Taylor', $view->data['comment']);
  100. }
  101. /**
  102. * Test the View __get method.
  103. *
  104. * @group laravel
  105. */
  106. public function testDataCanBeRetrievedFromViewsThroughMagicMethods()
  107. {
  108. $view = new View('home.index');
  109. $view->comment = 'Taylor';
  110. $this->assertEquals('Taylor', $view->comment);
  111. }
  112. /**
  113. * Test the View's ArrayAccess implementation.
  114. *
  115. * @group laravel
  116. */
  117. public function testDataCanBeSetOnTheViewThroughArrayAccess()
  118. {
  119. $view = new View('home.index');
  120. $view['comment'] = 'Taylor';
  121. $this->assertEquals('Taylor', $view->data['comment']);
  122. }
  123. /**
  124. * Test the View's ArrayAccess implementation.
  125. *
  126. * @group laravel
  127. */
  128. public function testDataCanBeRetrievedThroughArrayAccess()
  129. {
  130. $view = new View('home.index');
  131. $view['comment'] = 'Taylor';
  132. $this->assertEquals('Taylor', $view['comment']);
  133. }
  134. /**
  135. * Test the View::nest method.
  136. *
  137. * @group laravel
  138. */
  139. public function testNestMethodSetsViewInstanceInData()
  140. {
  141. $view = View::make('home.index')->nest('partial', 'tests.basic');
  142. $this->assertEquals('tests.basic', $view->data['partial']->view);
  143. $this->assertInstanceOf('Laravel\\View', $view->data['partial']);
  144. }
  145. /**
  146. * Test that the registered data is passed to the view correctly.
  147. *
  148. * @group laravel
  149. */
  150. public function testDataIsPassedToViewCorrectly()
  151. {
  152. View::share('name', 'Taylor');
  153. $view = View::make('tests.basic')->with('age', 25)->render();
  154. $this->assertEquals('Taylor is 25', $view);
  155. }
  156. /**
  157. * Test that the View class renders nested views.
  158. *
  159. * @group laravel
  160. */
  161. public function testNestedViewsAreRendered()
  162. {
  163. $view = View::make('tests.basic')
  164. ->with('age', 25)
  165. ->nest('name', 'tests.nested');
  166. $this->assertEquals('Taylor is 25', $view->render());
  167. }
  168. /**
  169. * Test that the View class renders nested responses.
  170. *
  171. * @group laravel
  172. */
  173. public function testNestedResponsesAreRendered()
  174. {
  175. $view = View::make('tests.basic')
  176. ->with('age', 25)
  177. ->with('name', Response::view('tests.nested'));
  178. $this->assertEquals('Taylor is 25', $view->render());
  179. }
  180. /**
  181. * Test the View class raises a composer event.
  182. *
  183. * @group laravel
  184. */
  185. public function testComposerEventIsCalledWhenViewIsRendering()
  186. {
  187. View::composer('tests.basic', function($view)
  188. {
  189. $view->data = array('name' => 'Taylor', 'age' => 25);
  190. });
  191. $view = View::make('tests.basic')->render();
  192. $this->assertEquals('Taylor is 25', $view);
  193. }
  194. }