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
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class FixtureFactoryTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
TestDataObject::class,
|
|
|
|
DataObjectRelation::class
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
public function testCreateRaw()
|
|
|
|
{
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$id = $factory->createRaw(
|
|
|
|
TestDataObject::singleton()->baseTable(),
|
|
|
|
'one',
|
2020-04-20 19:58:09 +02:00
|
|
|
['Name' => 'My Name']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertNotNull($id);
|
|
|
|
$this->assertGreaterThan(0, $id);
|
|
|
|
$obj = TestDataObject::get()->find('ID', $id);
|
|
|
|
$this->assertNotNull($obj);
|
|
|
|
$this->assertEquals('My Name', $obj->Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetId()
|
|
|
|
{
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$obj = new TestDataObject();
|
|
|
|
$obj->write();
|
|
|
|
$factory->setId(TestDataObject::class, 'one', $obj->ID);
|
|
|
|
$this->assertEquals(
|
|
|
|
$obj->ID,
|
|
|
|
$factory->getId(TestDataObject::class, 'one')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetId()
|
|
|
|
{
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$obj = $factory->createObject(TestDataObject::class, 'one');
|
|
|
|
$this->assertEquals(
|
|
|
|
$obj->ID,
|
|
|
|
$factory->getId(TestDataObject::class, 'one')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetIds()
|
|
|
|
{
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$obj = $factory->createObject(TestDataObject::class, 'one');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
['one' => $obj->ID],
|
2016-12-16 05:34:21 +01:00
|
|
|
$factory->getIds(TestDataObject::class)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefine()
|
|
|
|
{
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$this->assertFalse($factory->getBlueprint(TestDataObject::class));
|
|
|
|
$factory->define(TestDataObject::class);
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
FixtureBlueprint::class,
|
|
|
|
$factory->getBlueprint(TestDataObject::class)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefineWithCustomBlueprint()
|
|
|
|
{
|
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class);
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$this->assertFalse($factory->getBlueprint(TestDataObject::class));
|
|
|
|
$factory->define(TestDataObject::class, $blueprint);
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
FixtureBlueprint::class,
|
|
|
|
$factory->getBlueprint(TestDataObject::class)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$blueprint,
|
|
|
|
$factory->getBlueprint(TestDataObject::class)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefineWithDefaults()
|
|
|
|
{
|
|
|
|
$factory = new FixtureFactory();
|
2020-04-20 19:58:09 +02:00
|
|
|
$factory->define(TestDataObject::class, ['Name' => 'Default']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$obj = $factory->createObject(TestDataObject::class, 'one');
|
|
|
|
$this->assertEquals('Default', $obj->Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefineMultipleBlueprintsForClass()
|
|
|
|
{
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$factory->define(
|
|
|
|
TestDataObject::class,
|
|
|
|
new FixtureBlueprint(TestDataObject::class)
|
|
|
|
);
|
|
|
|
$factory->define(
|
|
|
|
'FixtureFactoryTest_DataObjectWithDefaults',
|
|
|
|
new FixtureBlueprint(
|
|
|
|
'FixtureFactoryTest_DataObjectWithDefaults',
|
|
|
|
TestDataObject::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
['Name' => 'Default']
|
2016-12-16 05:34:21 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$obj = $factory->createObject(TestDataObject::class, 'one');
|
|
|
|
$this->assertNull($obj->Name);
|
|
|
|
|
|
|
|
$objWithDefaults = $factory->createObject('FixtureFactoryTest_DataObjectWithDefaults', 'two');
|
|
|
|
$this->assertEquals('Default', $objWithDefaults->Name);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$obj->ID,
|
|
|
|
$factory->getId(TestDataObject::class, 'one')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$objWithDefaults->ID,
|
|
|
|
$factory->getId(TestDataObject::class, 'two'),
|
|
|
|
'Can access fixtures under class name, not blueprint name'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClear()
|
|
|
|
{
|
|
|
|
$factory = new FixtureFactory();
|
2018-06-14 05:56:46 +02:00
|
|
|
$table = TestDataObject::singleton()->baseTable();
|
2016-12-16 05:34:21 +01:00
|
|
|
$obj1Id = $factory->createRaw(
|
2018-06-14 05:56:46 +02:00
|
|
|
$table,
|
2016-12-16 05:34:21 +01:00
|
|
|
'one',
|
2020-04-20 19:58:09 +02:00
|
|
|
['Name' => 'My Name']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$obj2 = $factory->createObject(TestDataObject::class, 'two');
|
|
|
|
|
2018-06-14 05:56:46 +02:00
|
|
|
// Clear models only
|
2016-12-16 05:34:21 +01:00
|
|
|
$factory->clear();
|
2018-06-14 05:56:46 +02:00
|
|
|
$this->assertEquals($obj1Id, $factory->getId($table, 'one'));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNull(TestDataObject::get()->byID($obj1Id));
|
2018-06-14 05:56:46 +02:00
|
|
|
$this->assertEquals($obj2->ID, $factory->getId(TestDataObject::class, 'two'));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNull(TestDataObject::get()->byID($obj2->ID));
|
2018-06-14 05:56:46 +02:00
|
|
|
|
|
|
|
// Force metadata clear
|
|
|
|
$factory->clear(null, true);
|
|
|
|
$this->assertFalse($factory->getId($table, 'one'));
|
|
|
|
$this->assertFalse($factory->getId(TestDataObject::class, 'two'));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testClearWithClass()
|
|
|
|
{
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$obj1 = $factory->createObject(TestDataObject::class, 'object-one');
|
|
|
|
$relation1 = $factory->createObject(DataObjectRelation::class, 'relation-one');
|
|
|
|
|
|
|
|
$factory->clear(TestDataObject::class);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
$factory->getId(TestDataObject::class, 'one')
|
|
|
|
);
|
|
|
|
$this->assertNull(TestDataObject::get()->byID($obj1->ID));
|
|
|
|
$this->assertEquals(
|
|
|
|
$relation1->ID,
|
|
|
|
$factory->getId(DataObjectRelation::class, 'relation-one')
|
|
|
|
);
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
DataObjectRelation::class,
|
|
|
|
DataObjectRelation::get()->byID($relation1->ID)
|
|
|
|
);
|
|
|
|
}
|
2017-05-04 07:48:10 +02:00
|
|
|
|
|
|
|
public function testGetByClassOrTable()
|
|
|
|
{
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$obj1 = $factory->createObject(TestDataObject::class, 'object-one', [ 'Name' => 'test one' ]);
|
|
|
|
$this->assertInstanceOf(TestDataObject::class, $factory->get(TestDataObject::class, 'object-one'));
|
|
|
|
$this->assertEquals('test one', $factory->get(TestDataObject::class, 'object-one')->Name);
|
|
|
|
|
|
|
|
$obj2 = $factory->createRaw('FixtureFactoryTest_TestDataObject', 'object-two', [ 'Name' => 'test two' ]);
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
TestDataObject::class,
|
|
|
|
$factory->get('FixtureFactoryTest_TestDataObject', 'object-two')
|
|
|
|
);
|
|
|
|
$this->assertEquals('test two', $factory->get('FixtureFactoryTest_TestDataObject', 'object-two')->Name);
|
|
|
|
}
|
2012-12-07 18:44:00 +01:00
|
|
|
}
|