Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ENHANCEMENT Add YamlFixture String argument support

Enhanced YamlFixtureTest to cover this addition.
  • Loading branch information...
commit c2821902994bca9439a8579fd7d4df654db52c5b 1 parent 402297e
@michalochman michalochman authored
Showing with 47 additions and 15 deletions.
  1. +37 −15 dev/YamlFixture.php
  2. +10 −0 tests/testing/YamlFixtureTest.php
View
52 dev/YamlFixture.php
@@ -84,19 +84,30 @@ class YamlFixture extends Object {
* @var array
*/
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;
-
- if(!file_exists($fixtureFile)) {
- throw new InvalidArgumentException('YamlFixture::__construct(): Fixture path "' . $fixtureFile . '" not found');
+ function __construct($fixture) {
+ if(false !== strpos($fixture, "\n")) {
+ $this->fixtureString = $fixture;
+ } else {
+ if(!Director::is_absolute($fixture)) $fixture = Director::baseFolder().'/'. $fixture;
+
+ if(!file_exists($fixture)) {
+ throw new InvalidArgumentException('YamlFixture::__construct(): Fixture path "' . $fixture . '" not found');
+ }
+
+ $this->fixtureFile = $fixture;
}
- $this->fixtureFile = $fixtureFile;
-
parent::__construct();
}
@@ -106,6 +117,13 @@ function __construct($fixtureFile) {
function getFixtureFile() {
return $this->fixtureFile;
}
+
+ /**
+ * @return String Fixture string
+ */
+ function getFixtureString() {
+ return $this->fixtureString;
+ }
/**
* Get the ID of an object from the fixture.
@@ -162,7 +180,11 @@ public function saveIntoDatabase(DataModel $model) {
DataObject::set_validation_enabled(false);
$parser = new Spyc();
- $fixtureContent = $parser->loadFile($this->fixtureFile);
+ if (isset($this->fixtureString)) {
+ $fixtureContent = $parser->load($this->fixtureString);
+ } else {
+ $fixtureContent = $parser->loadFile($this->fixtureFile);
+ }
$this->fixtureDictionary = array();
foreach($fixtureContent as $dataClass => $items) {
@@ -228,12 +250,12 @@ protected function writeDataObject($model, $dataClass, $items) {
}
}
$obj->write();
- //If LastEdited was set in the fixture, set it here
- if (array_key_exists('LastEdited', $fields)) {
- $manip = array($dataClass => array("command" => "update", "id" => $obj->id,
- "fields" => array("LastEdited" => "'".$this->parseFixtureVal($fields['LastEdited'])."'")));
- DB::manipulate($manip);
- }
+ //If LastEdited was set in the fixture, set it here
+ if (array_key_exists('LastEdited', $fields)) {
+ $manip = array($dataClass => array("command" => "update", "id" => $obj->id,
+ "fields" => array("LastEdited" => "'".$this->parseFixtureVal($fields['LastEdited'])."'")));
+ DB::manipulate($manip);
+ }
}
}
View
10 tests/testing/YamlFixtureTest.php
@@ -13,12 +13,22 @@ function testAbsoluteFixturePath() {
$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());
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.