RemoteFilesystemTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. $res = $this->callGetOptionsForUrl($io, array('http://example.org'));
  30. $this->assertTrue(isset($res['http']['header']) && false !== strpos($res['http']['header'], 'User-Agent'), 'getOptions must return an array with a header containing a User-Agent');
  31. }
  32. public function testGetOptionsForUrlWithAuthorization()
  33. {
  34. $io = $this->getMock('Composer\IO\IOInterface');
  35. $io
  36. ->expects($this->once())
  37. ->method('hasAuthorization')
  38. ->will($this->returnValue(true))
  39. ;
  40. $io
  41. ->expects($this->once())
  42. ->method('getAuthorization')
  43. ->will($this->returnValue(array('username' => 'login', 'password' => 'password')))
  44. ;
  45. $options = $this->callGetOptionsForUrl($io, array('http://example.org'));
  46. $this->assertContains('Authorization: Basic', $options['http']['header']);
  47. }
  48. public function testGetOptionsForUrlWithLastUsername()
  49. {
  50. $io = $this->getMock('Composer\IO\IOInterface');
  51. $io
  52. ->expects($this->once())
  53. ->method('hasAuthorization')
  54. ->will($this->returnValue(false))
  55. ;
  56. $io
  57. ->expects($this->any())
  58. ->method('getLastUsername')
  59. ->will($this->returnValue('login'))
  60. ;
  61. $io
  62. ->expects($this->any())
  63. ->method('getLastPassword')
  64. ->will($this->returnValue('password'))
  65. ;
  66. $io
  67. ->expects($this->once())
  68. ->method('setAuthorization')
  69. ;
  70. $options = $this->callGetOptionsForUrl($io, array('http://example.org'));
  71. $this->assertContains('Authorization: Basic', $options['http']['header']);
  72. }
  73. public function testCallbackGetFileSize()
  74. {
  75. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  76. $this->callCallbackGet($fs, STREAM_NOTIFY_FILE_SIZE_IS, 0, '', 0, 0, 20);
  77. $this->assertAttributeEquals(20, 'bytesMax', $fs);
  78. }
  79. public function testCallbackGetNotifyProgress()
  80. {
  81. $io = $this->getMock('Composer\IO\IOInterface');
  82. $io
  83. ->expects($this->once())
  84. ->method('overwrite')
  85. ;
  86. $fs = new RemoteFilesystem($io);
  87. $this->setAttribute($fs, 'bytesMax', 20);
  88. $this->setAttribute($fs, 'progress', true);
  89. $this->callCallbackGet($fs, STREAM_NOTIFY_PROGRESS, 0, '', 0, 10, 20);
  90. $this->assertAttributeEquals(50, 'lastProgress', $fs);
  91. }
  92. public function testCallbackGetNotifyFailure404()
  93. {
  94. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  95. try {
  96. $this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, 'HTTP/1.1 404 Not Found', 404, 0, 0);
  97. $this->fail();
  98. } catch (\Exception $e) {
  99. $this->assertInstanceOf('Composer\Downloader\TransportException', $e);
  100. $this->assertEquals(404, $e->getCode());
  101. $this->assertContains('HTTP/1.1 404 Not Found', $e->getMessage());
  102. }
  103. }
  104. public function testGetContents()
  105. {
  106. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  107. $this->assertContains('testGetContents', $fs->getContents('http://example.org', 'file://'.__FILE__));
  108. }
  109. public function testCopy()
  110. {
  111. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  112. $file = tempnam(sys_get_temp_dir(), 'c');
  113. $this->assertTrue($fs->copy('http://example.org', 'file://'.__FILE__, $file));
  114. $this->assertFileExists($file);
  115. $this->assertContains('testCopy', file_get_contents($file));
  116. unlink($file);
  117. }
  118. protected function callGetOptionsForUrl($io, array $args = array())
  119. {
  120. $fs = new RemoteFilesystem($io);
  121. $ref = new \ReflectionMethod($fs, 'getOptionsForUrl');
  122. $ref->setAccessible(true);
  123. return $ref->invokeArgs($fs, $args);
  124. }
  125. protected function callCallbackGet(RemoteFilesystem $fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  126. {
  127. $ref = new \ReflectionMethod($fs, 'callbackGet');
  128. $ref->setAccessible(true);
  129. $ref->invoke($fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax);
  130. }
  131. protected function setAttribute($object, $attribute, $value)
  132. {
  133. $attr = new \ReflectionProperty($object, $attribute);
  134. $attr->setAccessible(true);
  135. $attr->setValue($object, $value);
  136. }
  137. }