2012-12-07 18:44:00 +01:00
|
|
|
<?php
|
2014-02-24 09:41:48 +01:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Dev\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\FixtureBlueprint;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Dev\FixtureFactory;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Dev\Tests\FixtureFactoryTest\DataObjectRelation;
|
|
|
|
use SilverStripe\Dev\Tests\FixtureFactoryTest\TestDataObject;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
class FixtureFactoryTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
2016-10-14 03:30:05 +02:00
|
|
|
TestDataObject::class,
|
|
|
|
DataObjectRelation::class
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
public function testCreateRaw() {
|
|
|
|
$factory = new FixtureFactory();
|
2016-10-14 03:30:05 +02:00
|
|
|
$id = $factory->createRaw(
|
|
|
|
TestDataObject::singleton()->baseTable(),
|
|
|
|
'one',
|
|
|
|
array('Name' => 'My Name')
|
|
|
|
);
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->assertNotNull($id);
|
|
|
|
$this->assertGreaterThan(0, $id);
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj = TestDataObject::get()->find('ID', $id);
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->assertNotNull($obj);
|
|
|
|
$this->assertEquals('My Name', $obj->Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetId() {
|
|
|
|
$factory = new FixtureFactory();
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj = new TestDataObject();
|
2012-12-07 18:44:00 +01:00
|
|
|
$obj->write();
|
2016-10-14 03:30:05 +02:00
|
|
|
$factory->setId(TestDataObject::class, 'one', $obj->ID);
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
$obj->ID,
|
2016-10-14 03:30:05 +02:00
|
|
|
$factory->getId(TestDataObject::class, 'one')
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetId() {
|
|
|
|
$factory = new FixtureFactory();
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj = $factory->createObject(TestDataObject::class, 'one');
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
$obj->ID,
|
2016-10-14 03:30:05 +02:00
|
|
|
$factory->getId(TestDataObject::class, 'one')
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetIds() {
|
|
|
|
$factory = new FixtureFactory();
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj = $factory->createObject(TestDataObject::class, 'one');
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
array('one' => $obj->ID),
|
2016-10-14 03:30:05 +02:00
|
|
|
$factory->getIds(TestDataObject::class)
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefine() {
|
|
|
|
$factory = new FixtureFactory();
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertFalse($factory->getBlueprint(TestDataObject::class));
|
|
|
|
$factory->define(TestDataObject::class);
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->assertInstanceOf(
|
2016-10-14 03:30:05 +02:00
|
|
|
FixtureBlueprint::class,
|
|
|
|
$factory->getBlueprint(TestDataObject::class)
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefineWithCustomBlueprint() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class);
|
2012-12-07 18:44:00 +01:00
|
|
|
$factory = new FixtureFactory();
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertFalse($factory->getBlueprint(TestDataObject::class));
|
|
|
|
$factory->define(TestDataObject::class, $blueprint);
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->assertInstanceOf(
|
2016-10-14 03:30:05 +02:00
|
|
|
FixtureBlueprint::class,
|
|
|
|
$factory->getBlueprint(TestDataObject::class)
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$blueprint,
|
2016-10-14 03:30:05 +02:00
|
|
|
$factory->getBlueprint(TestDataObject::class)
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefineWithDefaults() {
|
|
|
|
$factory = new FixtureFactory();
|
2016-10-14 03:30:05 +02:00
|
|
|
$factory->define(TestDataObject::class, array('Name' => 'Default'));
|
|
|
|
$obj = $factory->createObject(TestDataObject::class, 'one');
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->assertEquals('Default', $obj->Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefineMultipleBlueprintsForClass() {
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$factory->define(
|
2016-10-14 03:30:05 +02:00
|
|
|
TestDataObject::class,
|
|
|
|
new FixtureBlueprint(TestDataObject::class)
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
|
|
|
$factory->define(
|
2014-08-15 08:53:05 +02:00
|
|
|
'FixtureFactoryTest_DataObjectWithDefaults',
|
2012-12-07 18:44:00 +01:00
|
|
|
new FixtureBlueprint(
|
|
|
|
'FixtureFactoryTest_DataObjectWithDefaults',
|
2016-10-14 03:30:05 +02:00
|
|
|
TestDataObject::class,
|
2014-08-15 08:53:05 +02:00
|
|
|
array('Name' => 'Default')
|
2012-12-07 18:44:00 +01:00
|
|
|
)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj = $factory->createObject(TestDataObject::class, 'one');
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->assertNull($obj->Name);
|
|
|
|
|
|
|
|
$objWithDefaults = $factory->createObject('FixtureFactoryTest_DataObjectWithDefaults', 'two');
|
|
|
|
$this->assertEquals('Default', $objWithDefaults->Name);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$obj->ID,
|
2016-10-14 03:30:05 +02:00
|
|
|
$factory->getId(TestDataObject::class, 'one')
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$objWithDefaults->ID,
|
2016-10-14 03:30:05 +02:00
|
|
|
$factory->getId(TestDataObject::class, 'two'),
|
2012-12-07 18:44:00 +01:00
|
|
|
'Can access fixtures under class name, not blueprint name'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClear() {
|
|
|
|
$factory = new FixtureFactory();
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj1Id = $factory->createRaw(
|
|
|
|
TestDataObject::singleton()->baseTable(),
|
|
|
|
'one',
|
|
|
|
array('Name' => 'My Name')
|
|
|
|
);
|
|
|
|
$obj2 = $factory->createObject(TestDataObject::class, 'two');
|
2012-12-07 18:44:00 +01:00
|
|
|
|
|
|
|
$factory->clear();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertFalse($factory->getId(TestDataObject::class, 'one'));
|
|
|
|
$this->assertNull(TestDataObject::get()->byID($obj1Id));
|
|
|
|
$this->assertFalse($factory->getId(TestDataObject::class, 'two'));
|
|
|
|
$this->assertNull(TestDataObject::get()->byID($obj2->ID));
|
2012-12-07 18:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testClearWithClass() {
|
|
|
|
$factory = new FixtureFactory();
|
2016-10-14 03:30:05 +02:00
|
|
|
$obj1 = $factory->createObject(TestDataObject::class, 'object-one');
|
|
|
|
$relation1 = $factory->createObject(DataObjectRelation::class, 'relation-one');
|
2012-12-07 18:44:00 +01:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$factory->clear(TestDataObject::class);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->assertFalse(
|
2016-10-14 03:30:05 +02:00
|
|
|
$factory->getId(TestDataObject::class, 'one')
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertNull(TestDataObject::get()->byID($obj1->ID));
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$relation1->ID,
|
2016-10-14 03:30:05 +02:00
|
|
|
$factory->getId(DataObjectRelation::class, 'relation-one')
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
|
|
|
$this->assertInstanceOf(
|
2016-10-14 03:30:05 +02:00
|
|
|
DataObjectRelation::class,
|
|
|
|
DataObjectRelation::get()->byID($relation1->ID)
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|