PageRenderTime 35ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/kriswallsmith/assetic/tests/Assetic/Test/Factory/AssetFactoryTest.php

https://bitbucket.org/renta/jobeet2.loc
PHP | 219 lines | 161 code | 45 blank | 13 comment | 0 complexity | 4b62999ee1ba1cebc23d344840c05c56 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1, BSD-2-Clause, Apache-2.0, CC-BY-3.0
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2012 OpenSky Project Inc
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Assetic\Test\Factory;
  11. use Assetic\Factory\AssetFactory;
  12. class AssetFactoryTest extends \PHPUnit_Framework_TestCase
  13. {
  14. private $am;
  15. private $fm;
  16. private $factory;
  17. protected function setUp()
  18. {
  19. $this->am = $this->getMock('Assetic\\AssetManager');
  20. $this->fm = $this->getMock('Assetic\\FilterManager');
  21. $this->factory = new AssetFactory(__DIR__);
  22. $this->factory->setAssetManager($this->am);
  23. $this->factory->setFilterManager($this->fm);
  24. }
  25. public function testNoAssetManagerReference()
  26. {
  27. $this->setExpectedException('LogicException', 'There is no asset manager.');
  28. $factory = new AssetFactory('.');
  29. $factory->createAsset(array('@foo'));
  30. }
  31. public function testNoAssetManagerNotReference()
  32. {
  33. $factory = new AssetFactory('.');
  34. $this->assertInstanceOf('Assetic\\Asset\\AssetInterface', $factory->createAsset(array('foo')));
  35. }
  36. public function testNoFilterManager()
  37. {
  38. $this->setExpectedException('LogicException', 'There is no filter manager.');
  39. $factory = new AssetFactory('.');
  40. $factory->createAsset(array('foo'), array('foo'));
  41. }
  42. public function testCreateAssetReference()
  43. {
  44. $referenced = $this->getMock('Assetic\\Asset\\AssetInterface');
  45. $this->am->expects($this->any())
  46. ->method('get')
  47. ->with('jquery')
  48. ->will($this->returnValue($referenced));
  49. $assets = $this->factory->createAsset(array('@jquery'));
  50. $arr = iterator_to_array($assets);
  51. $this->assertInstanceOf('Assetic\\Asset\\AssetReference', $arr[0], '->createAsset() creates a reference');
  52. }
  53. /**
  54. * @dataProvider getHttpUrls
  55. */
  56. public function testCreateHttpAsset($sourceUrl)
  57. {
  58. $assets = $this->factory->createAsset(array($sourceUrl));
  59. $arr = iterator_to_array($assets);
  60. $this->assertInstanceOf('Assetic\\Asset\\HttpAsset', $arr[0], '->createAsset() creates an HTTP asset');
  61. }
  62. public function getHttpUrls()
  63. {
  64. return array(
  65. array('http://example.com/foo.css'),
  66. array('https://example.com/foo.css'),
  67. array('//example.com/foo.css'),
  68. );
  69. }
  70. public function testCreateFileAsset()
  71. {
  72. $assets = $this->factory->createAsset(array(basename(__FILE__)));
  73. $arr = iterator_to_array($assets);
  74. $this->assertInstanceOf('Assetic\\Asset\\FileAsset', $arr[0], '->createAsset() creates a file asset');
  75. }
  76. public function testCreateGlobAsset()
  77. {
  78. $assets = $this->factory->createAsset(array('*'));
  79. $arr = iterator_to_array($assets);
  80. $this->assertInstanceOf('Assetic\\Asset\\FileAsset', $arr[0], '->createAsset() uses a glob to create a file assets');
  81. }
  82. public function testCreateGlobAssetAndLoadFiles()
  83. {
  84. $assets = $this->factory->createAsset(array('*/Fixtures/*/*'));
  85. $assets->load();
  86. $this->assertEquals(5, count(iterator_to_array($assets)), '->createAsset() adds files');
  87. }
  88. public function testCreateGlobAssetAndExcludeDirectories()
  89. {
  90. $assets = $this->factory->createAsset(array('*/Fixtures/*', '*/Fixtures/*/*'));
  91. $assets->load();
  92. $this->assertEquals(5, count(iterator_to_array($assets)), '->createAsset() excludes directories and add files');
  93. }
  94. public function testCreateAssetCollection()
  95. {
  96. $asset = $this->factory->createAsset(array('*', basename(__FILE__)));
  97. $this->assertInstanceOf('Assetic\\Asset\\AssetCollection', $asset, '->createAsset() creates an asset collection');
  98. }
  99. public function testFilter()
  100. {
  101. $this->fm->expects($this->once())
  102. ->method('get')
  103. ->with('foo')
  104. ->will($this->returnValue($this->getMock('Assetic\\Filter\\FilterInterface')));
  105. $asset = $this->factory->createAsset(array(), array('foo'));
  106. $this->assertEquals(1, count($asset->getFilters()), '->createAsset() adds filters');
  107. }
  108. public function testInvalidFilter()
  109. {
  110. $this->setExpectedException('InvalidArgumentException');
  111. $this->fm->expects($this->once())
  112. ->method('get')
  113. ->with('foo')
  114. ->will($this->throwException(new \InvalidArgumentException()));
  115. $asset = $this->factory->createAsset(array(), array('foo'));
  116. }
  117. public function testOptionalInvalidFilter()
  118. {
  119. $this->factory->setDebug(true);
  120. $asset = $this->factory->createAsset(array(), array('?foo'));
  121. $this->assertEquals(0, count($asset->getFilters()), '->createAsset() does not add an optional invalid filter');
  122. }
  123. public function testIncludingOptionalFilter()
  124. {
  125. $this->fm->expects($this->once())
  126. ->method('get')
  127. ->with('foo')
  128. ->will($this->returnValue($this->getMock('Assetic\\Filter\\FilterInterface')));
  129. $this->factory->createAsset(array('foo.css'), array('?foo'));
  130. }
  131. public function testWorkers()
  132. {
  133. $worker = $this->getMock('Assetic\\Factory\\Worker\\WorkerInterface');
  134. // called once on the collection and once on each leaf
  135. $worker->expects($this->exactly(3))
  136. ->method('process')
  137. ->with($this->isInstanceOf('Assetic\\Asset\\AssetInterface'));
  138. $this->factory->addWorker($worker);
  139. $this->factory->createAsset(array('foo.js', 'bar.js'));
  140. }
  141. public function testWorkerReturn()
  142. {
  143. $worker = $this->getMock('Assetic\\Factory\\Worker\\WorkerInterface');
  144. $asset = $this->getMock('Assetic\\Asset\\AssetInterface');
  145. $worker->expects($this->at(2))
  146. ->method('process')
  147. ->with($this->isInstanceOf('Assetic\\Asset\\AssetCollectionInterface'))
  148. ->will($this->returnValue($asset));
  149. $this->factory->addWorker($worker);
  150. $coll = $this->factory->createAsset(array('foo.js', 'bar.js'));
  151. $this->assertEquals(1, count(iterator_to_array($coll)));
  152. }
  153. public function testNestedFormula()
  154. {
  155. $this->fm->expects($this->once())
  156. ->method('get')
  157. ->with('foo')
  158. ->will($this->returnValue($this->getMock('Assetic\\Filter\\FilterInterface')));
  159. $inputs = array(
  160. 'css/main.css',
  161. array(
  162. // nested formula
  163. array('css/more.sass'),
  164. array('foo'),
  165. ),
  166. );
  167. $asset = $this->factory->createAsset($inputs, array(), array('output' => 'css/*.css'));
  168. $i = 0;
  169. foreach ($asset as $leaf) {
  170. $i++;
  171. }
  172. $this->assertEquals(2, $i);
  173. }
  174. }