2007-08-15 08:38:41 +02:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
$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
|
|
|
|
|
|
|
|
|
|
|
// Check that PHPUnit is installed
|
|
|
|
$hasPhpUnit = false;
|
|
|
|
$paths = explode(PATH_SEPARATOR, ini_get('include_path'));
|
|
|
|
foreach($paths as $path) {
|
|
|
|
if(file_exists("$path/PHPUnit/Framework.php")) $hasPhpUnit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($hasPhpUnit) {
|
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
require_once 'PHPUnit/Framework.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
class SapphireTest extends PHPUnit_Framework_TestCase {
|
|
|
|
function setUp() {
|
|
|
|
|
|
|
|
// Create a temporary database
|
|
|
|
$dbConn = DB::getConn();
|
2007-08-15 12:12:59 +02:00
|
|
|
$dbname = 'tmpdb' . rand(1000000,9999999);
|
2007-08-15 08:38:41 +02:00
|
|
|
while(!$dbname || $dbConn->databaseExists($dbname)) {
|
|
|
|
$dbname = 'tmpdb' . rand(1000000,9999999);
|
|
|
|
}
|
|
|
|
$dbConn->selectDatabase($dbname);
|
|
|
|
|
|
|
|
// This code is a bit misplaced; we want some way of the whole session being reinitialised...
|
|
|
|
Versioned::reading_stage(null);
|
|
|
|
|
|
|
|
$dbConn->createDatabase();
|
|
|
|
singleton('DataObject')->flushCache();
|
|
|
|
|
|
|
|
$dbadmin = new DatabaseAdmin();
|
|
|
|
$dbadmin->doBuild(true, false);
|
|
|
|
|
|
|
|
// Load the fixture into the database
|
|
|
|
$className = get_class($this);
|
|
|
|
$fixtureFile = eval("return {$className}::\$fixture_file;");
|
|
|
|
$this->loadFixture($fixtureFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
// Parse a dictionary reference - used to set foreign keys
|
|
|
|
if(substr($fieldVal,0,2) == '=>') {
|
2007-08-16 08:36:02 +02:00
|
|
|
list($a, $b) = explode('.', substr($fieldVal,2), 2);
|
|
|
|
$obj->$fieldName = $this->fixtureDictionary[$a][$b];
|
2007-08-15 08:38:41 +02:00
|
|
|
|
|
|
|
// Regular field value setting
|
|
|
|
} else {
|
|
|
|
$obj->$fieldName = $fieldVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tearDown() {
|
|
|
|
// Delete our temporary database
|
|
|
|
$dbConn = DB::getConn();
|
|
|
|
if(substr($dbConn->currentDatabase(),0,5) == 'tmpdb') {
|
|
|
|
// echo "Deleted temp database " . $dbConn->currentDatabase() . "\n";
|
|
|
|
$dbConn->dropDatabase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-15 12:01:35 +02:00
|
|
|
} else {
|
|
|
|
// Stub
|
|
|
|
class SapphireTest extends Object {}
|
|
|
|
}
|
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
?>
|