Skip to content

Commit

Permalink
ENHANCEMENT Added support for SapphireTest::$fixture_file paths relat…
Browse files Browse the repository at this point in the history
…ive to current class location ('MyTest.yml' or '../otherfolder/MyTest.yml'). Added SapphireTest->getCurrentAbsolutePath() and getCurrentRelativePath() for easy access within unit tests
  • Loading branch information
chillu committed Mar 30, 2011
1 parent 6f1eab1 commit 14e1341
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
28 changes: 28 additions & 0 deletions dev/SapphireTest.php
Expand Up @@ -166,10 +166,19 @@ function setUp() {
}

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;
Expand Down Expand Up @@ -370,6 +379,25 @@ function clearFixtures() {
$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);
Expand Down
23 changes: 17 additions & 6 deletions dev/YamlFixture.php
Expand Up @@ -68,13 +68,11 @@
* @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
*/
Expand All @@ -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
Expand Down Expand Up @@ -151,7 +162,7 @@ public function saveIntoDatabase() {
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) {
Expand Down
23 changes: 22 additions & 1 deletion tests/testing/YamlFixtureTest.php
@@ -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");
Expand Down

0 comments on commit 14e1341

Please sign in to comment.