@@ -14,6 +14,20 @@ class PhpUnitTask extends TestFrameworkBase {
14
14
15
15
use SutSettingsTrait;
16
16
17
+ /**
18
+ * The DOM document for the PHPUnit configuration file.
19
+ *
20
+ * @var \DOMDocument
21
+ */
22
+ private $ doc ;
23
+
24
+ /**
25
+ * The XPath helper for the DOM document.
26
+ *
27
+ * @var \DOMXPath
28
+ */
29
+ private $ xpath ;
30
+
17
31
/**
18
32
* {@inheritdoc}
19
33
*/
@@ -44,16 +58,17 @@ public function execute(): void {
44
58
*/
45
59
private function ensurePhpUnitConfig () {
46
60
$ path = $ this ->fixture ->getPath ('docroot/core/phpunit.xml ' );
47
- $ doc = new \DOMDocument ();
48
- $ doc ->load ($ path );
49
- $ xpath = new \DOMXPath ($ doc );
61
+ $ this -> doc = new \DOMDocument ($ path );
62
+ $ this -> doc ->load ($ path );
63
+ $ this -> xpath = new \DOMXPath ($ this -> doc );
50
64
51
65
$ this ->ensureSimpleTestDirectory ();
52
- $ this ->setSimpletestSettings ($ path , $ doc , $ xpath );
53
- $ this ->setTestSuite ($ path , $ doc , $ xpath );
54
- $ this ->enableDrupalTestTraits ($ path , $ doc , $ xpath );
55
- $ this ->disableSymfonyDeprecationsHelper ($ path , $ doc , $ xpath );
56
- $ this ->setMinkDriverArguments ($ path , $ doc , $ xpath );
66
+ $ this ->setSimpletestSettings ();
67
+ $ this ->setTestSuite ();
68
+ $ this ->enableDrupalTestTraits ();
69
+ $ this ->disableSymfonyDeprecationsHelper ();
70
+ $ this ->setMinkDriverArguments ();
71
+ $ this ->writeConfiguration ($ path );
57
72
}
58
73
59
74
/**
@@ -65,123 +80,117 @@ private function ensureSimpleTestDirectory(): void {
65
80
66
81
/**
67
82
* Sets Simpletest settings.
68
- *
69
- * @param string $path
70
- * The path.
71
- * @param \DOMDocument $doc
72
- * The DOM document.
73
- * @param \DOMXPath $xpath
74
- * The XPath object.
75
83
*/
76
- private function setSimpletestSettings (string $ path , \ DOMDocument $ doc , \ DOMXPath $ xpath ): void {
77
- $ xpath ->query ('//phpunit/php/env[@name="SIMPLETEST_BASE_URL"] ' )
84
+ private function setSimpletestSettings (): void {
85
+ $ this -> xpath ->query ('//phpunit/php/env[@name="SIMPLETEST_BASE_URL"] ' )
78
86
->item (0 )
79
87
->setAttribute ('value ' , sprintf ('http://%s ' , Fixture::WEB_ADDRESS ));
80
- $ xpath ->query ('//phpunit/php/env[@name="SIMPLETEST_DB"] ' )
88
+ $ this -> xpath ->query ('//phpunit/php/env[@name="SIMPLETEST_DB"] ' )
81
89
->item (0 )
82
90
->setAttribute ('value ' , 'sqlite://localhost/sites/default/files/.ht.sqlite ' );
83
- $ doc ->save ($ path );
84
91
}
85
92
86
93
/**
87
94
* Sets TestSuite config in phpunit.xml.
88
- *
89
- * @param string $path
90
- * The path.
91
- * @param \DOMDocument $doc
92
- * The DOM document.
93
- * @param \DOMXPath $xpath
94
- * The XPath object.
95
95
*/
96
- private function setTestSuite (string $ path , \ DOMDocument $ doc , \ DOMXPath $ xpath ): void {
97
- $ directory = $ doc ->createElement ('directory ' , $ this ->getPath ());
98
- $ exclude = $ doc ->createElement ('exclude ' , "{$ this ->getPath ()}/vendor " );
99
- $ testsuite = $ doc ->createElement ('testsuite ' );
96
+ private function setTestSuite (): void {
97
+ $ directory = $ this -> doc ->createElement ('directory ' , $ this ->getPath ());
98
+ $ exclude = $ this -> doc ->createElement ('exclude ' , "{$ this ->getPath ()}/vendor " );
99
+ $ testsuite = $ this -> doc ->createElement ('testsuite ' );
100
100
$ testsuite ->setAttribute ('name ' , 'orca ' );
101
101
$ testsuite ->appendChild ($ directory );
102
102
$ testsuite ->appendChild ($ exclude );
103
- $ xpath ->query ('//phpunit/testsuites ' )
103
+ $ this -> xpath ->query ('//phpunit/testsuites ' )
104
104
->item (0 )
105
105
->appendChild ($ testsuite );
106
- $ doc ->save ($ path );
107
106
}
108
107
109
108
/**
110
109
* Sets PHPUnit environment variables so that Drupal Test Traits can work.
111
- *
112
- * @param string $path
113
- * The path.
114
- * @param \DOMDocument $doc
115
- * The DOM document.
116
- * @param \DOMXPath $xpath
117
- * The XPath object.
118
110
*/
119
- private function enableDrupalTestTraits (string $ path , \ DOMDocument $ doc , \ DOMXPath $ xpath ): void {
111
+ private function enableDrupalTestTraits (): void {
120
112
// The bootstrap script is located in ORCA's vendor directory, not the
121
113
// fixture's, since ORCA controls the available test frameworks and
122
114
// infrastructure.
123
- $ xpath ->query ('//phpunit ' )
115
+ $ this -> xpath ->query ('//phpunit ' )
124
116
->item (0 )
125
117
->setAttribute ('bootstrap ' , "{$ this ->projectDir }/vendor/weitzman/drupal-test-traits/src/bootstrap.php " );
126
118
127
- if (!$ xpath ->query ('//phpunit/php/env[@name="DTT_BASE_URL"] ' )->length ) {
128
- $ element = $ doc ->createElement ('env ' );
129
- $ element ->setAttribute ('name ' , 'DTT_BASE_URL ' );
130
- $ element ->setAttribute ('value ' , sprintf ('http://%s ' , Fixture::WEB_ADDRESS ));
131
- $ xpath ->query ('//phpunit/php ' )
132
- ->item (0 )
133
- ->appendChild ($ element );
134
- }
135
-
136
- $ doc ->save ($ path );
119
+ $ this ->setEnvironmentVariable ('DTT_BASE_URL ' , sprintf ('http://%s ' , Fixture::WEB_ADDRESS ));
120
+ $ this ->setEnvironmentVariable ('DTT_MINK_DRIVER_ARGS ' , $ this ->getMinkWebDriverArguments ());
137
121
}
138
122
139
123
/**
140
124
* Disables the Symfony Deprecations Helper.
125
+ */
126
+ private function disableSymfonyDeprecationsHelper (): void {
127
+ $ this ->setEnvironmentVariable ('SYMFONY_DEPRECATIONS_HELPER ' , 'disabled ' );
128
+ }
129
+
130
+ /**
131
+ * Sets an environment variable in the PHPUnit configuration.
141
132
*
142
- * @param string $path
143
- * The path.
144
- * @param \DOMDocument $doc
145
- * The DOM document.
146
- * @param \DOMXPath $xpath
147
- * The XPath object.
133
+ * @param string $name
134
+ * The name of the variable to set.
135
+ * @param string $value
136
+ * The value of the variable to set.
148
137
*/
149
- private function disableSymfonyDeprecationsHelper (string $ path , \DOMDocument $ doc , \DOMXPath $ xpath ): void {
150
- $ result = $ xpath ->query ('//phpunit/php/env[@name="SYMFONY_DEPRECATIONS_HELPER"] ' );
151
- // Before Drupal 8.6, the tag in question was present and merely needed the
152
- // value changed.
138
+ private function setEnvironmentVariable (string $ name , string $ value ): void {
139
+ $ result = $ this ->xpath ->query (sprintf ('//phpunit/php/env[@name="%s"] ' , $ name ));
140
+
153
141
if ($ result ->length ) {
154
142
$ element = $ result ->item (0 );
155
- $ element ->setAttribute ('value ' , 'disabled ' );
156
- $ doc ->save ($ path );
143
+ $ element ->setAttribute ('value ' , $ value );
157
144
}
158
- // Since Drupal 8.6, the tag in question has been commented out and must be
159
- // re-created rather than modified directly.
160
145
else {
161
- $ element = $ doc ->createElement ('env ' );
162
- $ element ->setAttribute ('name ' , ' SYMFONY_DEPRECATIONS_HELPER ' );
163
- $ element ->setAttribute ('value ' , ' disabled ' );
164
- $ xpath ->query ('//phpunit/php ' )
146
+ $ element = $ this -> doc ->createElement ('env ' );
147
+ $ element ->setAttribute ('name ' , $ name );
148
+ $ element ->setAttribute ('value ' , $ value );
149
+ $ this -> xpath ->query ('//phpunit/php ' )
165
150
->item (0 )
166
151
->appendChild ($ element );
167
- $ doc ->save ($ path );
168
152
}
169
153
}
170
154
171
155
/**
172
156
* Sets the mink driver arguments.
173
- *
174
- * @param string $path
175
- * The path.
176
- * @param \DOMDocument $doc
177
- * The DOM document.
178
- * @param \DOMXPath $xpath
179
- * The XPath object.
180
157
*/
181
- private function setMinkDriverArguments (string $ path , \ DOMDocument $ doc , \ DOMXPath $ xpath ): void {
158
+ private function setMinkDriverArguments (): void {
182
159
// Create an <env> element containing a JSON array which will control how
183
160
// the Mink driver interacts with Chromedriver.
184
- $ mink_arguments = json_encode ([
161
+ $ this ->setEnvironmentVariable ('MINK_DRIVER_ARGS_WEBDRIVER ' , $ this ->getMinkWebDriverArguments ());
162
+ }
163
+
164
+ /**
165
+ * Writes the PHPUnit configuration to disk.
166
+ *
167
+ * When dumping the XML document tree, PHP will encode all double quotes in
168
+ * the JSON string to ", since the XML attribute value is itself
169
+ * enclosed in double quotes. There's no way to change this behavior, so we
170
+ * must do a string replacement in order to wrap the Mink driver arguments
171
+ * in single quotes.
172
+ *
173
+ * @param string $path
174
+ * The path at which to write the configuration.
175
+ *
176
+ * @see https://stackoverflow.com/questions/5473520/php-dom-and-single-quotes#5473718
177
+ */
178
+ private function writeConfiguration (string $ path ): void {
179
+ $ mink_arguments = $ this ->getMinkWebDriverArguments ();
180
+ $ search = sprintf ('value="%s" ' , htmlentities ($ mink_arguments ));
181
+ $ replace = sprintf ("value='%s' " , $ mink_arguments );
182
+ $ xml = str_replace ($ search , $ replace , $ this ->doc ->saveXML ());
183
+ file_put_contents ($ path , $ xml );
184
+ }
185
+
186
+ /**
187
+ * Returns JSON-encoded arguments for the Mink WebDriver driver.
188
+ *
189
+ * @return string
190
+ * The arguments for the WebDriver Mink driver, encoded as JSON.
191
+ */
192
+ private function getMinkWebDriverArguments (): string {
193
+ return json_encode ([
185
194
'chrome ' ,
186
195
[
187
196
'chrome ' => [
@@ -197,32 +206,6 @@ private function setMinkDriverArguments(string $path, \DOMDocument $doc, \DOMXPa
197
206
],
198
207
'http://localhost:4444 ' ,
199
208
], JSON_UNESCAPED_SLASHES );
200
-
201
- $ name_attribute = 'MINK_DRIVER_ARGS_WEBDRIVER ' ;
202
- $ expression = "//phpunit/php/env[@name=' {$ name_attribute }'] " ;
203
-
204
- if (!$ xpath ->query ($ expression )->length ) {
205
- $ element = $ doc ->createElement ('env ' );
206
- $ element ->setAttribute ('name ' , $ name_attribute );
207
- $ xpath ->query ('//phpunit/php ' )
208
- ->item (0 )
209
- ->appendChild ($ element );
210
- }
211
-
212
- $ xpath ->query ($ expression )
213
- ->item (0 )
214
- ->setAttribute ('value ' , $ mink_arguments );
215
-
216
- // When dumping the XML document tree, PHP will encode all double quotes in
217
- // the JSON string to ", since the XML attribute value is itself
218
- // enclosed in double quotes. There's no way to change this behavior, so
219
- // we must do a string replacement in order to wrap the Mink driver
220
- // arguments in single quotes.
221
- // @see https://stackoverflow.com/questions/5473520/php-dom-and-single-quotes#5473718
222
- $ search = sprintf ('value="%s" ' , htmlentities ($ mink_arguments ));
223
- $ replace = sprintf ("value='%s' " , $ mink_arguments );
224
- $ xml = str_replace ($ search , $ replace , $ doc ->saveXML ());
225
- file_put_contents ($ path , $ xml );
226
209
}
227
210
228
211
/**
0 commit comments