ArchiveDownloaderTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. public function testGetFileName()
  16. {
  17. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  18. $packageMock->expects($this->any())
  19. ->method('getDistUrl')
  20. ->will($this->returnValue('http://example.com/script.js'))
  21. ;
  22. $downloader = $this->getArchiveDownloaderMock();
  23. $method = new \ReflectionMethod($downloader, 'getFileName');
  24. $method->setAccessible(true);
  25. $first = $method->invoke($downloader, $packageMock, '/path');
  26. $this->assertRegExp('#/path/[a-z0-9]+\.js#', $first);
  27. $this->assertSame($first, $method->invoke($downloader, $packageMock, '/path'));
  28. }
  29. public function testProcessUrl()
  30. {
  31. if (!extension_loaded('openssl')) {
  32. $this->markTestSkipped('Requires openssl');
  33. }
  34. $downloader = $this->getArchiveDownloaderMock();
  35. $method = new \ReflectionMethod($downloader, 'processUrl');
  36. $method->setAccessible(true);
  37. $expected = 'https://github.com/composer/composer/zipball/master';
  38. $url = $method->invoke($downloader, $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(), $expected);
  39. $this->assertEquals($expected, $url);
  40. }
  41. public function testProcessUrl2()
  42. {
  43. if (!extension_loaded('openssl')) {
  44. $this->markTestSkipped('Requires openssl');
  45. }
  46. $downloader = $this->getArchiveDownloaderMock();
  47. $method = new \ReflectionMethod($downloader, 'processUrl');
  48. $method->setAccessible(true);
  49. $expected = 'https://github.com/composer/composer/archive/master.tar.gz';
  50. $url = $method->invoke($downloader, $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(), $expected);
  51. $this->assertEquals($expected, $url);
  52. }
  53. public function testProcessUrl3()
  54. {
  55. if (!extension_loaded('openssl')) {
  56. $this->markTestSkipped('Requires openssl');
  57. }
  58. $downloader = $this->getArchiveDownloaderMock();
  59. $method = new \ReflectionMethod($downloader, 'processUrl');
  60. $method->setAccessible(true);
  61. $expected = 'https://api.github.com/repos/composer/composer/zipball/master';
  62. $url = $method->invoke($downloader, $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(), $expected);
  63. $this->assertEquals($expected, $url);
  64. }
  65. /**
  66. * @dataProvider provideUrls
  67. */
  68. public function testProcessUrlRewriteDist($url)
  69. {
  70. if (!extension_loaded('openssl')) {
  71. $this->markTestSkipped('Requires openssl');
  72. }
  73. $downloader = $this->getArchiveDownloaderMock();
  74. $method = new \ReflectionMethod($downloader, 'processUrl');
  75. $method->setAccessible(true);
  76. $type = strpos($url, 'tar') ? 'tar' : 'zip';
  77. $expected = 'https://api.github.com/repos/composer/composer/'.$type.'ball/ref';
  78. $package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  79. $package->expects($this->any())
  80. ->method('getDistReference')
  81. ->will($this->returnValue('ref'));
  82. $url = $method->invoke($downloader, $package, $url);
  83. $this->assertEquals($expected, $url);
  84. }
  85. public function provideUrls()
  86. {
  87. return array(
  88. array('https://api.github.com/repos/composer/composer/zipball/master'),
  89. array('https://api.github.com/repos/composer/composer/tarball/master'),
  90. array('https://github.com/composer/composer/zipball/master'),
  91. array('https://www.github.com/composer/composer/tarball/master'),
  92. array('https://github.com/composer/composer/archive/master.zip'),
  93. array('https://github.com/composer/composer/archive/master.tar.gz'),
  94. );
  95. }
  96. /**
  97. * @dataProvider provideBitbucketUrls
  98. */
  99. public function testProcessUrlRewriteBitbucketDist($url, $extension)
  100. {
  101. if (!extension_loaded('openssl')) {
  102. $this->markTestSkipped('Requires openssl');
  103. }
  104. $downloader = $this->getArchiveDownloaderMock();
  105. $method = new \ReflectionMethod($downloader, 'processUrl');
  106. $method->setAccessible(true);
  107. $url .= '.' . $extension;
  108. $expected = 'https://bitbucket.org/davereid/drush-virtualhost/get/ref.' . $extension;
  109. $package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  110. $package->expects($this->any())
  111. ->method('getDistReference')
  112. ->will($this->returnValue('ref'));
  113. $url = $method->invoke($downloader, $package, $url);
  114. $this->assertEquals($expected, $url);
  115. }
  116. public function provideBitbucketUrls()
  117. {
  118. return array(
  119. array('https://bitbucket.org/davereid/drush-virtualhost/get/77ca490c26ac818e024d1138aa8bd3677d1ef21f', 'zip'),
  120. array('https://bitbucket.org/davereid/drush-virtualhost/get/master', 'tar.gz'),
  121. array('https://bitbucket.org/davereid/drush-virtualhost/get/v1.0', 'tar.bz2'),
  122. );
  123. }
  124. private function getArchiveDownloaderMock()
  125. {
  126. return $this->getMockForAbstractClass(
  127. 'Composer\Downloader\ArchiveDownloader',
  128. array($this->getMockBuilder('Composer\IO\IOInterface')->getMock(), $this->getMockBuilder('Composer\Config')->getMock())
  129. );
  130. }
  131. }