2007-08-15 08:38:41 +02:00
|
|
|
<?php
|
2008-07-16 05:29:47 +02:00
|
|
|
require_once 'TestRunner.php';
|
2008-03-19 21:38:52 +01:00
|
|
|
if(hasPhpUnit()) {
|
2007-08-15 08:38:41 +02:00
|
|
|
require_once 'PHPUnit/Framework.php';
|
2008-03-19 21:38:52 +01:00
|
|
|
}
|
2007-08-15 08:38:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2008-08-11 01:17:51 +02:00
|
|
|
*
|
2008-01-10 01:33:18 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage testing
|
2007-08-15 08:38:41 +02:00
|
|
|
*/
|
|
|
|
class SapphireTest extends PHPUnit_Framework_TestCase {
|
2008-08-11 00:49:59 +02:00
|
|
|
/**
|
2008-08-11 01:17:51 +02:00
|
|
|
* Path to fixture data for this test run.
|
2009-07-08 02:06:16 +02:00
|
|
|
* If passed as an array, multiple fixture files will be loaded.
|
|
|
|
* Please note that you won't be able to refer with "=>" notation
|
|
|
|
* between the fixtures, they act independent of each other.
|
2008-08-11 01:17:51 +02:00
|
|
|
*
|
2009-07-08 02:06:16 +02:00
|
|
|
* @var string|array
|
2008-08-11 00:49:59 +02:00
|
|
|
*/
|
2008-08-11 01:29:30 +02:00
|
|
|
static $fixture_file = null;
|
2008-03-17 03:04:58 +01:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
protected $originalMailer;
|
2008-09-18 05:59:00 +02:00
|
|
|
protected $originalMemberPasswordValidator;
|
2008-11-10 02:13:42 +01:00
|
|
|
protected $originalRequirements;
|
2008-11-22 04:33:00 +01:00
|
|
|
protected $originalIsRunningTest;
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
protected $mailer;
|
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
protected static $is_running_test = false;
|
|
|
|
|
2010-01-13 00:03:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The keys of the are the classes that the extensions can't be applied the extensions to, and
|
|
|
|
* the values are an array of illegal extensions on that class.
|
|
|
|
*/
|
|
|
|
protected $illegalExtensions = array(
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The keys of the are the classes to apply the extensions to, and the values are an array
|
|
|
|
* of illegal required extensions on that class.
|
|
|
|
*/
|
|
|
|
protected $requiredExtensions = array(
|
|
|
|
);
|
|
|
|
|
2010-01-13 00:16:14 +01:00
|
|
|
/**
|
|
|
|
* By default, the test database won't contain any DataObjects that have the interface TestOnly.
|
|
|
|
* This variable lets you define additional TestOnly DataObjects to set up for this test.
|
|
|
|
* Set it to an array of DataObject subclass names.
|
|
|
|
*/
|
|
|
|
protected $extraDataObjects = array();
|
|
|
|
|
2009-08-27 06:45:58 +02:00
|
|
|
/**
|
|
|
|
* We need to disabling backing up of globals to avoid overriding
|
|
|
|
* the few globals SilverStripe relies on, like $lang for the i18n subsystem.
|
|
|
|
*
|
|
|
|
* @see http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html
|
|
|
|
*/
|
|
|
|
protected $backupGlobals = FALSE;
|
2010-01-13 00:03:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper arrays for illegalExtensions/requiredExtensions code
|
|
|
|
*/
|
|
|
|
private $extensionsToReapply = array(), $extensionsToRemove = array();
|
2009-08-27 06:45:58 +02:00
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
public static function is_running_test() {
|
|
|
|
return self::$is_running_test;
|
|
|
|
}
|
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
/**
|
2009-07-08 02:06:16 +02:00
|
|
|
* @var array $fixtures Array of {@link YamlFixture} instances
|
2008-08-11 01:17:51 +02:00
|
|
|
*/
|
2009-07-08 02:06:16 +02:00
|
|
|
protected $fixtures;
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
function setUp() {
|
2008-11-22 04:33:00 +01:00
|
|
|
// Mark test as being run
|
|
|
|
$this->originalIsRunningTest = self::$is_running_test;
|
|
|
|
self::$is_running_test = true;
|
|
|
|
|
2008-09-18 05:59:00 +02:00
|
|
|
// Remove password validation
|
|
|
|
$this->originalMemberPasswordValidator = Member::password_validator();
|
2008-11-10 02:13:42 +01:00
|
|
|
$this->originalRequirements = Requirements::backend();
|
2008-09-18 05:59:00 +02:00
|
|
|
Member::set_password_validator(null);
|
2008-11-22 04:33:00 +01:00
|
|
|
Cookie::set_report_errors(false);
|
2009-08-11 06:45:54 +02:00
|
|
|
|
2009-10-11 02:07:22 +02:00
|
|
|
RootURLController::reset();
|
2009-08-11 06:45:54 +02:00
|
|
|
Translatable::reset();
|
|
|
|
Versioned::reset();
|
|
|
|
DataObject::reset();
|
2009-08-27 08:55:32 +02:00
|
|
|
SiteTree::reset();
|
2009-08-11 06:45:54 +02:00
|
|
|
Controller::curr()->setSession(new Session(array()));
|
2008-09-18 05:59:00 +02:00
|
|
|
|
2008-03-17 03:04:58 +01:00
|
|
|
$className = get_class($this);
|
|
|
|
$fixtureFile = eval("return {$className}::\$fixture_file;");
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
// Set up fixture
|
2008-03-17 03:09:32 +01:00
|
|
|
if($fixtureFile) {
|
2008-08-13 04:47:14 +02:00
|
|
|
if(substr(DB::getConn()->currentDatabase(),0,5) != 'tmpdb') {
|
2008-08-13 05:41:54 +02:00
|
|
|
//echo "Re-creating temp database... ";
|
2008-08-13 04:47:14 +02:00
|
|
|
self::create_temp_db();
|
2008-08-13 05:41:54 +02:00
|
|
|
//echo "done.\n";
|
2008-03-17 03:04:58 +01:00
|
|
|
}
|
2008-08-13 04:47:14 +02:00
|
|
|
|
2008-03-17 03:04:58 +01:00
|
|
|
singleton('DataObject')->flushCache();
|
2009-03-04 04:44:11 +01:00
|
|
|
|
2010-01-13 00:44:37 +01:00
|
|
|
self::empty_temp_db();
|
|
|
|
|
2009-07-08 02:06:16 +02:00
|
|
|
$fixtureFiles = (is_array($fixtureFile)) ? $fixtureFile : array($fixtureFile);
|
|
|
|
|
2009-07-08 06:09:10 +02:00
|
|
|
$i = 0;
|
2009-07-08 02:06:16 +02:00
|
|
|
foreach($fixtureFiles as $fixtureFilePath) {
|
|
|
|
$fixture = new YamlFixture($fixtureFilePath);
|
|
|
|
$fixture->saveIntoDatabase();
|
|
|
|
$this->fixtures[] = $fixture;
|
2009-07-08 06:09:10 +02:00
|
|
|
|
|
|
|
// backwards compatibility: Load first fixture into $this->fixture
|
|
|
|
if($i == 0) $this->fixture = $fixture;
|
|
|
|
$i++;
|
2009-07-08 02:06:16 +02:00
|
|
|
}
|
|
|
|
|
2010-01-13 00:28:43 +01:00
|
|
|
$this->logInWithPermssion("ADMIN");
|
2008-03-17 03:04:58 +01:00
|
|
|
}
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
// Set up email
|
|
|
|
$this->originalMailer = Email::mailer();
|
|
|
|
$this->mailer = new TestMailer();
|
|
|
|
Email::set_mailer($this->mailer);
|
2009-04-29 01:52:15 +02:00
|
|
|
Email::send_all_emails_to(null);
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-22 05:22:09 +02:00
|
|
|
* Called once per test case ({@link SapphireTest} subclass).
|
|
|
|
* This is different to {@link setUp()}, which gets called once
|
|
|
|
* per method. Useful to initialize expensive operations which
|
|
|
|
* don't change state for any called method inside the test,
|
2010-01-13 00:03:27 +01:00
|
|
|
* e.g. dynamically adding an extension. See {@link tearDownOnce()}
|
2009-04-22 05:22:09 +02:00
|
|
|
* for tearing down the state again.
|
|
|
|
*/
|
2010-01-13 00:03:27 +01:00
|
|
|
function setUpOnce() {
|
|
|
|
// Remove any illegal extensions that are present
|
|
|
|
foreach($this->illegalExtensions as $class => $extensions) {
|
|
|
|
foreach($extensions as $extension) {
|
|
|
|
if (Object::has_extension($class, $extension)) {
|
|
|
|
if(!isset($this->extensionsToReapply[$class])) $this->extensionsToReapply[$class] = array();
|
|
|
|
$this->extensionsToReapply[$class][] = $extension;
|
|
|
|
Object::remove_extension($class, $extension);
|
|
|
|
$isAltered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add any required extensions that aren't present
|
|
|
|
foreach($this->requiredExtensions as $class => $extensions) {
|
|
|
|
$this->extensionsToRemove[$class] = array();
|
|
|
|
foreach($extensions as $extension) {
|
|
|
|
if(!Object::has_extension($class, $extension)) {
|
|
|
|
if(!isset($this->extensionsToRemove[$class])) $this->extensionsToReapply[$class] = array();
|
|
|
|
$this->extensionsToRemove[$class][] = $extension;
|
|
|
|
Object::add_extension($class, $extension);
|
|
|
|
$isAltered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have made changes to the extensions present, then migrate the database schema.
|
2010-01-13 00:16:14 +01:00
|
|
|
if($this->extensionsToReapply || $this->extensionsToRemove || $this->extraDataObjects) {
|
|
|
|
$this->resetDBSchema(true);
|
2010-01-13 00:03:27 +01:00
|
|
|
}
|
2009-10-22 02:30:21 +02:00
|
|
|
// clear singletons, they're caching old extension info
|
|
|
|
// which is used in DatabaseAdmin->doBuild()
|
|
|
|
global $_SINGLETONS;
|
|
|
|
$_SINGLETONS = array();
|
2009-04-22 05:22:09 +02:00
|
|
|
}
|
|
|
|
|
2010-01-13 00:03:27 +01:00
|
|
|
/**
|
|
|
|
* tearDown method that's called once per test class rather once per test method.
|
|
|
|
*/
|
|
|
|
function tearDownOnce() {
|
|
|
|
// If we have made changes to the extensions present, then migrate the database schema.
|
|
|
|
if($this->extensionsToReapply || $this->extensionsToRemove) {
|
|
|
|
// Remove extensions added for testing
|
|
|
|
foreach($this->extensionsToRemove as $class => $extensions) {
|
|
|
|
foreach($extensions as $extension) {
|
|
|
|
Object::remove_extension($class, $extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reapply ones removed
|
|
|
|
foreach($this->extensionsToReapply as $class => $extensions) {
|
|
|
|
foreach($extensions as $extension) {
|
|
|
|
Object::add_extension($class, $extension);
|
|
|
|
}
|
|
|
|
}
|
2010-01-13 00:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($this->extensionsToReapply || $this->extensionsToRemove || $this->extraDataObjects) {
|
|
|
|
$this->resetDBSchema();
|
2010-01-13 00:03:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-22 05:22:09 +02:00
|
|
|
/**
|
|
|
|
* Array
|
2007-08-15 08:38:41 +02:00
|
|
|
*/
|
|
|
|
protected $fixtureDictionary;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID of an object from the fixture.
|
|
|
|
* @param $className The data class, as specified in your fixture file. Parent classes won't work
|
|
|
|
* @param $identifier The identifier string, as provided in your fixture file
|
2009-07-08 02:06:16 +02:00
|
|
|
* @return int
|
2007-08-15 08:38:41 +02:00
|
|
|
*/
|
|
|
|
protected function idFromFixture($className, $identifier) {
|
2009-07-08 02:06:16 +02:00
|
|
|
if(!$this->fixtures) {
|
2009-07-08 03:38:01 +02:00
|
|
|
user_error("You've called idFromFixture() but you haven't specified static \$fixture_file.\n", E_USER_WARNING);
|
2009-07-08 02:06:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($this->fixtures as $fixture) {
|
|
|
|
$match = $fixture->idFromFixture($className, $identifier);
|
|
|
|
if($match) return $match;
|
|
|
|
}
|
|
|
|
|
2009-07-08 04:54:01 +02:00
|
|
|
$fixtureFiles = Object::get_static(get_class($this), 'fixture_file');
|
2009-07-08 04:36:05 +02:00
|
|
|
user_error(sprintf(
|
|
|
|
"Couldn't find object '%s' (class: %s) in files %s",
|
|
|
|
$identifier,
|
|
|
|
$className,
|
2009-07-08 04:54:01 +02:00
|
|
|
(is_array($fixtureFiles)) ? implode(',', $fixtureFiles) : $fixtureFiles
|
2009-07-08 04:36:05 +02:00
|
|
|
), E_USER_ERROR);
|
|
|
|
|
2009-07-08 02:06:16 +02:00
|
|
|
return false;
|
2007-08-16 08:36:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the IDs in the fixture of a particular class name.
|
2009-07-08 02:06:16 +02:00
|
|
|
* Will collate all IDs form all fixtures if multiple fixtures are provided.
|
|
|
|
*
|
|
|
|
* @param string $className
|
2007-08-16 08:36:02 +02:00
|
|
|
* @return A map of fixture-identifier => object-id
|
|
|
|
*/
|
|
|
|
protected function allFixtureIDs($className) {
|
2009-07-08 02:06:16 +02:00
|
|
|
if(!$this->fixtures) {
|
2009-07-08 03:38:01 +02:00
|
|
|
user_error("You've called allFixtureIDs() but you haven't specified static \$fixture_file.\n", E_USER_WARNING);
|
2009-07-08 02:06:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ids = array();
|
|
|
|
foreach($this->fixtures as $fixture) {
|
|
|
|
$ids += $fixture->allFixtureIDs($className);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ids;
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an object from the fixture.
|
|
|
|
* @param $className The data class, as specified in your fixture file. Parent classes won't work
|
|
|
|
* @param $identifier The identifier string, as provided in your fixture file
|
|
|
|
*/
|
|
|
|
protected function objFromFixture($className, $identifier) {
|
2009-07-08 02:06:16 +02:00
|
|
|
if(!$this->fixtures) {
|
2009-07-08 03:38:01 +02:00
|
|
|
user_error("You've called objFromFixture() but you haven't specified static \$fixture_file.\n", E_USER_WARNING);
|
2009-07-08 02:06:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($this->fixtures as $fixture) {
|
|
|
|
$match = $fixture->objFromFixture($className, $identifier);
|
|
|
|
if($match) return $match;
|
|
|
|
}
|
2009-07-08 04:54:01 +02:00
|
|
|
|
|
|
|
$fixtureFiles = Object::get_static(get_class($this), 'fixture_file');
|
2009-07-08 04:36:05 +02:00
|
|
|
user_error(sprintf(
|
|
|
|
"Couldn't find object '%s' (class: %s) in files %s",
|
|
|
|
$identifier,
|
|
|
|
$className,
|
2009-07-08 04:54:01 +02:00
|
|
|
(is_array($fixtureFiles)) ? implode(',', $fixtureFiles) : $fixtureFiles
|
2009-07-08 04:36:05 +02:00
|
|
|
), E_USER_ERROR);
|
|
|
|
|
2009-07-08 02:06:16 +02:00
|
|
|
return false;
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a YAML fixture file into the database.
|
2009-07-08 02:06:16 +02:00
|
|
|
* Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture.
|
|
|
|
* Doesn't clear existing fixtures.
|
|
|
|
*
|
2007-08-15 08:38:41 +02:00
|
|
|
* @param $fixtureFile The location of the .yml fixture file, relative to the site base dir
|
|
|
|
*/
|
|
|
|
function loadFixture($fixtureFile) {
|
|
|
|
$parser = new Spyc();
|
|
|
|
$fixtureContent = $parser->load(Director::baseFolder().'/'.$fixtureFile);
|
|
|
|
|
2009-07-08 02:06:16 +02:00
|
|
|
$fixture = new YamlFixture($fixtureFile);
|
|
|
|
$fixture->saveIntoDatabase();
|
|
|
|
$this->fixtures[] = $fixture;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all fixtures which were previously loaded through
|
|
|
|
* {@link loadFixture()}.
|
|
|
|
*/
|
|
|
|
function clearFixtures() {
|
|
|
|
$this->fixtures = array();
|
2007-08-17 07:43:14 +02:00
|
|
|
}
|
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
function tearDown() {
|
2008-04-26 08:32:31 +02:00
|
|
|
// Restore email configuration
|
|
|
|
Email::set_mailer($this->originalMailer);
|
|
|
|
$this->originalMailer = null;
|
|
|
|
$this->mailer = null;
|
2008-09-18 05:59:00 +02:00
|
|
|
|
|
|
|
// Restore password validation
|
|
|
|
Member::set_password_validator($this->originalMemberPasswordValidator);
|
2008-11-10 02:13:42 +01:00
|
|
|
|
|
|
|
// Restore requirements
|
|
|
|
Requirements::set_backend($this->originalRequirements);
|
2008-11-22 04:33:00 +01:00
|
|
|
|
|
|
|
// Mark test as no longer being run - we use originalIsRunningTest to allow for nested SapphireTest calls
|
|
|
|
self::$is_running_test = $this->originalIsRunningTest;
|
|
|
|
$this->originalIsRunningTest = null;
|
2010-01-13 00:44:37 +01:00
|
|
|
|
|
|
|
// Reset mocked datetime
|
2010-01-13 01:51:53 +01:00
|
|
|
SS_Datetime::clear_mock_now();
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Clear the log of emails sent
|
|
|
|
*/
|
|
|
|
function clearEmails() {
|
|
|
|
return $this->mailer->clearEmails();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @param $to
|
|
|
|
* @param $from
|
|
|
|
* @param $subject
|
|
|
|
* @param $content
|
|
|
|
* @return An array containing the keys: 'type','to','from','subject','content', 'plainContent','attachedFiles','customHeaders','htmlContent',inlineImages'
|
|
|
|
*/
|
|
|
|
function findEmail($to, $from = null, $subject = null, $content = null) {
|
|
|
|
return $this->mailer->findEmail($to, $from, $subject, $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @param $to
|
|
|
|
* @param $from
|
|
|
|
* @param $subject
|
|
|
|
* @param $content
|
|
|
|
* @return An array containing the keys: 'type','to','from','subject','content', 'plainContent','attachedFiles','customHeaders','htmlContent',inlineImages'
|
|
|
|
*/
|
|
|
|
function assertEmailSent($to, $from = null, $subject = null, $content = null) {
|
|
|
|
// To do - this needs to be turned into a "real" PHPUnit ass
|
|
|
|
if(!$this->findEmail($to, $from, $subject, $content)) {
|
|
|
|
|
|
|
|
$infoParts = "";
|
|
|
|
$withParts = array();
|
|
|
|
if($to) $infoParts .= " to '$to'";
|
|
|
|
if($from) $infoParts .= " from '$from'";
|
|
|
|
if($subject) $withParts[] = "subject '$subject'";
|
|
|
|
if($content) $withParts[] = "content '$content'";
|
|
|
|
if($withParts) $infoParts .= " with " . implode(" and ", $withParts);
|
|
|
|
|
|
|
|
throw new PHPUnit_Framework_AssertionFailedError(
|
|
|
|
"Failed asserting that an email was sent$infoParts."
|
|
|
|
);
|
|
|
|
}
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
2009-08-24 08:14:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that the given {@link DataObjectSet} includes DataObjects matching the given key-value
|
|
|
|
* pairs. Each match must correspond to 1 distinct record.
|
|
|
|
*
|
|
|
|
* @param $matches The patterns to match. Each pattern is a map of key-value pairs. You can
|
|
|
|
* either pass a single pattern or an array of patterns.
|
|
|
|
* @param $dataObjectSet The {@link DataObjectSet} to test.
|
|
|
|
*
|
|
|
|
* Examples
|
|
|
|
* --------
|
|
|
|
* Check that $members includes an entry with Email = sam@example.com:
|
|
|
|
* $this->assertDOSContains(array('Email' => '...@example.com'), $members);
|
|
|
|
*
|
|
|
|
* Check that $members includes entries with Email = sam@example.com and with
|
|
|
|
* Email = ingo@example.com:
|
|
|
|
* $this->assertDOSContains(array(
|
|
|
|
* array('Email' => '...@example.com'),
|
|
|
|
* array('Email' => 'i...@example.com'),
|
|
|
|
* ), $members);
|
|
|
|
*/
|
|
|
|
function assertDOSContains($matches, $dataObjectSet) {
|
|
|
|
$extracted = array();
|
|
|
|
foreach($dataObjectSet as $item) $extracted[] = $item->toMap();
|
|
|
|
|
|
|
|
foreach($matches as $match) {
|
|
|
|
$matched = false;
|
|
|
|
foreach($extracted as $i => $item) {
|
|
|
|
if($this->dataObjectArrayMatch($item, $match)) {
|
|
|
|
// Remove it from $extracted so that we don't get duplicate mapping.
|
|
|
|
unset($extracted[$i]);
|
|
|
|
$matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We couldn't find a match - assertion failed
|
|
|
|
if(!$matched) {
|
|
|
|
throw new PHPUnit_Framework_AssertionFailedError(
|
|
|
|
"Failed asserting that the DataObjectSet contains an item matching "
|
|
|
|
. var_export($match, true) . "\n\nIn the following DataObjectSet:\n"
|
|
|
|
. $this->DOSSummaryForMatch($dataObjectSet, $match)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2008-08-13 04:47:14 +02:00
|
|
|
|
2009-08-24 08:14:54 +02:00
|
|
|
/**
|
|
|
|
* Assert that the given {@link DataObjectSet} includes only DataObjects matching the given
|
|
|
|
* key-value pairs. Each match must correspond to 1 distinct record.
|
|
|
|
*
|
|
|
|
* @param $matches The patterns to match. Each pattern is a map of key-value pairs. You can
|
|
|
|
* either pass a single pattern or an array of patterns.
|
|
|
|
* @param $dataObjectSet The {@link DataObjectSet} to test.
|
|
|
|
*
|
|
|
|
* Example
|
|
|
|
* --------
|
|
|
|
* Check that *only* the entries Sam Minnee and Ingo Schommer exist in $members. Order doesn't
|
|
|
|
* matter:
|
|
|
|
* $this->assertDOSEquals(array(
|
|
|
|
* array('FirstName' =>'Sam', 'Surname' => 'Minnee'),
|
|
|
|
* array('FirstName' => 'Ingo', 'Surname' => 'Schommer'),
|
|
|
|
* ), $members);
|
|
|
|
*/
|
|
|
|
function assertDOSEquals($matches, $dataObjectSet) {
|
2009-09-14 07:16:15 +02:00
|
|
|
if(!$dataObjectSet) return false;
|
|
|
|
|
2009-08-24 08:14:54 +02:00
|
|
|
$extracted = array();
|
|
|
|
foreach($dataObjectSet as $item) $extracted[] = $item->toMap();
|
|
|
|
|
|
|
|
foreach($matches as $match) {
|
|
|
|
$matched = false;
|
|
|
|
foreach($extracted as $i => $item) {
|
|
|
|
if($this->dataObjectArrayMatch($item, $match)) {
|
|
|
|
// Remove it from $extracted so that we don't get duplicate mapping.
|
|
|
|
unset($extracted[$i]);
|
|
|
|
$matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We couldn't find a match - assertion failed
|
|
|
|
if(!$matched) {
|
|
|
|
throw new PHPUnit_Framework_AssertionFailedError(
|
|
|
|
"Failed asserting that the DataObjectSet contains an item matching "
|
|
|
|
. var_export($match, true) . "\n\nIn the following DataObjectSet:\n"
|
|
|
|
. $this->DOSSummaryForMatch($dataObjectSet, $match)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have leftovers than the DOS has extra data that shouldn't be there
|
|
|
|
if($extracted) {
|
|
|
|
// If we didn't break by this point then we couldn't find a match
|
|
|
|
throw new PHPUnit_Framework_AssertionFailedError(
|
|
|
|
"Failed asserting that the DataObjectSet contained only the given items, the "
|
|
|
|
. "following items were left over:\n" . var_export($extracted, true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that the every record in the given {@link DataObjectSet} matches the given key-value
|
|
|
|
* pairs.
|
|
|
|
*
|
|
|
|
* @param $match The pattern to match. The pattern is a map of key-value pairs.
|
|
|
|
* @param $dataObjectSet The {@link DataObjectSet} to test.
|
|
|
|
*
|
|
|
|
* Example
|
|
|
|
* --------
|
|
|
|
* Check that every entry in $members has a Status of 'Active':
|
|
|
|
* $this->assertDOSAllMatch(array('Status' => 'Active'), $members);
|
|
|
|
*/
|
|
|
|
function assertDOSAllMatch($match, $dataObjectSet) {
|
|
|
|
$extracted = array();
|
|
|
|
foreach($dataObjectSet as $item) $extracted[] = $item->toMap();
|
|
|
|
|
|
|
|
foreach($extracted as $i => $item) {
|
|
|
|
if(!$this->dataObjectArrayMatch($item, $match)) {
|
|
|
|
throw new PHPUnit_Framework_AssertionFailedError(
|
|
|
|
"Failed asserting that the the following item matched "
|
|
|
|
. var_export($match, true) . ": " . var_export($item, true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for the DOS matchers
|
|
|
|
*/
|
|
|
|
private function dataObjectArrayMatch($item, $match) {
|
|
|
|
foreach($match as $k => $v) {
|
|
|
|
if(!isset($item[$k]) || $item[$k] != $v) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for the DOS matchers
|
|
|
|
*/
|
|
|
|
private function DOSSummaryForMatch($dataObjectSet, $match) {
|
|
|
|
$extracted = array();
|
|
|
|
foreach($dataObjectSet as $item) $extracted[] = array_intersect_key($item->toMap(), $match);
|
|
|
|
return var_export($extracted, true);
|
|
|
|
}
|
|
|
|
|
2008-08-27 10:19:46 +02:00
|
|
|
/**
|
|
|
|
* Returns true if we are currently using a temporary database
|
|
|
|
*/
|
|
|
|
static function using_temp_db() {
|
|
|
|
$dbConn = DB::getConn();
|
|
|
|
return $dbConn && (substr($dbConn->currentDatabase(),0,5) == 'tmpdb');
|
|
|
|
}
|
|
|
|
|
2008-11-24 10:31:14 +01:00
|
|
|
/**
|
|
|
|
* @todo Make this db agnostic
|
|
|
|
*/
|
2008-08-13 04:47:14 +02:00
|
|
|
static function kill_temp_db() {
|
|
|
|
// Delete our temporary database
|
2008-08-27 10:19:46 +02:00
|
|
|
if(self::using_temp_db()) {
|
|
|
|
$dbConn = DB::getConn();
|
2008-08-13 04:47:14 +02:00
|
|
|
$dbName = $dbConn->currentDatabase();
|
2009-05-07 05:49:15 +02:00
|
|
|
if($dbName && DB::getConn()->databaseExists($dbName)) {
|
2009-06-18 07:01:15 +02:00
|
|
|
// Todo: it would be good to remove this inappropriate coupling, somehow.
|
|
|
|
// The versioned class keeps a static cache of information about temporary tables.
|
|
|
|
Versioned::on_db_reset();
|
2010-01-13 01:51:53 +01:00
|
|
|
|
|
|
|
// echo "Deleted temp database " . $dbConn->currentDatabase() . "\n";
|
|
|
|
$dbConn->dropDatabase();
|
2008-08-13 04:47:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-13 00:44:37 +01:00
|
|
|
/**
|
|
|
|
* Remove all content from the temporary database.
|
|
|
|
*/
|
|
|
|
static function empty_temp_db() {
|
|
|
|
if(self::using_temp_db()) {
|
|
|
|
$dbadmin = new DatabaseAdmin();
|
|
|
|
$dbadmin->clearAllData();
|
|
|
|
|
|
|
|
// Todo: it would be good to remove this inappropriate coupling, somehow.
|
|
|
|
// The versioned class keeps a static cache of information about temporary tables.
|
|
|
|
Versioned::on_db_reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-24 10:31:14 +01:00
|
|
|
/**
|
|
|
|
* @todo Make this db agnostic
|
|
|
|
*/
|
2008-08-13 04:47:14 +02:00
|
|
|
static function create_temp_db() {
|
|
|
|
// Create a temporary database
|
|
|
|
$dbConn = DB::getConn();
|
|
|
|
$dbname = 'tmpdb' . rand(1000000,9999999);
|
|
|
|
while(!$dbname || $dbConn->databaseExists($dbname)) {
|
|
|
|
$dbname = 'tmpdb' . rand(1000000,9999999);
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbConn->selectDatabase($dbname);
|
|
|
|
$dbConn->createDatabase();
|
2010-01-13 00:16:14 +01:00
|
|
|
|
|
|
|
singleton('SapphireTest')->resetDBSchema();
|
2008-08-27 10:19:46 +02:00
|
|
|
|
|
|
|
return $dbname;
|
2008-08-13 04:47:14 +02:00
|
|
|
}
|
2009-03-04 08:31:23 +01:00
|
|
|
|
|
|
|
static function delete_all_temp_dbs() {
|
|
|
|
foreach(DB::getConn()->allDatabaseNames() as $dbName) {
|
|
|
|
if(preg_match('/^tmpdb[0-9]+$/', $dbName)) {
|
|
|
|
DB::getConn()->dropDatabaseByName($dbName);
|
|
|
|
echo "<li>Dropped databse \"$dbName\"\n";
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-16 00:40:17 +02:00
|
|
|
|
2010-01-13 00:16:14 +01:00
|
|
|
/**
|
|
|
|
* Reset the testing database's schema.
|
|
|
|
* @param $includeExtraDataObjects If true, the extraDataObjects tables will also be included
|
|
|
|
*/
|
|
|
|
function resetDBSchema($includeExtraDataObjects = false) {
|
|
|
|
if(self::using_temp_db()) {
|
|
|
|
// clear singletons, they're caching old extension info which is used in DatabaseAdmin->doBuild()
|
|
|
|
global $_SINGLETONS;
|
|
|
|
$_SINGLETONS = array();
|
|
|
|
|
|
|
|
$dataClasses = ClassInfo::subclassesFor('DataObject');
|
|
|
|
array_shift($dataClasses);
|
|
|
|
|
|
|
|
$conn = DB::getConn();
|
|
|
|
$conn->beginSchemaUpdate();
|
|
|
|
DB::quiet();
|
|
|
|
|
|
|
|
foreach($dataClasses as $dataClass) {
|
|
|
|
// Check if class exists before trying to instantiate - this sidesteps any manifest weirdness
|
|
|
|
if(class_exists($dataClass)) {
|
|
|
|
$SNG = singleton($dataClass);
|
|
|
|
if(!($SNG instanceof TestOnly)) $SNG->requireTable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have additional dataobjects which need schema, do so here:
|
|
|
|
if($includeExtraDataObjects && $this->extraDataObjects) {
|
|
|
|
foreach($this->extraDataObjects as $dataClass) {
|
|
|
|
$SNG = singleton($dataClass);
|
|
|
|
if(singleton($dataClass) instanceof DataObject) $SNG->requireTable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$conn->endSchemaUpdate();
|
|
|
|
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
singleton('DataObject')->flushCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-16 00:40:17 +02:00
|
|
|
/**
|
|
|
|
* Create a member and group with the given permission code, and log in with it.
|
|
|
|
* Returns the member ID.
|
|
|
|
*/
|
|
|
|
function logInWithPermssion($permCode = "ADMIN") {
|
|
|
|
if(!isset($this->cache_generatedMembers[$permCode])) {
|
|
|
|
$group = new Group();
|
|
|
|
$group->Title = "$permCode group";
|
|
|
|
$group->write();
|
|
|
|
|
|
|
|
$permission = new Permission();
|
|
|
|
$permission->Code = $permCode;
|
|
|
|
$permission->write();
|
|
|
|
$group->Permissions()->add($permission);
|
|
|
|
|
|
|
|
$member = new Member();
|
|
|
|
$member->FirstName = $permCode;
|
|
|
|
$member->Surname = "User";
|
|
|
|
$member->Email = "$permCode@example.org";
|
|
|
|
$member->write();
|
|
|
|
$group->Members()->add($member);
|
|
|
|
|
|
|
|
$this->cache_generatedMembers[$permCode] = $member;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cache_generatedMembers[$permCode]->logIn();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache for logInWithPermission()
|
|
|
|
*/
|
|
|
|
protected $cache_generatedMembers = array();
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
?>
|