Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SampleDataDeployCommandTest.php
Go to the documentation of this file.
1 <?php
7 
11 use Symfony\Component\Console\Tester\CommandTester;
12 
14 {
18  protected function setupMocksForAuthFile($authExist)
19  {
20  $this->directoryWriteMock->expects($this->once())
21  ->method('isExist')
23  ->willReturn($authExist);
24  $this->directoryWriteMock->expects($authExist ? $this->never() : $this->once())->method('writeFile')->with(
26  '{}'
27  );
28  $this->filesystemMock->expects($this->once())
29  ->method('getDirectoryWrite')
31  ->willReturn($this->directoryWriteMock);
32  }
33 
43  public function testExecute(array $sampleDataPackages, $appRunResult, $expectedMsg, $authExist)
44  {
45  $this->setupMocks($sampleDataPackages, '/path/to/composer.json', $appRunResult);
46  $this->setupMocksForAuthFile($authExist);
47  $commandTester = $this->createCommandTester();
48  $commandTester->execute([]);
49 
50  $this->assertEquals($expectedMsg, $commandTester->getDisplay());
51  }
52 
62  public function testExecuteWithNoUpdate(array $sampleDataPackages, $appRunResult, $expectedMsg, $authExist)
63  {
64  $this->setupMocks(
65  $sampleDataPackages,
66  '/path/to/composer.json',
67  $appRunResult,
68  ['--no-update' => 1]
69  );
70  $this->setupMocksForAuthFile($authExist);
71  $commandInput = ['--no-update' => 1];
72 
73  $commandTester = $this->createCommandTester();
74  $commandTester->execute($commandInput);
75 
76  $this->assertEquals($expectedMsg, $commandTester->getDisplay());
77  }
78 
82  public function processDataProvider()
83  {
84  return [
85  'No sample data found' => [
86  'sampleDataPackages' => [],
87  'appRunResult' => 1,
88  'expectedMsg' => 'There is no sample data for current set of modules.' . PHP_EOL,
89  'authExist' => true,
90  ],
91  'No auth.json found' => [
92  'sampleDataPackages' => [
93  'magento/module-cms-sample-data' => '1.0.0-beta',
94  ],
95  'appRunResult' => 1,
96  'expectedMsg' => 'There is an error during sample data deployment. Composer file will be reverted.'
97  . PHP_EOL,
98  'authExist' => false,
99  ],
100  'Successful sample data installation' => [
101  'sampleDataPackages' => [
102  'magento/module-cms-sample-data' => '1.0.0-beta',
103  ],
104  'appRunResult' => 0,
105  'expectedMsg' => '',
106  'authExist' => true,
107  ],
108  ];
109  }
110 
116  public function testExecuteWithException()
117  {
118  $this->directoryReadMock->expects($this->once())
119  ->method('readFile')
120  ->with('composer.json')
121  ->willReturn('{"version": "0.0.1"}');
122  $this->filesystemMock->expects($this->once())
123  ->method('getDirectoryRead')
124  ->with(DirectoryList::ROOT)
125  ->willReturn($this->directoryReadMock);
126 
127  $this->directoryWriteMock->expects($this->once())
128  ->method('isExist')
130  ->willReturn(false);
131  $this->directoryWriteMock->expects($this->once())
132  ->method('writeFile')
133  ->with(PackagesAuth::PATH_TO_AUTH_FILE, '{}')
134  ->willThrowException(new \Exception('Something went wrong...'));
135  $this->directoryWriteMock->expects($this->once())
136  ->method('getAbsolutePath')
138  ->willReturn('path/to/auth.json');
139  $this->filesystemMock->expects($this->once())
140  ->method('getDirectoryWrite')
142  ->willReturn($this->directoryWriteMock);
143 
144  $this->createCommandTester()->execute([]);
145  }
146 
150  private function createCommandTester(): CommandTester
151  {
152  $commandTester = new CommandTester(
154  $this->filesystemMock,
155  $this->sampleDataDependencyMock,
156  $this->arrayInputFactoryMock,
157  $this->applicationFactoryMock
158  )
159  );
160  return $commandTester;
161  }
162 
168  protected function expectedComposerArguments(
169  array $sampleDataPackages,
170  string $pathToComposerJson
171  ) : array {
172  return [
173  'command' => 'require',
174  '--working-dir' => $pathToComposerJson,
175  '--no-progress' => 1,
176  'packages' => $this->packageVersionStrings($sampleDataPackages),
177  ];
178  }
179 
184  private function packageVersionStrings(array $sampleDataPackages): array
185  {
186  array_walk($sampleDataPackages, function (&$v, $k) {
187  $v = "$k:$v";
188  });
189 
190  return array_values($sampleDataPackages);
191  }
192 }
setupMocks( $sampleDataPackages, $pathToComposerJson, $appRunResult, $additionalComposerArgs=[])
testExecute(array $sampleDataPackages, $appRunResult, $expectedMsg, $authExist)
expectedComposerArguments(array $sampleDataPackages, string $pathToComposerJson)
testExecuteWithNoUpdate(array $sampleDataPackages, $appRunResult, $expectedMsg, $authExist)