11<?php
2+ declare (strict_types=1 );
23/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details.
4+ * Unit test for PatchDriver class.
55 */
6- declare (strict_types=1 );
76
87namespace Magento \CloudPatches \Test \Unit \Shell \Command ;
98
10- use Magento \CloudPatches \Patch \ PatchCommandException ;
9+ use Magento \CloudPatches \Shell \ Command \ DriverException ;
1110use Magento \CloudPatches \Shell \Command \PatchDriver ;
1211use Magento \CloudPatches \Shell \ProcessFactory ;
1312use PHPUnit \Framework \TestCase ;
13+ use PHPUnit \Framework \MockObject \MockObject ;
14+ use Symfony \Component \Process \Exception \ProcessFailedException ;
1415use Symfony \Component \Process \Process ;
1516
1617/**
17- * Tests unix patch driver
18+ * Class PatchDriverTest
1819 */
1920class PatchDriverTest extends TestCase
2021{
2122 /**
22- * @var PatchDriver
23+ * @var string
2324 */
24- private $ command ;
25+ private string $ baseDir ;
26+
2527 /**
2628 * @var string
2729 */
28- private $ baseDir ;
30+ private string $ cwd ;
31+
2932 /**
30- * @var string
33+ * @var ProcessFactory|MockObject
3134 */
32- private $ cwd ;
35+ private ProcessFactory $ processFactoryMock ;
3336
3437 /**
35- * @inheritDoc
38+ * Setup test dependencies and environment.
39+ *
40+ * @return void
3641 */
3742 protected function setUp (): void
3843 {
3944 parent ::setUp ();
4045 $ this ->baseDir = dirname (__DIR__ , 5 ) . '/tests/unit/ ' ;
4146 $ this ->cwd = $ this ->baseDir . 'var/ ' ;
42- $ processFactory = $ this ->createMock (ProcessFactory::class);
43- $ processFactory ->method ('create ' )
44- ->willReturnCallback (
45- function (array $ cmd , ?string $ input = null ) {
46- return new Process (
47- $ cmd ,
48- $ this ->cwd ,
49- null ,
50- $ input
51- );
52- }
53- );
54- $ this ->command = new PatchDriver (
55- $ processFactory
56- );
47+ $ this ->processFactoryMock = $ this ->createMock (ProcessFactory::class);
5748 }
5849
5950 /**
60- * @inheritDoc
51+ * Clean up files after tests.
52+ *
53+ * @return void
6154 */
6255 protected function tearDown (): void
6356 {
@@ -70,81 +63,101 @@ protected function tearDown(): void
7063 }
7164
7265 /**
73- * Tests that patch is applied
66+ * Test successful patch apply.
67+ *
68+ * @return void
7469 */
75- public function testApply ()
70+ public function testApply (): void
7671 {
7772 $ this ->copyFileToWorkingDir ($ this ->getFixtureFile ('file1.md ' ));
7873 $ patchContent = $ this ->getFileContent ($ this ->getFixtureFile ('file1.patch ' ));
79- $ this ->command ->apply ($ patchContent );
74+
75+ $ this ->processFactoryMock ->method ('create ' )->willReturnCallback (
76+ function (array $ cmd , ?string $ input = null ) {
77+ return new Process ($ cmd , $ this ->cwd , null , $ input );
78+ }
79+ );
80+
81+ $ command = new PatchDriver ($ this ->processFactoryMock );
82+ $ command ->apply ($ patchContent );
83+
8084 $ expected = $ this ->getFileContent ($ this ->getFixtureFile ('file1_applied_patch.md ' ));
8185 $ actual = $ this ->getFileContent ($ this ->getVarFile ('file1.md ' ));
86+
8287 $ this ->assertEquals ($ expected , $ actual );
8388 }
8489
8590 /**
86- * Tests that patch is not applied to any target files if an error occurs
91+ * Test patch apply failure handling.
92+ *
93+ * @return void
8794 */
88- public function testApplyFailure ()
95+ public function testApplyFailure (): void
8996 {
9097 $ this ->copyFileToWorkingDir ($ this ->getFixtureFile ('file1.md ' ));
9198 $ this ->copyFileToWorkingDir ($ this ->getFixtureFile ('file2_applied_patch.md ' ), 'file2.md ' );
9299 $ patchContent = $ this ->getFileContent ($ this ->getFixtureFile ('file1_and_file2.patch ' ));
93- $ exception = null ;
94- try {
95- $ this ->command ->apply ($ patchContent );
96- } catch (PatchCommandException $ e ) {
97- $ exception = $ e ;
98- }
99- $ this ->assertNotNull ($ exception );
100- $ expected = $ this ->getFileContent ($ this ->getFixtureFile ('file1.md ' ));
101- $ actual = $ this ->getFileContent ($ this ->getVarFile ('file1.md ' ));
102- $ this ->assertEquals ($ expected , $ actual );
103- $ expected = $ this ->getFileContent ($ this ->getFixtureFile ('file2_applied_patch.md ' ));
104- $ actual = $ this ->getFileContent ($ this ->getVarFile ('file2.md ' ));
105- $ this ->assertEquals ($ expected , $ actual );
100+
101+ $ processMock = $ this ->createMock (Process::class);
102+ $ processMock ->method ('mustRun ' )->willThrowException (new ProcessFailedException ($ processMock ));
103+
104+ $ this ->processFactoryMock ->method ('create ' )->willReturn ($ processMock );
105+ $ command = new PatchDriver ($ this ->processFactoryMock );
106+
107+ $ this ->expectException (DriverException::class);
108+ $ command ->apply ($ patchContent );
106109 }
107110
108111 /**
109- * Tests that patch is reverted
112+ * Test successful patch revert.
113+ *
114+ * @return void
110115 */
111- public function testRevert ()
116+ public function testRevert (): void
112117 {
113118 $ this ->copyFileToWorkingDir ($ this ->getFixtureFile ('file1_applied_patch.md ' ), 'file1.md ' );
114119 $ patchContent = $ this ->getFileContent ($ this ->getFixtureFile ('file1.patch ' ));
115- $ this ->command ->revert ($ patchContent );
120+
121+ $ this ->processFactoryMock ->method ('create ' )->willReturnCallback (
122+ function (array $ cmd , ?string $ input = null ) {
123+ return new Process ($ cmd , $ this ->cwd , null , $ input );
124+ }
125+ );
126+
127+ $ command = new PatchDriver ($ this ->processFactoryMock );
128+ $ command ->revert ($ patchContent );
129+
116130 $ expected = $ this ->getFileContent ($ this ->getFixtureFile ('file1.md ' ));
117131 $ actual = $ this ->getFileContent ($ this ->getVarFile ('file1.md ' ));
132+
118133 $ this ->assertEquals ($ expected , $ actual );
119134 }
120135
121136 /**
122- * Tests that patch is not reverted in any target files if an error occurs
137+ * Test patch revert failure handling
138+ *
139+ * @return void
123140 */
124- public function testRevertFailure ()
141+ public function testRevertFailure (): void
125142 {
126143 $ this ->copyFileToWorkingDir ($ this ->getFixtureFile ('file1_applied_patch.md ' ), 'file1.md ' );
127144 $ this ->copyFileToWorkingDir ($ this ->getFixtureFile ('file2.md ' ));
128145 $ patchContent = $ this ->getFileContent ($ this ->getFixtureFile ('file1_and_file2.patch ' ));
129- $ exception = null ;
130- try {
131- $ this ->command ->revert ($ patchContent );
132- } catch (PatchCommandException $ e ) {
133- $ exception = $ e ;
134- }
135- $ this ->assertNotNull ($ exception );
136- $ expected = $ this ->getFileContent ($ this ->getFixtureFile ('file1_applied_patch.md ' ));
137- $ actual = $ this ->getFileContent ($ this ->getVarFile ('file1.md ' ));
138- $ this ->assertEquals ($ expected , $ actual );
139- $ expected = $ this ->getFileContent ($ this ->getFixtureFile ('file2.md ' ));
140- $ actual = $ this ->getFileContent ($ this ->getVarFile ('file2.md ' ));
141- $ this ->assertEquals ($ expected , $ actual );
146+
147+ $ processMock = $ this ->createMock (Process::class);
148+ $ processMock ->method ('mustRun ' )->willThrowException (new ProcessFailedException ($ processMock ));
149+
150+ $ this ->processFactoryMock ->method ('create ' )->willReturn ($ processMock );
151+ $ command = new PatchDriver ($ this ->processFactoryMock );
152+
153+ $ this ->expectException (DriverException::class);
154+ $ command ->revert ($ patchContent );
142155 }
143156
144157 /**
145- * Get file path in var directory
158+ * Get full path to a file in the test working directory.
146159 *
147- * @param string $name
160+ * @param string $name
148161 * @return string
149162 */
150163 private function getVarFile (string $ name ): string
@@ -153,9 +166,9 @@ private function getVarFile(string $name): string
153166 }
154167
155168 /**
156- * Get file path in files directory
169+ * Get full path to a fixture file.
157170 *
158- * @param string $name
171+ * @param string $name
159172 * @return string
160173 */
161174 private function getFixtureFile (string $ name ): string
@@ -164,9 +177,9 @@ private function getFixtureFile(string $name): string
164177 }
165178
166179 /**
167- * Get the file content
180+ * Get content from a file.
168181 *
169- * @param string $path
182+ * @param string $path
170183 * @return string
171184 */
172185 private function getFileContent (string $ path ): string
@@ -175,12 +188,13 @@ private function getFileContent(string $path): string
175188 }
176189
177190 /**
178- * Copy file to working directory
191+ * Copy a file to the test working directory.
179192 *
180- * @param string $path
181- * @param string|null $name
193+ * @param string $path
194+ * @param string|null $name
195+ * @return void
182196 */
183- private function copyFileToWorkingDir (string $ path , ?string $ name = null )
197+ private function copyFileToWorkingDir (string $ path , ?string $ name = null ): void
184198 {
185199 $ name = $ name ?? basename ($ path );
186200 copy ($ path , $ this ->getVarFile ($ name ));
0 commit comments