RemoteFilesystemTest.php 6.3 KB

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