RemoteFilesystemTest.php 4.3 KB

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