PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/dudesl/pyresys
PHP | 187 lines | 137 code | 37 blank | 13 comment | 0 complexity | a189abb91dae9774d2f1c79d35af90b7 MD5 | raw file
Possible License(s): BSD-2-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 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 testCreateAssetCollection()
  83. {
  84. $asset = $this->factory->createAsset(array('*', basename(__FILE__)));
  85. $this->assertInstanceOf('Assetic\\Asset\\AssetCollection', $asset, '->createAsset() creates an asset collection');
  86. }
  87. public function testFilter()
  88. {
  89. $this->fm->expects($this->once())
  90. ->method('get')
  91. ->with('foo')
  92. ->will($this->returnValue($this->getMock('Assetic\\Filter\\FilterInterface')));
  93. $asset = $this->factory->createAsset(array(), array('foo'));
  94. $this->assertEquals(1, count($asset->getFilters()), '->createAsset() adds filters');
  95. }
  96. public function testInvalidFilter()
  97. {
  98. $this->setExpectedException('InvalidArgumentException');
  99. $this->fm->expects($this->once())
  100. ->method('get')
  101. ->with('foo')
  102. ->will($this->throwException(new \InvalidArgumentException()));
  103. $asset = $this->factory->createAsset(array(), array('foo'));
  104. }
  105. public function testOptionalInvalidFilter()
  106. {
  107. $this->factory->setDebug(true);
  108. $asset = $this->factory->createAsset(array(), array('?foo'));
  109. $this->assertEquals(0, count($asset->getFilters()), '->createAsset() does not add an optional invalid filter');
  110. }
  111. public function testIncludingOptionalFilter()
  112. {
  113. $this->fm->expects($this->once())
  114. ->method('get')
  115. ->with('foo')
  116. ->will($this->returnValue($this->getMock('Assetic\\Filter\\FilterInterface')));
  117. $this->factory->createAsset(array('foo.css'), array('?foo'));
  118. }
  119. public function testWorkers()
  120. {
  121. $worker = $this->getMock('Assetic\\Factory\\Worker\\WorkerInterface');
  122. // called once on the collection and once on each leaf
  123. $worker->expects($this->exactly(3))
  124. ->method('process')
  125. ->with($this->isInstanceOf('Assetic\\Asset\\AssetInterface'));
  126. $this->factory->addWorker($worker);
  127. $this->factory->createAsset(array('foo.js', 'bar.js'));
  128. }
  129. public function testNestedFormula()
  130. {
  131. $this->fm->expects($this->once())
  132. ->method('get')
  133. ->with('foo')
  134. ->will($this->returnValue($this->getMock('Assetic\\Filter\\FilterInterface')));
  135. $inputs = array(
  136. 'css/main.css',
  137. array(
  138. // nested formula
  139. array('css/more.sass'),
  140. array('foo'),
  141. ),
  142. );
  143. $asset = $this->factory->createAsset($inputs, array(), array('output' => 'css/*.css'));
  144. $i = 0;
  145. foreach ($asset as $leaf) {
  146. $i++;
  147. }
  148. $this->assertEquals(2, $i);
  149. }
  150. }