Merge pull request #6179 from open-sausages/pulls/4.0/relative-fixtures

API Support fixture paths relative to current directory
This commit is contained in:
Daniel Hensby 2016-10-13 11:49:32 +01:00 committed by GitHub
commit b923475dcb
2 changed files with 83 additions and 24 deletions

View File

@ -263,13 +263,13 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
'$Controller//$Action/$ID/$OtherID' => '*'
));
$fixtureFile = static::get_fixture_file();
$fixtureFiles = $this->getFixturePaths();
// Todo: this could be a special test model
$this->model = DataModel::inst();
// Set up fixture
if($fixtureFile || $this->usesDatabase) {
if($fixtureFiles || $this->usesDatabase) {
if (!self::using_temp_db()) {
self::create_temp_db();
}
@ -284,28 +284,9 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
if (method_exists($instance, 'augmentDefaultRecords')) $instance->augmentDefaultRecords();
}
if($fixtureFile) {
$pathForClass = $this->getCurrentAbsolutePath();
$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 = YamlFixture::create($fixtureFilePath);
$fixture->writeInto($this->getFixtureFactory());
$i++;
}
foreach($fixtureFiles as $fixtureFilePath) {
$fixture = YamlFixture::create($fixtureFilePath);
$fixture->writeInto($this->getFixtureFactory());
}
$this->logInWithPermission("ADMIN");
@ -1120,6 +1101,55 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
}
}
/**
* Get fixture paths for this test
*
* @return array List of paths
*/
protected function getFixturePaths()
{
$fixtureFile = static::get_fixture_file();
if (empty($fixtureFile)) {
return [];
}
$fixtureFiles = (is_array($fixtureFile)) ? $fixtureFile : [$fixtureFile];
return array_map(function($fixtureFilePath) {
return $this->resolveFixturePath($fixtureFilePath);
}, $fixtureFiles);
}
/**
* Map a fixture path to a physical file
*
* @param string $fixtureFilePath
* @return string
*/
protected function resolveFixturePath($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('/^(\.){1,2}/', $fixtureFilePath);
if ($isRelativeToFile) {
$resolvedPath = realpath($this->getCurrentAbsolutePath() . '/' . $fixtureFilePath);
if ($resolvedPath) {
return $resolvedPath;
}
}
// Check if file exists relative to base dir
$resolvedPath = realpath(Director::baseFolder() . '/' . $fixtureFilePath);
if ($resolvedPath) {
return $resolvedPath;
}
return $fixtureFilePath;
}
}

View File

@ -0,0 +1,29 @@
<?php
use SilverStripe\Dev\SapphireTest;
class SapphireTestTest extends SapphireTest
{
public function testResolveFixturePath() {
// Same directory
$this->assertEquals(
__DIR__ . '/CsvBulkLoaderTest.yml',
$this->resolveFixturePath('./CsvBulkLoaderTest.yml')
);
// Filename only
$this->assertEquals(
__DIR__ . '/CsvBulkLoaderTest.yml',
$this->resolveFixturePath('CsvBulkLoaderTest.yml')
);
// Parent path
$this->assertEquals(
dirname(__DIR__) . '/model/DataObjectTest.yml',
$this->resolveFixturePath('../model/DataObjectTest.yml')
);
// Absolute path
$this->assertEquals(
dirname(__DIR__) . '/model/DataObjectTest.yml',
$this->resolveFixturePath(dirname(__DIR__) .'/model/DataObjectTest.yml')
);
}
}