HttpTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Filesystem\Test\Unit\Driver;
  7. use \Magento\Framework\Filesystem\Driver\Http;
  8. class HttpTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var array Result of get_headers() function */
  11. public static $headers;
  12. /** @var string Result of file_get_contents() function */
  13. public static $fileGetContents;
  14. /** @var bool Result of file_put_contents() function */
  15. public static $filePutContents;
  16. /** @var bool Result of fsockopen() function */
  17. public static $fsockopen;
  18. protected function setUp()
  19. {
  20. require_once __DIR__ . '/../_files/http_mock.php';
  21. self::$headers = [];
  22. self::$fileGetContents = '';
  23. self::$filePutContents = true;
  24. self::$fsockopen = true;
  25. }
  26. /**
  27. * @dataProvider dataProviderForTestIsExists
  28. */
  29. public function testIsExists($status, $result)
  30. {
  31. self::$headers = [$status];
  32. $this->assertEquals($result, (new Http())->isExists(''));
  33. }
  34. /**
  35. * @return array
  36. */
  37. public function dataProviderForTestIsExists()
  38. {
  39. return [['200 OK', true], ['404 Not Found', false]];
  40. }
  41. /**
  42. * @dataProvider dataProviderForTestStat
  43. */
  44. public function testStat($headers, $result)
  45. {
  46. self::$headers = $headers;
  47. $this->assertEquals($result, (new Http())->stat(''));
  48. }
  49. /**
  50. * @return array
  51. */
  52. public function dataProviderForTestStat()
  53. {
  54. $headers1 = [
  55. 'Content-Length' => 128,
  56. 'Content-Type' => 'type',
  57. 'Last-Modified' => '2013-12-19T17:41:45+00:00',
  58. 'Content-Disposition' => 1024,
  59. ];
  60. $result1 = $this->_resultForStat(
  61. ['size' => 128, 'type' => 'type', 'mtime' => '2013-12-19T17:41:45+00:00', 'disposition' => 1024]
  62. );
  63. return [[[], $this->_resultForStat()], [$headers1, $result1]];
  64. }
  65. /**
  66. * Form a result array similar to what stat() produces
  67. *
  68. * @param array $nonEmptyValues
  69. * @return array
  70. */
  71. protected function _resultForStat($nonEmptyValues = [])
  72. {
  73. $result = [
  74. 'dev' => 0,
  75. 'ino' => 0,
  76. 'mode' => 0,
  77. 'nlink' => 0,
  78. 'uid' => 0,
  79. 'gid' => 0,
  80. 'rdev' => 0,
  81. 'atime' => 0,
  82. 'ctime' => 0,
  83. 'blksize' => 0,
  84. 'blocks' => 0,
  85. 'size' => 0,
  86. 'type' => '',
  87. 'mtime' => 0,
  88. 'disposition' => null,
  89. ];
  90. return array_merge($result, $nonEmptyValues);
  91. }
  92. public function testFileGetContents()
  93. {
  94. $content = 'some content';
  95. self::$fileGetContents = $content;
  96. $this->assertEquals($content, (new Http())->fileGetContents(''));
  97. }
  98. public function testFileGetContentsNoContent()
  99. {
  100. $content = '';
  101. self::$fileGetContents = '';
  102. $this->assertEquals($content, (new Http())->fileGetContents(''));
  103. }
  104. public function testFilePutContents()
  105. {
  106. self::$filePutContents = true;
  107. $this->assertTrue((new Http())->filePutContents('', ''));
  108. }
  109. /**
  110. * @expectedException \Magento\Framework\Exception\FileSystemException
  111. */
  112. public function testFilePutContentsFail()
  113. {
  114. self::$filePutContents = false;
  115. (new Http())->filePutContents('', '');
  116. }
  117. /**
  118. * @expectedException \Magento\Framework\Exception\FileSystemException
  119. * @expectedExceptionMessage The download URL is incorrect. Verify and try again.
  120. */
  121. public function testFileOpenInvalidUrl()
  122. {
  123. (new Http())->fileOpen('', '');
  124. }
  125. public function testFileOpen()
  126. {
  127. $fsockopenResult = 'resource';
  128. self::$fsockopen = $fsockopenResult;
  129. $this->assertEquals($fsockopenResult, (new Http())->fileOpen('example.com', 'r'));
  130. }
  131. }