2008-11-11 00:18:31 +01:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Dev\Tests;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use SilverStripe\Dev\Tests\YamlFixtureTest\TestDataObject;
|
|
|
|
use SilverStripe\Dev\Tests\YamlFixtureTest\DataObjectRelation;
|
|
|
|
use SilverStripe\Dev\YamlFixture;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Dev\FixtureFactory;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class YamlFixtureTest extends SapphireTest
|
|
|
|
{
|
2011-03-30 08:47:30 +02:00
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_dataobjects = array(
|
2016-12-16 05:34:21 +01:00
|
|
|
TestDataObject::class,
|
|
|
|
DataObjectRelation::class,
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testAbsoluteFixturePath()
|
|
|
|
{
|
|
|
|
$absPath = __DIR__ . '/YamlFixtureTest.yml';
|
|
|
|
$obj = Injector::inst()->create(YamlFixture::class, $absPath);
|
|
|
|
$this->assertEquals($absPath, $obj->getFixtureFile());
|
|
|
|
$this->assertNull($obj->getFixtureString());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testRelativeFixturePath()
|
|
|
|
{
|
|
|
|
$relPath = ltrim(FRAMEWORK_DIR . '/tests/php/Dev/YamlFixtureTest.yml', '/');
|
|
|
|
$obj = Injector::inst()->create(YamlFixture::class, $relPath);
|
|
|
|
$this->assertEquals(Director::baseFolder() . '/' . $relPath, $obj->getFixtureFile());
|
|
|
|
$this->assertNull($obj->getFixtureString());
|
|
|
|
}
|
2012-06-29 00:33:00 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testStringFixture()
|
|
|
|
{
|
|
|
|
$absPath = __DIR__ . '/YamlFixtureTest.yml';
|
|
|
|
$string = file_get_contents($absPath);
|
|
|
|
$obj = Injector::inst()->create(YamlFixture::class, $string);
|
|
|
|
$this->assertEquals($string, $obj->getFixtureString());
|
|
|
|
$this->assertNull($obj->getFixtureFile());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testFailsWithInvalidFixturePath()
|
|
|
|
{
|
|
|
|
$invalidPath = ltrim(FRAMEWORK_DIR . '/tests/testing/invalid.yml', '/');
|
|
|
|
$obj = Injector::inst()->create(YamlFixture::class, $invalidPath);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testSQLInsert()
|
|
|
|
{
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$absPath = __DIR__ . '/YamlFixtureTest.yml';
|
|
|
|
$fixture = Injector::inst()->create(YamlFixture::class, $absPath);
|
|
|
|
$fixture->writeInto($factory);
|
2012-12-07 18:44:00 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertGreaterThan(0, $factory->getId(TestDataObject::class, "testobject1"));
|
|
|
|
$object1 = DataObject::get_by_id(
|
|
|
|
TestDataObject::class,
|
|
|
|
$factory->getId(TestDataObject::class, "testobject1")
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$object1->ManyManyRelation()->Count() == 2,
|
|
|
|
"Should be two items in this relationship"
|
|
|
|
);
|
|
|
|
$this->assertGreaterThan(0, $factory->getId(TestDataObject::class, "testobject2"));
|
|
|
|
$object2 = DataObject::get_by_id(
|
|
|
|
TestDataObject::class,
|
|
|
|
$factory->getId(TestDataObject::class, "testobject2")
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$object2->ManyManyRelation()->Count() == 1,
|
|
|
|
"Should be one item in this relationship"
|
|
|
|
);
|
|
|
|
}
|
2012-12-07 18:44:00 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testWriteInto()
|
|
|
|
{
|
|
|
|
$factory = Injector::inst()->create(FixtureFactory::class);
|
2012-12-07 18:44:00 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$absPath = __DIR__ . '/YamlFixtureTest.yml';
|
|
|
|
$fixture = Injector::inst()->create(YamlFixture::class, $absPath);
|
|
|
|
$fixture->writeInto($factory);
|
2012-12-07 18:44:00 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertGreaterThan(
|
|
|
|
0,
|
|
|
|
$factory->getId(TestDataObject::class, "testobject1")
|
|
|
|
);
|
|
|
|
}
|
2008-11-11 00:18:31 +01:00
|
|
|
}
|