| Total Complexity | 53 |
| Total Lines | 404 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FunctionalTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FunctionalTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class FunctionalTest extends SapphireTest implements TestOnly |
||
| 55 | { |
||
| 56 | // @codingStandardsIgnoreEnd |
||
| 57 | /** |
||
| 58 | * Set this to true on your sub-class to disable the use of themes in this test. |
||
| 59 | * This can be handy for functional testing of modules without having to worry about whether a user has changed |
||
| 60 | * behaviour by replacing the theme. |
||
| 61 | * |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | protected static $disable_themes = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set this to true on your sub-class to use the draft site by default for every test in this class. |
||
| 68 | * |
||
| 69 | * @deprecated 4.2.0:5.0.0 Use ?stage=Stage in your ->get() querystring requests instead |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | protected static $use_draft_site = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var TestSession |
||
| 76 | */ |
||
| 77 | protected $mainSession = null; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * CSSContentParser for the most recently requested page. |
||
| 81 | * |
||
| 82 | * @var CSSContentParser |
||
| 83 | */ |
||
| 84 | protected $cssParser = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * If this is true, then 30x Location headers will be automatically followed. |
||
| 88 | * If not, then you will have to manually call $this->mainSession->followRedirection() to follow them. |
||
| 89 | * However, this will let you inspect the intermediary headers |
||
| 90 | * |
||
| 91 | * @var bool |
||
| 92 | */ |
||
| 93 | protected $autoFollowRedirection = true; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns the {@link Session} object for this test |
||
| 97 | * |
||
| 98 | * @return Session |
||
| 99 | */ |
||
| 100 | public function session() |
||
| 101 | { |
||
| 102 | return $this->mainSession->session(); |
||
| 103 | } |
||
| 104 | |||
| 105 | protected function setUp(): void |
||
| 106 | { |
||
| 107 | parent::setUp(); |
||
| 108 | |||
| 109 | // Skip calling FunctionalTest directly. |
||
| 110 | if (static::class == __CLASS__) { |
||
| 111 | $this->markTestSkipped(sprintf('Skipping %s ', static::class)); |
||
| 112 | } |
||
| 113 | |||
| 114 | $this->mainSession = new TestSession(); |
||
| 115 | |||
| 116 | // Disable theme, if necessary |
||
| 117 | if (static::get_disable_themes()) { |
||
| 118 | SSViewer::config()->update('theme_enabled', false); |
||
| 119 | } |
||
| 120 | |||
| 121 | // Flush user |
||
| 122 | $this->logOut(); |
||
| 123 | |||
| 124 | // Switch to draft site, if necessary |
||
| 125 | // If you rely on this you should be crafting stage-specific urls instead though. |
||
| 126 | if (static::get_use_draft_site()) { |
||
| 127 | $this->useDraftSite(); |
||
| 128 | } |
||
| 129 | |||
| 130 | // Unprotect the site, tests are running with the assumption it's off. They will enable it on a case-by-case |
||
| 131 | // basis. |
||
| 132 | BasicAuth::protect_entire_site(false); |
||
| 133 | |||
| 134 | SecurityToken::disable(); |
||
| 135 | } |
||
| 136 | |||
| 137 | protected function tearDown(): void |
||
| 138 | { |
||
| 139 | SecurityToken::enable(); |
||
| 140 | unset($this->mainSession); |
||
| 141 | parent::tearDown(); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Run a test while mocking the base url with the provided value |
||
| 146 | * @param string $url The base URL to use for this test |
||
| 147 | * @param callable $callback The test to run |
||
| 148 | */ |
||
| 149 | protected function withBaseURL($url, $callback) |
||
| 150 | { |
||
| 151 | $oldBase = Config::inst()->get(Director::class, 'alternate_base_url'); |
||
| 152 | Config::modify()->set(Director::class, 'alternate_base_url', $url); |
||
| 153 | $callback($this); |
||
| 154 | Config::modify()->set(Director::class, 'alternate_base_url', $oldBase); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Run a test while mocking the base folder with the provided value |
||
| 159 | * @param string $folder The base folder to use for this test |
||
| 160 | * @param callable $callback The test to run |
||
| 161 | */ |
||
| 162 | protected function withBaseFolder($folder, $callback) |
||
| 163 | { |
||
| 164 | $oldFolder = Config::inst()->get(Director::class, 'alternate_base_folder'); |
||
| 165 | Config::modify()->set(Director::class, 'alternate_base_folder', $folder); |
||
| 166 | $callback($this); |
||
| 167 | Config::modify()->set(Director::class, 'alternate_base_folder', $oldFolder); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Submit a get request |
||
| 172 | * @uses Director::test() |
||
| 173 | * |
||
| 174 | * @param string $url |
||
| 175 | * @param Session $session |
||
| 176 | * @param array $headers |
||
| 177 | * @param array $cookies |
||
| 178 | * @return HTTPResponse |
||
| 179 | */ |
||
| 180 | public function get($url, $session = null, $headers = null, $cookies = null) |
||
| 181 | { |
||
| 182 | $this->cssParser = null; |
||
| 183 | $response = $this->mainSession->get($url, $session, $headers, $cookies); |
||
| 184 | if ($this->autoFollowRedirection && is_object($response) && $response->getHeader('Location')) { |
||
| 185 | $response = $this->mainSession->followRedirection(); |
||
| 186 | } |
||
| 187 | return $response; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Submit a post request |
||
| 192 | * |
||
| 193 | * @uses Director::test() |
||
| 194 | * @param string $url |
||
| 195 | * @param array $data |
||
| 196 | * @param array $headers |
||
| 197 | * @param Session $session |
||
| 198 | * @param string $body |
||
| 199 | * @param array $cookies |
||
| 200 | * @return HTTPResponse |
||
| 201 | */ |
||
| 202 | public function post($url, $data, $headers = null, $session = null, $body = null, $cookies = null) |
||
| 203 | { |
||
| 204 | $this->cssParser = null; |
||
| 205 | $response = $this->mainSession->post($url, $data, $headers, $session, $body, $cookies); |
||
| 206 | if ($this->autoFollowRedirection && is_object($response) && $response->getHeader('Location')) { |
||
| 207 | $response = $this->mainSession->followRedirection(); |
||
| 208 | } |
||
| 209 | return $response; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Submit the form with the given HTML ID, filling it out with the given data. |
||
| 214 | * Acts on the most recent response. |
||
| 215 | * |
||
| 216 | * Any data parameters have to be present in the form, with exact form field name |
||
| 217 | * and values, otherwise they are removed from the submission. |
||
| 218 | * |
||
| 219 | * Caution: Parameter names have to be formatted |
||
| 220 | * as they are in the form submission, not as they are interpreted by PHP. |
||
| 221 | * Wrong: array('mycheckboxvalues' => array(1 => 'one', 2 => 'two')) |
||
| 222 | * Right: array('mycheckboxvalues[1]' => 'one', 'mycheckboxvalues[2]' => 'two') |
||
| 223 | * |
||
| 224 | * @see http://www.simpletest.org/en/form_testing_documentation.html |
||
| 225 | * |
||
| 226 | * @param string $formID HTML 'id' attribute of a form (loaded through a previous response) |
||
| 227 | * @param string $button HTML 'name' attribute of the button (NOT the 'id' attribute) |
||
| 228 | * @param array $data Map of GET/POST data. |
||
| 229 | * @return HTTPResponse |
||
| 230 | */ |
||
| 231 | public function submitForm($formID, $button = null, $data = []) |
||
| 232 | { |
||
| 233 | $this->cssParser = null; |
||
| 234 | $response = $this->mainSession->submitForm($formID, $button, $data); |
||
| 235 | if ($this->autoFollowRedirection && is_object($response) && $response->getHeader('Location')) { |
||
| 236 | $response = $this->mainSession->followRedirection(); |
||
| 237 | } |
||
| 238 | return $response; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Return the most recent content |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function content() |
||
| 247 | { |
||
| 248 | return $this->mainSession->lastContent(); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Find an attribute in a SimpleXMLElement object by name. |
||
| 253 | * @param SimpleXMLElement $object |
||
| 254 | * @param string $attribute Name of attribute to find |
||
| 255 | * @return SimpleXMLElement object of the attribute |
||
| 256 | */ |
||
| 257 | public function findAttribute($object, $attribute) |
||
| 258 | { |
||
| 259 | $found = false; |
||
| 260 | foreach ($object->attributes() as $a => $b) { |
||
| 261 | if ($a == $attribute) { |
||
| 262 | $found = $b; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | return $found; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Return a CSSContentParser for the most recent content. |
||
| 270 | * |
||
| 271 | * @return CSSContentParser |
||
| 272 | */ |
||
| 273 | public function cssParser() |
||
| 274 | { |
||
| 275 | if (!$this->cssParser) { |
||
| 276 | $this->cssParser = new CSSContentParser($this->mainSession->lastContent()); |
||
| 277 | } |
||
| 278 | return $this->cssParser; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Assert that the most recently queried page contains a number of content tags specified by a CSS selector. |
||
| 283 | * The given CSS selector will be applied to the HTML of the most recent page. The content of every matching tag |
||
| 284 | * will be examined. The assertion fails if one of the expectedMatches fails to appear. |
||
| 285 | * |
||
| 286 | * Note: characters are stripped from the content; make sure that your assertions take this into account. |
||
| 287 | * |
||
| 288 | * @param string $selector A basic CSS selector, e.g. 'li.jobs h3' |
||
| 289 | * @param array|string $expectedMatches The content of at least one of the matched tags |
||
| 290 | * @param string $message |
||
| 291 | * @throws AssertionFailedError |
||
| 292 | */ |
||
| 293 | public function assertPartialMatchBySelector($selector, $expectedMatches, $message = null) |
||
| 294 | { |
||
| 295 | if (is_string($expectedMatches)) { |
||
| 296 | $expectedMatches = [$expectedMatches]; |
||
| 297 | } |
||
| 298 | |||
| 299 | $items = $this->cssParser()->getBySelector($selector); |
||
| 300 | |||
| 301 | $actuals = []; |
||
| 302 | if ($items) { |
||
| 303 | foreach ($items as $item) { |
||
| 304 | $actuals[trim(preg_replace('/\s+/', ' ', (string)$item))] = true; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | $message = $message ?: |
||
| 309 | "Failed asserting the CSS selector '$selector' has a partial match to the expected elements:\n'" |
||
| 310 | . implode("'\n'", $expectedMatches) . "'\n\n" |
||
| 311 | . "Instead the following elements were found:\n'" . implode("'\n'", array_keys($actuals ?: [])) . "'"; |
||
| 312 | |||
| 313 | foreach ($expectedMatches as $match) { |
||
| 314 | $this->assertTrue(isset($actuals[$match]), $message); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Assert that the most recently queried page contains a number of content tags specified by a CSS selector. |
||
| 320 | * The given CSS selector will be applied to the HTML of the most recent page. The full HTML of every matching tag |
||
| 321 | * will be examined. The assertion fails if one of the expectedMatches fails to appear. |
||
| 322 | * |
||
| 323 | * Note: characters are stripped from the content; make sure that your assertions take this into account. |
||
| 324 | * |
||
| 325 | * @param string $selector A basic CSS selector, e.g. 'li.jobs h3' |
||
| 326 | * @param array|string $expectedMatches The content of *all* matching tags as an array |
||
| 327 | * @param string $message |
||
| 328 | * @throws AssertionFailedError |
||
| 329 | */ |
||
| 330 | public function assertExactMatchBySelector($selector, $expectedMatches, $message = null) |
||
| 331 | { |
||
| 332 | if (is_string($expectedMatches)) { |
||
| 333 | $expectedMatches = [$expectedMatches]; |
||
| 334 | } |
||
| 335 | |||
| 336 | $items = $this->cssParser()->getBySelector($selector); |
||
| 337 | |||
| 338 | $actuals = []; |
||
| 339 | if ($items) { |
||
| 340 | foreach ($items as $item) { |
||
| 341 | $actuals[] = trim((string) preg_replace('/\s+/', ' ', (string)$item)); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | $message = $message ?: |
||
| 346 | "Failed asserting the CSS selector '$selector' has an exact match to the expected elements:\n'" |
||
| 347 | . implode("'\n'", $expectedMatches) . "'\n\n" |
||
| 348 | . "Instead the following elements were found:\n'" . implode("'\n'", $actuals) . "'"; |
||
| 349 | |||
| 350 | $this->assertTrue($expectedMatches == $actuals, $message); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Assert that the most recently queried page contains a number of content tags specified by a CSS selector. |
||
| 355 | * The given CSS selector will be applied to the HTML of the most recent page. The content of every matching tag |
||
| 356 | * will be examined. The assertion fails if one of the expectedMatches fails to appear. |
||
| 357 | * |
||
| 358 | * Note: characters are stripped from the content; make sure that your assertions take this into account. |
||
| 359 | * |
||
| 360 | * @param string $selector A basic CSS selector, e.g. 'li.jobs h3' |
||
| 361 | * @param array|string $expectedMatches The content of at least one of the matched tags |
||
| 362 | * @param string $message |
||
| 363 | * @throws AssertionFailedError |
||
| 364 | */ |
||
| 365 | public function assertPartialHTMLMatchBySelector($selector, $expectedMatches, $message = null) |
||
| 366 | { |
||
| 367 | if (is_string($expectedMatches)) { |
||
| 368 | $expectedMatches = [$expectedMatches]; |
||
| 369 | } |
||
| 370 | |||
| 371 | $items = $this->cssParser()->getBySelector($selector); |
||
| 372 | |||
| 373 | $actuals = []; |
||
| 374 | if ($items) { |
||
| 375 | /** @var SimpleXMLElement $item */ |
||
| 376 | foreach ($items as $item) { |
||
| 377 | $actuals[$item->asXML()] = true; |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | $message = $message ?: |
||
| 382 | "Failed asserting the CSS selector '$selector' has a partial match to the expected elements:\n'" |
||
| 383 | . implode("'\n'", $expectedMatches) . "'\n\n" |
||
| 384 | . "Instead the following elements were found:\n'" . implode("'\n'", array_keys($actuals ?: [])) . "'"; |
||
| 385 | |||
| 386 | foreach ($expectedMatches as $match) { |
||
| 387 | $this->assertTrue(isset($actuals[$match]), $message); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Assert that the most recently queried page contains a number of content tags specified by a CSS selector. |
||
| 393 | * The given CSS selector will be applied to the HTML of the most recent page. The full HTML of every matching tag |
||
| 394 | * will be examined. The assertion fails if one of the expectedMatches fails to appear. |
||
| 395 | * |
||
| 396 | * Note: characters are stripped from the content; make sure that your assertions take this into account. |
||
| 397 | * |
||
| 398 | * @param string $selector A basic CSS selector, e.g. 'li.jobs h3' |
||
| 399 | * @param array|string $expectedMatches The content of *all* matched tags as an array |
||
| 400 | * @param string $message |
||
| 401 | * @throws AssertionFailedError |
||
| 402 | */ |
||
| 403 | public function assertExactHTMLMatchBySelector($selector, $expectedMatches, $message = null) |
||
| 404 | { |
||
| 405 | $items = $this->cssParser()->getBySelector($selector); |
||
| 406 | |||
| 407 | $actuals = []; |
||
| 408 | if ($items) { |
||
| 409 | /** @var SimpleXMLElement $item */ |
||
| 410 | foreach ($items as $item) { |
||
| 411 | $actuals[] = $item->asXML(); |
||
| 412 | } |
||
| 413 | } |
||
| 414 | |||
| 415 | $message = $message ?: |
||
| 416 | "Failed asserting the CSS selector '$selector' has an exact match to the expected elements:\n'" |
||
| 417 | . implode("'\n'", $expectedMatches) . "'\n\n" |
||
| 418 | . "Instead the following elements were found:\n'" . implode("'\n'", $actuals) . "'"; |
||
| 419 | |||
| 420 | $this->assertTrue($expectedMatches == $actuals, $message); |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Use the draft (stage) site for testing. |
||
| 425 | * This is helpful if you're not testing publication functionality and don't want "stage management" cluttering |
||
| 426 | * your test. |
||
| 427 | * |
||
| 428 | * @deprecated 4.2.0:5.0.0 Use ?stage=Stage querystring arguments instead of useDraftSite |
||
| 429 | * @param bool $enabled toggle the use of the draft site |
||
| 430 | */ |
||
| 431 | public function useDraftSite($enabled = true) |
||
| 432 | { |
||
| 433 | Deprecation::notice('5.0', 'Use ?stage=Stage querystring arguments instead of useDraftSite'); |
||
| 434 | if ($enabled) { |
||
| 435 | $this->session()->set('readingMode', 'Stage.Stage'); |
||
| 436 | $this->session()->set('unsecuredDraftSite', true); |
||
| 437 | } else { |
||
| 438 | $this->session()->clear('readingMode'); |
||
| 439 | $this->session()->clear('unsecuredDraftSite'); |
||
| 440 | } |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | public static function get_disable_themes() |
||
| 447 | { |
||
| 448 | return static::$disable_themes; |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @deprecated 4.2.0:5.0.0 Use ?stage=Stage in your querystring arguments instead |
||
| 453 | * @return bool |
||
| 454 | */ |
||
| 455 | public static function get_use_draft_site() |
||
| 458 | } |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | /* ------------------------------------------------- |
||
| 463 | * |
||
| 464 | * This version of FunctionalTest is for PHPUnit 5 |
||
| 465 | * The PHPUnit 9 version is at the top of this file |
||
| 466 | * |
||
| 467 | * PHPUnit_Extensions_GroupTestSuite is a class that only exists in PHPUnit 5 |
||
| 468 | * |
||
| 469 | * ------------------------------------------------- |
||
| 470 | */ |
||
| 471 | if (!class_exists(PHPUnit_Extensions_GroupTestSuite::class)) { |
||
| 472 | return; |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 902 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths