SapphireTest
class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly (View source)
Test case class for the Sapphire framework.
Sapphire unit testing is based on PHPUnit, but provides a number of hooks into our data model that make it easier to work with.
This class should not be used anywhere outside of unit tests, as phpunit may not be installed in production sites.
Properties
protected static | string|array | $fixture_file | Path to fixture data for this test run. |
|
protected deprecated | FixtureFactory | $fixtureFactory | ||
protected | bool | $usesDatabase | ||
protected | bool | $usesTransactions | This test will cleanup its state via transactions. |
|
protected static | bool | $is_running_test | ||
protected | array | $requireDefaultRecordsFrom | By default, setUp() does not require default records. Pass class names in here, and the require/augment default records function will be called on them. |
|
protected static | array | $illegal_extensions | A list of extensions that can't be applied during the execution of this run. If they are applied, they will be temporarily removed and a database migration called. |
|
protected static | array | $required_extensions | A list of extensions that must be applied during the execution of this run. If they are not applied, they will be temporarily added and a database migration called. |
|
protected static | array | $extra_dataobjects | By default, the test database won't contain any DataObjects that have the interface TestOnly. |
|
protected static | array | $extra_controllers | List of class names of {Controller} objects to register routes for Controllers must implement Link() method |
|
protected | $backupGlobals | We need to disabling backing up of globals to avoid overriding the few globals SilverStripe relies on, like $lang for the i18n subsystem. |
||
protected static | SapphireTestState | $state | State management container for SapphireTest |
|
protected static | TempDatabase | $tempDB | Temp database helper |
|
protected | $cache_generatedMembers | Cache for logInWithPermission() |
Methods
Check if test bootstrapping has been performed. Must not be relied on outside of unit tests.
Helper method to determine if the current test should enable a test database
Helper method to check, if the current test uses the database.
Helper method to check, if the current test uses the database.
tearDown method that's called once per test class rather once per test method.
Get the ID of an object from the fixture.
Return all of the IDs in the fixture of a particular class name.
Get an object from the fixture.
Useful for writing unit tests without hardcoding folder structures.
No description
No description
Search for an email that was sent.
Assert that the matching email was sent since the last call to clearEmails() All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
Assert that the given SS_List includes DataObjects matching the given key-value pairs. Each match must correspond to 1 distinct record.
Asserts that no items in a given list appear in the given dataobject list
Assert that the given SS_List includes only DataObjects matching the given key-value pairs. Each match must correspond to 1 distinct record.
Assert that the every record in the given SS_List matches the given key-value pairs.
Removes sequences of repeated whitespace characters from SQL queries making them suitable for string comparison
Asserts that two SQL queries are equivalent
Asserts that a SQL query contains a SQL fragment
Asserts that a SQL query contains a SQL fragment
Reset the testing database's schema, but only if it is active
A wrapper for automatically performing callbacks as a user with a specific permission
Create Member and Group objects on demand with specific permission code
Create a member and group with the given permission code, and log in with it.
Test against a theme.
Details
static TempDatabase
tempDB()
No description
static array
getIllegalExtensions()
Gets illegal extensions for this class
static array
getRequiredExtensions()
Gets required extensions for this class
static protected bool
is_running_test()
Check if test bootstrapping has been performed. Must not be relied on outside of unit tests.
static protected
set_is_running_test(bool $bool)
Set test running state
static string
get_fixture_file()
No description
bool
getUsesDatabase()
No description
bool
getUsesTransactions()
No description
array
getRequireDefaultRecordsFrom()
No description
protected
setUp()
Setup the test.
Always sets up in order:
- Reset php state
- Nest
- Custom state helpers
User code should call parent::setUp() before custom setup code
protected bool
shouldSetupDatabaseForCurrentTest($fixtureFiles)
Helper method to determine if the current test should enable a test database
protected bool
currentTestEnablesDatabase()
Helper method to check, if the current test uses the database.
This can be switched on with the annotation "@useDatabase"
protected bool
currentTestDisablesDatabase()
Helper method to check, if the current test uses the database.
This can be switched on with the annotation "@useDatabase false"
static
setUpBeforeClass()
Called once per test case (SapphireTest subclass).
This is different to setUp(), which gets called once per method. Useful to initialize expensive operations which don't change state for any called method inside the test, e.g. dynamically adding an extension. See teardownAfterClass() for tearing down the state again.
Always sets up in order:
- Reset php state
- Nest
- Custom state helpers
User code should call parent::setUpBeforeClass() before custom setup code
static
tearDownAfterClass()
tearDown method that's called once per test class rather once per test method.
Always sets up in order:
- Custom state helpers
- Unnest
- Reset php state
User code should call parent::tearDownAfterClass() after custom tear down code
FixtureFactory|false
getFixtureFactory()
deprecated
deprecated
No description
$this
setFixtureFactory(FixtureFactory $factory)
deprecated
deprecated
Sets a new fixture factory
protected int
idFromFixture(string $className, string $identifier)
Get the ID of an object from the fixture.
protected array
allFixtureIDs(string $className)
Return all of the IDs in the fixture of a particular class name.
Will collate all IDs form all fixtures if multiple fixtures are provided.
protected DataObject
objFromFixture(string $className, string $identifier)
Get an object from the fixture.
loadFixture(string $fixtureFile)
deprecated
deprecated
Load a YAML fixture file into the database.
Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture. Doesn't clear existing fixtures.
clearFixtures()
Clear all fixtures which were previously loaded through loadFixture()
protected string
getCurrentAbsolutePath()
Useful for writing unit tests without hardcoding folder structures.
protected string
getCurrentRelativePath()
No description
protected
tearDown()
Setup the test.
Always sets up in order:
- Custom state helpers
- Unnest
- Reset php state
User code should call parent::tearDown() after custom tear down code
static
assertContains($needle, $haystack, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
No description
static
assertNotContains($needle, $haystack, $message = '', $ignoreCase = false, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
No description
bool
clearEmails()
Clear the log of emails sent
static array|null
findEmail(string $to, string $from = null, string $subject = null, string $content = null)
Search for an email that was sent.
All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
static
assertEmailSent(string $to, string $from = null, string $subject = null, string $content = null)
Assert that the matching email was sent since the last call to clearEmails() All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
static
assertListContains(SS_List|array $matches, SS_List $list, string $message = '')
Assert that the given SS_List includes DataObjects matching the given key-value pairs. Each match must correspond to 1 distinct record.
assertDOSContains($matches, $dataObjectSet)
deprecated
deprecated
No description
static
assertListNotContains(SS_List|array $matches, SS_List $list, string $message = '')
Asserts that no items in a given list appear in the given dataobject list
static
assertNotDOSContains($matches, $dataObjectSet)
deprecated
deprecated
No description
static
assertListEquals(mixed $matches, SS_List $list, string $message = '')
Assert that the given SS_List includes only DataObjects matching the given key-value pairs. Each match must correspond to 1 distinct record.
Example
Check that only the entries Sam Minnee and Ingo Schommer exist in $members. Order doesn't matter: $this->assertListEquals([ ['FirstName' =>'Sam', 'Surname' => 'Minnee'], ['FirstName' => 'Ingo', 'Surname' => 'Schommer'], ], $members);
assertDOSEquals($matches, SS_List $dataObjectSet)
deprecated
deprecated
No description
static
assertListAllMatch(mixed $match, SS_List $list, string $message = '')
Assert that the every record in the given SS_List matches the given key-value pairs.
Example
Check that every entry in $members has a Status of 'Active': $this->assertListAllMatch(['Status' => 'Active'], $members);
assertDOSAllMatch($match, SS_List $dataObjectSet)
deprecated
deprecated
No description
static protected string
normaliseSQL(string $sql)
Removes sequences of repeated whitespace characters from SQL queries making them suitable for string comparison
static
assertSQLEquals(string $expectedSQL, string $actualSQL, string $message = '', float|int $delta = 0, int $maxDepth = 10, bool $canonicalize = false, bool $ignoreCase = false)
Asserts that two SQL queries are equivalent
static
assertSQLContains(string $needleSQL, string $haystackSQL, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true)
Asserts that a SQL query contains a SQL fragment
static
assertSQLNotContains(string $needleSQL, string $haystackSQL, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true)
Asserts that a SQL query contains a SQL fragment
static
start()
Start test environment
static
resetDBSchema(bool $includeExtraDataObjects = false, bool $forceCreate = false)
Reset the testing database's schema, but only if it is active
mixed
actWithPermission(string|array $permCode, callable $callback)
A wrapper for automatically performing callbacks as a user with a specific permission
protected Member
createMemberWithPermission(string|array $permCode)
Create Member and Group objects on demand with specific permission code
int
logInWithPermission(string|array $permCode = 'ADMIN')
Create a member and group with the given permission code, and log in with it.
Returns the member ID.
logInAs(Member|int|string $member)
Log in as the given member
logOut()
Log out the current user
protected
useTestTheme(string $themeBaseDir, string $theme, callable $callback)
Test against a theme.
protected array
getFixturePaths()
Get fixture paths for this test
static array
getExtraDataObjects()
Return all extra objects to scaffold for this test
static array
getExtraControllers()
Get additional controller classes to register routes for
protected string
resolveFixturePath(string $fixtureFilePath)
Map a fixture path to a physical file
protected
setUpRoutes()
No description
protected array
getExtraRoutes()
Get extra routes to merge into Director.rules
protected DBDatetime
mockSleep(int $seconds)
Test safe version of sleep()