You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.4KB

  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\Composer;
  11. use Composer\Composer;
  12. use Composer\DependencyResolver\Operation\InstallOperation;
  13. use Composer\DependencyResolver\Operation\OperationInterface;
  14. use Composer\DependencyResolver\Operation\UpdateOperation;
  15. use Composer\DependencyResolver\PolicyInterface;
  16. use Composer\DependencyResolver\Pool;
  17. use Composer\DependencyResolver\Request;
  18. use Composer\IO\IOInterface;
  19. use Composer\Package\PackageInterface;
  20. use Composer\Installer\PackageEvent;
  21. use Composer\Repository\CompositeRepository;
  22. use Fxp\Composer\AssetPlugin\Composer\ScriptHandler;
  23. /**
  24. * Tests for the composer script handler.
  25. *
  26. * @author François Pluchino <francois.pluchino@gmail.com>
  27. */
  28. class ScriptHandlerTest extends \PHPUnit_Framework_TestCase
  29. {
  30. /**
  31. * @var Composer|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $composer;
  34. /**
  35. * @var IOInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $io;
  38. /**
  39. * @var OperationInterface|InstallOperation|UpdateOperation|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $operation;
  42. /**
  43. * @var PackageInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $package;
  46. public function setUp()
  47. {
  48. $this->composer = $this->getMock('Composer\Composer');
  49. $this->io = $this->getMock('Composer\IO\IOInterface');
  50. $this->package = $this->getMock('Composer\Package\PackageInterface');
  51. $config = $this->getMock('Composer\Config');
  52. $config->expects($this->any())
  53. ->method('get')
  54. ->will($this->returnCallback(function ($key) {
  55. $val = null;
  56. switch ($key) {
  57. case 'cache-repo-dir':
  58. return sys_get_temp_dir().'/composer-test-repo-cache';
  59. case 'vendor-dir':
  60. return sys_get_temp_dir().'/composer-test/vendor';
  61. }
  62. return $val;
  63. }));
  64. $rootPackage = $this->getMock('Composer\Package\RootPackageInterface');
  65. $this->composer->expects($this->any())
  66. ->method('getConfig')
  67. ->will($this->returnValue($config));
  68. $this->composer->expects($this->any())
  69. ->method('getPackage')
  70. ->will($this->returnValue($rootPackage));
  71. }
  72. public function tearDown()
  73. {
  74. $this->composer = null;
  75. $this->io = null;
  76. $this->operation = null;
  77. $this->package = null;
  78. }
  79. public function getPackageComposerTypes()
  80. {
  81. return array(
  82. array('npm-asset-library'),
  83. array('bower-asset-library'),
  84. array('library'),
  85. );
  86. }
  87. /**
  88. * @dataProvider getPackageComposerTypes
  89. */
  90. public function testDeleteIgnoreFiles($composerType)
  91. {
  92. $this->operation = $this->getMock('Composer\DependencyResolver\Operation\OperationInterface');
  93. ScriptHandler::deleteIgnoredFiles($this->createEvent($composerType));
  94. }
  95. /**
  96. * @dataProvider getPackageComposerTypes
  97. */
  98. public function testDeleteIgnoreFilesWithInstallOperation($composerType)
  99. {
  100. $this->operation = $this->getMockBuilder('Composer\DependencyResolver\Operation\InstallOperation')
  101. ->disableOriginalConstructor()
  102. ->getMock();
  103. ScriptHandler::deleteIgnoredFiles($this->createEvent($composerType));
  104. }
  105. /**
  106. * @dataProvider getPackageComposerTypes
  107. */
  108. public function testDeleteIgnoreFilesWithUpdateOperation($composerType)
  109. {
  110. $this->operation = $this->getMockBuilder('Composer\DependencyResolver\Operation\UpdateOperation')
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. ScriptHandler::deleteIgnoredFiles($this->createEvent($composerType));
  114. }
  115. /**
  116. * @param string $composerType
  117. *
  118. * @return PackageEvent
  119. */
  120. protected function createEvent($composerType)
  121. {
  122. $this->package->expects($this->any())
  123. ->method('getType')
  124. ->will($this->returnValue($composerType));
  125. $this->operation->expects($this->any())
  126. ->method('getTargetPackage')
  127. ->will($this->returnValue($this->package));
  128. $this->operation->expects($this->any())
  129. ->method('getPackage')
  130. ->will($this->returnValue($this->package));
  131. /* @var PolicyInterface $policy */
  132. $policy = $this->getMock('Composer\DependencyResolver\PolicyInterface');
  133. /* @var Pool $pool */
  134. $pool = $this->getMockBuilder('Composer\DependencyResolver\Pool')->disableOriginalConstructor()->getMock();
  135. /* @var CompositeRepository $installedRepo */
  136. $installedRepo = $this->getMockBuilder('Composer\Repository\CompositeRepository')->disableOriginalConstructor()->getMock();
  137. /* @var Request $request */
  138. $request = $this->getMockBuilder('Composer\DependencyResolver\Request')->disableOriginalConstructor()->getMock();
  139. $operations = array($this->getMock('Composer\DependencyResolver\Operation\OperationInterface'));
  140. return new PackageEvent('foo-event', $this->composer, $this->io, true, $policy, $pool, $installedRepo, $request, $operations, $this->operation);
  141. }
  142. }