mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT Add YamlFixture String argument support
Enhanced YamlFixtureTest to cover this addition.
This commit is contained in:
parent
402297ee1f
commit
c282190299
@ -85,17 +85,28 @@ class YamlFixture extends Object {
|
||||
*/
|
||||
protected $fixtureDictionary;
|
||||
|
||||
/**
|
||||
* String containing fixture
|
||||
*
|
||||
* @var String
|
||||
*/
|
||||
protected $fixtureString;
|
||||
|
||||
/**
|
||||
* @param String Absolute file path, or relative path to {@link Director::baseFolder()}
|
||||
*/
|
||||
function __construct($fixtureFile) {
|
||||
if(!Director::is_absolute($fixtureFile)) $fixtureFile = Director::baseFolder().'/'. $fixtureFile;
|
||||
function __construct($fixture) {
|
||||
if(false !== strpos($fixture, "\n")) {
|
||||
$this->fixtureString = $fixture;
|
||||
} else {
|
||||
if(!Director::is_absolute($fixture)) $fixture = Director::baseFolder().'/'. $fixture;
|
||||
|
||||
if(!file_exists($fixtureFile)) {
|
||||
throw new InvalidArgumentException('YamlFixture::__construct(): Fixture path "' . $fixtureFile . '" not found');
|
||||
if(!file_exists($fixture)) {
|
||||
throw new InvalidArgumentException('YamlFixture::__construct(): Fixture path "' . $fixture . '" not found');
|
||||
}
|
||||
|
||||
$this->fixtureFile = $fixtureFile;
|
||||
$this->fixtureFile = $fixture;
|
||||
}
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
@ -107,6 +118,13 @@ class YamlFixture extends Object {
|
||||
return $this->fixtureFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return String Fixture string
|
||||
*/
|
||||
function getFixtureString() {
|
||||
return $this->fixtureString;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@ -162,7 +180,11 @@ class YamlFixture extends Object {
|
||||
DataObject::set_validation_enabled(false);
|
||||
|
||||
$parser = new Spyc();
|
||||
if (isset($this->fixtureString)) {
|
||||
$fixtureContent = $parser->load($this->fixtureString);
|
||||
} else {
|
||||
$fixtureContent = $parser->loadFile($this->fixtureFile);
|
||||
}
|
||||
|
||||
$this->fixtureDictionary = array();
|
||||
foreach($fixtureContent as $dataClass => $items) {
|
||||
|
@ -13,12 +13,22 @@ class YamlFixtureTest extends SapphireTest {
|
||||
$absPath = FRAMEWORK_PATH . '/tests/testing/YamlFixtureTest.yml';
|
||||
$obj = new YamlFixture($absPath);
|
||||
$this->assertEquals($absPath, $obj->getFixtureFile());
|
||||
$this->assertNull($obj->getFixtureString());
|
||||
}
|
||||
|
||||
function testRelativeFixturePath() {
|
||||
$relPath = FRAMEWORK_DIR . '/tests/testing/YamlFixtureTest.yml';
|
||||
$obj = new YamlFixture($relPath);
|
||||
$this->assertEquals(Director::baseFolder() . '/' . $relPath, $obj->getFixtureFile());
|
||||
$this->assertNull($obj->getFixtureString());
|
||||
}
|
||||
|
||||
function testStringFixture() {
|
||||
$absPath = FRAMEWORK_PATH . '/tests/testing/YamlFixtureTest.yml';
|
||||
$string = file_get_contents($absPath);
|
||||
$obj = new YamlFixture($string);
|
||||
$this->assertEquals($string, $obj->getFixtureString());
|
||||
$this->assertNull($obj->getFixtureFile());
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user