RemoteFilesystemTest.php 4.3 KB

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