2012-11-03 04:16:51 +01:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\ORM\RelationList;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2012-11-03 04:16:51 +01:00
|
|
|
class UnsavedRelationListTest extends SapphireTest {
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'UnsavedRelationListTest.yml';
|
2012-11-03 04:16:51 +01:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
protected $extraDataObjects = [
|
|
|
|
UnsavedRelationListTest\TestObject::class
|
|
|
|
];
|
2012-11-03 04:16:51 +01:00
|
|
|
|
|
|
|
public function testReturnedList() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
2012-11-03 04:16:51 +01:00
|
|
|
$children = $object->Children();
|
|
|
|
$siblings = $object->Siblings();
|
|
|
|
$this->assertEquals($children, $object->Children(),
|
|
|
|
'Returned UnsavedRelationList should be the same.');
|
|
|
|
$this->assertEquals($siblings, $object->Siblings(),
|
|
|
|
'Returned UnsavedRelationList should be the same.');
|
|
|
|
|
|
|
|
$object->write();
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertInstanceOf(RelationList::class, $object->Children());
|
2012-11-03 04:16:51 +01:00
|
|
|
$this->assertNotEquals($children, $object->Children(),
|
|
|
|
'Return should be a RelationList after first write');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertInstanceOf(RelationList::class, $object->Siblings());
|
2012-11-03 04:16:51 +01:00
|
|
|
$this->assertNotEquals($siblings, $object->Siblings(),
|
|
|
|
'Return should be a RelationList after first write');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasManyExisting() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
2012-11-03 04:16:51 +01:00
|
|
|
|
|
|
|
$children = $object->Children();
|
2016-10-14 03:30:05 +02:00
|
|
|
$children->add($this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectA'));
|
|
|
|
$children->add($this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectB'));
|
|
|
|
$children->add($this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectC'));
|
2012-11-03 04:16:51 +01:00
|
|
|
|
|
|
|
$children = $object->Children();
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Name' => 'A'),
|
|
|
|
array('Name' => 'B'),
|
|
|
|
array('Name' => 'C')
|
|
|
|
), $children);
|
|
|
|
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$this->assertNotEquals($children, $object->Children());
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Name' => 'A'),
|
|
|
|
array('Name' => 'B'),
|
|
|
|
array('Name' => 'C')
|
|
|
|
), $object->Children());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testManyManyExisting() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
2012-11-03 04:16:51 +01:00
|
|
|
|
|
|
|
$Siblings = $object->Siblings();
|
2016-10-14 03:30:05 +02:00
|
|
|
$Siblings->add($this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectA'));
|
|
|
|
$Siblings->add($this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectB'));
|
|
|
|
$Siblings->add($this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectC'));
|
2012-11-03 04:16:51 +01:00
|
|
|
|
|
|
|
$siblings = $object->Siblings();
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Name' => 'A'),
|
|
|
|
array('Name' => 'B'),
|
|
|
|
array('Name' => 'C')
|
|
|
|
), $siblings);
|
|
|
|
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$this->assertNotEquals($siblings, $object->Siblings());
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Name' => 'A'),
|
|
|
|
array('Name' => 'B'),
|
|
|
|
array('Name' => 'C')
|
|
|
|
), $object->Siblings());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHasManyNew() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
2012-11-03 04:16:51 +01:00
|
|
|
|
|
|
|
$children = $object->Children();
|
2016-10-14 03:30:05 +02:00
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(array('Name' => 'A')));
|
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(array('Name' => 'B')));
|
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(array('Name' => 'C')));
|
2012-11-03 04:16:51 +01:00
|
|
|
|
|
|
|
$children = $object->Children();
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Name' => 'A'),
|
|
|
|
array('Name' => 'B'),
|
|
|
|
array('Name' => 'C')
|
|
|
|
), $children);
|
|
|
|
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$this->assertNotEquals($children, $object->Children());
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Name' => 'A'),
|
|
|
|
array('Name' => 'B'),
|
|
|
|
array('Name' => 'C')
|
|
|
|
), $object->Children());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-07-10 02:44:24 +02:00
|
|
|
public function testHasManyPolymorphic() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
2013-07-10 02:44:24 +02:00
|
|
|
|
|
|
|
$children = $object->RelatedObjects();
|
2016-10-14 03:30:05 +02:00
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(array('Name' => 'A')));
|
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(array('Name' => 'B')));
|
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(array('Name' => 'C')));
|
2013-07-10 02:44:24 +02:00
|
|
|
|
|
|
|
$children = $object->RelatedObjects();
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Name' => 'A'),
|
|
|
|
array('Name' => 'B'),
|
|
|
|
array('Name' => 'C')
|
|
|
|
), $children);
|
|
|
|
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$this->assertNotEquals($children, $object->RelatedObjects());
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Name' => 'A'),
|
|
|
|
array('Name' => 'B'),
|
|
|
|
array('Name' => 'C')
|
|
|
|
), $object->RelatedObjects());
|
|
|
|
}
|
2012-11-03 04:16:51 +01:00
|
|
|
|
|
|
|
public function testManyManyNew() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
2012-11-03 04:16:51 +01:00
|
|
|
|
|
|
|
$Siblings = $object->Siblings();
|
2016-10-14 03:30:05 +02:00
|
|
|
$Siblings->add(new UnsavedRelationListTest\TestObject(array('Name' => 'A')));
|
|
|
|
$Siblings->add(new UnsavedRelationListTest\TestObject(array('Name' => 'B')));
|
|
|
|
$Siblings->add(new UnsavedRelationListTest\TestObject(array('Name' => 'C')));
|
2012-11-03 04:16:51 +01:00
|
|
|
|
|
|
|
$siblings = $object->Siblings();
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Name' => 'A'),
|
|
|
|
array('Name' => 'B'),
|
|
|
|
array('Name' => 'C')
|
|
|
|
), $siblings);
|
|
|
|
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$this->assertNotEquals($siblings, $object->Siblings());
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Name' => 'A'),
|
|
|
|
array('Name' => 'B'),
|
|
|
|
array('Name' => 'C')
|
|
|
|
), $object->Siblings());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testManyManyExtraFields() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
2012-11-03 04:16:51 +01:00
|
|
|
|
|
|
|
$Siblings = $object->Siblings();
|
2016-10-14 03:30:05 +02:00
|
|
|
$Siblings->add(new UnsavedRelationListTest\TestObject(array('Name' => 'A')), array('Number' => 1));
|
|
|
|
$Siblings->add(new UnsavedRelationListTest\TestObject(array('Name' => 'B')), array('Number' => 2));
|
|
|
|
$Siblings->add(new UnsavedRelationListTest\TestObject(array('Name' => 'C')), array('Number' => 3));
|
2012-11-03 04:16:51 +01:00
|
|
|
|
|
|
|
$siblings = $object->Siblings();
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Name' => 'A', 'Number' => 1),
|
|
|
|
array('Name' => 'B', 'Number' => 2),
|
|
|
|
array('Name' => 'C', 'Number' => 3)
|
|
|
|
), $siblings);
|
|
|
|
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
$this->assertNotEquals($siblings, $object->Siblings());
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Name' => 'A', 'Number' => 1),
|
|
|
|
array('Name' => 'B', 'Number' => 2),
|
|
|
|
array('Name' => 'C', 'Number' => 3)
|
|
|
|
), $object->Siblings());
|
|
|
|
}
|
2013-03-08 00:33:27 +01:00
|
|
|
|
|
|
|
public function testGetIDList() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
2013-03-08 00:33:27 +01:00
|
|
|
|
|
|
|
$children = $object->Children();
|
|
|
|
$this->assertEquals($children->getIDList(), array());
|
2016-10-14 03:30:05 +02:00
|
|
|
$children->add($child1 = new UnsavedRelationListTest\TestObject(array('Name' => 'A')));
|
|
|
|
$children->add($child2 = new UnsavedRelationListTest\TestObject(array('Name' => 'B')));
|
|
|
|
$children->add($child3 = new UnsavedRelationListTest\TestObject(array('Name' => 'C')));
|
2013-03-08 00:33:27 +01:00
|
|
|
$children->add($child1);
|
|
|
|
|
|
|
|
$this->assertEquals($children->getIDList(), array());
|
|
|
|
|
|
|
|
$child1->write();
|
|
|
|
$this->assertEquals($children->getIDList(), array(
|
|
|
|
$child1->ID => $child1->ID
|
|
|
|
));
|
|
|
|
|
|
|
|
$child2->write();
|
|
|
|
$child3->write();
|
|
|
|
$this->assertEquals($children->getIDList(), array(
|
|
|
|
$child1->ID => $child1->ID,
|
|
|
|
$child2->ID => $child2->ID,
|
|
|
|
$child3->ID => $child3->ID
|
|
|
|
));
|
|
|
|
}
|
2014-08-26 13:27:18 +02:00
|
|
|
|
|
|
|
public function testColumn() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$object = new UnsavedRelationListTest\TestObject();
|
2014-08-26 13:27:18 +02:00
|
|
|
|
|
|
|
$children = $object->Children();
|
2016-10-14 03:30:05 +02:00
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(array('Name' => 'A')));
|
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(array('Name' => 'B')));
|
|
|
|
$children->add(new UnsavedRelationListTest\TestObject(array('Name' => 'C')));
|
2014-08-26 13:27:18 +02:00
|
|
|
|
|
|
|
$children = $object->Children();
|
|
|
|
|
|
|
|
$this->assertDOSEquals(array(
|
|
|
|
array('Name' => 'A'),
|
|
|
|
array('Name' => 'B'),
|
|
|
|
array('Name' => 'C')
|
|
|
|
), $children);
|
|
|
|
|
|
|
|
$this->assertEquals($children->column('Name'), array(
|
|
|
|
'A',
|
|
|
|
'B',
|
|
|
|
'C'
|
|
|
|
));
|
|
|
|
}
|
2012-11-03 04:16:51 +01:00
|
|
|
}
|