2012-12-07 18:44:00 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Dev\Tests;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\FixtureBlueprint;
|
|
|
|
use SilverStripe\Dev\FixtureFactory;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Dev\Tests\FixtureBlueprintTest\Article;
|
|
|
|
use SilverStripe\Dev\Tests\FixtureBlueprintTest\TestPage;
|
|
|
|
use SilverStripe\Dev\Tests\FixtureBlueprintTest\TestSiteTree;
|
|
|
|
use SilverStripe\Dev\Tests\FixtureFactoryTest\DataObjectRelation;
|
|
|
|
use SilverStripe\Dev\Tests\FixtureFactoryTest\TestDataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class FixtureBlueprintTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
2022-12-21 02:44:47 +01:00
|
|
|
private int $_called = 0;
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
TestDataObject::class,
|
|
|
|
DataObjectRelation::class,
|
|
|
|
TestSiteTree::class,
|
|
|
|
TestPage::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
public function testCreateWithRelationshipExtraFields()
|
|
|
|
{
|
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class);
|
|
|
|
|
|
|
|
$relation1 = new DataObjectRelation();
|
|
|
|
$relation1->write();
|
|
|
|
$relation2 = new DataObjectRelation();
|
|
|
|
$relation2->write();
|
|
|
|
|
|
|
|
// in YAML these look like
|
|
|
|
// RelationName:
|
|
|
|
// - =>Relational.obj:
|
|
|
|
// ExtraFieldName: test
|
|
|
|
// - =>..
|
|
|
|
$obj = $blueprint->createObject(
|
|
|
|
'one',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'ManyManyRelation' =>
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
[
|
|
|
|
"=>" . DataObjectRelation::class . ".relation1" => [],
|
2016-12-16 05:34:21 +01:00
|
|
|
"Label" => 'This is a label for relation 1'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"=>" . DataObjectRelation::class . ".relation2" => [],
|
2016-12-16 05:34:21 +01:00
|
|
|
"Label" => 'This is a label for relation 2'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
DataObjectRelation::class => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'relation1' => $relation1->ID,
|
|
|
|
'relation2' => $relation2->ID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(2, $obj->ManyManyRelation()->Count());
|
|
|
|
$this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation1->ID));
|
|
|
|
$this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation2->ID));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
['Label' => 'This is a label for relation 1'],
|
2016-12-16 05:34:21 +01:00
|
|
|
$obj->ManyManyRelation()->getExtraData('ManyManyRelation', $relation1->ID)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
['Label' => 'This is a label for relation 2'],
|
2016-12-16 05:34:21 +01:00
|
|
|
$obj->ManyManyRelation()->getExtraData('ManyManyRelation', $relation2->ID)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testCreateWithoutData()
|
|
|
|
{
|
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class);
|
|
|
|
$obj = $blueprint->createObject('one');
|
|
|
|
$this->assertNotNull($obj);
|
|
|
|
$this->assertGreaterThan(0, $obj->ID);
|
|
|
|
$this->assertEquals('', $obj->Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateWithData()
|
|
|
|
{
|
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class);
|
2020-04-20 19:58:09 +02:00
|
|
|
$obj = $blueprint->createObject('one', ['Name' => 'My Name']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNotNull($obj);
|
|
|
|
$this->assertGreaterThan(0, $obj->ID);
|
|
|
|
$this->assertEquals('My Name', $obj->Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testCreateWithRelationship()
|
|
|
|
{
|
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class);
|
|
|
|
|
|
|
|
$relation1 = new DataObjectRelation();
|
|
|
|
$relation1->write();
|
|
|
|
$relation2 = new DataObjectRelation();
|
|
|
|
$relation2->write();
|
|
|
|
|
|
|
|
$obj = $blueprint->createObject(
|
|
|
|
'one',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'ManyManyRelation' =>
|
2018-01-16 19:39:30 +01:00
|
|
|
'=>' . DataObjectRelation::class . '.relation1,' . '=>' . DataObjectRelation::class . '.relation2'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
DataObjectRelation::class => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'relation1' => $relation1->ID,
|
|
|
|
'relation2' => $relation2->ID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(2, $obj->ManyManyRelation()->Count());
|
|
|
|
$this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation1->ID));
|
|
|
|
$this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation2->ID));
|
|
|
|
|
|
|
|
$obj2 = $blueprint->createObject(
|
|
|
|
'two',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
// Note; using array format here, not comma separated
|
2020-04-20 19:58:09 +02:00
|
|
|
'HasManyRelation' => [
|
2018-01-16 19:39:30 +01:00
|
|
|
'=>' . DataObjectRelation::class . '.relation1',
|
|
|
|
'=>' . DataObjectRelation::class . '.relation2'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
DataObjectRelation::class => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'relation1' => $relation1->ID,
|
|
|
|
'relation2' => $relation2->ID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(2, $obj2->HasManyRelation()->Count());
|
|
|
|
$this->assertNotNull($obj2->HasManyRelation()->find('ID', $relation1->ID));
|
|
|
|
$this->assertNotNull($obj2->HasManyRelation()->find('ID', $relation2->ID));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateWithInvalidRelationName()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('No fixture definitions found');
|
2016-12-16 05:34:21 +01:00
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class);
|
|
|
|
|
|
|
|
$obj = $blueprint->createObject(
|
|
|
|
'one',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'ManyManyRelation' => '=>UnknownClass.relation1'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
DataObjectRelation::class => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'relation1' => 99
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateWithInvalidRelationIdentifier()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('No fixture definitions found');
|
2016-12-16 05:34:21 +01:00
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class);
|
|
|
|
|
|
|
|
$obj = $blueprint->createObject(
|
|
|
|
'one',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2018-01-16 19:39:30 +01:00
|
|
|
'ManyManyRelation' => '=>' . DataObjectRelation::class . '.unknown_identifier'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
DataObjectRelation::class => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'relation1' => 99
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateWithInvalidRelationFormat()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('Invalid format');
|
2016-12-16 05:34:21 +01:00
|
|
|
$factory = new FixtureFactory();
|
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class);
|
|
|
|
|
|
|
|
$relation1 = new DataObjectRelation();
|
|
|
|
$relation1->write();
|
|
|
|
|
|
|
|
$obj = $blueprint->createObject(
|
|
|
|
'one',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2018-01-16 19:39:30 +01:00
|
|
|
'ManyManyRelation' => DataObjectRelation::class . '.relation1'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
DataObjectRelation::class => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'relation1' => $relation1->ID
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateWithId()
|
|
|
|
{
|
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class);
|
2020-04-20 19:58:09 +02:00
|
|
|
$obj = $blueprint->createObject('ninetynine', ['ID' => 99]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNotNull($obj);
|
|
|
|
$this->assertEquals(99, $obj->ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateWithLastEdited()
|
|
|
|
{
|
|
|
|
$extpectedDate = '2010-12-14 16:18:20';
|
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class);
|
2020-04-20 19:58:09 +02:00
|
|
|
$obj = $blueprint->createObject('lastedited', ['LastEdited' => $extpectedDate]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNotNull($obj);
|
|
|
|
$this->assertEquals($extpectedDate, $obj->LastEdited);
|
|
|
|
|
|
|
|
$obj = TestDataObject::get()->byID($obj->ID);
|
|
|
|
$this->assertEquals($extpectedDate, $obj->LastEdited);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateWithClassAncestry()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$data = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'Title' => 'My Title',
|
|
|
|
'Created' => '2010-12-14 16:18:20',
|
|
|
|
'LastEdited' => '2010-12-14 16:18:20',
|
|
|
|
'PublishDate' => '2015-12-09 06:03:00'
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$blueprint = new FixtureBlueprint(Article::class);
|
|
|
|
$obj = $blueprint->createObject('home', $data);
|
|
|
|
$this->assertNotNull($obj);
|
|
|
|
$this->assertEquals($data['Title'], $obj->Title);
|
|
|
|
$this->assertEquals($data['Created'], $obj->Created);
|
|
|
|
$this->assertEquals($data['LastEdited'], $obj->LastEdited);
|
|
|
|
$this->assertEquals($data['PublishDate'], $obj->PublishDate);
|
|
|
|
|
|
|
|
$obj = Article::get()->byID($obj->ID);
|
|
|
|
$this->assertNotNull($obj);
|
|
|
|
$this->assertEquals($data['Title'], $obj->Title);
|
|
|
|
$this->assertEquals($data['Created'], $obj->Created);
|
|
|
|
$this->assertEquals($data['LastEdited'], $obj->LastEdited);
|
|
|
|
$this->assertEquals($data['PublishDate'], $obj->PublishDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCallbackOnBeforeCreate()
|
|
|
|
{
|
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class);
|
|
|
|
$this->_called = 0;
|
|
|
|
$self = $this;
|
|
|
|
$cb = function ($identifier, $data, $fixtures) use ($self) {
|
|
|
|
$self->_called = $self->_called + 1;
|
|
|
|
};
|
|
|
|
$blueprint->addCallback('beforeCreate', $cb);
|
|
|
|
$this->assertEquals(0, $this->_called);
|
|
|
|
$obj1 = $blueprint->createObject('one');
|
|
|
|
$this->assertEquals(1, $this->_called);
|
|
|
|
$obj2 = $blueprint->createObject('two');
|
|
|
|
$this->assertEquals(2, $this->_called);
|
|
|
|
|
|
|
|
$this->_called = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCallbackOnAfterCreate()
|
|
|
|
{
|
|
|
|
$blueprint = new FixtureBlueprint(TestDataObject::class);
|
|
|
|
$this->_called = 0;
|
|
|
|
$self = $this;
|
|
|
|
$cb = function ($obj, $identifier, $data, $fixtures) use ($self) {
|
|
|
|
$self->_called = $self->_called + 1;
|
|
|
|
};
|
|
|
|
$blueprint->addCallback('afterCreate', $cb);
|
|
|
|
$this->assertEquals(0, $this->_called);
|
|
|
|
$obj1 = $blueprint->createObject('one');
|
|
|
|
$this->assertEquals(1, $this->_called);
|
|
|
|
$obj2 = $blueprint->createObject('two');
|
|
|
|
$this->assertEquals(2, $this->_called);
|
|
|
|
|
|
|
|
$this->_called = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefineWithDefaultCustomSetters()
|
|
|
|
{
|
|
|
|
$blueprint = new FixtureBlueprint(
|
|
|
|
TestDataObject::class,
|
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => function ($obj, $data, $fixtures) {
|
|
|
|
return 'Default Name';
|
|
|
|
}
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$obj1 = $blueprint->createObject('one');
|
|
|
|
$this->assertEquals('Default Name', $obj1->Name);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$obj2 = $blueprint->createObject('one', ['Name' => 'Override Name']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('Override Name', $obj2->Name);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|