AssetTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. class AssetTest extends PHPUnit_Framework_TestCase {
  3. public function testContainerMethodReturnsContainer()
  4. {
  5. $asset = Laravel\IoC::resolve('laravel.asset');
  6. $this->assertInstanceOf('Laravel\\Asset_Container', $asset->container());
  7. $this->assertInstanceOf('Laravel\\Asset_Container', $asset->container('footer'));
  8. $this->assertEquals($asset->container()->name, 'default');
  9. $this->assertEquals($asset->container('footer')->name, 'footer');
  10. }
  11. public function testAssetManagerMagicallyCallsDefaultContainer()
  12. {
  13. $asset = Laravel\IoC::resolve('laravel.asset');
  14. $mock = $this->getMockBuilder('Laravel\\Asset_Container')->disableOriginalConstructor()->getMock();
  15. $mock->expects($this->any())->method('styles')->will($this->returnValue('styles'));
  16. $asset->containers['default'] = $mock;
  17. $this->assertEquals($asset->styles(), 'styles');
  18. }
  19. public function testAddMethodAddsAssetBasedOnExtension()
  20. {
  21. $container = $this->getContainer();
  22. $container->add('jquery', 'js/jquery.js');
  23. $container->add('jquery-css', 'css/jquery.css');
  24. $this->assertEquals($container->assets['script']['jquery']['source'], 'js/jquery.js');
  25. $this->assertEquals($container->assets['style']['jquery-css']['source'], 'css/jquery.css');
  26. }
  27. /**
  28. * @dataProvider assetProvider
  29. */
  30. public function testStyleMethodRegistersStylesheetAsset($type, $source, $attributes, $testAttributes)
  31. {
  32. $container = $this->getContainer();
  33. $dependencies = array('jquery');
  34. $container->$type('reset', $source, $dependencies, $attributes);
  35. $this->assertEquals($container->assets[$type]['reset']['source'], $source);
  36. $this->assertEquals($container->assets[$type]['reset']['dependencies'], $dependencies);
  37. $this->assertEquals($container->assets[$type]['reset']['attributes'], $testAttributes);
  38. }
  39. public function assetProvider()
  40. {
  41. $attributes = array('test' => 'test');
  42. return array(
  43. array('style', 'css/reset.css', $attributes, array_merge($attributes, array('media' => 'all'))),
  44. array('script', 'js/jquery.js', $attributes, $attributes),
  45. );
  46. }
  47. public function testAllStylesCanBeRetrievedViaStylesMethod()
  48. {
  49. $container = new Laravel\Asset_Container('default', new HTMLAssetStub);
  50. $container->style('reset', 'css/reset.css');
  51. $container->style('jquery', 'css/jquery.css');
  52. $this->assertEquals($container->styles(), 'css/reset.css media:allcss/jquery.css media:all');
  53. }
  54. public function testAllScriptsCanBeRetrievedViaScriptsMethod()
  55. {
  56. $container = new Laravel\Asset_Container('default', new HTMLAssetStub);
  57. $container->script('jquery-ui', 'js/jquery-ui.js');
  58. $container->script('jquery', 'js/jquery.js', array(), array('test' => 'value'));
  59. $this->assertEquals($container->scripts(), 'js/jquery-ui.js js/jquery.js test:value');
  60. }
  61. public function testAssetsAreSortedBasedOnDependencies()
  62. {
  63. $container = $this->getContainer();
  64. $container->script('jquery', 'js/jquery.js', array('jquery-ui'));
  65. $container->script('jquery-ui', 'js/jquery-ui.js');
  66. $scripts = $container->scripts();
  67. $this->assertTrue(strpos($scripts, 'js/jquery-ui.js') < strpos($scripts, 'js/jquery.js'));
  68. }
  69. /**
  70. * @expectedException Exception
  71. */
  72. public function testAssetsCannotBeDependentOnSelf()
  73. {
  74. $container = $this->getContainer();
  75. $container->script('jquery', 'js/jquery.js', array('jquery'));
  76. $container->scripts();
  77. }
  78. /**
  79. * @expectedException Exception
  80. */
  81. public function testAssetDependenciesCannotBeCircular()
  82. {
  83. $container = $this->getContainer();
  84. $container->script('jquery', 'js/jquery.js', array('jquery-ui'));
  85. $container->script('jquery-ui', 'js/jquery-ui.js', array('jquery'));
  86. $container->scripts();
  87. }
  88. private function getContainer()
  89. {
  90. return new Laravel\Asset_Container('default', Laravel\IoC::resolve('laravel.html'));
  91. }
  92. }
  93. class HTMLAssetStub extends Laravel\HTML {
  94. public function __construct() {}
  95. public function style($source, $attributes)
  96. {
  97. return $source.' '.$this->getAttributes($attributes);
  98. }
  99. public function script($source, $attributes)
  100. {
  101. return $source.' '.$this->getAttributes($attributes);
  102. }
  103. private function getAttributes($attributes)
  104. {
  105. $html = '';
  106. foreach ($attributes as $key => $value)
  107. {
  108. $html .= $key.':'.$value;
  109. }
  110. return $html;
  111. }
  112. }