RemoteFilesystemTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. try {
  95. $this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, 'HTTP/1.1 404 Not Found', 404, 0, 0);
  96. $this->fail();
  97. } catch (\Exception $e) {
  98. $this->assertInstanceOf('Composer\Downloader\TransportException', $e);
  99. $this->assertEquals(404, $e->getCode());
  100. $this->assertContains('HTTP/1.1 404 Not Found', $e->getMessage());
  101. }
  102. }
  103. public function testGetContents()
  104. {
  105. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  106. $this->assertContains('RFC 2606', $fs->getContents('http://example.org', 'http://example.org'));
  107. }
  108. public function testCopy()
  109. {
  110. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  111. $file = tempnam(sys_get_temp_dir(), 'c');
  112. $this->assertTrue($fs->copy('http://example.org', 'http://example.org', $file));
  113. $this->assertFileExists($file);
  114. $this->assertContains('RFC 2606', file_get_contents($file));
  115. unlink($file);
  116. }
  117. protected function callGetOptionsForUrl($io, array $args = array())
  118. {
  119. $fs = new RemoteFilesystem($io);
  120. $ref = new \ReflectionMethod($fs, 'getOptionsForUrl');
  121. $ref->setAccessible(true);
  122. return $ref->invokeArgs($fs, $args);
  123. }
  124. protected function callCallbackGet(RemoteFilesystem $fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  125. {
  126. $ref = new \ReflectionMethod($fs, 'callbackGet');
  127. $ref->setAccessible(true);
  128. $ref->invoke($fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax);
  129. }
  130. protected function setAttribute($object, $attribute, $value)
  131. {
  132. $attr = new \ReflectionProperty($object, $attribute);
  133. $attr->setAccessible(true);
  134. $attr->setValue($object, $value);
  135. }
  136. }