RemoteFilesystemTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 testCallbackGetNotifyFailure404()
  112. {
  113. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  114. try {
  115. $this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, 'HTTP/1.1 404 Not Found', 404, 0, 0);
  116. $this->fail();
  117. } catch (\Exception $e) {
  118. $this->assertInstanceOf('Composer\Downloader\TransportException', $e);
  119. $this->assertEquals(404, $e->getCode());
  120. $this->assertContains('HTTP/1.1 404 Not Found', $e->getMessage());
  121. }
  122. }
  123. public function testCaptureAuthenticationParamsFromUrl()
  124. {
  125. $io = $this->getMock('Composer\IO\IOInterface');
  126. $io->expects($this->once())
  127. ->method('setAuthentication')
  128. ->with($this->equalTo('example.com'), $this->equalTo('user'), $this->equalTo('pass'));
  129. $fs = new RemoteFilesystem($io);
  130. try {
  131. $fs->getContents('example.com', 'http://user:pass@www.example.com/something');
  132. } catch (\Exception $e) {
  133. $this->assertInstanceOf('Composer\Downloader\TransportException', $e);
  134. $this->assertEquals(404, $e->getCode());
  135. $this->assertContains('404 Not Found', $e->getMessage());
  136. }
  137. }
  138. public function testGetContents()
  139. {
  140. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  141. $this->assertContains('testGetContents', $fs->getContents('http://example.org', 'file://'.__FILE__));
  142. }
  143. public function testCopy()
  144. {
  145. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  146. $file = tempnam(sys_get_temp_dir(), 'c');
  147. $this->assertTrue($fs->copy('http://example.org', 'file://'.__FILE__, $file));
  148. $this->assertFileExists($file);
  149. $this->assertContains('testCopy', file_get_contents($file));
  150. unlink($file);
  151. }
  152. protected function callGetOptionsForUrl($io, array $args = array(), array $options = array())
  153. {
  154. $fs = new RemoteFilesystem($io, $options);
  155. $ref = new \ReflectionMethod($fs, 'getOptionsForUrl');
  156. $ref->setAccessible(true);
  157. return $ref->invokeArgs($fs, $args);
  158. }
  159. protected function callCallbackGet(RemoteFilesystem $fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  160. {
  161. $ref = new \ReflectionMethod($fs, 'callbackGet');
  162. $ref->setAccessible(true);
  163. $ref->invoke($fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax);
  164. }
  165. protected function setAttribute($object, $attribute, $value)
  166. {
  167. $attr = new \ReflectionProperty($object, $attribute);
  168. $attr->setAccessible(true);
  169. $attr->setValue($object, $value);
  170. }
  171. }