RemoteFilesystemTest.php 6.0 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. use Composer\Test\TestCase;
  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('hasAuthorization')
  22. ->will($this->returnValue(false))
  23. ;
  24. $io
  25. ->expects($this->once())
  26. ->method('getLastUsername')
  27. ->will($this->returnValue(null))
  28. ;
  29. $this->assertEquals(array(), $this->callGetOptionsForUrl($io, array('http://example.org')));
  30. }
  31. public function testGetOptionsForUrlWithAuthorization()
  32. {
  33. $io = $this->getMock('Composer\IO\IOInterface');
  34. $io
  35. ->expects($this->once())
  36. ->method('hasAuthorization')
  37. ->will($this->returnValue(true))
  38. ;
  39. $io
  40. ->expects($this->once())
  41. ->method('getAuthorization')
  42. ->will($this->returnValue(array('username' => 'login', 'password' => 'password')))
  43. ;
  44. $options = $this->callGetOptionsForUrl($io, array('http://example.org'));
  45. $this->assertContains('Authorization: Basic', $options['http']['header']);
  46. }
  47. public function testGetOptionsForUrlWithLastUsername()
  48. {
  49. $io = $this->getMock('Composer\IO\IOInterface');
  50. $io
  51. ->expects($this->once())
  52. ->method('hasAuthorization')
  53. ->will($this->returnValue(false))
  54. ;
  55. $io
  56. ->expects($this->any())
  57. ->method('getLastUsername')
  58. ->will($this->returnValue('login'))
  59. ;
  60. $io
  61. ->expects($this->any())
  62. ->method('getLastPassword')
  63. ->will($this->returnValue('password'))
  64. ;
  65. $io
  66. ->expects($this->once())
  67. ->method('setAuthorization')
  68. ;
  69. $options = $this->callGetOptionsForUrl($io, array('http://example.org'));
  70. $this->assertContains('Authorization: Basic', $options['http']['header']);
  71. }
  72. public function testCallbackGetFileSize()
  73. {
  74. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  75. $this->callCallbackGet($fs, STREAM_NOTIFY_FILE_SIZE_IS, 0, '', 0, 0, 20);
  76. $this->assertAttributeEquals(20, 'bytesMax', $fs);
  77. }
  78. public function testCallbackGetNotifyProgress()
  79. {
  80. $io = $this->getMock('Composer\IO\IOInterface');
  81. $io
  82. ->expects($this->once())
  83. ->method('overwrite')
  84. ;
  85. $fs = new RemoteFilesystem($io);
  86. $this->setAttribute($fs, 'bytesMax', 20);
  87. $this->setAttribute($fs, 'progress', true);
  88. $this->callCallbackGet($fs, STREAM_NOTIFY_PROGRESS, 0, '', 0, 10, 20);
  89. $this->assertAttributeEquals(50, 'lastProgress', $fs);
  90. }
  91. public function testCallbackGetNotifyFailure404()
  92. {
  93. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  94. $this->setAttribute($fs, 'firstCall', false);
  95. try {
  96. $this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, '', 404, 0, 0);
  97. $this->fail();
  98. } catch (\Exception $e) {
  99. $this->assertInstanceOf('Composer\Downloader\TransportException', $e);
  100. $this->assertContains('URL not found', $e->getMessage());
  101. }
  102. }
  103. public function testCallbackGetNotifyFailure404FirstCall()
  104. {
  105. $io = $this->getMock('Composer\IO\IOInterface');
  106. $io
  107. ->expects($this->once())
  108. ->method('getAuthorization')
  109. ->will($this->returnValue(array('username' => null)))
  110. ;
  111. $io
  112. ->expects($this->once())
  113. ->method('isInteractive')
  114. ->will($this->returnValue(false))
  115. ;
  116. $fs = new RemoteFilesystem($io);
  117. $this->setAttribute($fs, 'firstCall', true);
  118. try {
  119. $this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, '', 404, 0, 0);
  120. $this->fail();
  121. } catch (\Exception $e) {
  122. $this->assertInstanceOf('Composer\Downloader\TransportException', $e);
  123. $this->assertContains('URL required authentication', $e->getMessage());
  124. $this->assertAttributeEquals(false, 'firstCall', $fs);
  125. }
  126. }
  127. public function testGetContents()
  128. {
  129. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  130. $this->assertContains('RFC 2606', $fs->getContents('http://example.org', 'http://example.org'));
  131. }
  132. public function testCopy()
  133. {
  134. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  135. $file = tempnam(sys_get_temp_dir(), 'c');
  136. $this->assertTrue($fs->copy('http://example.org', 'http://example.org', $file));
  137. $this->assertFileExists($file);
  138. $this->assertContains('RFC 2606', file_get_contents($file));
  139. unlink($file);
  140. }
  141. protected function callGetOptionsForUrl($io, array $args = array())
  142. {
  143. $fs = new RemoteFilesystem($io);
  144. $ref = new \ReflectionMethod($fs, 'getOptionsForUrl');
  145. $ref->setAccessible(true);
  146. return $ref->invokeArgs($fs, $args);
  147. }
  148. protected function callCallbackGet(RemoteFilesystem $fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  149. {
  150. $ref = new \ReflectionMethod($fs, 'callbackGet');
  151. $ref->setAccessible(true);
  152. $ref->invoke($fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax);
  153. }
  154. protected function setAttribute($object, $attribute, $value)
  155. {
  156. $attr = new \ReflectionProperty($object, $attribute);
  157. $attr->setAccessible(true);
  158. $attr->setValue($object, $value);
  159. }
  160. }