asset.test.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. $container = $this->getContainer();
  154. $container->bundle('eloquent');
  155. $this->assertEquals('/bundles/eloquent/foo.jpg', $container->path('foo.jpg'));
  156. }
  157. /**
  158. * Test the Asset_Container::path method.
  159. *
  160. * @group laravel
  161. */
  162. public function testPathMethodReturnsCorrectPathForAnApplicationAsset()
  163. {
  164. $container = $this->getContainer();
  165. $this->assertEquals('/foo.jpg', $container->path('foo.jpg'));
  166. }
  167. /**
  168. * Test the Asset_Container::scripts method.
  169. *
  170. * @group laravel
  171. */
  172. public function testScriptsCanBeRetrieved()
  173. {
  174. $container = $this->getContainer();
  175. $container->script('dojo', 'dojo.js', array('jquery-ui'));
  176. $container->script('jquery', 'jquery.js', array('jquery-ui', 'dojo'));
  177. $container->script('jquery-ui', 'jquery-ui.js');
  178. $scripts = $container->scripts();
  179. $this->assertTrue(strpos($scripts, 'jquery.js') > 0);
  180. $this->assertTrue(strpos($scripts, 'jquery.js') > strpos($scripts, 'jquery-ui.js'));
  181. $this->assertTrue(strpos($scripts, 'dojo.js') > strpos($scripts, 'jquery-ui.js'));
  182. }
  183. /**
  184. * Test the Asset_Container::styles method.
  185. *
  186. * @group laravel
  187. */
  188. public function testStylesCanBeRetrieved()
  189. {
  190. $container = $this->getContainer();
  191. $container->style('dojo', 'dojo.css', array('jquery-ui'), array('media' => 'print'));
  192. $container->style('jquery', 'jquery.css', array('jquery-ui', 'dojo'));
  193. $container->style('jquery-ui', 'jquery-ui.css');
  194. $styles = $container->styles();
  195. $this->assertTrue(strpos($styles, 'jquery.css') > 0);
  196. $this->assertTrue(strpos($styles, 'media="print"') > 0);
  197. $this->assertTrue(strpos($styles, 'jquery.css') > strpos($styles, 'jquery-ui.css'));
  198. $this->assertTrue(strpos($styles, 'dojo.css') > strpos($styles, 'jquery-ui.css'));
  199. }
  200. /**
  201. * Get an asset container instance.
  202. *
  203. * @param string $name
  204. * @return Asset_Container
  205. */
  206. private function getContainer($name = 'foo')
  207. {
  208. return new Laravel\Asset_Container($name);
  209. }
  210. }