RemoteFilesystemTest.php 5.7 KB

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