RemoteFilesystemTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\Util;
  12. use Composer\Util\RemoteFilesystem;
  13. use Installer\Exception;
  14. class RemoteFilesystemTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testGetOptionsForUrl()
  17. {
  18. $io = $this->getMock('Composer\IO\IOInterface');
  19. $io
  20. ->expects($this->once())
  21. ->method('hasAuthentication')
  22. ->will($this->returnValue(false))
  23. ;
  24. $res = $this->callGetOptionsForUrl($io, array('http://example.org', array()));
  25. $this->assertTrue(isset($res['http']['header']) && is_array($res['http']['header']), 'getOptions must return an array with headers');
  26. $found = false;
  27. foreach ($res['http']['header'] as $header) {
  28. if (0 === strpos($header, 'User-Agent:')) {
  29. $found = true;
  30. }
  31. }
  32. $this->assertTrue($found, 'getOptions must have a User-Agent header');
  33. }
  34. public function testGetOptionsForUrlWithAuthorization()
  35. {
  36. $io = $this->getMock('Composer\IO\IOInterface');
  37. $io
  38. ->expects($this->once())
  39. ->method('hasAuthentication')
  40. ->will($this->returnValue(true))
  41. ;
  42. $io
  43. ->expects($this->once())
  44. ->method('getAuthentication')
  45. ->will($this->returnValue(array('username' => 'login', 'password' => 'password')))
  46. ;
  47. $options = $this->callGetOptionsForUrl($io, array('http://example.org', array()));
  48. $found = false;
  49. foreach ($options['http']['header'] as $header) {
  50. if (0 === strpos($header, 'Authorization: Basic')) {
  51. $found = true;
  52. }
  53. }
  54. $this->assertTrue($found, 'getOptions must have an Authorization header');
  55. }
  56. public function testGetOptionsForUrlWithStreamOptions()
  57. {
  58. $io = $this->getMock('Composer\IO\IOInterface');
  59. $io
  60. ->expects($this->once())
  61. ->method('hasAuthentication')
  62. ->will($this->returnValue(true))
  63. ;
  64. $streamOptions = array('ssl' => array(
  65. 'allow_self_signed' => true,
  66. ));
  67. $res = $this->callGetOptionsForUrl($io, array('https://example.org', array()), $streamOptions);
  68. $this->assertTrue(isset($res['ssl']) && isset($res['ssl']['allow_self_signed']) && true === $res['ssl']['allow_self_signed'], 'getOptions must return an array with a allow_self_signed set to true');
  69. }
  70. public function testGetOptionsForUrlWithCallOptionsKeepsHeader()
  71. {
  72. $io = $this->getMock('Composer\IO\IOInterface');
  73. $io
  74. ->expects($this->once())
  75. ->method('hasAuthentication')
  76. ->will($this->returnValue(true))
  77. ;
  78. $streamOptions = array('http' => array(
  79. 'header' => 'Foo: bar',
  80. ));
  81. $res = $this->callGetOptionsForUrl($io, array('https://example.org', $streamOptions));
  82. $this->assertTrue(isset($res['http']['header']), 'getOptions must return an array with a http.header key');
  83. $found = false;
  84. foreach ($res['http']['header'] as $header) {
  85. if ($header === 'Foo: bar') {
  86. $found = true;
  87. }
  88. }
  89. $this->assertTrue($found, 'getOptions must have a Foo: bar header');
  90. $this->assertGreaterThan(1, count($res['http']['header']));
  91. }
  92. public function testCallbackGetFileSize()
  93. {
  94. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  95. $this->callCallbackGet($fs, STREAM_NOTIFY_FILE_SIZE_IS, 0, '', 0, 0, 20);
  96. $this->assertAttributeEquals(20, 'bytesMax', $fs);
  97. }
  98. public function testCallbackGetNotifyProgress()
  99. {
  100. $io = $this->getMock('Composer\IO\IOInterface');
  101. $io
  102. ->expects($this->once())
  103. ->method('overwrite')
  104. ;
  105. $fs = new RemoteFilesystem($io);
  106. $this->setAttribute($fs, 'bytesMax', 20);
  107. $this->setAttribute($fs, 'progress', true);
  108. $this->callCallbackGet($fs, STREAM_NOTIFY_PROGRESS, 0, '', 0, 10, 20);
  109. $this->assertAttributeEquals(50, 'lastProgress', $fs);
  110. }
  111. public function testCallbackGetPassesThrough404()
  112. {
  113. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  114. $this->assertNull($this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, 'HTTP/1.1 404 Not Found', 404, 0, 0));
  115. }
  116. public function testCaptureAuthenticationParamsFromUrl()
  117. {
  118. $io = $this->getMock('Composer\IO\IOInterface');
  119. $io->expects($this->once())
  120. ->method('setAuthentication')
  121. ->with($this->equalTo('example.com'), $this->equalTo('user'), $this->equalTo('pass'));
  122. $fs = new RemoteFilesystem($io);
  123. try {
  124. $fs->getContents('example.com', 'http://user:pass@www.example.com/something');
  125. } catch (\Exception $e) {
  126. $this->assertInstanceOf('Composer\Downloader\TransportException', $e);
  127. $this->assertEquals(404, $e->getCode());
  128. }
  129. }
  130. public function testGetContents()
  131. {
  132. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  133. $this->assertContains('testGetContents', $fs->getContents('http://example.org', 'file://'.__FILE__));
  134. }
  135. public function testCopy()
  136. {
  137. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  138. $file = tempnam(sys_get_temp_dir(), 'c');
  139. $this->assertTrue($fs->copy('http://example.org', 'file://'.__FILE__, $file));
  140. $this->assertFileExists($file);
  141. $this->assertContains('testCopy', file_get_contents($file));
  142. unlink($file);
  143. }
  144. protected function callGetOptionsForUrl($io, array $args = array(), array $options = array())
  145. {
  146. $fs = new RemoteFilesystem($io, $options);
  147. $ref = new \ReflectionMethod($fs, 'getOptionsForUrl');
  148. $ref->setAccessible(true);
  149. return $ref->invokeArgs($fs, $args);
  150. }
  151. protected function callCallbackGet(RemoteFilesystem $fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  152. {
  153. $ref = new \ReflectionMethod($fs, 'callbackGet');
  154. $ref->setAccessible(true);
  155. $ref->invoke($fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax);
  156. }
  157. protected function setAttribute($object, $attribute, $value)
  158. {
  159. $attr = new \ReflectionProperty($object, $attribute);
  160. $attr->setAccessible(true);
  161. $attr->setValue($object, $value);
  162. }
  163. }