2007-08-15 08:38:41 +02:00
|
|
|
<?php
|
2008-01-08 07:37:50 +01:00
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage testing
|
|
|
|
*/
|
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
/*
|
|
|
|
$dir = dirname(dirname((__FILE__)));
|
|
|
|
$_SERVER['SCRIPT_FILENAME'] = "$dir/main.php";
|
|
|
|
chdir($dir);
|
|
|
|
|
|
|
|
require_once 'core/Core.php';
|
|
|
|
require_once("core/ManifestBuilder.php");
|
|
|
|
require_once("core/ClassInfo.php");
|
|
|
|
require_once('core/Object.php');
|
|
|
|
require_once('core/control/Director.php');
|
|
|
|
require_once('filesystem/Filesystem.php');
|
|
|
|
require_once("core/Session.php");
|
|
|
|
|
|
|
|
$envFiles = array('../_ss_environment.php', '../../_ss_environment.php', '../../../_ss_environment.php');
|
|
|
|
foreach($envFiles as $envFile) {
|
|
|
|
if(@file_exists($envFile)) {
|
|
|
|
include($envFile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once(MANIFEST_FILE);
|
|
|
|
print_r($_CLASS_MANIFEST);
|
|
|
|
|
|
|
|
if(ManifestBuilder::staleManifest()) ManifestBuilder::compileManifest();
|
|
|
|
|
|
|
|
require_once(MANIFEST_FILE);
|
|
|
|
*/
|
2007-08-15 12:01:35 +02:00
|
|
|
|
2008-01-10 01:33:18 +01:00
|
|
|
/**
|
|
|
|
*/
|
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-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-03-17 03:04:58 +01:00
|
|
|
static $fixture_file = null;
|
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
protected $originalMailer;
|
|
|
|
protected $mailer;
|
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
function setUp() {
|
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-03-17 03:04:58 +01:00
|
|
|
// Create a temporary database
|
|
|
|
$dbConn = DB::getConn();
|
2007-08-15 08:38:41 +02:00
|
|
|
$dbname = 'tmpdb' . rand(1000000,9999999);
|
2008-03-17 03:04:58 +01:00
|
|
|
while(!$dbname || $dbConn->databaseExists($dbname)) {
|
|
|
|
$dbname = 'tmpdb' . rand(1000000,9999999);
|
|
|
|
}
|
|
|
|
$dbConn->selectDatabase($dbname);
|
2007-08-15 08:38:41 +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
|
|
|
$dbConn->createDatabase();
|
|
|
|
singleton('DataObject')->flushCache();
|
2007-08-15 08:38:41 +02:00
|
|
|
|
2008-03-17 03:04:58 +01:00
|
|
|
$dbadmin = new DatabaseAdmin();
|
Merged revisions 53150,53681,53700,53820,54200,54459 via svnmerge from
svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/roa
........
r53150 | ischommer | 2008-04-22 11:12:43 +1200 (Tue, 22 Apr 2008) | 1 line
FEATURE Added a "test mode" for /db/build which allows mock-DataObject-subclasses which are just built in a test run
........
r53681 | mrickerby | 2008-04-29 15:26:52 +1200 (Tue, 29 Apr 2008) | 1 line
adding default wrapping header and footer methods, and configurable reporting to the TestRunner
........
r53700 | mrickerby | 2008-04-29 16:41:57 +1200 (Tue, 29 Apr 2008) | 1 line
FEATURE: adding support for /dev/tests --> DevelopmentAdmin-->tests() --> TestRunner, /dev/tasks --> DevelopmentAdmin-->tasks() --> TaskRunner
........
r53820 | mrickerby | 2008-04-30 19:27:52 +1200 (Wed, 30 Apr 2008) | 1 line
BUGFIX fixing up BuildTask interface and task runner action
........
r54200 | sminnee | 2008-05-09 00:28:44 +1200 (Fri, 09 May 2008) | 1 line
Added TestSession object to help with the testing of forms
........
r54459 | sminnee | 2008-05-13 17:28:25 +1200 (Tue, 13 May 2008) | 1 line
Added a basic menu of options to /dev
........
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@54456 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-05-13 07:57:09 +02:00
|
|
|
$dbadmin->doBuild(true, false, true);
|
2007-08-15 08:38:41 +02:00
|
|
|
|
2008-03-17 03:04:58 +01:00
|
|
|
// Load the fixture into the database
|
|
|
|
$className = get_class($this);
|
|
|
|
$this->loadFixture($fixtureFile);
|
|
|
|
}
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
// Set up email
|
|
|
|
$this->originalMailer = Email::mailer();
|
|
|
|
$this->mailer = new TestMailer();
|
|
|
|
Email::set_mailer($this->mailer);
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
protected function idFromFixture($className, $identifier) {
|
2007-08-16 08:36:02 +02:00
|
|
|
return $this->fixtureDictionary[$className][$identifier];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the IDs in the fixture of a particular class name.
|
|
|
|
* @return A map of fixture-identifier => object-id
|
|
|
|
*/
|
|
|
|
protected function allFixtureIDs($className) {
|
|
|
|
return $this->fixtureDictionary[$className];
|
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) {
|
|
|
|
return DataObject::get_by_id($className, $this->idFromFixture($className, $identifier));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a YAML fixture file into the database.
|
|
|
|
* Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture
|
|
|
|
* @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);
|
|
|
|
|
|
|
|
$this->fixtureDictionary = array();
|
|
|
|
|
|
|
|
foreach($fixtureContent as $dataClass => $items) {
|
|
|
|
foreach($items as $identifier => $fields) {
|
|
|
|
$obj = new $dataClass();
|
|
|
|
foreach($fields as $fieldName => $fieldVal) {
|
2008-01-08 20:59:00 +01:00
|
|
|
if($obj->many_many($fieldName) || $obj->has_many($fieldName)) {
|
2008-01-08 22:32:13 +01:00
|
|
|
$parsedItems = array();
|
2007-08-17 07:43:14 +02:00
|
|
|
$items = split(' *, *',trim($fieldVal));
|
|
|
|
foreach($items as $item) {
|
|
|
|
$parsedItems[] = $this->parseFixtureVal($item);
|
|
|
|
}
|
|
|
|
$obj->write();
|
2008-03-19 21:38:52 +01:00
|
|
|
if($obj->many_many($fieldName)) {
|
|
|
|
$obj->getManyManyComponents($fieldName)->setByIDList($parsedItems);
|
|
|
|
} else {
|
|
|
|
$obj->getComponents($fieldName)->setByIDList($parsedItems);
|
|
|
|
}
|
2007-08-15 08:38:41 +02:00
|
|
|
|
|
|
|
} else {
|
2007-08-17 07:43:14 +02:00
|
|
|
$obj->$fieldName = $this->parseFixtureVal($fieldVal);
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$obj->write();
|
|
|
|
|
|
|
|
// Populate the dictionary with the ID
|
2007-08-16 08:36:02 +02:00
|
|
|
$this->fixtureDictionary[$dataClass][$identifier] = $obj->ID;
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-17 07:43:14 +02:00
|
|
|
/**
|
|
|
|
* Parse a value from a fixture file. If it starts with => it will get an ID from the fixture dictionary
|
|
|
|
*/
|
|
|
|
protected function parseFixtureVal($fieldVal) {
|
|
|
|
// Parse a dictionary reference - used to set foreign keys
|
|
|
|
if(substr($fieldVal,0,2) == '=>') {
|
|
|
|
list($a, $b) = explode('.', substr($fieldVal,2), 2);
|
|
|
|
return $this->fixtureDictionary[$a][$b];
|
|
|
|
|
|
|
|
// Regular field value setting
|
|
|
|
} else {
|
|
|
|
return $fieldVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
function tearDown() {
|
|
|
|
// Delete our temporary database
|
|
|
|
$dbConn = DB::getConn();
|
2008-03-17 03:04:58 +01:00
|
|
|
if($dbConn && substr($dbConn->currentDatabase(),0,5) == 'tmpdb') {
|
2008-03-19 21:38:52 +01:00
|
|
|
$dbName = $dbConn->currentDatabase();
|
|
|
|
if($dbName && DB::query("SHOW DATABASES LIKE '$dbName'")->value()) {
|
|
|
|
// echo "Deleted temp database " . $dbConn->currentDatabase() . "\n";
|
|
|
|
$dbConn->dropDatabase();
|
|
|
|
}
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
// Restore email configuration
|
|
|
|
Email::set_mailer($this->originalMailer);
|
|
|
|
$this->originalMailer = null;
|
|
|
|
$this->mailer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-01-08 04:00:38 +01:00
|
|
|
?>
|