mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
commit
b923475dcb
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
29
tests/dev/SapphireTestTest.php
Normal file
29
tests/dev/SapphireTestTest.php
Normal 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')
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user