2012-12-07 18:44:00 +01:00
|
|
|
<?php
|
2014-02-24 09:41:48 +01:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\FixtureFactory;
|
|
|
|
use SilverStripe\Dev\FixtureBlueprint;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Dev\TestOnly;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class FixtureFactoryTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'FixtureFactoryTest_DataObject',
|
|
|
|
'FixtureFactoryTest_DataObjectRelation'
|
|
|
|
);
|
|
|
|
|
|
|
|
public function testCreateRaw() {
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$id = $factory->createRaw('FixtureFactoryTest_DataObject', 'one', array('Name' => 'My Name'));
|
|
|
|
$this->assertNotNull($id);
|
|
|
|
$this->assertGreaterThan(0, $id);
|
|
|
|
$obj = FixtureFactoryTest_DataObject::get()->find('ID', $id);
|
|
|
|
$this->assertNotNull($obj);
|
|
|
|
$this->assertEquals('My Name', $obj->Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetId() {
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$obj = new FixtureFactoryTest_DataObject();
|
|
|
|
$obj->write();
|
|
|
|
$factory->setId('FixtureFactoryTest_DataObject', 'one', $obj->ID);
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
$obj->ID,
|
2012-12-07 18:44:00 +01:00
|
|
|
$factory->getId('FixtureFactoryTest_DataObject', 'one')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetId() {
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$obj = $factory->createObject('FixtureFactoryTest_DataObject', 'one');
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
$obj->ID,
|
2012-12-07 18:44:00 +01:00
|
|
|
$factory->getId('FixtureFactoryTest_DataObject', 'one')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetIds() {
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$obj = $factory->createObject('FixtureFactoryTest_DataObject', 'one');
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
array('one' => $obj->ID),
|
2012-12-07 18:44:00 +01:00
|
|
|
$factory->getIds('FixtureFactoryTest_DataObject')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefine() {
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$this->assertFalse($factory->getBlueprint('FixtureFactoryTest_DataObject'));
|
|
|
|
$factory->define('FixtureFactoryTest_DataObject');
|
|
|
|
$this->assertInstanceOf(
|
2016-08-19 00:51:35 +02:00
|
|
|
'SilverStripe\\Dev\\FixtureBluePrint',
|
2012-12-07 18:44:00 +01:00
|
|
|
$factory->getBlueprint('FixtureFactoryTest_DataObject')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefineWithCustomBlueprint() {
|
|
|
|
$blueprint = new FixtureBlueprint('FixtureFactoryTest_DataObject');
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$this->assertFalse($factory->getBlueprint('FixtureFactoryTest_DataObject'));
|
|
|
|
$factory->define('FixtureFactoryTest_DataObject', $blueprint);
|
|
|
|
$this->assertInstanceOf(
|
2016-08-19 00:51:35 +02:00
|
|
|
'SilverStripe\\Dev\\FixtureBluePrint',
|
2012-12-07 18:44:00 +01:00
|
|
|
$factory->getBlueprint('FixtureFactoryTest_DataObject')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$blueprint,
|
|
|
|
$factory->getBlueprint('FixtureFactoryTest_DataObject')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefineWithDefaults() {
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$factory->define('FixtureFactoryTest_DataObject', array('Name' => 'Default'));
|
|
|
|
$obj = $factory->createObject('FixtureFactoryTest_DataObject', 'one');
|
|
|
|
$this->assertEquals('Default', $obj->Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefineMultipleBlueprintsForClass() {
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$factory->define(
|
2014-08-15 08:53:05 +02:00
|
|
|
'FixtureFactoryTest_DataObject',
|
2012-12-07 18:44:00 +01:00
|
|
|
new FixtureBlueprint('FixtureFactoryTest_DataObject')
|
|
|
|
);
|
|
|
|
$factory->define(
|
2014-08-15 08:53:05 +02:00
|
|
|
'FixtureFactoryTest_DataObjectWithDefaults',
|
2012-12-07 18:44:00 +01:00
|
|
|
new FixtureBlueprint(
|
|
|
|
'FixtureFactoryTest_DataObjectWithDefaults',
|
|
|
|
'FixtureFactoryTest_DataObject',
|
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
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
$obj = $factory->createObject('FixtureFactoryTest_DataObject', 'one');
|
|
|
|
$this->assertNull($obj->Name);
|
|
|
|
|
|
|
|
$objWithDefaults = $factory->createObject('FixtureFactoryTest_DataObjectWithDefaults', 'two');
|
|
|
|
$this->assertEquals('Default', $objWithDefaults->Name);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$obj->ID,
|
|
|
|
$factory->getId('FixtureFactoryTest_DataObject', 'one')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$objWithDefaults->ID,
|
|
|
|
$factory->getId('FixtureFactoryTest_DataObject', 'two'),
|
|
|
|
'Can access fixtures under class name, not blueprint name'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClear() {
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$obj1Id = $factory->createRaw('FixtureFactoryTest_DataObject', 'one', array('Name' => 'My Name'));
|
|
|
|
$obj2 = $factory->createObject('FixtureFactoryTest_DataObject', 'two');
|
|
|
|
|
|
|
|
$factory->clear();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->assertFalse($factory->getId('FixtureFactoryTest_DataObject', 'one'));
|
|
|
|
$this->assertNull(FixtureFactoryTest_DataObject::get()->byId($obj1Id));
|
|
|
|
$this->assertFalse($factory->getId('FixtureFactoryTest_DataObject', 'two'));
|
|
|
|
$this->assertNull(FixtureFactoryTest_DataObject::get()->byId($obj2->ID));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClearWithClass() {
|
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$obj1 = $factory->createObject('FixtureFactoryTest_DataObject', 'object-one');
|
|
|
|
$relation1 = $factory->createObject('FixtureFactoryTest_DataObjectRelation', 'relation-one');
|
|
|
|
|
|
|
|
$factory->clear('FixtureFactoryTest_DataObject');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$factory->getId('FixtureFactoryTest_DataObject', 'one')
|
|
|
|
);
|
|
|
|
$this->assertNull(FixtureFactoryTest_DataObject::get()->byId($obj1->ID));
|
|
|
|
$this->assertEquals(
|
|
|
|
$relation1->ID,
|
|
|
|
$factory->getId('FixtureFactoryTest_DataObjectRelation', 'relation-one')
|
|
|
|
);
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
'FixtureFactoryTest_DataObjectRelation',
|
|
|
|
FixtureFactoryTest_DataObjectRelation::get()->byId($relation1->ID)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-24 09:41:48 +01:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2012-12-07 18:44:00 +01:00
|
|
|
class FixtureFactoryTest_DataObject extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-12-07 18:44:00 +01:00
|
|
|
"Name" => "Varchar"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-09-05 06:58:36 +02:00
|
|
|
private static $has_many = array(
|
|
|
|
"HasManyRelation" => "FixtureFactoryTest_DataObjectRelation"
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = array(
|
2015-02-25 12:13:26 +01:00
|
|
|
"ManyManyRelation" => "FixtureFactoryTest_DataObjectRelation"
|
2012-12-07 18:44:00 +01:00
|
|
|
);
|
2014-02-24 09:41:48 +01:00
|
|
|
|
|
|
|
private static $many_many_extraFields = array(
|
2015-02-25 12:13:26 +01:00
|
|
|
"ManyManyRelation" => array(
|
2014-02-24 09:41:48 +01:00
|
|
|
"Label" => "Varchar"
|
|
|
|
)
|
|
|
|
);
|
2012-12-07 18:44:00 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 09:41:48 +01:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2012-12-07 18:44:00 +01:00
|
|
|
class FixtureFactoryTest_DataObjectRelation extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-12-07 18:44:00 +01:00
|
|
|
"Name" => "Varchar"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $belongs_many_many = array(
|
2012-12-07 18:44:00 +01:00
|
|
|
"TestParent" => "FixtureFactoryTest_DataObject"
|
2014-08-15 08:53:05 +02:00
|
|
|
);
|
2016-09-05 06:58:36 +02:00
|
|
|
|
|
|
|
private static $has_one = array(
|
|
|
|
'MyParent' => 'FixtureFactoryTest_DataObject'
|
|
|
|
);
|
2012-12-07 18:44:00 +01:00
|
|
|
}
|