2008-11-11 00:18:31 +01:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
|
2008-11-11 00:18:31 +01:00
|
|
|
class YamlFixtureTest extends SapphireTest {
|
2011-03-30 08:47:30 +02:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'YamlFixtureTest_DataObject',
|
|
|
|
'YamlFixtureTest_DataObjectRelation',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAbsoluteFixturePath() {
|
2012-03-24 04:38:57 +01:00
|
|
|
$absPath = FRAMEWORK_PATH . '/tests/testing/YamlFixtureTest.yml';
|
2012-12-07 18:44:00 +01:00
|
|
|
$obj = Injector::inst()->create('YamlFixture', $absPath);
|
2011-03-30 08:47:30 +02:00
|
|
|
$this->assertEquals($absPath, $obj->getFixtureFile());
|
2012-06-29 00:33:00 +02:00
|
|
|
$this->assertNull($obj->getFixtureString());
|
2011-03-30 08:47:30 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRelativeFixturePath() {
|
2012-03-24 04:38:57 +01:00
|
|
|
$relPath = FRAMEWORK_DIR . '/tests/testing/YamlFixtureTest.yml';
|
2012-12-07 18:44:00 +01:00
|
|
|
$obj = Injector::inst()->create('YamlFixture', $relPath);
|
2011-03-30 08:47:30 +02:00
|
|
|
$this->assertEquals(Director::baseFolder() . '/' . $relPath, $obj->getFixtureFile());
|
2012-06-29 00:33:00 +02:00
|
|
|
$this->assertNull($obj->getFixtureString());
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testStringFixture() {
|
2012-06-29 00:33:00 +02:00
|
|
|
$absPath = FRAMEWORK_PATH . '/tests/testing/YamlFixtureTest.yml';
|
|
|
|
$string = file_get_contents($absPath);
|
2012-12-07 18:44:00 +01:00
|
|
|
$obj = Injector::inst()->create('YamlFixture', $string);
|
2012-06-29 00:33:00 +02:00
|
|
|
$this->assertEquals($string, $obj->getFixtureString());
|
|
|
|
$this->assertNull($obj->getFixtureFile());
|
2011-03-30 08:47:30 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-30 08:47:30 +02:00
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFailsWithInvalidFixturePath() {
|
2012-03-24 04:38:57 +01:00
|
|
|
$invalidPath = FRAMEWORK_DIR . '/tests/testing/invalid.yml';
|
2012-12-07 18:44:00 +01:00
|
|
|
$obj = Injector::inst()->create('YamlFixture', $invalidPath);
|
2011-03-30 08:47:30 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSQLInsert() {
|
2013-03-19 12:57:48 +01:00
|
|
|
$factory = new FixtureFactory();
|
2012-12-07 18:44:00 +01:00
|
|
|
$relPath = FRAMEWORK_DIR . '/tests/testing/YamlFixtureTest.yml';
|
|
|
|
$fixture = Injector::inst()->create('YamlFixture', $relPath);
|
2013-03-19 12:57:48 +01:00
|
|
|
$fixture->writeInto($factory);
|
2012-12-07 18:44:00 +01:00
|
|
|
|
2013-03-19 12:57:48 +01:00
|
|
|
$this->assertGreaterThan(0, $factory->getId("YamlFixtureTest_DataObject", "testobject1"));
|
2012-12-07 18:44:00 +01:00
|
|
|
$object1 = DataObject::get_by_id(
|
|
|
|
"YamlFixtureTest_DataObject",
|
2013-03-19 12:57:48 +01:00
|
|
|
$factory->getId("YamlFixtureTest_DataObject", "testobject1")
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
|
|
|
$this->assertTrue(
|
2015-02-25 12:13:26 +01:00
|
|
|
$object1->ManyManyRelation()->Count() == 2,
|
2012-12-07 18:44:00 +01:00
|
|
|
"Should be two items in this relationship"
|
|
|
|
);
|
2013-03-19 12:57:48 +01:00
|
|
|
$this->assertGreaterThan(0, $factory->getId("YamlFixtureTest_DataObject", "testobject2"));
|
2012-12-07 18:44:00 +01:00
|
|
|
$object2 = DataObject::get_by_id(
|
|
|
|
"YamlFixtureTest_DataObject",
|
2013-03-19 12:57:48 +01:00
|
|
|
$factory->getId("YamlFixtureTest_DataObject", "testobject2")
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
|
|
|
$this->assertTrue(
|
2015-02-25 12:13:26 +01:00
|
|
|
$object2->ManyManyRelation()->Count() == 1,
|
2012-12-07 18:44:00 +01:00
|
|
|
"Should be one item in this relationship"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWriteInto() {
|
|
|
|
$factory = Injector::inst()->create('FixtureFactory');
|
|
|
|
|
|
|
|
$relPath = FRAMEWORK_DIR . '/tests/testing/YamlFixtureTest.yml';
|
|
|
|
$fixture = Injector::inst()->create('YamlFixture', $relPath);
|
|
|
|
$fixture->writeInto($factory);
|
|
|
|
|
|
|
|
$this->assertGreaterThan(0, $factory->getId("YamlFixtureTest_DataObject", "testobject1"));
|
2008-11-11 00:18:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
class YamlFixtureTest_DataObject extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2008-11-11 00:18:31 +01:00
|
|
|
"Name" => "Varchar"
|
|
|
|
);
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = array(
|
2015-02-25 12:13:26 +01:00
|
|
|
"ManyManyRelation" => "YamlFixtureTest_DataObjectRelation"
|
2008-11-11 00:18:31 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
class YamlFixtureTest_DataObjectRelation extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2008-11-11 00:18:31 +01:00
|
|
|
"Name" => "Varchar"
|
|
|
|
);
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $belongs_many_many = array(
|
2008-11-11 00:18:31 +01:00
|
|
|
"TestParent" => "YamlFixtureTest_DataObject"
|
2014-08-15 08:53:05 +02:00
|
|
|
);
|
2008-11-11 00:18:31 +01:00
|
|
|
}
|