| Total Complexity | 127 |
| Total Lines | 1264 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SapphireTest 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 SapphireTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 85 | class SapphireTest extends TestCase implements TestOnly |
||
| 86 | { |
||
| 87 | // @codingStandardsIgnoreEnd |
||
| 88 | /** |
||
| 89 | * Path to fixture data for this test run. |
||
| 90 | * If passed as an array, multiple fixture files will be loaded. |
||
| 91 | * Please note that you won't be able to refer with "=>" notation |
||
| 92 | * between the fixtures, they act independent of each other. |
||
| 93 | * |
||
| 94 | * @var string|array |
||
| 95 | */ |
||
| 96 | protected static $fixture_file = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @deprecated 4.0..5.0 Use FixtureTestState instead |
||
| 100 | * @var FixtureFactory |
||
| 101 | */ |
||
| 102 | protected $fixtureFactory; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var Boolean If set to TRUE, this will force a test database to be generated |
||
| 106 | * in {@link setUp()}. Note that this flag is overruled by the presence of a |
||
| 107 | * {@link $fixture_file}, which always forces a database build. |
||
| 108 | * |
||
| 109 | * @var bool |
||
| 110 | */ |
||
| 111 | protected $usesDatabase = null; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * This test will cleanup its state via transactions. |
||
| 115 | * If set to false a full schema is forced between tests, but at a performance cost. |
||
| 116 | * |
||
| 117 | * @var bool |
||
| 118 | */ |
||
| 119 | protected $usesTransactions = true; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var bool |
||
| 123 | */ |
||
| 124 | protected static $is_running_test = false; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * By default, setUp() does not require default records. Pass |
||
| 128 | * class names in here, and the require/augment default records |
||
| 129 | * function will be called on them. |
||
| 130 | * |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | protected $requireDefaultRecordsFrom = []; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * A list of extensions that can't be applied during the execution of this run. If they are |
||
| 137 | * applied, they will be temporarily removed and a database migration called. |
||
| 138 | * |
||
| 139 | * The keys of the are the classes that the extensions can't be applied the extensions to, and |
||
| 140 | * the values are an array of illegal extensions on that class. |
||
| 141 | * |
||
| 142 | * Set a class to `*` to remove all extensions (unadvised) |
||
| 143 | * |
||
| 144 | * @var array |
||
| 145 | */ |
||
| 146 | protected static $illegal_extensions = []; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * A list of extensions that must be applied during the execution of this run. If they are |
||
| 150 | * not applied, they will be temporarily added and a database migration called. |
||
| 151 | * |
||
| 152 | * The keys of the are the classes to apply the extensions to, and the values are an array |
||
| 153 | * of required extensions on that class. |
||
| 154 | * |
||
| 155 | * Example: |
||
| 156 | * <code> |
||
| 157 | * array("MyTreeDataObject" => array("Versioned", "Hierarchy")) |
||
| 158 | * </code> |
||
| 159 | * |
||
| 160 | * @var array |
||
| 161 | */ |
||
| 162 | protected static $required_extensions = []; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * By default, the test database won't contain any DataObjects that have the interface TestOnly. |
||
| 166 | * This variable lets you define additional TestOnly DataObjects to set up for this test. |
||
| 167 | * Set it to an array of DataObject subclass names. |
||
| 168 | * |
||
| 169 | * @var array |
||
| 170 | */ |
||
| 171 | protected static $extra_dataobjects = []; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * List of class names of {@see Controller} objects to register routes for |
||
| 175 | * Controllers must implement Link() method |
||
| 176 | * |
||
| 177 | * @var array |
||
| 178 | */ |
||
| 179 | protected static $extra_controllers = []; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * We need to disabling backing up of globals to avoid overriding |
||
| 183 | * the few globals SilverStripe relies on, like $lang for the i18n subsystem. |
||
| 184 | * |
||
| 185 | * @see http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html |
||
| 186 | */ |
||
| 187 | protected $backupGlobals = false; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * State management container for SapphireTest |
||
| 191 | * |
||
| 192 | * @var SapphireTestState |
||
| 193 | */ |
||
| 194 | protected static $state = null; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Temp database helper |
||
| 198 | * |
||
| 199 | * @var TempDatabase |
||
| 200 | */ |
||
| 201 | protected static $tempDB = null; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return TempDatabase |
||
| 205 | */ |
||
| 206 | public static function tempDB() |
||
| 207 | { |
||
| 208 | if (!class_exists(TempDatabase::class)) { |
||
| 209 | return null; |
||
| 210 | } |
||
| 211 | |||
| 212 | if (!static::$tempDB) { |
||
| 213 | static::$tempDB = TempDatabase::create(); |
||
| 214 | } |
||
| 215 | return static::$tempDB; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Gets illegal extensions for this class |
||
| 220 | * |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | public static function getIllegalExtensions() |
||
| 224 | { |
||
| 225 | return static::$illegal_extensions; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Gets required extensions for this class |
||
| 230 | * |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | public static function getRequiredExtensions() |
||
| 234 | { |
||
| 235 | return static::$required_extensions; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Check if test bootstrapping has been performed. Must not be relied on |
||
| 240 | * outside of unit tests. |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | protected static function is_running_test() |
||
| 245 | { |
||
| 246 | return self::$is_running_test; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set test running state |
||
| 251 | * |
||
| 252 | * @param bool $bool |
||
| 253 | */ |
||
| 254 | protected static function set_is_running_test($bool) |
||
| 255 | { |
||
| 256 | self::$is_running_test = $bool; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return String |
||
| 261 | */ |
||
| 262 | public static function get_fixture_file() |
||
| 263 | { |
||
| 264 | return static::$fixture_file; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | public function getUsesDatabase() |
||
| 271 | { |
||
| 272 | return $this->usesDatabase; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | public function getUsesTransactions() |
||
| 279 | { |
||
| 280 | return $this->usesTransactions; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function getRequireDefaultRecordsFrom() |
||
| 287 | { |
||
| 288 | return $this->requireDefaultRecordsFrom; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Setup the test. |
||
| 293 | * Always sets up in order: |
||
| 294 | * - Reset php state |
||
| 295 | * - Nest |
||
| 296 | * - Custom state helpers |
||
| 297 | * |
||
| 298 | * User code should call parent::setUp() before custom setup code |
||
| 299 | */ |
||
| 300 | protected function setUp(): void |
||
| 301 | { |
||
| 302 | if (!defined('FRAMEWORK_PATH')) { |
||
| 303 | trigger_error( |
||
| 304 | 'Missing constants, did you remember to include the test bootstrap in your phpunit.xml file?', |
||
| 305 | E_USER_WARNING |
||
| 306 | ); |
||
| 307 | } |
||
| 308 | |||
| 309 | // Call state helpers |
||
| 310 | static::$state->setUp($this); |
||
| 311 | |||
| 312 | // We cannot run the tests on this abstract class. |
||
| 313 | if (static::class == __CLASS__) { |
||
| 314 | $this->markTestSkipped(sprintf('Skipping %s ', static::class)); |
||
| 315 | } |
||
| 316 | |||
| 317 | // i18n needs to be set to the defaults or tests fail |
||
| 318 | if (class_exists(i18n::class)) { |
||
| 319 | i18n::set_locale(i18n::config()->uninherited('default_locale')); |
||
| 320 | } |
||
| 321 | |||
| 322 | // Set default timezone consistently to avoid NZ-specific dependencies |
||
| 323 | date_default_timezone_set('UTC'); |
||
| 324 | |||
| 325 | if (class_exists(Member::class)) { |
||
| 326 | Member::set_password_validator(null); |
||
| 327 | } |
||
| 328 | |||
| 329 | if (class_exists(Cookie::class)) { |
||
| 330 | Cookie::config()->update('report_errors', false); |
||
| 331 | } |
||
| 332 | |||
| 333 | if (class_exists(RootURLController::class)) { |
||
| 334 | RootURLController::reset(); |
||
| 335 | } |
||
| 336 | |||
| 337 | if (class_exists(Security::class)) { |
||
| 338 | Security::clear_database_is_ready(); |
||
| 339 | } |
||
| 340 | |||
| 341 | // Set up test routes |
||
| 342 | $this->setUpRoutes(); |
||
| 343 | |||
| 344 | $fixtureFiles = $this->getFixturePaths(); |
||
| 345 | |||
| 346 | if ($this->shouldSetupDatabaseForCurrentTest($fixtureFiles)) { |
||
| 347 | // Assign fixture factory to deprecated prop in case old tests use it over the getter |
||
| 348 | /** @var FixtureTestState $fixtureState */ |
||
| 349 | $fixtureState = static::$state->getStateByName('fixtures'); |
||
| 350 | $this->fixtureFactory = $fixtureState->getFixtureFactory(static::class); |
||
| 351 | |||
| 352 | $this->logInWithPermission('ADMIN'); |
||
| 353 | } |
||
| 354 | |||
| 355 | // turn off template debugging |
||
| 356 | if (class_exists(SSViewer::class)) { |
||
| 357 | SSViewer::config()->update('source_file_comments', false); |
||
| 358 | } |
||
| 359 | |||
| 360 | // Set up the test mailer |
||
| 361 | if (class_exists(TestMailer::class)) { |
||
| 362 | Injector::inst()->registerService(new TestMailer(), Mailer::class); |
||
| 363 | } |
||
| 364 | |||
| 365 | if (class_exists(Email::class)) { |
||
| 366 | Email::config()->remove('send_all_emails_to'); |
||
| 367 | Email::config()->remove('send_all_emails_from'); |
||
| 368 | Email::config()->remove('cc_all_emails_to'); |
||
| 369 | Email::config()->remove('bcc_all_emails_to'); |
||
| 370 | } |
||
| 371 | } |
||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * Helper method to determine if the current test should enable a test database |
||
| 376 | * |
||
| 377 | * @param $fixtureFiles |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | protected function shouldSetupDatabaseForCurrentTest($fixtureFiles) |
||
| 381 | { |
||
| 382 | $databaseEnabledByDefault = $fixtureFiles || $this->usesDatabase; |
||
| 383 | |||
| 384 | return ($databaseEnabledByDefault && !$this->currentTestDisablesDatabase()) |
||
| 385 | || $this->currentTestEnablesDatabase(); |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Helper method to check, if the current test uses the database. |
||
| 390 | * This can be switched on with the annotation "@useDatabase" |
||
| 391 | * |
||
| 392 | * @return bool |
||
| 393 | */ |
||
| 394 | protected function currentTestEnablesDatabase() |
||
| 395 | { |
||
| 396 | $annotations = $this->getAnnotations(); |
||
| 397 | |||
| 398 | return array_key_exists('useDatabase', $annotations['method']) |
||
| 399 | && $annotations['method']['useDatabase'][0] !== 'false'; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Helper method to check, if the current test uses the database. |
||
| 404 | * This can be switched on with the annotation "@useDatabase false" |
||
| 405 | * |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | protected function currentTestDisablesDatabase() |
||
| 409 | { |
||
| 410 | $annotations = $this->getAnnotations(); |
||
| 411 | |||
| 412 | return array_key_exists('useDatabase', $annotations['method']) |
||
| 413 | && $annotations['method']['useDatabase'][0] === 'false'; |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Called once per test case ({@link SapphireTest} subclass). |
||
| 418 | * This is different to {@link setUp()}, which gets called once |
||
| 419 | * per method. Useful to initialize expensive operations which |
||
| 420 | * don't change state for any called method inside the test, |
||
| 421 | * e.g. dynamically adding an extension. See {@link teardownAfterClass()} |
||
| 422 | * for tearing down the state again. |
||
| 423 | * |
||
| 424 | * Always sets up in order: |
||
| 425 | * - Reset php state |
||
| 426 | * - Nest |
||
| 427 | * - Custom state helpers |
||
| 428 | * |
||
| 429 | * User code should call parent::setUpBeforeClass() before custom setup code |
||
| 430 | * |
||
| 431 | * @throws Exception |
||
| 432 | */ |
||
| 433 | public static function setUpBeforeClass(): void |
||
| 434 | { |
||
| 435 | // Start tests |
||
| 436 | static::start(); |
||
| 437 | |||
| 438 | if (!static::$state) { |
||
| 439 | throw new Exception('SapphireTest failed to bootstrap!'); |
||
| 440 | } |
||
| 441 | |||
| 442 | // Call state helpers |
||
| 443 | static::$state->setUpOnce(static::class); |
||
| 444 | |||
| 445 | // Build DB if we have objects |
||
| 446 | if (class_exists(DataObject::class) && static::getExtraDataObjects()) { |
||
| 447 | DataObject::reset(); |
||
| 448 | static::resetDBSchema(true, true); |
||
| 449 | } |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * tearDown method that's called once per test class rather once per test method. |
||
| 454 | * |
||
| 455 | * Always sets up in order: |
||
| 456 | * - Custom state helpers |
||
| 457 | * - Unnest |
||
| 458 | * - Reset php state |
||
| 459 | * |
||
| 460 | * User code should call parent::tearDownAfterClass() after custom tear down code |
||
| 461 | */ |
||
| 462 | public static function tearDownAfterClass(): void |
||
| 463 | { |
||
| 464 | // Call state helpers |
||
| 465 | static::$state->tearDownOnce(static::class); |
||
| 466 | |||
| 467 | // Reset DB schema |
||
| 468 | static::resetDBSchema(); |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @return FixtureFactory|false |
||
| 473 | * @deprecated 4.0.0:5.0.0 |
||
| 474 | */ |
||
| 475 | public function getFixtureFactory() |
||
| 476 | { |
||
| 477 | Deprecation::notice('5.0', __FUNCTION__ . ' is deprecated, use ' . FixtureTestState::class . ' instead'); |
||
| 478 | /** @var FixtureTestState $state */ |
||
| 479 | $state = static::$state->getStateByName('fixtures'); |
||
| 480 | return $state->getFixtureFactory(static::class); |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Sets a new fixture factory |
||
| 485 | * @param FixtureFactory $factory |
||
| 486 | * @return $this |
||
| 487 | * @deprecated 4.0.0:5.0.0 |
||
| 488 | */ |
||
| 489 | public function setFixtureFactory(FixtureFactory $factory) |
||
| 490 | { |
||
| 491 | Deprecation::notice('5.0', __FUNCTION__ . ' is deprecated, use ' . FixtureTestState::class . ' instead'); |
||
| 492 | /** @var FixtureTestState $state */ |
||
| 493 | $state = static::$state->getStateByName('fixtures'); |
||
| 494 | $state->setFixtureFactory($factory, static::class); |
||
| 495 | $this->fixtureFactory = $factory; |
||
| 496 | return $this; |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Get the ID of an object from the fixture. |
||
| 501 | * |
||
| 502 | * @param string $className The data class or table name, as specified in your fixture file. Parent classes won't work |
||
| 503 | * @param string $identifier The identifier string, as provided in your fixture file |
||
| 504 | * @return int |
||
| 505 | */ |
||
| 506 | protected function idFromFixture($className, $identifier) |
||
| 507 | { |
||
| 508 | /** @var FixtureTestState $state */ |
||
| 509 | $state = static::$state->getStateByName('fixtures'); |
||
| 510 | $id = $state->getFixtureFactory(static::class)->getId($className, $identifier); |
||
| 511 | |||
| 512 | if (!$id) { |
||
| 513 | throw new InvalidArgumentException(sprintf( |
||
| 514 | "Couldn't find object '%s' (class: %s)", |
||
| 515 | $identifier, |
||
| 516 | $className |
||
| 517 | )); |
||
| 518 | } |
||
| 519 | |||
| 520 | return $id; |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Return all of the IDs in the fixture of a particular class name. |
||
| 525 | * Will collate all IDs form all fixtures if multiple fixtures are provided. |
||
| 526 | * |
||
| 527 | * @param string $className The data class or table name, as specified in your fixture file |
||
| 528 | * @return array A map of fixture-identifier => object-id |
||
| 529 | */ |
||
| 530 | protected function allFixtureIDs($className) |
||
| 531 | { |
||
| 532 | /** @var FixtureTestState $state */ |
||
| 533 | $state = static::$state->getStateByName('fixtures'); |
||
| 534 | return $state->getFixtureFactory(static::class)->getIds($className); |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Get an object from the fixture. |
||
| 539 | * |
||
| 540 | * @param string $className The data class or table name, as specified in your fixture file. Parent classes won't work |
||
| 541 | * @param string $identifier The identifier string, as provided in your fixture file |
||
| 542 | * |
||
| 543 | * @return DataObject |
||
| 544 | */ |
||
| 545 | protected function objFromFixture($className, $identifier) |
||
| 546 | { |
||
| 547 | /** @var FixtureTestState $state */ |
||
| 548 | $state = static::$state->getStateByName('fixtures'); |
||
| 549 | $obj = $state->getFixtureFactory(static::class)->get($className, $identifier); |
||
| 550 | |||
| 551 | if (!$obj) { |
||
| 552 | throw new InvalidArgumentException(sprintf( |
||
| 553 | "Couldn't find object '%s' (class: %s)", |
||
| 554 | $identifier, |
||
| 555 | $className |
||
| 556 | )); |
||
| 557 | } |
||
| 558 | |||
| 559 | return $obj; |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Load a YAML fixture file into the database. |
||
| 564 | * Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture. |
||
| 565 | * Doesn't clear existing fixtures. |
||
| 566 | * @param string $fixtureFile The location of the .yml fixture file, relative to the site base dir |
||
| 567 | * @deprecated 4.0.0:5.0.0 |
||
| 568 | * |
||
| 569 | */ |
||
| 570 | public function loadFixture($fixtureFile) |
||
| 571 | { |
||
| 572 | Deprecation::notice('5.0', __FUNCTION__ . ' is deprecated, use ' . FixtureTestState::class . ' instead'); |
||
| 573 | $fixture = Injector::inst()->create(YamlFixture::class, $fixtureFile); |
||
| 574 | $fixture->writeInto($this->getFixtureFactory()); |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Clear all fixtures which were previously loaded through |
||
| 579 | * {@link loadFixture()} |
||
| 580 | */ |
||
| 581 | public function clearFixtures() |
||
| 582 | { |
||
| 583 | /** @var FixtureTestState $state */ |
||
| 584 | $state = static::$state->getStateByName('fixtures'); |
||
| 585 | $state->getFixtureFactory(static::class)->clear(); |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Useful for writing unit tests without hardcoding folder structures. |
||
| 590 | * |
||
| 591 | * @return string Absolute path to current class. |
||
| 592 | */ |
||
| 593 | protected function getCurrentAbsolutePath() |
||
| 594 | { |
||
| 595 | $filename = ClassLoader::inst()->getItemPath(static::class); |
||
| 596 | if (!$filename) { |
||
| 597 | throw new LogicException('getItemPath returned null for ' . static::class |
||
| 598 | . '. Try adding flush=1 to the test run.'); |
||
| 599 | } |
||
| 600 | return dirname($filename); |
||
| 601 | } |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @return string File path relative to webroot |
||
| 605 | */ |
||
| 606 | protected function getCurrentRelativePath() |
||
| 607 | { |
||
| 608 | $base = Director::baseFolder(); |
||
| 609 | $path = $this->getCurrentAbsolutePath(); |
||
| 610 | if (substr($path, 0, strlen($base)) == $base) { |
||
| 611 | $path = preg_replace('/^\/*/', '', substr($path, strlen($base))); |
||
| 612 | } |
||
| 613 | return $path; |
||
| 614 | } |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Setup the test. |
||
| 618 | * Always sets up in order: |
||
| 619 | * - Custom state helpers |
||
| 620 | * - Unnest |
||
| 621 | * - Reset php state |
||
| 622 | * |
||
| 623 | * User code should call parent::tearDown() after custom tear down code |
||
| 624 | */ |
||
| 625 | protected function tearDown(): void |
||
| 626 | { |
||
| 627 | // Reset mocked datetime |
||
| 628 | if (class_exists(DBDatetime::class)) { |
||
| 629 | DBDatetime::clear_mock_now(); |
||
| 630 | } |
||
| 631 | |||
| 632 | // Stop the redirection that might have been requested in the test. |
||
| 633 | // Note: Ideally a clean Controller should be created for each test. |
||
| 634 | // Now all tests executed in a batch share the same controller. |
||
| 635 | if (class_exists(Controller::class)) { |
||
| 636 | $controller = Controller::has_curr() ? Controller::curr() : null; |
||
| 637 | if ($controller && ($response = $controller->getResponse()) && $response->getHeader('Location')) { |
||
| 638 | $response->setStatusCode(200); |
||
| 639 | $response->removeHeader('Location'); |
||
| 640 | } |
||
| 641 | } |
||
| 642 | |||
| 643 | // Call state helpers |
||
| 644 | static::$state->tearDown($this); |
||
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Clear the log of emails sent |
||
| 649 | * |
||
| 650 | * @return bool True if emails cleared |
||
| 651 | */ |
||
| 652 | public function clearEmails() |
||
| 653 | { |
||
| 654 | /** @var Mailer $mailer */ |
||
| 655 | $mailer = Injector::inst()->get(Mailer::class); |
||
| 656 | if ($mailer instanceof TestMailer) { |
||
| 657 | $mailer->clearEmails(); |
||
| 658 | return true; |
||
| 659 | } |
||
| 660 | return false; |
||
| 661 | } |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Search for an email that was sent. |
||
| 665 | * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
||
| 666 | * @param string $to |
||
| 667 | * @param string $from |
||
| 668 | * @param string $subject |
||
| 669 | * @param string $content |
||
| 670 | * @return array|null Contains keys: 'Type', 'To', 'From', 'Subject', 'Content', 'PlainContent', 'AttachedFiles', |
||
| 671 | * 'HtmlContent' |
||
| 672 | */ |
||
| 673 | public static function findEmail($to, $from = null, $subject = null, $content = null) |
||
| 674 | { |
||
| 675 | /** @var Mailer $mailer */ |
||
| 676 | $mailer = Injector::inst()->get(Mailer::class); |
||
| 677 | if ($mailer instanceof TestMailer) { |
||
| 678 | return $mailer->findEmail($to, $from, $subject, $content); |
||
| 679 | } |
||
| 680 | return null; |
||
| 681 | } |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Assert that the matching email was sent since the last call to clearEmails() |
||
| 685 | * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. |
||
| 686 | * |
||
| 687 | * @param string $to |
||
| 688 | * @param string $from |
||
| 689 | * @param string $subject |
||
| 690 | * @param string $content |
||
| 691 | */ |
||
| 692 | public static function assertEmailSent($to, $from = null, $subject = null, $content = null) |
||
| 693 | { |
||
| 694 | $found = (bool)static::findEmail($to, $from, $subject, $content); |
||
| 695 | |||
| 696 | $infoParts = ''; |
||
| 697 | $withParts = []; |
||
| 698 | if ($to) { |
||
| 699 | $infoParts .= " to '$to'"; |
||
| 700 | } |
||
| 701 | if ($from) { |
||
| 702 | $infoParts .= " from '$from'"; |
||
| 703 | } |
||
| 704 | if ($subject) { |
||
| 705 | $withParts[] = "subject '$subject'"; |
||
| 706 | } |
||
| 707 | if ($content) { |
||
| 708 | $withParts[] = "content '$content'"; |
||
| 709 | } |
||
| 710 | if ($withParts) { |
||
| 711 | $infoParts .= ' with ' . implode(' and ', $withParts); |
||
| 712 | } |
||
| 713 | |||
| 714 | static::assertTrue( |
||
| 715 | $found, |
||
| 716 | "Failed asserting that an email was sent$infoParts." |
||
| 717 | ); |
||
| 718 | } |
||
| 719 | |||
| 720 | |||
| 721 | /** |
||
| 722 | * Assert that the given {@link SS_List} includes DataObjects matching the given key-value |
||
| 723 | * pairs. Each match must correspond to 1 distinct record. |
||
| 724 | * |
||
| 725 | * @param SS_List|array $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 726 | * either pass a single pattern or an array of patterns. |
||
| 727 | * @param SS_List $list The {@link SS_List} to test. |
||
| 728 | * @param string $message |
||
| 729 | * |
||
| 730 | * Examples |
||
| 731 | * -------- |
||
| 732 | * Check that $members includes an entry with Email = [email protected]: |
||
| 733 | * $this->assertListContains(['Email' => '[email protected]'], $members); |
||
| 734 | * |
||
| 735 | * Check that $members includes entries with Email = [email protected] and with |
||
| 736 | * Email = [email protected]: |
||
| 737 | * $this->assertListContains([ |
||
| 738 | * ['Email' => '[email protected]'], |
||
| 739 | * ['Email' => '[email protected]'], |
||
| 740 | * ], $members); |
||
| 741 | */ |
||
| 742 | public static function assertListContains($matches, SS_List $list, $message = '') |
||
| 743 | { |
||
| 744 | if (!is_array($matches)) { |
||
| 745 | throw self::createInvalidArgumentException( |
||
| 746 | 1, |
||
| 747 | 'array' |
||
| 748 | ); |
||
| 749 | } |
||
| 750 | |||
| 751 | static::assertThat( |
||
| 752 | $list, |
||
| 753 | new SSListContains( |
||
| 754 | $matches |
||
| 755 | ), |
||
| 756 | $message |
||
| 757 | ); |
||
| 758 | } |
||
| 759 | |||
| 760 | /** |
||
| 761 | * @param $matches |
||
| 762 | * @param $dataObjectSet |
||
| 763 | * @deprecated 4.0.0:5.0.0 Use assertListContains() instead |
||
| 764 | * |
||
| 765 | */ |
||
| 766 | public function assertDOSContains($matches, $dataObjectSet) |
||
| 767 | { |
||
| 768 | Deprecation::notice('5.0', 'Use assertListContains() instead'); |
||
| 769 | static::assertListContains($matches, $dataObjectSet); |
||
| 770 | } |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Asserts that no items in a given list appear in the given dataobject list |
||
| 774 | * |
||
| 775 | * @param SS_List|array $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 776 | * either pass a single pattern or an array of patterns. |
||
| 777 | * @param SS_List $list The {@link SS_List} to test. |
||
| 778 | * @param string $message |
||
| 779 | * |
||
| 780 | * Examples |
||
| 781 | * -------- |
||
| 782 | * Check that $members doesn't have an entry with Email = [email protected]: |
||
| 783 | * $this->assertListNotContains(['Email' => '[email protected]'], $members); |
||
| 784 | * |
||
| 785 | * Check that $members doesn't have entries with Email = [email protected] and with |
||
| 786 | * Email = [email protected]: |
||
| 787 | * $this->assertListNotContains([ |
||
| 788 | * ['Email' => '[email protected]'], |
||
| 789 | * ['Email' => '[email protected]'], |
||
| 790 | * ], $members); |
||
| 791 | */ |
||
| 792 | public static function assertListNotContains($matches, SS_List $list, $message = '') |
||
| 793 | { |
||
| 794 | if (!is_array($matches)) { |
||
| 795 | throw self::createInvalidArgumentException( |
||
| 796 | 1, |
||
| 797 | 'array' |
||
| 798 | ); |
||
| 799 | } |
||
| 800 | |||
| 801 | $constraint = new LogicalNot( |
||
| 802 | new SSListContains( |
||
| 803 | $matches |
||
| 804 | ) |
||
| 805 | ); |
||
| 806 | |||
| 807 | static::assertThat( |
||
| 808 | $list, |
||
| 809 | $constraint, |
||
| 810 | $message |
||
| 811 | ); |
||
| 812 | } |
||
| 813 | |||
| 814 | /** |
||
| 815 | * @param $matches |
||
| 816 | * @param $dataObjectSet |
||
| 817 | * @deprecated 4.0.0:5.0.0 Use assertListNotContains() instead |
||
| 818 | * |
||
| 819 | */ |
||
| 820 | public static function assertNotDOSContains($matches, $dataObjectSet) |
||
| 821 | { |
||
| 822 | Deprecation::notice('5.0', 'Use assertListNotContains() instead'); |
||
| 823 | static::assertListNotContains($matches, $dataObjectSet); |
||
| 824 | } |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Assert that the given {@link SS_List} includes only DataObjects matching the given |
||
| 828 | * key-value pairs. Each match must correspond to 1 distinct record. |
||
| 829 | * |
||
| 830 | * Example |
||
| 831 | * -------- |
||
| 832 | * Check that *only* the entries Sam Minnee and Ingo Schommer exist in $members. Order doesn't |
||
| 833 | * matter: |
||
| 834 | * $this->assertListEquals([ |
||
| 835 | * ['FirstName' =>'Sam', 'Surname' => 'Minnee'], |
||
| 836 | * ['FirstName' => 'Ingo', 'Surname' => 'Schommer'], |
||
| 837 | * ], $members); |
||
| 838 | * |
||
| 839 | * @param mixed $matches The patterns to match. Each pattern is a map of key-value pairs. You can |
||
| 840 | * either pass a single pattern or an array of patterns. |
||
| 841 | * @param mixed $list The {@link SS_List} to test. |
||
| 842 | * @param string $message |
||
| 843 | */ |
||
| 844 | public static function assertListEquals($matches, SS_List $list, $message = '') |
||
| 845 | { |
||
| 846 | if (!is_array($matches)) { |
||
| 847 | throw self::createInvalidArgumentException( |
||
| 848 | 1, |
||
| 849 | 'array' |
||
| 850 | ); |
||
| 851 | } |
||
| 852 | |||
| 853 | static::assertThat( |
||
| 854 | $list, |
||
| 855 | new SSListContainsOnly( |
||
| 856 | $matches |
||
| 857 | ), |
||
| 858 | $message |
||
| 859 | ); |
||
| 860 | } |
||
| 861 | |||
| 862 | /** |
||
| 863 | * @param $matches |
||
| 864 | * @param SS_List $dataObjectSet |
||
| 865 | * @deprecated 4.0.0:5.0.0 Use assertListEquals() instead |
||
| 866 | * |
||
| 867 | */ |
||
| 868 | public function assertDOSEquals($matches, $dataObjectSet) |
||
| 869 | { |
||
| 870 | Deprecation::notice('5.0', 'Use assertListEquals() instead'); |
||
| 871 | static::assertListEquals($matches, $dataObjectSet); |
||
| 872 | } |
||
| 873 | |||
| 874 | |||
| 875 | /** |
||
| 876 | * Assert that the every record in the given {@link SS_List} matches the given key-value |
||
| 877 | * pairs. |
||
| 878 | * |
||
| 879 | * Example |
||
| 880 | * -------- |
||
| 881 | * Check that every entry in $members has a Status of 'Active': |
||
| 882 | * $this->assertListAllMatch(['Status' => 'Active'], $members); |
||
| 883 | * |
||
| 884 | * @param mixed $match The pattern to match. The pattern is a map of key-value pairs. |
||
| 885 | * @param mixed $list The {@link SS_List} to test. |
||
| 886 | * @param string $message |
||
| 887 | */ |
||
| 888 | public static function assertListAllMatch($match, SS_List $list, $message = '') |
||
| 889 | { |
||
| 890 | if (!is_array($match)) { |
||
| 891 | throw self::createInvalidArgumentException( |
||
| 892 | 1, |
||
| 893 | 'array' |
||
| 894 | ); |
||
| 895 | } |
||
| 896 | |||
| 897 | static::assertThat( |
||
| 898 | $list, |
||
| 899 | new SSListContainsOnlyMatchingItems( |
||
| 900 | $match |
||
| 901 | ), |
||
| 902 | $message |
||
| 903 | ); |
||
| 904 | } |
||
| 905 | |||
| 906 | /** |
||
| 907 | * @param $match |
||
| 908 | * @param SS_List $dataObjectSet |
||
| 909 | * @deprecated 4.0.0:5.0.0 Use assertListAllMatch() instead |
||
| 910 | * |
||
| 911 | */ |
||
| 912 | public function assertDOSAllMatch($match, SS_List $dataObjectSet) |
||
| 913 | { |
||
| 914 | Deprecation::notice('5.0', 'Use assertListAllMatch() instead'); |
||
| 915 | static::assertListAllMatch($match, $dataObjectSet); |
||
| 916 | } |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Removes sequences of repeated whitespace characters from SQL queries |
||
| 920 | * making them suitable for string comparison |
||
| 921 | * |
||
| 922 | * @param string $sql |
||
| 923 | * @return string The cleaned and normalised SQL string |
||
| 924 | */ |
||
| 925 | protected static function normaliseSQL($sql) |
||
| 926 | { |
||
| 927 | return trim(preg_replace('/\s+/m', ' ', $sql)); |
||
| 928 | } |
||
| 929 | |||
| 930 | /** |
||
| 931 | * Asserts that two SQL queries are equivalent |
||
| 932 | * |
||
| 933 | * @param string $expectedSQL |
||
| 934 | * @param string $actualSQL |
||
| 935 | * @param string $message |
||
| 936 | */ |
||
| 937 | public static function assertSQLEquals( |
||
| 938 | $expectedSQL, |
||
| 939 | $actualSQL, |
||
| 940 | $message = '' |
||
| 941 | ) { |
||
| 942 | // Normalise SQL queries to remove patterns of repeating whitespace |
||
| 943 | $expectedSQL = static::normaliseSQL($expectedSQL); |
||
| 944 | $actualSQL = static::normaliseSQL($actualSQL); |
||
| 945 | |||
| 946 | static::assertEquals($expectedSQL, $actualSQL, $message); |
||
| 947 | } |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Asserts that a SQL query contains a SQL fragment |
||
| 951 | * |
||
| 952 | * @param string $needleSQL |
||
| 953 | * @param string $haystackSQL |
||
| 954 | * @param string $message |
||
| 955 | */ |
||
| 956 | public static function assertSQLContains( |
||
| 957 | $needleSQL, |
||
| 958 | $haystackSQL, |
||
| 959 | $message = '' |
||
| 960 | ) { |
||
| 961 | $needleSQL = static::normaliseSQL($needleSQL); |
||
| 962 | $haystackSQL = static::normaliseSQL($haystackSQL); |
||
| 963 | if (is_iterable($haystackSQL)) { |
||
| 964 | /** @var iterable $iterableHaystackSQL */ |
||
| 965 | $iterableHaystackSQL = $haystackSQL; |
||
| 966 | static::assertContains($needleSQL, $iterableHaystackSQL, $message); |
||
| 967 | } else { |
||
| 968 | static::assertStringContainsString($needleSQL, $haystackSQL, $message); |
||
| 969 | } |
||
| 970 | } |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Asserts that a SQL query contains a SQL fragment |
||
| 974 | * |
||
| 975 | * @param string $needleSQL |
||
| 976 | * @param string $haystackSQL |
||
| 977 | * @param string $message |
||
| 978 | */ |
||
| 979 | public static function assertSQLNotContains( |
||
| 980 | $needleSQL, |
||
| 981 | $haystackSQL, |
||
| 982 | $message = '' |
||
| 983 | ) { |
||
| 984 | $needleSQL = static::normaliseSQL($needleSQL); |
||
| 985 | $haystackSQL = static::normaliseSQL($haystackSQL); |
||
| 986 | if (is_iterable($haystackSQL)) { |
||
| 987 | /** @var iterable $iterableHaystackSQL */ |
||
| 988 | $iterableHaystackSQL = $haystackSQL; |
||
| 989 | static::assertNotContains($needleSQL, $iterableHaystackSQL, $message); |
||
| 990 | } else { |
||
| 991 | static::assertStringNotContainsString($needleSQL, $haystackSQL, $message); |
||
| 992 | } |
||
| 993 | } |
||
| 994 | |||
| 995 | /** |
||
| 996 | * Start test environment |
||
| 997 | */ |
||
| 998 | public static function start() |
||
| 999 | { |
||
| 1000 | if (static::is_running_test()) { |
||
| 1001 | return; |
||
| 1002 | } |
||
| 1003 | |||
| 1004 | // Health check |
||
| 1005 | if (InjectorLoader::inst()->countManifests()) { |
||
| 1006 | throw new LogicException('SapphireTest::start() cannot be called within another application'); |
||
| 1007 | } |
||
| 1008 | static::set_is_running_test(true); |
||
| 1009 | |||
| 1010 | // Test application |
||
| 1011 | $kernel = new TestKernel(BASE_PATH); |
||
| 1012 | |||
| 1013 | if (class_exists(HTTPApplication::class)) { |
||
| 1014 | // Mock request |
||
| 1015 | $_SERVER['argv'] = ['vendor/bin/phpunit', '/']; |
||
| 1016 | $request = CLIRequestBuilder::createFromEnvironment(); |
||
| 1017 | |||
| 1018 | $app = new HTTPApplication($kernel); |
||
| 1019 | $flush = array_key_exists('flush', $request->getVars()); |
||
| 1020 | |||
| 1021 | // Custom application |
||
| 1022 | $res = $app->execute($request, function (HTTPRequest $request) { |
||
| 1023 | // Start session and execute |
||
| 1024 | $request->getSession()->init($request); |
||
| 1025 | |||
| 1026 | // Invalidate classname spec since the test manifest will now pull out new subclasses for each internal class |
||
| 1027 | // (e.g. Member will now have various subclasses of DataObjects that implement TestOnly) |
||
| 1028 | DataObject::reset(); |
||
| 1029 | |||
| 1030 | // Set dummy controller; |
||
| 1031 | $controller = Controller::create(); |
||
| 1032 | $controller->setRequest($request); |
||
| 1033 | $controller->pushCurrent(); |
||
| 1034 | $controller->doInit(); |
||
| 1035 | }, $flush); |
||
| 1036 | |||
| 1037 | if ($res && $res->isError()) { |
||
| 1038 | throw new LogicException($res->getBody()); |
||
| 1039 | } |
||
| 1040 | } else { |
||
| 1041 | // Allow flush from the command line in the absence of HTTPApplication's special sauce |
||
| 1042 | $flush = false; |
||
| 1043 | foreach ($_SERVER['argv'] as $arg) { |
||
| 1044 | if (preg_match('/^(--)?flush(=1)?$/', $arg)) { |
||
| 1045 | $flush = true; |
||
| 1046 | } |
||
| 1047 | } |
||
| 1048 | $kernel->boot($flush); |
||
| 1049 | } |
||
| 1050 | |||
| 1051 | // Register state |
||
| 1052 | static::$state = SapphireTestState::singleton(); |
||
| 1053 | // Register temp DB holder |
||
| 1054 | static::tempDB(); |
||
| 1055 | } |
||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Reset the testing database's schema, but only if it is active |
||
| 1059 | * @param bool $includeExtraDataObjects If true, the extraDataObjects tables will also be included |
||
| 1060 | * @param bool $forceCreate Force DB to be created if it doesn't exist |
||
| 1061 | */ |
||
| 1062 | public static function resetDBSchema($includeExtraDataObjects = false, $forceCreate = false) |
||
| 1063 | { |
||
| 1064 | if (!static::$tempDB) { |
||
| 1065 | return; |
||
| 1066 | } |
||
| 1067 | |||
| 1068 | // Check if DB is active before reset |
||
| 1069 | if (!static::$tempDB->isUsed()) { |
||
| 1070 | if (!$forceCreate) { |
||
| 1071 | return; |
||
| 1072 | } |
||
| 1073 | static::$tempDB->build(); |
||
| 1074 | } |
||
| 1075 | $extraDataObjects = $includeExtraDataObjects ? static::getExtraDataObjects() : []; |
||
| 1076 | static::$tempDB->resetDBSchema((array)$extraDataObjects); |
||
| 1077 | } |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * A wrapper for automatically performing callbacks as a user with a specific permission |
||
| 1081 | * |
||
| 1082 | * @param string|array $permCode |
||
| 1083 | * @param callable $callback |
||
| 1084 | * @return mixed |
||
| 1085 | */ |
||
| 1086 | public function actWithPermission($permCode, $callback) |
||
| 1087 | { |
||
| 1088 | return Member::actAs($this->createMemberWithPermission($permCode), $callback); |
||
| 1089 | } |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Create Member and Group objects on demand with specific permission code |
||
| 1093 | * |
||
| 1094 | * @param string|array $permCode |
||
| 1095 | * @return Member |
||
| 1096 | */ |
||
| 1097 | protected function createMemberWithPermission($permCode) |
||
| 1098 | { |
||
| 1099 | if (is_array($permCode)) { |
||
| 1100 | $permArray = $permCode; |
||
| 1101 | $permCode = implode('.', $permCode); |
||
| 1102 | } else { |
||
| 1103 | $permArray = [$permCode]; |
||
| 1104 | } |
||
| 1105 | |||
| 1106 | // Check cached member |
||
| 1107 | if (isset($this->cache_generatedMembers[$permCode])) { |
||
| 1108 | $member = $this->cache_generatedMembers[$permCode]; |
||
| 1109 | } else { |
||
| 1110 | // Generate group with these permissions |
||
| 1111 | $group = Group::create(); |
||
| 1112 | $group->Title = "$permCode group"; |
||
| 1113 | $group->write(); |
||
| 1114 | |||
| 1115 | // Create each individual permission |
||
| 1116 | foreach ($permArray as $permArrayItem) { |
||
| 1117 | $permission = Permission::create(); |
||
| 1118 | $permission->Code = $permArrayItem; |
||
| 1119 | $permission->write(); |
||
| 1120 | $group->Permissions()->add($permission); |
||
| 1121 | } |
||
| 1122 | |||
| 1123 | $member = Member::get()->filter([ |
||
| 1124 | 'Email' => "[email protected]", |
||
| 1125 | ])->first(); |
||
| 1126 | if (!$member) { |
||
| 1127 | $member = Member::create(); |
||
| 1128 | } |
||
| 1129 | |||
| 1130 | $member->FirstName = $permCode; |
||
| 1131 | $member->Surname = 'User'; |
||
| 1132 | $member->Email = "[email protected]"; |
||
| 1133 | $member->write(); |
||
| 1134 | $group->Members()->add($member); |
||
| 1135 | |||
| 1136 | $this->cache_generatedMembers[$permCode] = $member; |
||
| 1137 | } |
||
| 1138 | return $member; |
||
| 1139 | } |
||
| 1140 | |||
| 1141 | /** |
||
| 1142 | * Create a member and group with the given permission code, and log in with it. |
||
| 1143 | * Returns the member ID. |
||
| 1144 | * |
||
| 1145 | * @param string|array $permCode Either a permission, or list of permissions |
||
| 1146 | * @return int Member ID |
||
| 1147 | */ |
||
| 1148 | public function logInWithPermission($permCode = 'ADMIN') |
||
| 1149 | { |
||
| 1150 | $member = $this->createMemberWithPermission($permCode); |
||
| 1151 | $this->logInAs($member); |
||
| 1152 | return $member->ID; |
||
| 1153 | } |
||
| 1154 | |||
| 1155 | /** |
||
| 1156 | * Log in as the given member |
||
| 1157 | * |
||
| 1158 | * @param Member|int|string $member The ID, fixture codename, or Member object of the member that you want to log in |
||
| 1159 | */ |
||
| 1160 | public function logInAs($member) |
||
| 1161 | { |
||
| 1162 | if (is_numeric($member)) { |
||
| 1163 | $member = DataObject::get_by_id(Member::class, $member); |
||
| 1164 | } elseif (!is_object($member)) { |
||
| 1165 | $member = $this->objFromFixture(Member::class, $member); |
||
| 1166 | } |
||
| 1167 | Injector::inst()->get(IdentityStore::class)->logIn($member); |
||
| 1168 | } |
||
| 1169 | |||
| 1170 | /** |
||
| 1171 | * Log out the current user |
||
| 1172 | */ |
||
| 1173 | public function logOut() |
||
| 1174 | { |
||
| 1175 | /** @var IdentityStore $store */ |
||
| 1176 | $store = Injector::inst()->get(IdentityStore::class); |
||
| 1177 | $store->logOut(); |
||
| 1178 | } |
||
| 1179 | |||
| 1180 | /** |
||
| 1181 | * Cache for logInWithPermission() |
||
| 1182 | */ |
||
| 1183 | protected $cache_generatedMembers = []; |
||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * Test against a theme. |
||
| 1187 | * |
||
| 1188 | * @param string $themeBaseDir themes directory |
||
| 1189 | * @param string $theme Theme name |
||
| 1190 | * @param callable $callback |
||
| 1191 | * @throws Exception |
||
| 1192 | */ |
||
| 1193 | protected function useTestTheme($themeBaseDir, $theme, $callback) |
||
| 1194 | { |
||
| 1195 | Config::nest(); |
||
| 1196 | if (strpos($themeBaseDir, BASE_PATH) === 0) { |
||
| 1197 | $themeBaseDir = substr($themeBaseDir, strlen(BASE_PATH)); |
||
| 1198 | } |
||
| 1199 | SSViewer::config()->update('theme_enabled', true); |
||
| 1200 | SSViewer::set_themes([$themeBaseDir . '/themes/' . $theme, '$default']); |
||
| 1201 | |||
| 1202 | try { |
||
| 1203 | $callback(); |
||
| 1204 | } finally { |
||
| 1205 | Config::unnest(); |
||
| 1206 | } |
||
| 1207 | } |
||
| 1208 | |||
| 1209 | /** |
||
| 1210 | * Get fixture paths for this test |
||
| 1211 | * |
||
| 1212 | * @return array List of paths |
||
| 1213 | */ |
||
| 1214 | protected function getFixturePaths() |
||
| 1215 | { |
||
| 1216 | $fixtureFile = static::get_fixture_file(); |
||
| 1217 | if (empty($fixtureFile)) { |
||
| 1218 | return []; |
||
| 1219 | } |
||
| 1220 | |||
| 1221 | $fixtureFiles = is_array($fixtureFile) ? $fixtureFile : [$fixtureFile]; |
||
| 1222 | |||
| 1223 | return array_map(function ($fixtureFilePath) { |
||
| 1224 | return $this->resolveFixturePath($fixtureFilePath); |
||
| 1225 | }, $fixtureFiles); |
||
| 1226 | } |
||
| 1227 | |||
| 1228 | /** |
||
| 1229 | * Return all extra objects to scaffold for this test |
||
| 1230 | * @return array |
||
| 1231 | */ |
||
| 1232 | public static function getExtraDataObjects() |
||
| 1235 | } |
||
| 1236 | |||
| 1237 | /** |
||
| 1238 | * Get additional controller classes to register routes for |
||
| 1239 | * |
||
| 1240 | * @return array |
||
| 1241 | */ |
||
| 1242 | public static function getExtraControllers() |
||
| 1243 | { |
||
| 1244 | return static::$extra_controllers; |
||
| 1245 | } |
||
| 1246 | |||
| 1247 | /** |
||
| 1248 | * Map a fixture path to a physical file |
||
| 1249 | * |
||
| 1250 | * @param string $fixtureFilePath |
||
| 1251 | * @return string |
||
| 1252 | */ |
||
| 1253 | protected function resolveFixturePath($fixtureFilePath) |
||
| 1254 | { |
||
| 1255 | // support loading via composer name path. |
||
| 1256 | if (strpos($fixtureFilePath, ':') !== false) { |
||
| 1257 | return ModuleResourceLoader::singleton()->resolvePath($fixtureFilePath); |
||
| 1258 | } |
||
| 1259 | |||
| 1260 | // Support fixture paths relative to the test class, rather than relative to webroot |
||
| 1261 | // String checking is faster than file_exists() calls. |
||
| 1262 | $resolvedPath = realpath($this->getCurrentAbsolutePath() . '/' . $fixtureFilePath); |
||
| 1263 | if ($resolvedPath) { |
||
| 1264 | return $resolvedPath; |
||
| 1265 | } |
||
| 1266 | |||
| 1267 | // Check if file exists relative to base dir |
||
| 1268 | $resolvedPath = realpath(Director::baseFolder() . '/' . $fixtureFilePath); |
||
| 1269 | if ($resolvedPath) { |
||
| 1270 | return $resolvedPath; |
||
| 1271 | } |
||
| 1272 | |||
| 1273 | return $fixtureFilePath; |
||
| 1274 | } |
||
| 1275 | |||
| 1276 | protected function setUpRoutes() |
||
| 1277 | { |
||
| 1278 | if (!class_exists(Director::class)) { |
||
| 1279 | return; |
||
| 1280 | } |
||
| 1281 | |||
| 1282 | // Get overridden routes |
||
| 1283 | $rules = $this->getExtraRoutes(); |
||
| 1284 | |||
| 1285 | // Add all other routes |
||
| 1286 | foreach (Director::config()->uninherited('rules') as $route => $rule) { |
||
| 1287 | if (!isset($rules[$route])) { |
||
| 1288 | $rules[$route] = $rule; |
||
| 1289 | } |
||
| 1290 | } |
||
| 1291 | |||
| 1292 | // Add default catch-all rule |
||
| 1293 | $rules['$Controller//$Action/$ID/$OtherID'] = '*'; |
||
| 1294 | |||
| 1295 | // Add controller-name auto-routing |
||
| 1296 | Director::config()->set('rules', $rules); |
||
| 1297 | } |
||
| 1298 | |||
| 1299 | /** |
||
| 1300 | * Get extra routes to merge into Director.rules |
||
| 1301 | * |
||
| 1302 | * @return array |
||
| 1303 | */ |
||
| 1304 | protected function getExtraRoutes() |
||
| 1314 | } |
||
| 1315 | |||
| 1316 | /** |
||
| 1317 | * Reimplementation of phpunit5 PHPUnit_Util_InvalidArgumentHelper::factory() |
||
| 1318 | * |
||
| 1319 | * @param $argument |
||
| 1320 | * @param $type |
||
| 1321 | * @param $value |
||
| 1322 | */ |
||
| 1323 | public static function createInvalidArgumentException($argument, $type, $value = null) |
||
| 1324 | { |
||
| 1325 | $stack = debug_backtrace(false); |
||
| 1326 | |||
| 1327 | return new PHPUnitFrameworkException( |
||
| 1328 | sprintf( |
||
| 1329 | 'Argument #%d%sof %s::%s() must be a %s', |
||
| 1330 | $argument, |
||
| 1331 | $value !== null ? ' (' . gettype($value) . '#' . $value . ')' : ' (No Value) ', |
||
| 1332 | $stack[1]['class'], |
||
| 1333 | $stack[1]['function'], |
||
| 1334 | $type |
||
| 1335 | ) |
||
| 1336 | ); |
||
| 1337 | } |
||
| 1338 | |||
| 1339 | /** |
||
| 1340 | * Returns the annotations for this test. |
||
| 1341 | * |
||
| 1342 | * @return array |
||
| 1343 | */ |
||
| 1344 | public function getAnnotations(): array |
||
| 1345 | { |
||
| 1346 | return TestUtil::parseTestMethodAnnotations( |
||
| 1347 | get_class($this), |
||
| 1348 | $this->getName(false) |
||
| 1349 | ); |
||
| 1350 | } |
||
| 1351 | } |
||
| 1352 | } |
||
| 1353 | |||
| 1354 | /* ------------------------------------------------- |
||
| 1355 | * |
||
| 1356 | * This version of SapphireTest is for phpunit 5 |
||
| 1357 | * The phpunit 9 verison is at the top of this file |
||
| 1358 | * |
||
| 1359 | * PHPUnit_Extensions_GroupTestSuite is a class that only exists in phpunit 5 |
||
| 1360 | * |
||
| 1361 | * ------------------------------------------------- |
||
| 1362 | */ |
||
| 1363 | if (!class_exists(PHPUnit_Extensions_GroupTestSuite::class)) { |
||
| 1364 | return; |
||
| 1365 | } |
||
| 1366 | |||
| 1367 | /** |
||
| 1368 | * Test case class for the Sapphire framework. |
||
| 2642 |
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