asset.test.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. class AssetTest extends PHPUnit_Framework_TestCase {
  3. /**
  4. * Initialize the test environment.
  5. */
  6. public function setUp()
  7. {
  8. Config::$items = array();
  9. Config::$cache = array();
  10. Asset::$containers = array();
  11. }
  12. /**
  13. * Test the Asset::container method.
  14. *
  15. * @group laravel
  16. */
  17. public function testContainersCanBeCreated()
  18. {
  19. $container = Asset::container('foo');
  20. $this->assertTrue($container === Asset::container('foo'));
  21. $this->assertInstanceOf('\\Laravel\\Asset_Container', $container);
  22. }
  23. /**
  24. * Test the Asset::container method for default container creation.
  25. *
  26. * @group laravel
  27. */
  28. public function testDefaultContainerCreatedByDefault()
  29. {
  30. $this->assertEquals('default', Asset::container()->name);
  31. }
  32. /**
  33. * Test the Asset::__callStatic method.
  34. *
  35. * @group laravel
  36. */
  37. public function testContainerMethodsCanBeDynamicallyCalled()
  38. {
  39. Asset::style('common', 'common.css');
  40. $this->assertEquals('common.css', Asset::container()->assets['style']['common']['source']);
  41. }
  42. /**
  43. * Test the Asset_Container constructor.
  44. *
  45. * @group laravel
  46. */
  47. public function testNameIsSetOnAssetContainerConstruction()
  48. {
  49. $container = $this->getContainer();
  50. $this->assertEquals('foo', $container->name);
  51. }
  52. /**
  53. * Test the Asset_Container::add method.
  54. *
  55. * @group laravel
  56. */
  57. public function testAddMethodProperlySniffsAssetType()
  58. {
  59. $container = $this->getContainer();
  60. $container->add('jquery', 'jquery.js');
  61. $container->add('common', 'common.css');
  62. $this->assertEquals('jquery.js', $container->assets['script']['jquery']['source']);
  63. $this->assertEquals('common.css', $container->assets['style']['common']['source']);
  64. }
  65. /**
  66. * Test the Asset_Container::style method.
  67. *
  68. * @group laravel
  69. */
  70. public function testStyleMethodProperlyRegistersAnAsset()
  71. {
  72. $container = $this->getContainer();
  73. $container->style('common', 'common.css');
  74. $this->assertEquals('common.css', $container->assets['style']['common']['source']);
  75. }
  76. /**
  77. * Test the Asset_Container::style method sets media attribute.
  78. *
  79. * @group laravel
  80. */
  81. public function testStyleMethodProperlySetsMediaAttributeIfNotSet()
  82. {
  83. $container = $this->getContainer();
  84. $container->style('common', 'common.css');
  85. $this->assertEquals('all', $container->assets['style']['common']['attributes']['media']);
  86. }
  87. /**
  88. * Test the Asset_Container::style method sets media attribute.
  89. *
  90. * @group laravel
  91. */
  92. public function testStyleMethodProperlyIgnoresMediaAttributeIfSet()
  93. {
  94. $container = $this->getContainer();
  95. $container->style('common', 'common.css', array(), array('media' => 'print'));
  96. $this->assertEquals('print', $container->assets['style']['common']['attributes']['media']);
  97. }
  98. /**
  99. * Test the Asset_Container::script method.
  100. *
  101. * @group laravel
  102. */
  103. public function testScriptMethodProperlyRegistersAnAsset()
  104. {
  105. $container = $this->getContainer();
  106. $container->script('jquery', 'jquery.js');
  107. $this->assertEquals('jquery.js', $container->assets['script']['jquery']['source']);
  108. }
  109. /**
  110. * Test the Asset_Container::add method properly sets dependencies.
  111. *
  112. * @group laravel
  113. */
  114. public function testAddMethodProperlySetsDependencies()
  115. {
  116. $container = $this->getContainer();
  117. $container->add('common', 'common.css', 'jquery');
  118. $container->add('jquery', 'jquery.js', array('jquery-ui'));
  119. $this->assertEquals(array('jquery'), $container->assets['style']['common']['dependencies']);
  120. $this->assertEquals(array('jquery-ui'), $container->assets['script']['jquery']['dependencies']);
  121. }
  122. /**
  123. * Test the Asset_Container::add method properly sets attributes.
  124. *
  125. * @group laravel
  126. */
  127. public function testAddMethodProperlySetsAttributes()
  128. {
  129. $container = $this->getContainer();
  130. $container->add('common', 'common.css', array(), array('media' => 'print'));
  131. $container->add('jquery', 'jquery.js', array(), array('defer'));
  132. $this->assertEquals(array('media' => 'print'), $container->assets['style']['common']['attributes']);
  133. $this->assertEquals(array('defer'), $container->assets['script']['jquery']['attributes']);
  134. }
  135. /**
  136. * Test the Asset_Container::bundle method.
  137. *
  138. * @group laravel
  139. */
  140. public function testBundleMethodCorrectlySetsTheAssetBundle()
  141. {
  142. $container = $this->getContainer();
  143. $container->bundle('eloquent');
  144. $this->assertEquals('eloquent', $container->bundle);
  145. }
  146. /**
  147. * Test the Asset_Container::path method.
  148. *
  149. * @group laravel
  150. */
  151. public function testPathMethodReturnsCorrectPathForABundleAsset()
  152. {
  153. Config::set('application.url', 'http://localhost');
  154. $container = $this->getContainer();
  155. $container->bundle('eloquent');
  156. $this->assertEquals('http://localhost/bundles/eloquent/foo.jpg', $container->path('foo.jpg'));
  157. }
  158. /**
  159. * Test the Asset_Container::path method.
  160. *
  161. * @group laravel
  162. */
  163. public function testPathMethodReturnsCorrectPathForAnApplicationAsset()
  164. {
  165. Config::set('application.url', 'http://localhost');
  166. $container = $this->getContainer();
  167. $this->assertEquals('http://localhost/foo.jpg', $container->path('foo.jpg'));
  168. }
  169. /**
  170. * Test the Asset_Container::scripts method.
  171. *
  172. * @group laravel
  173. */
  174. public function testScriptsCanBeRetrieved()
  175. {
  176. $container = $this->getContainer();
  177. $container->script('dojo', 'dojo.js', array('jquery-ui'));
  178. $container->script('jquery', 'jquery.js', array('jquery-ui', 'dojo'));
  179. $container->script('jquery-ui', 'jquery-ui.js');
  180. $scripts = $container->scripts();
  181. $this->assertTrue(strpos($scripts, 'jquery.js') > 0);
  182. $this->assertTrue(strpos($scripts, 'jquery.js') > strpos($scripts, 'jquery-ui.js'));
  183. $this->assertTrue(strpos($scripts, 'dojo.js') > strpos($scripts, 'jquery-ui.js'));
  184. }
  185. /**
  186. * Test the Asset_Container::styles method.
  187. *
  188. * @group laravel
  189. */
  190. public function testStylesCanBeRetrieved()
  191. {
  192. $container = $this->getContainer();
  193. $container->style('dojo', 'dojo.css', array('jquery-ui'), array('media' => 'print'));
  194. $container->style('jquery', 'jquery.css', array('jquery-ui', 'dojo'));
  195. $container->style('jquery-ui', 'jquery-ui.css');
  196. $styles = $container->styles();
  197. $this->assertTrue(strpos($styles, 'jquery.css') > 0);
  198. $this->assertTrue(strpos($styles, 'media="print"') > 0);
  199. $this->assertTrue(strpos($styles, 'jquery.css') > strpos($styles, 'jquery-ui.css'));
  200. $this->assertTrue(strpos($styles, 'dojo.css') > strpos($styles, 'jquery-ui.css'));
  201. }
  202. /**
  203. * Get an asset container instance.
  204. *
  205. * @param string $name
  206. * @return Asset_Container
  207. */
  208. private function getContainer($name = 'foo')
  209. {
  210. return new Laravel\Asset_Container($name);
  211. }
  212. }