FileIteratorTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Config\Test\Unit;
  7. use \Magento\Framework\Config\FileIterator;
  8. /**
  9. * Class FileIteratorTest
  10. */
  11. class FileIteratorTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var FileIterator
  15. */
  16. protected $fileIterator;
  17. /**
  18. * @var \Magento\Framework\Filesystem\File\Read|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $fileRead;
  21. /**
  22. * Array of relative file paths
  23. *
  24. * @var array
  25. */
  26. protected $filePaths;
  27. /**
  28. * @var \Magento\Framework\Filesystem\File\ReadFactory|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $fileReadFactory;
  31. protected function setUp()
  32. {
  33. $this->filePaths = ['/file1', '/file2'];
  34. $this->fileReadFactory = $this->createMock(\Magento\Framework\Filesystem\File\ReadFactory::class);
  35. $this->fileRead = $this->createMock(\Magento\Framework\Filesystem\File\Read::class);
  36. $this->fileIterator = new FileIterator($this->fileReadFactory, $this->filePaths);
  37. }
  38. protected function tearDown()
  39. {
  40. $this->fileIterator = null;
  41. $this->filePaths = null;
  42. }
  43. public function testIterator()
  44. {
  45. $contents = ['content1', 'content2'];
  46. $index = 0;
  47. foreach ($this->filePaths as $filePath) {
  48. $this->fileReadFactory->expects($this->at($index))
  49. ->method('create')
  50. ->with($filePath)
  51. ->willReturn($this->fileRead);
  52. $this->fileRead->expects($this->at($index))
  53. ->method('readAll')
  54. ->will($this->returnValue($contents[$index++]));
  55. }
  56. $index = 0;
  57. foreach ($this->fileIterator as $fileContent) {
  58. $this->assertEquals($contents[$index++], $fileContent);
  59. }
  60. }
  61. public function testToArray()
  62. {
  63. $contents = ['content1', 'content2'];
  64. $expectedArray = [];
  65. $index = 0;
  66. foreach ($this->filePaths as $filePath) {
  67. $expectedArray[$filePath] = $contents[$index];
  68. $this->fileReadFactory->expects($this->at($index))
  69. ->method('create')
  70. ->with($filePath)
  71. ->willReturn($this->fileRead);
  72. $this->fileRead->expects($this->at($index))
  73. ->method('readAll')
  74. ->will($this->returnValue($contents[$index++]));
  75. }
  76. $this->assertEquals($expectedArray, $this->fileIterator->toArray());
  77. }
  78. }