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.

198 lines
6.2KB

  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\Downloader\TransportException;
  12. use Composer\IO\IOInterface;
  13. use Composer\Util\Filesystem;
  14. use Composer\Config;
  15. use Composer\Util\RemoteFilesystem;
  16. use Fxp\Composer\AssetPlugin\Repository\Vcs\GitBitbucketDriver;
  17. /**
  18. * Tests of vcs git bitbucket repository.
  19. *
  20. * @author François Pluchino <francois.pluchino@gmail.com>
  21. */
  22. class GitBitbucketDriverTest extends \PHPUnit_Framework_TestCase
  23. {
  24. /**
  25. * @var Config
  26. */
  27. private $config;
  28. public function setUp()
  29. {
  30. $this->config = new Config();
  31. $this->config->merge(array(
  32. 'config' => array(
  33. 'home' => sys_get_temp_dir().'/composer-test',
  34. 'cache-repo-dir' => sys_get_temp_dir().'/composer-test-cache',
  35. ),
  36. ));
  37. }
  38. public function tearDown()
  39. {
  40. $fs = new Filesystem();
  41. $fs->removeDirectory(sys_get_temp_dir().'/composer-test');
  42. $fs->removeDirectory(sys_get_temp_dir().'/composer-test-cache');
  43. }
  44. public function getAssetTypes()
  45. {
  46. return array(
  47. array('npm', 'package.json'),
  48. array('bower', 'bower.json'),
  49. );
  50. }
  51. /**
  52. * @dataProvider getAssetTypes
  53. */
  54. public function testPublicRepositoryWithComposer($type, $filename)
  55. {
  56. $repoBaseUrl = 'https://bitbucket.org/composer-test/repo-name';
  57. $repoUrl = $repoBaseUrl.'.git';
  58. $repoApiUrl = 'https://api.bitbucket.org/1.0/repositories/composer-test/repo-name';
  59. $identifier = 'v0.0.0';
  60. $sha = 'SOMESHA';
  61. $io = $this->getMock('Composer\IO\IOInterface');
  62. $io->expects($this->any())
  63. ->method('isInteractive')
  64. ->will($this->returnValue(true));
  65. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  66. ->setConstructorArgs(array($io))
  67. ->getMock();
  68. $remoteFilesystem->expects($this->at(0))
  69. ->method('getContents')
  70. ->with($this->equalTo('bitbucket.org'), $this->equalTo($this->getScheme($repoApiUrl)), $this->equalTo(false))
  71. ->will($this->returnValue($this->createJsonComposer(array('main_branch' => 'test_master'))));
  72. $remoteFilesystem->expects($this->at(1))
  73. ->method('getContents')
  74. ->with($this->equalTo('bitbucket.org'), $this->equalTo($this->getScheme($repoBaseUrl).'/raw/'.$identifier.'/'.$filename), $this->equalTo(false))
  75. ->will($this->returnValue($this->createJsonComposer(array())));
  76. $repoConfig = array(
  77. 'url' => $repoUrl,
  78. 'asset-type' => $type,
  79. 'filename' => $filename,
  80. );
  81. /* @var IOInterface $io */
  82. /* @var RemoteFilesystem $remoteFilesystem */
  83. $driver = new GitBitbucketDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
  84. $driver->initialize();
  85. $this->setAttribute($driver, 'tags', array($identifier => $sha));
  86. $this->assertEquals('test_master', $driver->getRootIdentifier());
  87. $dist = $driver->getDist($sha);
  88. $this->assertEquals('zip', $dist['type']);
  89. $this->assertEquals($this->getScheme($repoBaseUrl).'/get/SOMESHA.zip', $dist['url']);
  90. $this->assertEquals($sha, $dist['reference']);
  91. $source = $driver->getSource($sha);
  92. $this->assertEquals('git', $source['type']);
  93. $this->assertEquals($repoUrl, $source['url']);
  94. $this->assertEquals($sha, $source['reference']);
  95. $driver->getComposerInformation($identifier);
  96. }
  97. /**
  98. * @dataProvider getAssetTypes
  99. */
  100. public function testPublicRepositoryWithEmptyComposer($type, $filename)
  101. {
  102. $repoBaseUrl = 'https://bitbucket.org/composer-test/repo-name';
  103. $repoUrl = $repoBaseUrl.'.git';
  104. $identifier = 'v0.0.0';
  105. $io = $this->getMock('Composer\IO\IOInterface');
  106. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  107. ->setConstructorArgs(array($io))
  108. ->getMock();
  109. $remoteFilesystem->expects($this->at(0))
  110. ->method('getContents')
  111. ->with($this->equalTo('bitbucket.org'), $this->equalTo($this->getScheme($repoBaseUrl).'/raw/'.$identifier.'/'.$filename), $this->equalTo(false))
  112. ->will($this->throwException(new TransportException('Not Found', 404)));
  113. $repoConfig = array(
  114. 'url' => $repoUrl,
  115. 'asset-type' => $type,
  116. 'filename' => $filename,
  117. );
  118. /* @var IOInterface $io */
  119. /* @var RemoteFilesystem $remoteFilesystem */
  120. $driver = new GitBitbucketDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
  121. $driver->initialize();
  122. $validEmpty = array(
  123. '_nonexistent_package' => true,
  124. );
  125. $this->assertSame($validEmpty, $driver->getComposerInformation($identifier));
  126. }
  127. /**
  128. * @param object $object
  129. * @param string $attribute
  130. * @param mixed $value
  131. */
  132. protected function setAttribute($object, $attribute, $value)
  133. {
  134. $attr = new \ReflectionProperty($object, $attribute);
  135. $attr->setAccessible(true);
  136. $attr->setValue($object, $value);
  137. }
  138. /**
  139. * Creates the json composer content.
  140. *
  141. * @param array $content The composer content
  142. * @param string $name The name of repository
  143. *
  144. * @return string The json content
  145. */
  146. protected function createJsonComposer(array $content, $name = 'repo-name')
  147. {
  148. return json_encode(array_merge_recursive($content, array(
  149. 'name' => $name,
  150. )));
  151. }
  152. /**
  153. * Get the url with https or http protocol depending on SSL support.
  154. *
  155. * @param string $url
  156. *
  157. * @return string The correct url
  158. */
  159. protected function getScheme($url)
  160. {
  161. if (extension_loaded('openssl')) {
  162. return $url;
  163. }
  164. return str_replace('https:', 'http:', $url);
  165. }
  166. }