ENHANCEMENT Added support for SapphireTest::$fixture_file paths relative to current class location ('MyTest.yml' or '../otherfolder/MyTest.yml'). Added SapphireTest->getCurrentAbsolutePath() and getCurrentRelativePath() for easy access within unit tests

This commit is contained in:
Ingo Schommer 2011-03-30 19:47:30 +13:00
parent 6f1eab18c1
commit 14e1341df6
3 changed files with 67 additions and 7 deletions

View File

@ -166,10 +166,19 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
}
if($fixtureFile) {
$pathForClass = $this->getCurrentPath();
$fixtureFiles = (is_array($fixtureFile)) ? $fixtureFile : array($fixtureFile);
$i = 0;
foreach($fixtureFiles as $fixtureFilePath) {
// Support fixture paths relative to the test class, rather than relative to webroot
// String checking is faster than file_exists() calls.
$isRelativeToFile = (strpos('/', $fixtureFilePath) === false || preg_match('/^\.\./', $fixtureFilePath));
if($isRelativeToFile) {
$resolvedPath = realpath($pathForClass . '/' . $fixtureFilePath);
if($resolvedPath) $fixtureFilePath = $resolvedPath;
}
$fixture = new YamlFixture($fixtureFilePath);
$fixture->saveIntoDatabase();
$this->fixtures[] = $fixture;
@ -370,6 +379,25 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
$this->fixtures = array();
}
/**
* Useful for writing unit tests without hardcoding folder structures.
*
* @return String Absolute path to current class.
*/
protected function getCurrentAbsolutePath() {
return dirname(SS_ClassLoader::instance()->getManifest()->getItemPath(get_class($this)));
}
/**
* @return String File path relative to webroot
*/
protected function getCurrentRelativePath() {
$base = Director::baseFolder();
$path = $this->getCurrentAbsolutePath();
if(substr($path,0,strlen($base)) == $base) $path = preg_replace('/^\/*/', '', substr($path,strlen($base)));
return $path;
}
function tearDown() {
// Preserve memory settings
ini_set('memory_limit', ($this->originalMemoryLimit) ? $this->originalMemoryLimit : -1);

View File

@ -68,13 +68,11 @@ require_once 'thirdparty/spyc/spyc.php';
* @see http://code.google.com/p/spyc/
*
* @todo Write unit test for YamlFixture
*
* @param $fixtureFile The location of the .yml fixture file, relative to the site base dir
*/
class YamlFixture extends Object {
/**
* The location of the .yml fixture file, relative to the site base dir
* Absolute path to the .yml fixture file
*
* @var string
*/
@ -87,15 +85,28 @@ class YamlFixture extends Object {
*/
protected $fixtureDictionary;
/**
* @param String Absolute file path, or relative path to {@link Director::baseFolder()}
*/
function __construct($fixtureFile) {
if(!file_exists(Director::baseFolder().'/'. $fixtureFile)) {
user_error('YamlFixture::__construct(): Fixture path "' . $fixtureFile . '" not found', E_USER_ERROR);
if(!Director::is_absolute($fixtureFile)) $fixtureFile = Director::baseFolder().'/'. $fixtureFile;
if(!file_exists($fixtureFile)) {
throw new InvalidArgumentException('YamlFixture::__construct(): Fixture path "' . $fixtureFile . '" not found');
}
$this->fixtureFile = $fixtureFile;
parent::__construct();
}
/**
* @return String Absolute file path
*/
function getFixtureFile() {
return $this->fixtureFile;
}
/**
* 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
@ -151,7 +162,7 @@ class YamlFixture extends Object {
DataObject::set_validation_enabled(false);
$parser = new Spyc();
$fixtureContent = $parser->loadFile(Director::baseFolder().'/'.$this->fixtureFile);
$fixtureContent = $parser->loadFile($this->fixtureFile);
$this->fixtureDictionary = array();
foreach($fixtureContent as $dataClass => $items) {

View File

@ -1,13 +1,34 @@
<?php
class YamlFixtureTest extends SapphireTest {
static $fixture_file = 'sapphire/tests/testing/YamlFixtureTest.yml';
static $fixture_file = 'YamlFixtureTest.yml';
protected $extraDataObjects = array(
'YamlFixtureTest_DataObject',
'YamlFixtureTest_DataObjectRelation',
);
function testAbsoluteFixturePath() {
$absPath = Director::baseFolder() . '/sapphire/tests/testing/YamlFixtureTest.yml';
$obj = new YamlFixture($absPath);
$this->assertEquals($absPath, $obj->getFixtureFile());
}
function testRelativeFixturePath() {
$relPath = 'sapphire/tests/testing/YamlFixtureTest.yml';
$obj = new YamlFixture($relPath);
$this->assertEquals(Director::baseFolder() . '/' . $relPath, $obj->getFixtureFile());
}
/**
* @expectedException InvalidArgumentException
*/
function testFailsWithInvalidFixturePath() {
$invalidPath = 'sapphire/tests/testing/invalid.yml';
$obj = new YamlFixture($invalidPath);
}
function testSQLInsert() {
$object1 = DataObject::get_by_id("YamlFixtureTest_DataObject", $this->idFromFixture("YamlFixtureTest_DataObject", "testobject1"));
$this->assertTrue($object1->ManyMany()->Count() == 2, "Should be 2 items in this manymany relationship");