Source of file UnionListTest.php
Size: 4,739 Bytes - Last Modified: 2021-12-23T10:50:25+00:00
/var/www/docs.ssmods.com/process/src/tests/UnionListTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 | <?php class UnionListTest extends SapphireTest { protected static $fixture_file = 'UnionListTest.yml'; protected $extraDataObjects = array( 'DataObjectTest_ValidatedObject' ); /** * @var UnionList */ protected $unionList = null; /** * For Silverstripe 4 support (when this module gets upgraded) * * @return array */ protected function getExtraDataObjects() { return $this->extraDataObjects; } public function setUp() { parent::setUp(); $obj3 = DataObjectTest_ValidatedObject::get()->find('Name', 'test obj 3'); $obj4 = DataObjectTest_ValidatedObject::get()->find('Name', 'test obj 4'); $this->assertTrue($obj3->isInDB()); $this->assertTrue($obj4->isInDB()); $list1 = DataObjectTest_ValidatedObject::get()->filter(array('Name' => 'test obj 1')); $list2 = DataObjectTest_ValidatedObject::get()->filter(array('Name' => 'test obj 2')); $list3 = new ArrayList(array($obj3, $obj4)); $this->unionList = UnionList::create(array($list1, $list2, $list3)); } public function testFirst() { $unionList = $this->unionList; $obj1 = $unionList->first(); $this->assertTrue($obj1->isInDB()); $this->assertEquals('test obj 1', $obj1->Name); } public function testLast() { $unionList = $this->unionList; $obj1 = $unionList->last(); $this->assertTrue($obj1->isInDB()); $this->assertEquals('test obj 4', $obj1->Name); } public function testToArray() { $unionList = $this->unionList; $recordSet = $unionList->toArray(); $this->assertEquals('test obj 1', $recordSet[0]->Name); $this->assertEquals('test obj 2', $recordSet[1]->Name); $this->assertEquals('test obj 3', $recordSet[2]->Name); $this->assertEquals('test obj 4', $recordSet[3]->Name); } public function testExists() { $unionList = $this->unionList; $this->assertTrue($unionList->exists()); $unionList = UnionList::create(array(DataObjectTest_ValidatedObject::get()->filter(array('Name' => 'non existant')))); $this->assertFalse($unionList->exists()); } public function testFind() { $unionList = $this->unionList; $this->assertNotNull($unionList->find('Name', 'test obj 1')); $this->assertNotNull($unionList->find('Name', 'test obj 2')); $this->assertNotNull($unionList->find('Name', 'test obj 3')); $this->assertNotNull($unionList->find('Name', 'test obj 4')); } public function testMap() { $unionList = $this->unionList; $map = $unionList->map('Name', 'Name'); $this->assertEquals([ 'test obj 1' => 'test obj 1', 'test obj 2' => 'test obj 2', 'test obj 3' => 'test obj 3', 'test obj 4' => 'test obj 4', ], $map->toArray()); } public function testToNestedArray() { $unionList = $this->unionList; $recordSet = $unionList->toNestedArray(); $expected = [ 0 => [ 'Name' => 'test obj 1', ], 1 => [ 'Name' => 'test obj 2', ], 2 => [ 'Name' => 'test obj 3', ], 3 => [ 'Name' => 'test obj 4', ], ]; foreach ($recordSet as $i => $record) { unset($recordSet[$i]['ID']); unset($recordSet[$i]['ClassName']); unset($recordSet[$i]['RecordClassName']); unset($recordSet[$i]['Created']); unset($recordSet[$i]['LastEdited']); } $this->assertEquals($expected, $recordSet); } public function testIterator() { $unionList = $this->unionList; $expected = [ 0 => 'test obj 1', 1 => 'test obj 2', 2 => 'test obj 3', 3 => 'test obj 4', ]; $counter = 0; foreach ($unionList as $i => $record) { // enforce numeric keys $this->assertEquals($counter, $i); $this->assertEquals($expected[$i], $record->Name); ++$counter; } } public function testColumn() { $unionList = $this->unionList; $expected = [ 0 => 'test obj 1', 1 => 'test obj 2', 2 => 'test obj 3', 3 => 'test obj 4', ]; $this->assertEquals($expected, $unionList->column('Name')); } public function testCount() { $unionList = $this->unionList; $this->assertEquals(4, $unionList->count()); } } |