-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVirtualFilesystemTestTrait.php
More file actions
173 lines (149 loc) · 3.75 KB
/
Copy pathVirtualFilesystemTestTrait.php
File metadata and controls
173 lines (149 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace WPMedia\PHPUnit;
trait VirtualFilesystemTestTrait {
use ArrayTrait;
/**
* Instance of the virtual filesystem.
*
* @var VirtualFilesystemDirect
*/
protected $filesystem;
/**
* Overwrite with the structure for this test. Gets merged with the default structure.
*
* @var array
*/
protected $structure = [];
/**
* URL to the root directory of the virtual filesystem.
*
* @var string
*/
protected $rootVirtualUrl;
/**
* Structure + test data configuration.
*
* @var array
*/
protected $config = [];
/**
* Virtual filestructure for this test, i.e. default merged with configured.
*
* @var array
*/
private $merged_structure = [];
/**
* Overwrite in the test class to skip running the "initOriginals()" method.
*
* @var bool
*/
protected $skip_initOriginals = false;
/**
* Original virtual files with flattened full paths.
*
* @var array
*/
protected $original_files = [];
/**
* Original virtual directories with flattened full paths.
*
* @var array
*/
protected $original_dirs = [];
/**
* Initializes the test environment.
*/
public function init() {
if ( empty( $this->config ) ) {
$this->loadConfig();
}
$this->initOriginals();
$this->filesystem = new VirtualFilesystemDirect( $this->rootVirtualDir, $this->mergeStructure(), $this->permissions );
$this->rootVirtualUrl = $this->filesystem->getUrl( '/' );
}
/**
* Test Data Provider that uses the `'test_data'` in the config file.
*
* @return mixed
*/
public function providerTestData() {
$this->loadConfig();
return $this->config['test_data'];
}
/**
* Loads the configuration for the vfs structure and test data.
*/
protected function loadConfig() {
$this->config = array_merge(
[
'vfs_dir' => '',
'structure' => [],
'test_data' => [],
],
require $this->getPathToFixturesDir() . $this->path_to_test_data
);
}
/**
* Overload in your test case with the path to the Fixtures directory.
*
* @return string
*/
public function getPathToFixturesDir() {
return '';
}
/**
* Merges the configured and default virtual filesystem structures.
*
* @return array merged structure
*/
protected function mergeStructure() {
// If already merged, return it.
if ( ! empty( $this->merged_structure ) ) {
return $this->merged_structure;
}
$this->structure = $this->config['structure'];
$this->merged_structure = array_replace_recursive( $this->getDefaultVfs(), $this->config['structure'] );
return $this->merged_structure;
}
/**
* Gets the default virtual directory filesystem structure.
*
* @return array default structure.
*/
public function getDefaultVfs() {
return [
'wp-admin' => [],
'wp-content' => [
'mu-plugins' => [],
'plugins' => [
'wp-rocket' => [],
],
'themes' => [
'twentytwenty' => [],
],
'uploads' => [],
],
'wp-includes' => [],
'wp-config.php' => '',
];
}
/**
* Initializes the original files and directories properties for use in the tests.
*/
protected function initOriginals() {
// Bail out when "skip_initOriginals" is set to true.
if ( $this->skip_initOriginals ) {
return;
}
if ( ! empty( $this->config['vfs_dir'] ) && '/' !== $this->config['vfs_dir'] ) {
$vfs_dir = rtrim( $this->config['vfs_dir'], '/\\' ); // Remove trailing slash for the get.
$structure = $this->get( $this->config['structure'], $vfs_dir, [], '/' );
$vfs_dir .= '/'; // Add the trailing slash for the flattening.
} else {
$vfs_dir = '';
$structure = $this->config['structure'];
}
$this->original_files = array_keys( $this->flatten( $structure, $vfs_dir ) );
$this->original_dirs = array_keys( $this->flatten( $structure, $vfs_dir, true ) );
}
}