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;
|
|
|
|
|
|
|
|
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);
|
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
|
|
|
// This code is a bit misplaced; we want some way of the whole session being reinitialised...
|
|
|
|
Versioned::reading_stage(null);
|
2007-08-15 08:38:41 +02:00
|
|
|
|
2008-03-17 03:04:58 +01:00
|
|
|
singleton('DataObject')->flushCache();
|
2007-08-15 08:38:41 +02:00
|
|
|
|
2008-03-17 03:04:58 +01:00
|
|
|
$dbadmin = new DatabaseAdmin();
|
2008-08-13 04:47:14 +02:00
|
|
|
$dbadmin->clearAllData();
|
2009-03-04 04:44:11 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
* e.g. dynamically adding an extension. See {@link tear_down_once()}
|
|
|
|
* for tearing down the state again.
|
|
|
|
*/
|
|
|
|
static function set_up_once() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
|
2009-04-22 05:22:09 +02:00
|
|
|
static function tear_down_once() {
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2008-08-13 04:47:14 +02:00
|
|
|
|
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)) {
|
2008-08-13 04:47:14 +02:00
|
|
|
// echo "Deleted temp database " . $dbConn->currentDatabase() . "\n";
|
|
|
|
$dbConn->dropDatabase();
|
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();
|
2008-08-13 04:47:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
$dbadmin = new DatabaseAdmin();
|
|
|
|
$dbadmin->doBuild(true, false, true);
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
?>
|