Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

203 linhas
6.3KB

  1. <?php
  2. /*
  3. * This file is part of the Fxp Composer Asset Plugin package.
  4. *
  5. * (c) François Pluchino <francois.pluchino@gmail.com>
  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 Fxp\Composer\AssetPlugin\Tests\Installer;
  11. use Composer\Composer;
  12. use Composer\Config;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Package\RootPackageInterface;
  15. use Fxp\Composer\AssetPlugin\Installer\IgnoreFactory;
  16. use Fxp\Composer\AssetPlugin\Installer\IgnoreManager;
  17. /**
  18. * Tests of ignore factory.
  19. *
  20. * @author François Pluchino <francois.pluchino@gmail.com>
  21. */
  22. class IgnoreFactoryTest extends \PHPUnit_Framework_TestCase
  23. {
  24. /**
  25. * @var Composer|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $composer;
  28. /**
  29. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $config;
  32. /**
  33. * @var RootPackageInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $rootPackage;
  36. /**
  37. * @var PackageInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $package;
  40. public function setUp()
  41. {
  42. $this->config = $this->getMock('Composer\Config');
  43. $this->config->expects($this->any())
  44. ->method('get')
  45. ->will($this->returnCallback(function ($key) {
  46. switch ($key) {
  47. case 'cache-repo-dir':
  48. return sys_get_temp_dir().'/composer-test-repo-cache';
  49. case 'vendor-dir':
  50. return sys_get_temp_dir().'/composer-test/vendor';
  51. }
  52. return;
  53. }));
  54. $this->rootPackage = $this->getMock('Composer\Package\RootPackageInterface');
  55. $this->package = $this->getMock('Composer\Package\PackageInterface');
  56. $this->package->expects($this->any())
  57. ->method('getName')
  58. ->will($this->returnValue('foo-asset/foo'));
  59. $this->composer = $this->getMock('Composer\Composer');
  60. $this->composer->expects($this->any())
  61. ->method('getPackage')
  62. ->will($this->returnValue($this->rootPackage));
  63. $this->composer->expects($this->any())
  64. ->method('getConfig')
  65. ->will($this->returnValue($this->config));
  66. }
  67. public function tearDown()
  68. {
  69. $this->composer = null;
  70. $this->config = null;
  71. $this->rootPackage = null;
  72. $this->package = null;
  73. }
  74. public function testCreateWithoutIgnoreFiles()
  75. {
  76. $manager = IgnoreFactory::create($this->composer, $this->package);
  77. $this->assertTrue($manager->isEnabled());
  78. $this->assertFalse($manager->hasPattern());
  79. $this->validateInstallDir($manager, $this->config->get('vendor-dir').'/'.$this->package->getName());
  80. }
  81. public function testCreateWithIgnoreFiles()
  82. {
  83. $extra = array(
  84. 'asset-ignore-files' => array(
  85. 'foo-asset/foo' => array(
  86. 'PATTERN',
  87. ),
  88. 'foo-asset/bar' => array(),
  89. ),
  90. );
  91. $this->rootPackage->expects($this->any())
  92. ->method('getExtra')
  93. ->will($this->returnValue($extra));
  94. $manager = IgnoreFactory::create($this->composer, $this->package);
  95. $this->assertTrue($manager->isEnabled());
  96. $this->assertTrue($manager->hasPattern());
  97. $this->validateInstallDir($manager, $this->config->get('vendor-dir').'/'.$this->package->getName());
  98. }
  99. public function testCreateWithCustomInstallDir()
  100. {
  101. $installDir = 'web/assets/';
  102. $manager = IgnoreFactory::create($this->composer, $this->package, $installDir);
  103. $this->assertTrue($manager->isEnabled());
  104. $this->assertFalse($manager->hasPattern());
  105. $this->validateInstallDir($manager, rtrim($installDir, '/'));
  106. }
  107. public function testCreateWithEnablingOfIgnoreFiles()
  108. {
  109. $extra = array(
  110. 'asset-ignore-files' => array(
  111. 'foo-asset/foo' => true,
  112. 'foo-asset/bar' => array(),
  113. ),
  114. );
  115. $this->rootPackage->expects($this->any())
  116. ->method('getExtra')
  117. ->will($this->returnValue($extra));
  118. $manager = IgnoreFactory::create($this->composer, $this->package);
  119. $this->assertTrue($manager->isEnabled());
  120. $this->assertFalse($manager->hasPattern());
  121. $this->validateInstallDir($manager, $this->config->get('vendor-dir').'/'.$this->package->getName());
  122. }
  123. public function testCreateWithDisablingOfIgnoreFiles()
  124. {
  125. $extra = array(
  126. 'asset-ignore-files' => array(
  127. 'foo-asset/foo' => false,
  128. 'foo-asset/bar' => array(),
  129. ),
  130. );
  131. $this->rootPackage->expects($this->any())
  132. ->method('getExtra')
  133. ->will($this->returnValue($extra));
  134. $manager = IgnoreFactory::create($this->composer, $this->package);
  135. $this->assertFalse($manager->isEnabled());
  136. $this->assertFalse($manager->hasPattern());
  137. $this->validateInstallDir($manager, $this->config->get('vendor-dir').'/'.$this->package->getName());
  138. }
  139. public function testCreateWithCustomIgnoreSection()
  140. {
  141. $extra = array(
  142. 'custom-ignore-files' => array(
  143. 'foo-asset/foo' => array(
  144. 'PATTERN',
  145. ),
  146. 'foo-asset/bar' => array(),
  147. ),
  148. );
  149. $this->rootPackage->expects($this->any())
  150. ->method('getExtra')
  151. ->will($this->returnValue($extra));
  152. $manager = IgnoreFactory::create($this->composer, $this->package, null, 'custom-ignore-files');
  153. $this->assertTrue($manager->isEnabled());
  154. $this->assertTrue($manager->hasPattern());
  155. $this->validateInstallDir($manager, $this->config->get('vendor-dir').'/'.$this->package->getName());
  156. }
  157. /**
  158. * @param IgnoreManager $manager
  159. * @param string $installDir
  160. */
  161. protected function validateInstallDir(IgnoreManager $manager, $installDir)
  162. {
  163. $ref = new \ReflectionClass($manager);
  164. $prop = $ref->getProperty('installDir');
  165. $prop->setAccessible(true);
  166. $this->assertSame($installDir, $prop->getValue($manager));
  167. }
  168. }