silverstripe-framework/tests/php/Dev/FixtureFactoryTest.php

167 lines
4.6 KiB
PHP
Raw Normal View History

<?php
2016-10-14 03:30:05 +02:00
namespace SilverStripe\Dev\Tests;
use SilverStripe\Dev\FixtureBlueprint;
2016-10-14 03:30:05 +02:00
use SilverStripe\Dev\FixtureFactory;
use SilverStripe\Dev\SapphireTest;
2016-10-14 03:30:05 +02:00
use SilverStripe\Dev\Tests\FixtureFactoryTest\DataObjectRelation;
use SilverStripe\Dev\Tests\FixtureFactoryTest\TestDataObject;
class FixtureFactoryTest extends SapphireTest {
protected $usesDatabase = true;
protected $extraDataObjects = array(
2016-10-14 03:30:05 +02:00
TestDataObject::class,
DataObjectRelation::class
);
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')
);
$this->assertNotNull($id);
$this->assertGreaterThan(0, $id);
2016-10-14 03:30:05 +02:00
$obj = TestDataObject::get()->find('ID', $id);
$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();
$obj->write();
2016-10-14 03:30:05 +02:00
$factory->setId(TestDataObject::class, 'one', $obj->ID);
$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')
);
}
public function testGetId() {
$factory = new FixtureFactory();
2016-10-14 03:30:05 +02:00
$obj = $factory->createObject(TestDataObject::class, 'one');
$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')
);
}
public function testGetIds() {
$factory = new FixtureFactory();
2016-10-14 03:30:05 +02:00
$obj = $factory->createObject(TestDataObject::class, 'one');
$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)
);
}
public function testDefine() {
$factory = new FixtureFactory();
2016-10-14 03:30:05 +02:00
$this->assertFalse($factory->getBlueprint(TestDataObject::class));
$factory->define(TestDataObject::class);
$this->assertInstanceOf(
2016-10-14 03:30:05 +02:00
FixtureBlueprint::class,
$factory->getBlueprint(TestDataObject::class)
);
}
public function testDefineWithCustomBlueprint() {
2016-10-14 03:30:05 +02:00
$blueprint = new FixtureBlueprint(TestDataObject::class);
$factory = new FixtureFactory();
2016-10-14 03:30:05 +02:00
$this->assertFalse($factory->getBlueprint(TestDataObject::class));
$factory->define(TestDataObject::class, $blueprint);
$this->assertInstanceOf(
2016-10-14 03:30:05 +02:00
FixtureBlueprint::class,
$factory->getBlueprint(TestDataObject::class)
);
$this->assertEquals(
$blueprint,
2016-10-14 03:30:05 +02:00
$factory->getBlueprint(TestDataObject::class)
);
}
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');
$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)
);
$factory->define(
2014-08-15 08:53:05 +02:00
'FixtureFactoryTest_DataObjectWithDefaults',
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')
)
);
2014-08-15 08:53:05 +02:00
2016-10-14 03:30:05 +02: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,
2016-10-14 03:30:05 +02:00
$factory->getId(TestDataObject::class, 'one')
);
$this->assertEquals(
$objWithDefaults->ID,
2016-10-14 03:30:05 +02:00
$factory->getId(TestDataObject::class, 'two'),
'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');
$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));
}
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');
2016-10-14 03:30:05 +02:00
$factory->clear(TestDataObject::class);
2014-08-15 08:53:05 +02:00
$this->assertFalse(
2016-10-14 03:30:05 +02:00
$factory->getId(TestDataObject::class, 'one')
);
2016-10-14 03:30:05 +02:00
$this->assertNull(TestDataObject::get()->byID($obj1->ID));
$this->assertEquals(
$relation1->ID,
2016-10-14 03:30:05 +02:00
$factory->getId(DataObjectRelation::class, 'relation-one')
);
$this->assertInstanceOf(
2016-10-14 03:30:05 +02:00
DataObjectRelation::class,
DataObjectRelation::get()->byID($relation1->ID)
);
}
}