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.

195 lines
6.1KB

  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\HgBitbucketDriver;
  17. /**
  18. * Tests of vcs mercurial bitbucket repository.
  19. *
  20. * @author François Pluchino <francois.pluchino@gmail.com>
  21. */
  22. class HgBitbucketDriverTest 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. $repoUrl = 'https://bitbucket.org/composer-test/repo-name';
  57. $identifier = 'v0.0.0';
  58. $sha = 'SOMESHA';
  59. $io = $this->getMock('Composer\IO\IOInterface');
  60. $io->expects($this->any())
  61. ->method('isInteractive')
  62. ->will($this->returnValue(true));
  63. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  64. ->setConstructorArgs(array($io))
  65. ->getMock();
  66. $remoteFilesystem->expects($this->at(0))
  67. ->method('getContents')
  68. ->with($this->equalTo('bitbucket.org'), $this->equalTo($this->getScheme('https://bitbucket.org/api/1.0/repositories/composer-test/repo-name/tags')), $this->equalTo(false))
  69. ->will($this->returnValue($this->createJsonComposer(array('tip' => array('raw_node' => 'test_master')))));
  70. $remoteFilesystem->expects($this->at(1))
  71. ->method('getContents')
  72. ->with($this->equalTo('bitbucket.org'), $this->equalTo($this->getScheme($repoUrl).'/raw/'.$identifier.'/'.$filename), $this->equalTo(false))
  73. ->will($this->returnValue($this->createJsonComposer(array())));
  74. $repoConfig = array(
  75. 'url' => $repoUrl,
  76. 'asset-type' => $type,
  77. 'filename' => $filename,
  78. );
  79. /* @var IOInterface $io */
  80. /* @var RemoteFilesystem $remoteFilesystem */
  81. $driver = new HgBitbucketDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
  82. $driver->initialize();
  83. $this->setAttribute($driver, 'tags', array($identifier => $sha));
  84. $this->assertEquals('test_master', $driver->getRootIdentifier());
  85. $dist = $driver->getDist($sha);
  86. $this->assertEquals('zip', $dist['type']);
  87. $this->assertEquals($this->getScheme($repoUrl).'/get/SOMESHA.zip', $dist['url']);
  88. $this->assertEquals($sha, $dist['reference']);
  89. $source = $driver->getSource($sha);
  90. $this->assertEquals('hg', $source['type']);
  91. $this->assertEquals($repoUrl, $source['url']);
  92. $this->assertEquals($sha, $source['reference']);
  93. $driver->getComposerInformation($identifier);
  94. }
  95. /**
  96. * @dataProvider getAssetTypes
  97. */
  98. public function testPublicRepositoryWithEmptyComposer($type, $filename)
  99. {
  100. $repoUrl = 'https://bitbucket.org/composer-test/repo-name';
  101. $identifier = 'v0.0.0';
  102. $io = $this->getMock('Composer\IO\IOInterface');
  103. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  104. ->setConstructorArgs(array($io))
  105. ->getMock();
  106. $remoteFilesystem->expects($this->at(0))
  107. ->method('getContents')
  108. ->with($this->equalTo('bitbucket.org'), $this->equalTo($this->getScheme($repoUrl).'/raw/'.$identifier.'/'.$filename), $this->equalTo(false))
  109. ->will($this->throwException(new TransportException('Not Found', 404)));
  110. $repoConfig = array(
  111. 'url' => $repoUrl,
  112. 'asset-type' => $type,
  113. 'filename' => $filename,
  114. );
  115. /* @var IOInterface $io */
  116. /* @var RemoteFilesystem $remoteFilesystem */
  117. $driver = new HgBitbucketDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
  118. $driver->initialize();
  119. $validEmpty = array(
  120. '_nonexistent_package' => true,
  121. );
  122. $this->assertSame($validEmpty, $driver->getComposerInformation($identifier));
  123. }
  124. /**
  125. * @param object $object
  126. * @param string $attribute
  127. * @param mixed $value
  128. */
  129. protected function setAttribute($object, $attribute, $value)
  130. {
  131. $attr = new \ReflectionProperty($object, $attribute);
  132. $attr->setAccessible(true);
  133. $attr->setValue($object, $value);
  134. }
  135. /**
  136. * Creates the json composer content.
  137. *
  138. * @param array $content The composer content
  139. * @param string $name The name of repository
  140. *
  141. * @return string The json content
  142. */
  143. protected function createJsonComposer(array $content, $name = 'repo-name')
  144. {
  145. return json_encode(array_merge_recursive($content, array(
  146. 'name' => $name,
  147. )));
  148. }
  149. /**
  150. * Get the url with https or http protocol depending on SSL support.
  151. *
  152. * @param string $url
  153. *
  154. * @return string The correct url
  155. */
  156. protected function getScheme($url)
  157. {
  158. if (extension_loaded('openssl')) {
  159. return $url;
  160. }
  161. return str_replace('https:', 'http:', $url);
  162. }
  163. }