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