Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DiCompileCommandTest.php
Go to the documentation of this file.
1 <?php
7 
11 use Symfony\Component\Console\Tester\CommandTester;
12 
16 class DiCompileCommandTest extends \PHPUnit\Framework\TestCase
17 {
19  private $deploymentConfigMock;
20 
22  private $managerMock;
23 
25  private $objectManagerMock;
26 
28  private $command;
29 
31  private $cacheMock;
32 
34  private $filesystemMock;
35 
37  private $fileDriverMock;
38 
40  private $directoryListMock;
41 
43  private $componentRegistrarMock;
44 
46  private $outputMock;
47 
49  private $outputFormatterMock;
50 
51  public function setUp()
52  {
53  $this->deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
54  $objectManagerProviderMock = $this->createMock(\Magento\Setup\Model\ObjectManagerProvider::class);
55  $this->objectManagerMock = $this->getMockForAbstractClass(
56  \Magento\Framework\ObjectManagerInterface::class,
57  [],
58  '',
59  false
60  );
61  $this->cacheMock = $this->getMockBuilder(\Magento\Framework\App\Cache::class)
62  ->disableOriginalConstructor()
63  ->getMock();
64 
65  $objectManagerProviderMock->expects($this->once())
66  ->method('get')
67  ->willReturn($this->objectManagerMock);
68  $this->managerMock = $this->createMock(\Magento\Setup\Module\Di\App\Task\Manager::class);
69  $this->directoryListMock =
70  $this->createMock(\Magento\Framework\App\Filesystem\DirectoryList::class);
71  $this->directoryListMock->expects($this->any())->method('getPath')->willReturnMap([
72  [\Magento\Framework\App\Filesystem\DirectoryList::SETUP, '/path (1)/to/setup/'],
73  ]);
74 
75  $this->filesystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78 
79  $this->fileDriverMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Driver\File::class)
80  ->disableOriginalConstructor()
81  ->getMock();
82  $this->componentRegistrarMock = $this->createMock(\Magento\Framework\Component\ComponentRegistrar::class);
83  $this->componentRegistrarMock->expects($this->any())->method('getPaths')->willReturnMap([
84  [ComponentRegistrar::MODULE, ['/path/to/module/one', '/path (1)/to/module/two']],
85  [ComponentRegistrar::LIBRARY, ['/path/to/library/one', '/path (1)/to/library/two']],
86  ]);
87 
88  $this->outputFormatterMock = $this->createMock(
89  \Symfony\Component\Console\Formatter\OutputFormatterInterface::class
90  );
91  $this->outputMock = $this->createMock(\Symfony\Component\Console\Output\OutputInterface::class);
92  $this->outputMock->method('getFormatter')
93  ->willReturn($this->outputFormatterMock);
94 
95  $this->command = new DiCompileCommand(
96  $this->deploymentConfigMock,
97  $this->directoryListMock,
98  $this->managerMock,
99  $objectManagerProviderMock,
100  $this->filesystemMock,
101  $this->fileDriverMock,
102  $this->componentRegistrarMock
103  );
104  }
105 
107  {
108  $this->deploymentConfigMock->expects($this->once())
109  ->method('get')
111  ->willReturn(null);
112  $tester = new CommandTester($this->command);
113  $tester->execute([]);
114  $this->assertEquals(
115  'You cannot run this command because modules are not enabled. You can enable modules by running the '
116  . "'module:enable --all' command." . PHP_EOL,
117  $tester->getDisplay()
118  );
119  }
120 
121  public function testExecute()
122  {
123  $this->directoryListMock->expects($this->atLeastOnce())->method('getPath')->willReturn(null);
124  $this->objectManagerMock->expects($this->once())
125  ->method('get')
126  ->with(\Magento\Framework\App\Cache::class)
127  ->willReturn($this->cacheMock);
128  $this->cacheMock->expects($this->once())->method('clean');
129  $writeDirectory = $this->createMock(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
130  $writeDirectory->expects($this->atLeastOnce())->method('delete');
131  $this->filesystemMock->expects($this->atLeastOnce())->method('getDirectoryWrite')->willReturn($writeDirectory);
132 
133  $this->deploymentConfigMock->expects($this->once())
134  ->method('get')
136  ->willReturn(['Magento_Catalog' => 1]);
137  $progressBar = new \Symfony\Component\Console\Helper\ProgressBar($this->outputMock);
138 
139  $this->objectManagerMock->expects($this->once())->method('configure');
140  $this->objectManagerMock
141  ->expects($this->once())
142  ->method('create')
143  ->with(\Symfony\Component\Console\Helper\ProgressBar::class)
144  ->willReturn($progressBar);
145 
146  $this->managerMock->expects($this->exactly(7))->method('addOperation')
147  ->withConsecutive(
149  [OperationFactory::REPOSITORY_GENERATOR, $this->anything()],
151  [OperationFactory::APPLICATION_CODE_GENERATOR, $this->callback(function ($subject) {
152  $this->assertEmpty(array_diff($subject['excludePatterns'], [
153  "#^(?:/path \(1\)/to/setup/)(/[\w]+)*/Test#",
154  "#^(?:/path/to/library/one|/path \(1\)/to/library/two)/([\w]+/)?Test#",
155  "#^(?:/path/to/library/one|/path \(1\)/to/library/two)/([\w]+/)?tests#",
156  "#^(?:/path/to/(?:module/(?:one))|/path \(1\)/to/(?:module/(?:two)))/Test#",
157  "#^(?:/path/to/(?:module/(?:one))|/path \(1\)/to/(?:module/(?:two)))/tests#"
158  ]));
159  return true;
160  })],
161  [OperationFactory::INTERCEPTION, $this->anything()],
162  [OperationFactory::AREA_CONFIG_GENERATOR, $this->anything()],
163  [OperationFactory::INTERCEPTION_CACHE, $this->anything()]
164  )
165  ;
166 
167  $this->managerMock->expects($this->once())->method('process');
168  $tester = new CommandTester($this->command);
169  $tester->execute([]);
170  $this->assertContains(
171  'Generated code and dependency injection configuration successfully.',
172  explode(PHP_EOL, $tester->getDisplay())
173  );
174  $this->assertSame(DiCompileCommand::NAME, $this->command->getName());
175  }
176 }