1
1
<?php
2
+ declare (strict_types=1 );
2
3
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details.
4
+ * Unit test for PatchDriver class.
5
5
*/
6
- declare (strict_types=1 );
7
6
8
7
namespace Magento \CloudPatches \Test \Unit \Shell \Command ;
9
8
10
- use Magento \CloudPatches \Patch \ PatchCommandException ;
9
+ use Magento \CloudPatches \Shell \ Command \ DriverException ;
11
10
use Magento \CloudPatches \Shell \Command \PatchDriver ;
12
11
use Magento \CloudPatches \Shell \ProcessFactory ;
13
12
use PHPUnit \Framework \TestCase ;
13
+ use PHPUnit \Framework \MockObject \MockObject ;
14
+ use Symfony \Component \Process \Exception \ProcessFailedException ;
14
15
use Symfony \Component \Process \Process ;
15
16
16
17
/**
17
- * Tests unix patch driver
18
+ * Class PatchDriverTest
18
19
*/
19
20
class PatchDriverTest extends TestCase
20
21
{
21
22
/**
22
- * @var PatchDriver
23
+ * @var string
23
24
*/
24
- private $ command ;
25
+ private string $ baseDir ;
26
+
25
27
/**
26
28
* @var string
27
29
*/
28
- private $ baseDir ;
30
+ private string $ cwd ;
31
+
29
32
/**
30
- * @var string
33
+ * @var ProcessFactory|MockObject
31
34
*/
32
- private $ cwd ;
35
+ private ProcessFactory $ processFactoryMock ;
33
36
34
37
/**
35
- * @inheritDoc
38
+ * Setup test dependencies and environment.
39
+ *
40
+ * @return void
36
41
*/
37
42
protected function setUp (): void
38
43
{
39
44
parent ::setUp ();
40
45
$ this ->baseDir = dirname (__DIR__ , 5 ) . '/tests/unit/ ' ;
41
46
$ 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);
57
48
}
58
49
59
50
/**
60
- * @inheritDoc
51
+ * Clean up files after tests.
52
+ *
53
+ * @return void
61
54
*/
62
55
protected function tearDown (): void
63
56
{
@@ -70,81 +63,101 @@ protected function tearDown(): void
70
63
}
71
64
72
65
/**
73
- * Tests that patch is applied
66
+ * Test successful patch apply.
67
+ *
68
+ * @return void
74
69
*/
75
- public function testApply ()
70
+ public function testApply (): void
76
71
{
77
72
$ this ->copyFileToWorkingDir ($ this ->getFixtureFile ('file1.md ' ));
78
73
$ 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
+
80
84
$ expected = $ this ->getFileContent ($ this ->getFixtureFile ('file1_applied_patch.md ' ));
81
85
$ actual = $ this ->getFileContent ($ this ->getVarFile ('file1.md ' ));
86
+
82
87
$ this ->assertEquals ($ expected , $ actual );
83
88
}
84
89
85
90
/**
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
87
94
*/
88
- public function testApplyFailure ()
95
+ public function testApplyFailure (): void
89
96
{
90
97
$ this ->copyFileToWorkingDir ($ this ->getFixtureFile ('file1.md ' ));
91
98
$ this ->copyFileToWorkingDir ($ this ->getFixtureFile ('file2_applied_patch.md ' ), 'file2.md ' );
92
99
$ 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 );
106
109
}
107
110
108
111
/**
109
- * Tests that patch is reverted
112
+ * Test successful patch revert.
113
+ *
114
+ * @return void
110
115
*/
111
- public function testRevert ()
116
+ public function testRevert (): void
112
117
{
113
118
$ this ->copyFileToWorkingDir ($ this ->getFixtureFile ('file1_applied_patch.md ' ), 'file1.md ' );
114
119
$ 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
+
116
130
$ expected = $ this ->getFileContent ($ this ->getFixtureFile ('file1.md ' ));
117
131
$ actual = $ this ->getFileContent ($ this ->getVarFile ('file1.md ' ));
132
+
118
133
$ this ->assertEquals ($ expected , $ actual );
119
134
}
120
135
121
136
/**
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
123
140
*/
124
- public function testRevertFailure ()
141
+ public function testRevertFailure (): void
125
142
{
126
143
$ this ->copyFileToWorkingDir ($ this ->getFixtureFile ('file1_applied_patch.md ' ), 'file1.md ' );
127
144
$ this ->copyFileToWorkingDir ($ this ->getFixtureFile ('file2.md ' ));
128
145
$ 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 );
142
155
}
143
156
144
157
/**
145
- * Get file path in var directory
158
+ * Get full path to a file in the test working directory.
146
159
*
147
- * @param string $name
160
+ * @param string $name
148
161
* @return string
149
162
*/
150
163
private function getVarFile (string $ name ): string
@@ -153,9 +166,9 @@ private function getVarFile(string $name): string
153
166
}
154
167
155
168
/**
156
- * Get file path in files directory
169
+ * Get full path to a fixture file.
157
170
*
158
- * @param string $name
171
+ * @param string $name
159
172
* @return string
160
173
*/
161
174
private function getFixtureFile (string $ name ): string
@@ -164,9 +177,9 @@ private function getFixtureFile(string $name): string
164
177
}
165
178
166
179
/**
167
- * Get the file content
180
+ * Get content from a file.
168
181
*
169
- * @param string $path
182
+ * @param string $path
170
183
* @return string
171
184
*/
172
185
private function getFileContent (string $ path ): string
@@ -175,12 +188,13 @@ private function getFileContent(string $path): string
175
188
}
176
189
177
190
/**
178
- * Copy file to working directory
191
+ * Copy a file to the test working directory.
179
192
*
180
- * @param string $path
181
- * @param string|null $name
193
+ * @param string $path
194
+ * @param string|null $name
195
+ * @return void
182
196
*/
183
- private function copyFileToWorkingDir (string $ path , ?string $ name = null )
197
+ private function copyFileToWorkingDir (string $ path , ?string $ name = null ): void
184
198
{
185
199
$ name = $ name ?? basename ($ path );
186
200
copy ($ path , $ this ->getVarFile ($ name ));
0 commit comments