mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
FEATURE #2767 wakeless: Allow popuplation of non-DataObject tables with YamlFixture
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@65555 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
5c9f0f120b
commit
9565fec5ad
@ -132,6 +132,21 @@ class YamlFixture extends Object {
|
||||
$fixtureContent = $parser->load(Director::baseFolder().'/'.$this->fixtureFile);
|
||||
$this->fixtureDictionary = array();
|
||||
foreach($fixtureContent as $dataClass => $items) {
|
||||
if(ClassInfo::exists($dataClass)) {
|
||||
$this->writeDataObject($dataClass, $items);
|
||||
} else {
|
||||
$this->writeSQL($dataClass, $items);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the fixture into the database using DataObjects
|
||||
*
|
||||
* @param string $dataClass
|
||||
* @param array $items
|
||||
*/
|
||||
protected function writeDataObject($dataClass, $items) {
|
||||
foreach($items as $identifier => $fields) {
|
||||
$obj = new $dataClass();
|
||||
|
||||
@ -174,6 +189,22 @@ class YamlFixture extends Object {
|
||||
$obj->write();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the fixture into the database directly using a database manipulation
|
||||
*
|
||||
* @param string $table
|
||||
* @param array $items
|
||||
*/
|
||||
protected function writeSQL($table, $items) {
|
||||
foreach($items as $identifier => $fields) {
|
||||
$manipulation = array($table => array("fields" => array(), "command" => "insert"));
|
||||
foreach($fields as $fieldName=> $fieldVal) {
|
||||
$manipulation[$table]["fields"][$fieldName] = "'".$this->parseFixtureVal($fieldVal)."'";
|
||||
}
|
||||
DB::manipulate($manipulation);
|
||||
$this->fixtureDictionary[$table][$identifier] = DB::getGeneratedID($table);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -191,4 +222,3 @@ class YamlFixture extends Object {
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
30
tests/testing/YamlFixtureTest.php
Normal file
30
tests/testing/YamlFixtureTest.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
class YamlFixtureTest extends SapphireTest {
|
||||
static $fixture_file = 'sapphire/tests/testing/YamlFixtureTest.yml';
|
||||
|
||||
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");
|
||||
$object2 = DataObject::get_by_id("YamlFixtureTest_DataObject", $this->idFromFixture("YamlFixtureTest_DataObject", "testobject2"));
|
||||
$this->assertTrue($object2->ManyMany()->Count() == 2, "Should be 2 items in this manymany relationship");
|
||||
}
|
||||
}
|
||||
|
||||
class YamlFixtureTest_DataObject extends DataObject {
|
||||
static $db = array(
|
||||
"Name" => "Varchar"
|
||||
);
|
||||
static $many_many = array(
|
||||
"ManyMany" => "YamlFixtureTest_DataObjectRelation"
|
||||
);
|
||||
}
|
||||
|
||||
class YamlFixtureTest_DataObjectRelation extends DataObject {
|
||||
static $db = array(
|
||||
"Name" => "Varchar"
|
||||
);
|
||||
static $belongs_many_many = array(
|
||||
"TestParent" => "YamlFixtureTest_DataObject"
|
||||
);
|
||||
}
|
23
tests/testing/YamlFixtureTest.yml
Normal file
23
tests/testing/YamlFixtureTest.yml
Normal file
@ -0,0 +1,23 @@
|
||||
YamlFixtureTest_DataObject:
|
||||
testobject1:
|
||||
Name: TestObject1
|
||||
testobject2:
|
||||
Name: TestObject2
|
||||
YamlFixtureTest_DataObjectRelation:
|
||||
relation1:
|
||||
Name: Relation1
|
||||
relation2:
|
||||
Name: Relation2
|
||||
YamlFixtureTest_DataObject_ManyMany:
|
||||
manymany1:
|
||||
YamlFixtureTest_DataObjectID: =>YamlFixtureTest_DataObject.testobject1
|
||||
YamlFixtureTest_DataObjectRelationID: =>YamlFixtureTest_DataObjectRelation.relation1
|
||||
manymany2:
|
||||
YamlFixtureTest_DataObjectID: =>YamlFixtureTest_DataObject.testobject1
|
||||
YamlFixtureTest_DataObjectRelationID: =>YamlFixtureTest_DataObjectRelation.relation2
|
||||
manymany3:
|
||||
YamlFixtureTest_DataObjectID: =>YamlFixtureTest_DataObject.testobject2
|
||||
YamlFixtureTest_DataObjectRelationID: =>YamlFixtureTest_DataObjectRelation.relation1
|
||||
manymany4:
|
||||
YamlFixtureTest_DataObjectID: =>YamlFixtureTest_DataObject.testobject2
|
||||
YamlFixtureTest_DataObjectRelationID: =>YamlFixtureTest_DataObjectRelation.relation2
|
Loading…
x
Reference in New Issue
Block a user