ArchiveDownloaderTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Downloader;
  12. use Composer\Test\TestCase;
  13. class ArchiveDownloaderTest extends TestCase
  14. {
  15. protected $config;
  16. public function testGetFileName()
  17. {
  18. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  19. $packageMock->expects($this->any())
  20. ->method('getDistUrl')
  21. ->will($this->returnValue('http://example.com/script.js'))
  22. ;
  23. $downloader = $this->getArchiveDownloaderMock();
  24. $method = new \ReflectionMethod($downloader, 'getFileName');
  25. $method->setAccessible(true);
  26. $this->config->expects($this->any())
  27. ->method('get')
  28. ->with('vendor-dir')
  29. ->will($this->returnValue('/vendor'));
  30. $first = $method->invoke($downloader, $packageMock, '/path');
  31. $this->assertRegExp('#/vendor/composer/tmp-[a-z0-9]+\.js#', $first);
  32. $this->assertSame($first, $method->invoke($downloader, $packageMock, '/path'));
  33. }
  34. public function testProcessUrl()
  35. {
  36. if (!extension_loaded('openssl')) {
  37. $this->markTestSkipped('Requires openssl');
  38. }
  39. $downloader = $this->getArchiveDownloaderMock();
  40. $method = new \ReflectionMethod($downloader, 'processUrl');
  41. $method->setAccessible(true);
  42. $expected = 'https://github.com/composer/composer/zipball/master';
  43. $url = $method->invoke($downloader, $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(), $expected);
  44. $this->assertEquals($expected, $url);
  45. }
  46. public function testProcessUrl2()
  47. {
  48. if (!extension_loaded('openssl')) {
  49. $this->markTestSkipped('Requires openssl');
  50. }
  51. $downloader = $this->getArchiveDownloaderMock();
  52. $method = new \ReflectionMethod($downloader, 'processUrl');
  53. $method->setAccessible(true);
  54. $expected = 'https://github.com/composer/composer/archive/master.tar.gz';
  55. $url = $method->invoke($downloader, $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(), $expected);
  56. $this->assertEquals($expected, $url);
  57. }
  58. public function testProcessUrl3()
  59. {
  60. if (!extension_loaded('openssl')) {
  61. $this->markTestSkipped('Requires openssl');
  62. }
  63. $downloader = $this->getArchiveDownloaderMock();
  64. $method = new \ReflectionMethod($downloader, 'processUrl');
  65. $method->setAccessible(true);
  66. $expected = 'https://api.github.com/repos/composer/composer/zipball/master';
  67. $url = $method->invoke($downloader, $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(), $expected);
  68. $this->assertEquals($expected, $url);
  69. }
  70. /**
  71. * @dataProvider provideUrls
  72. */
  73. public function testProcessUrlRewriteDist($url)
  74. {
  75. if (!extension_loaded('openssl')) {
  76. $this->markTestSkipped('Requires openssl');
  77. }
  78. $downloader = $this->getArchiveDownloaderMock();
  79. $method = new \ReflectionMethod($downloader, 'processUrl');
  80. $method->setAccessible(true);
  81. $type = strpos($url, 'tar') ? 'tar' : 'zip';
  82. $expected = 'https://api.github.com/repos/composer/composer/'.$type.'ball/ref';
  83. $package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  84. $package->expects($this->any())
  85. ->method('getDistReference')
  86. ->will($this->returnValue('ref'));
  87. $url = $method->invoke($downloader, $package, $url);
  88. $this->assertEquals($expected, $url);
  89. }
  90. public function provideUrls()
  91. {
  92. return array(
  93. array('https://api.github.com/repos/composer/composer/zipball/master'),
  94. array('https://api.github.com/repos/composer/composer/tarball/master'),
  95. array('https://github.com/composer/composer/zipball/master'),
  96. array('https://www.github.com/composer/composer/tarball/master'),
  97. array('https://github.com/composer/composer/archive/master.zip'),
  98. array('https://github.com/composer/composer/archive/master.tar.gz'),
  99. );
  100. }
  101. /**
  102. * @dataProvider provideBitbucketUrls
  103. */
  104. public function testProcessUrlRewriteBitbucketDist($url, $extension)
  105. {
  106. if (!extension_loaded('openssl')) {
  107. $this->markTestSkipped('Requires openssl');
  108. }
  109. $downloader = $this->getArchiveDownloaderMock();
  110. $method = new \ReflectionMethod($downloader, 'processUrl');
  111. $method->setAccessible(true);
  112. $url .= '.' . $extension;
  113. $expected = 'https://bitbucket.org/davereid/drush-virtualhost/get/ref.' . $extension;
  114. $package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  115. $package->expects($this->any())
  116. ->method('getDistReference')
  117. ->will($this->returnValue('ref'));
  118. $url = $method->invoke($downloader, $package, $url);
  119. $this->assertEquals($expected, $url);
  120. }
  121. public function provideBitbucketUrls()
  122. {
  123. return array(
  124. array('https://bitbucket.org/davereid/drush-virtualhost/get/77ca490c26ac818e024d1138aa8bd3677d1ef21f', 'zip'),
  125. array('https://bitbucket.org/davereid/drush-virtualhost/get/master', 'tar.gz'),
  126. array('https://bitbucket.org/davereid/drush-virtualhost/get/v1.0', 'tar.bz2'),
  127. );
  128. }
  129. private function getArchiveDownloaderMock()
  130. {
  131. return $this->getMockForAbstractClass(
  132. 'Composer\Downloader\ArchiveDownloader',
  133. array(
  134. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
  135. $this->config = $this->getMockBuilder('Composer\Config')->getMock(),
  136. new \Composer\Util\HttpDownloader($io, $this->config),
  137. )
  138. );
  139. }
  140. }