DirSearchTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Component\Test\Unit;
  7. use Magento\Framework\Component\DirSearch;
  8. use Magento\Framework\Filesystem\DriverPool;
  9. class DirSearchTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\Filesystem\Directory\ReadInterface|\PHPUnit_Framework_MockObject_MockObject
  13. */
  14. private $dir;
  15. /**
  16. * @var \Magento\Framework\Component\ComponentRegistrarInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $registrar;
  19. /**
  20. * @var \Magento\Framework\Filesystem\Directory\ReadFactory|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $readFactory;
  23. /**
  24. * @var DirSearch
  25. */
  26. private $object;
  27. protected function setUp()
  28. {
  29. $this->registrar = $this->getMockForAbstractClass(
  30. \Magento\Framework\Component\ComponentRegistrarInterface::class
  31. );
  32. $this->readFactory = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
  33. $this->dir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
  34. $this->dir->expects($this->any())
  35. ->method('getAbsolutePath')
  36. ->willReturnArgument(0);
  37. $this->object = new DirSearch($this->registrar, $this->readFactory);
  38. }
  39. public function testCollectFilesNothingFound()
  40. {
  41. $componentType = 'component_type';
  42. $this->registrar->expects($this->exactly(2))
  43. ->method('getPaths')
  44. ->with($componentType)
  45. ->willReturn([]);
  46. $this->readFactory->expects($this->never())
  47. ->method('create');
  48. $this->assertSame([], $this->object->collectFiles($componentType, '*/file.xml'));
  49. $this->assertSame([], $this->object->collectFilesWithContext($componentType, '*/file.xml'));
  50. }
  51. public function testCollectFiles()
  52. {
  53. $componentType = 'component_type';
  54. $componentPaths = ['component1' => 'path1', 'component2' => 'path2'];
  55. $pattern = '*/file.xml';
  56. $this->registrar->expects($this->once())
  57. ->method('getPaths')
  58. ->with($componentType)
  59. ->willReturn($componentPaths);
  60. $this->readFactory->expects($this->exactly(2))
  61. ->method('create')
  62. ->willReturnMap([
  63. ['path1', DriverPool::FILE, $this->dir],
  64. ['path2', DriverPool::FILE, $this->dir],
  65. ]);
  66. $this->dir->method('search')
  67. ->with($pattern)
  68. ->willReturnOnConsecutiveCalls(['one/file.xml'], ['two/file.xml']);
  69. $expected = ['one/file.xml', 'two/file.xml'];
  70. $this->assertSame($expected, $this->object->collectFiles($componentType, $pattern));
  71. }
  72. public function testCollectFilesWithContext()
  73. {
  74. $componentType = 'component_type';
  75. $componentPaths = ['component1' => 'path1', 'component2' => 'path2'];
  76. $pattern = '*/file.xml';
  77. $this->registrar->expects($this->once())
  78. ->method('getPaths')
  79. ->with($componentType)
  80. ->willReturn($componentPaths);
  81. $this->readFactory->expects($this->exactly(2))
  82. ->method('create')
  83. ->willReturnMap([
  84. ['path1', DriverPool::FILE, $this->dir],
  85. ['path2', DriverPool::FILE, $this->dir],
  86. ]);
  87. $this->dir->method('search')
  88. ->with($pattern)
  89. ->willReturnOnConsecutiveCalls(['one/file.xml'], ['two/file.xml']);
  90. $actualFiles = $this->object->collectFilesWithContext($componentType, $pattern);
  91. $this->assertNotEmpty($actualFiles);
  92. /** @var \Magento\Framework\Component\ComponentFile $file */
  93. foreach ($actualFiles as $file) {
  94. $this->assertInstanceOf(\Magento\Framework\Component\ComponentFile::class, $file);
  95. $this->assertSame($componentType, $file->getComponentType());
  96. }
  97. $this->assertCount(2, $actualFiles);
  98. $this->assertSame('component1', $actualFiles[0]->getComponentName());
  99. $this->assertSame('one/file.xml', $actualFiles[0]->getFullPath());
  100. $this->assertSame('component2', $actualFiles[1]->getComponentName());
  101. $this->assertSame('two/file.xml', $actualFiles[1]->getFullPath());
  102. }
  103. }