RemoteFilesystemTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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'));
  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'));
  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'), $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 testCallbackGetFileSize()
  57. {
  58. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  59. $this->callCallbackGet($fs, STREAM_NOTIFY_FILE_SIZE_IS, 0, '', 0, 0, 20);
  60. $this->assertAttributeEquals(20, 'bytesMax', $fs);
  61. }
  62. public function testCallbackGetNotifyProgress()
  63. {
  64. $io = $this->getMock('Composer\IO\IOInterface');
  65. $io
  66. ->expects($this->once())
  67. ->method('overwrite')
  68. ;
  69. $fs = new RemoteFilesystem($io);
  70. $this->setAttribute($fs, 'bytesMax', 20);
  71. $this->setAttribute($fs, 'progress', true);
  72. $this->callCallbackGet($fs, STREAM_NOTIFY_PROGRESS, 0, '', 0, 10, 20);
  73. $this->assertAttributeEquals(50, 'lastProgress', $fs);
  74. }
  75. public function testCallbackGetNotifyFailure404()
  76. {
  77. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  78. try {
  79. $this->callCallbackGet($fs, STREAM_NOTIFY_FAILURE, 0, 'HTTP/1.1 404 Not Found', 404, 0, 0);
  80. $this->fail();
  81. } catch (\Exception $e) {
  82. $this->assertInstanceOf('Composer\Downloader\TransportException', $e);
  83. $this->assertEquals(404, $e->getCode());
  84. $this->assertContains('HTTP/1.1 404 Not Found', $e->getMessage());
  85. }
  86. }
  87. public function testGetContents()
  88. {
  89. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  90. $this->assertContains('testGetContents', $fs->getContents('http://example.org', 'file://'.__FILE__));
  91. }
  92. public function testCopy()
  93. {
  94. $fs = new RemoteFilesystem($this->getMock('Composer\IO\IOInterface'));
  95. $file = tempnam(sys_get_temp_dir(), 'c');
  96. $this->assertTrue($fs->copy('http://example.org', 'file://'.__FILE__, $file));
  97. $this->assertFileExists($file);
  98. $this->assertContains('testCopy', file_get_contents($file));
  99. unlink($file);
  100. }
  101. protected function callGetOptionsForUrl($io, array $args = array(), array $options = array())
  102. {
  103. $fs = new RemoteFilesystem($io, $options);
  104. $ref = new \ReflectionMethod($fs, 'getOptionsForUrl');
  105. $ref->setAccessible(true);
  106. return $ref->invokeArgs($fs, $args);
  107. }
  108. protected function callCallbackGet(RemoteFilesystem $fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  109. {
  110. $ref = new \ReflectionMethod($fs, 'callbackGet');
  111. $ref->setAccessible(true);
  112. $ref->invoke($fs, $notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax);
  113. }
  114. protected function setAttribute($object, $attribute, $value)
  115. {
  116. $attr = new \ReflectionProperty($object, $attribute);
  117. $attr->setAccessible(true);
  118. $attr->setValue($object, $value);
  119. }
  120. }