Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

GitDriverTest.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\Repository\Vcs;
  11. use Composer\IO\IOInterface;
  12. use Composer\Util\Filesystem;
  13. use Composer\Config;
  14. use Composer\Util\ProcessExecutor;
  15. use Fxp\Composer\AssetPlugin\Repository\Vcs\GitDriver;
  16. /**
  17. * Tests of vcs git repository.
  18. *
  19. * @author François Pluchino <francois.pluchino@gmail.com>
  20. */
  21. class GitDriverTest extends \PHPUnit_Framework_TestCase
  22. {
  23. /**
  24. * @var Config
  25. */
  26. private $config;
  27. public function setUp()
  28. {
  29. $this->config = new Config();
  30. $this->config->merge(array(
  31. 'config' => array(
  32. 'home' => sys_get_temp_dir().'/composer-test',
  33. 'cache-repo-dir' => sys_get_temp_dir().'/composer-test-cache',
  34. ),
  35. ));
  36. }
  37. public function tearDown()
  38. {
  39. $fs = new Filesystem();
  40. $fs->removeDirectory(sys_get_temp_dir().'/composer-test');
  41. $fs->removeDirectory(sys_get_temp_dir().'/composer-test-cache');
  42. }
  43. public function getAssetTypes()
  44. {
  45. return array(
  46. array('npm', 'package.json'),
  47. array('bower', 'bower.json'),
  48. );
  49. }
  50. /**
  51. * @dataProvider getAssetTypes
  52. */
  53. public function testPublicRepositoryWithEmptyComposer($type, $filename)
  54. {
  55. $repoUrl = 'https://github.com/francoispluchino/composer-asset-plugin';
  56. $identifier = 'v0.0.0';
  57. $io = $this->getMock('Composer\IO\IOInterface');
  58. $repoConfig = array(
  59. 'url' => $repoUrl,
  60. 'asset-type' => $type,
  61. 'filename' => $filename,
  62. );
  63. $process = $this->getMock('Composer\Util\ProcessExecutor');
  64. $process->expects($this->any())
  65. ->method('splitLines')
  66. ->will($this->returnValue(array()));
  67. $process->expects($this->any())
  68. ->method('execute')
  69. ->will($this->returnCallback(function () {
  70. return 0;
  71. }));
  72. /* @var IOInterface $io */
  73. /* @var ProcessExecutor $process */
  74. $gitDriver = new GitDriver($repoConfig, $io, $this->config, $process, null);
  75. $gitDriver->initialize();
  76. $validEmpty = array(
  77. '_nonexistent_package' => true,
  78. );
  79. $this->assertSame($validEmpty, $gitDriver->getComposerInformation($identifier));
  80. }
  81. /**
  82. * @dataProvider getAssetTypes
  83. */
  84. public function testPublicRepositoryWithCodeCache($type, $filename)
  85. {
  86. $repoUrl = 'https://github.com/francoispluchino/composer-asset-plugin.git';
  87. $identifier = '92bebbfdcde75ef2368317830e54b605bc938123';
  88. $repoConfig = array(
  89. 'url' => $repoUrl,
  90. 'asset-type' => $type,
  91. 'filename' => $filename,
  92. );
  93. $io = $this->getMock('Composer\IO\IOInterface');
  94. $process = $this->getMock('Composer\Util\ProcessExecutor');
  95. $process->expects($this->any())
  96. ->method('splitLines')
  97. ->will($this->returnValue(array()));
  98. $process->expects($this->any())
  99. ->method('execute')
  100. ->will($this->returnCallback(function ($command, &$output = null) use ($identifier, $repoConfig) {
  101. if ($command === sprintf('git show %s', sprintf('%s:%s', escapeshellarg($identifier), $repoConfig['filename']))) {
  102. $output = '{"name": "foo"}';
  103. } elseif (false !== strpos($command, 'git log')) {
  104. $date = new \DateTime(null, new \DateTimeZone('UTC'));
  105. $output = $date->getTimestamp();
  106. }
  107. return 0;
  108. }));
  109. /* @var IOInterface $io */
  110. /* @var ProcessExecutor $process */
  111. $gitDriver = new GitDriver($repoConfig, $io, $this->config, $process, null);
  112. $gitDriver->initialize();
  113. $composer1 = $gitDriver->getComposerInformation($identifier);
  114. $composer2 = $gitDriver->getComposerInformation($identifier);
  115. $this->assertNotNull($composer1);
  116. $this->assertNotNull($composer2);
  117. $this->assertSame($composer1, $composer2);
  118. }
  119. /**
  120. * @dataProvider getAssetTypes
  121. */
  122. public function testPublicRepositoryWithFilesystemCache($type, $filename)
  123. {
  124. $repoUrl = 'https://github.com/francoispluchino/composer-asset-plugin.git';
  125. $identifier = '92bebbfdcde75ef2368317830e54b605bc938123';
  126. $repoConfig = array(
  127. 'url' => $repoUrl,
  128. 'asset-type' => $type,
  129. 'filename' => $filename,
  130. );
  131. $io = $this->getMock('Composer\IO\IOInterface');
  132. $process = $this->getMock('Composer\Util\ProcessExecutor');
  133. $process->expects($this->any())
  134. ->method('splitLines')
  135. ->will($this->returnValue(array()));
  136. $process->expects($this->any())
  137. ->method('execute')
  138. ->will($this->returnCallback(function ($command, &$output = null) use ($identifier, $repoConfig) {
  139. if ($command === sprintf('git show %s', sprintf('%s:%s', escapeshellarg($identifier), $repoConfig['filename']))) {
  140. $output = '{"name": "foo"}';
  141. } elseif (false !== strpos($command, 'git log')) {
  142. $date = new \DateTime(null, new \DateTimeZone('UTC'));
  143. $output = $date->getTimestamp();
  144. }
  145. return 0;
  146. }));
  147. /* @var IOInterface $io */
  148. /* @var ProcessExecutor $process */
  149. $gitDriver1 = new GitDriver($repoConfig, $io, $this->config, $process, null);
  150. $gitDriver2 = new GitDriver($repoConfig, $io, $this->config, $process, null);
  151. $gitDriver1->initialize();
  152. $gitDriver2->initialize();
  153. $composer1 = $gitDriver1->getComposerInformation($identifier);
  154. $composer2 = $gitDriver2->getComposerInformation($identifier);
  155. $this->assertNotNull($composer1);
  156. $this->assertNotNull($composer2);
  157. $this->assertSame($composer1, $composer2);
  158. }
  159. protected function setAttribute($object, $attribute, $value)
  160. {
  161. $attr = new \ReflectionProperty($object, $attribute);
  162. $attr->setAccessible(true);
  163. $attr->setValue($object, $value);
  164. }
  165. }