Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DirSearchTest.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class DirSearchTest extends \PHPUnit\Framework\TestCase
12 {
16  private $dir;
17 
21  private $registrar;
22 
26  private $readFactory;
27 
31  private $object;
32 
33  protected function setUp()
34  {
35  $this->registrar = $this->getMockForAbstractClass(
36  \Magento\Framework\Component\ComponentRegistrarInterface::class
37  );
38  $this->readFactory = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
39  $this->dir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
40  $this->dir->expects($this->any())
41  ->method('getAbsolutePath')
42  ->willReturnArgument(0);
43  $this->object = new DirSearch($this->registrar, $this->readFactory);
44  }
45 
46  public function testCollectFilesNothingFound()
47  {
48  $componentType = 'component_type';
49  $this->registrar->expects($this->exactly(2))
50  ->method('getPaths')
51  ->with($componentType)
52  ->willReturn([]);
53  $this->readFactory->expects($this->never())
54  ->method('create');
55  $this->assertSame([], $this->object->collectFiles($componentType, '*/file.xml'));
56  $this->assertSame([], $this->object->collectFilesWithContext($componentType, '*/file.xml'));
57  }
58 
59  public function testCollectFiles()
60  {
61  $componentType = 'component_type';
62  $componentPaths = ['component1' => 'path1', 'component2' => 'path2'];
63  $pattern = '*/file.xml';
64  $this->registrar->expects($this->once())
65  ->method('getPaths')
66  ->with($componentType)
67  ->willReturn($componentPaths);
68  $this->readFactory->expects($this->exactly(2))
69  ->method('create')
70  ->willReturnMap([
71  ['path1', DriverPool::FILE, $this->dir],
72  ['path2', DriverPool::FILE, $this->dir],
73  ]);
74  $this->dir->method('search')
75  ->with($pattern)
76  ->willReturnOnConsecutiveCalls(['one/file.xml'], ['two/file.xml']);
77  $expected = ['one/file.xml', 'two/file.xml'];
78  $this->assertSame($expected, $this->object->collectFiles($componentType, $pattern));
79  }
80 
81  public function testCollectFilesWithContext()
82  {
83  $componentType = 'component_type';
84  $componentPaths = ['component1' => 'path1', 'component2' => 'path2'];
85  $pattern = '*/file.xml';
86  $this->registrar->expects($this->once())
87  ->method('getPaths')
88  ->with($componentType)
89  ->willReturn($componentPaths);
90  $this->readFactory->expects($this->exactly(2))
91  ->method('create')
92  ->willReturnMap([
93  ['path1', DriverPool::FILE, $this->dir],
94  ['path2', DriverPool::FILE, $this->dir],
95  ]);
96  $this->dir->method('search')
97  ->with($pattern)
98  ->willReturnOnConsecutiveCalls(['one/file.xml'], ['two/file.xml']);
99  $actualFiles = $this->object->collectFilesWithContext($componentType, $pattern);
100  $this->assertNotEmpty($actualFiles);
102  foreach ($actualFiles as $file) {
103  $this->assertInstanceOf(\Magento\Framework\Component\ComponentFile::class, $file);
104  $this->assertSame($componentType, $file->getComponentType());
105  }
106  $this->assertCount(2, $actualFiles);
107  $this->assertSame('component1', $actualFiles[0]->getComponentName());
108  $this->assertSame('one/file.xml', $actualFiles[0]->getFullPath());
109  $this->assertSame('component2', $actualFiles[1]->getComponentName());
110  $this->assertSame('two/file.xml', $actualFiles[1]->getFullPath());
111  }
112 }
$pattern
Definition: website.php:22