2011-10-29 03:04:17 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\ORM\Map;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Team;
|
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\TeamComment;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
class MapTest extends SapphireTest {
|
2016-05-25 07:09:29 +02:00
|
|
|
|
2011-10-29 03:04:17 +02:00
|
|
|
// Borrow the model from DataObjectTest
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'DataObjectTest.yml';
|
2011-10-29 03:04:17 +02:00
|
|
|
|
2016-11-13 08:35:43 +01:00
|
|
|
protected function getExtraDataObjects()
|
|
|
|
{
|
|
|
|
return array_merge(
|
|
|
|
DataObjectTest::$extra_data_objects,
|
|
|
|
ManyManyListTest::$extra_data_objects
|
|
|
|
);
|
2016-05-25 07:09:29 +02:00
|
|
|
}
|
|
|
|
|
2013-11-16 01:05:30 +01:00
|
|
|
public function testValues() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = TeamComment::get()->sort('Name');
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map($list, 'Name', 'Comment');
|
2013-11-16 01:05:30 +01:00
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'This is a team comment by Bob',
|
|
|
|
'This is a team comment by Joe',
|
|
|
|
'Phil is a unique guy, and comments on team2'
|
|
|
|
), $map->values());
|
|
|
|
|
|
|
|
|
|
|
|
$map->push('Push', 'Item');
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'This is a team comment by Bob',
|
|
|
|
'This is a team comment by Joe',
|
|
|
|
'Phil is a unique guy, and comments on team2',
|
|
|
|
'Item'
|
|
|
|
), $map->values());
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map(new ArrayList());
|
2013-11-16 01:05:30 +01:00
|
|
|
$map->push('Push', 'Pushed value');
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'Pushed value'
|
|
|
|
), $map->values());
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map(new ArrayList());
|
2013-11-16 01:05:30 +01:00
|
|
|
$map->unshift('Unshift', 'Unshift item');
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'Unshift item'
|
|
|
|
), $map->values());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testArrayAccess() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = TeamComment::get();
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map($list, 'Name', 'Comment');
|
2011-10-29 03:04:17 +02:00
|
|
|
$this->assertEquals('This is a team comment by Joe', $map['Joe']);
|
|
|
|
$this->assertNull($map['DoesntExist']);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testIteration() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = TeamComment::get()->sort('ID');
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map($list, 'Name', 'Comment');
|
2011-10-29 03:04:17 +02:00
|
|
|
$text = "";
|
|
|
|
foreach($map as $k => $v) {
|
|
|
|
$text .= "$k: $v\n";
|
|
|
|
}
|
|
|
|
$this->assertEquals("Joe: This is a team comment by Joe\n"
|
|
|
|
. "Bob: This is a team comment by Bob\n"
|
|
|
|
. "Phil: Phil is a unique guy, and comments on team2\n", $text);
|
|
|
|
}
|
2016-05-25 07:09:29 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDefaultConfigIsIDAndTitle() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = Team::get();
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map($list);
|
2016-11-13 08:35:43 +01:00
|
|
|
$this->assertEquals('Team 1', $map[$this->idFromFixture(DataObjectTest\Team::class, 'team1')]);
|
2011-10-29 03:04:17 +02:00
|
|
|
}
|
2016-05-25 07:09:29 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetKeyFieldAndValueField() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = TeamComment::get();
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map($list);
|
2011-10-29 03:04:17 +02:00
|
|
|
$map->setKeyField('Name');
|
|
|
|
$map->setValueField('Comment');
|
|
|
|
$this->assertEquals('This is a team comment by Joe', $map['Joe']);
|
|
|
|
}
|
2016-05-25 07:09:29 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testToArray() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = TeamComment::get();
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map($list, 'Name', 'Comment');
|
2011-10-29 03:04:17 +02:00
|
|
|
$this->assertEquals(array("Joe" => "This is a team comment by Joe",
|
|
|
|
"Bob" => "This is a team comment by Bob",
|
|
|
|
"Phil" => "Phil is a unique guy, and comments on team2"), $map->toArray());
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testKeys() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = TeamComment::get()->sort('Name');
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map($list, 'Name', 'Comment');
|
2012-02-10 23:44:04 +01:00
|
|
|
$this->assertEquals(array(
|
|
|
|
'Bob',
|
2012-02-16 00:19:33 +01:00
|
|
|
'Joe',
|
2012-02-10 23:44:04 +01:00
|
|
|
'Phil'
|
|
|
|
), $map->keys());
|
2013-11-16 01:05:30 +01:00
|
|
|
|
|
|
|
$map->unshift('Unshift', 'Item');
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'Unshift',
|
|
|
|
'Bob',
|
|
|
|
'Joe',
|
|
|
|
'Phil'
|
|
|
|
), $map->keys());
|
|
|
|
|
|
|
|
$map->push('Push', 'Item');
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'Unshift',
|
|
|
|
'Bob',
|
|
|
|
'Joe',
|
|
|
|
'Phil',
|
|
|
|
'Push'
|
|
|
|
), $map->keys());
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map(new ArrayList());
|
2013-11-16 01:05:30 +01:00
|
|
|
$map->push('Push', 'Item');
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'Push'
|
|
|
|
), $map->keys());
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map(new ArrayList());
|
2013-11-16 01:05:30 +01:00
|
|
|
$map->unshift('Unshift', 'Item');
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'Unshift'
|
|
|
|
), $map->keys());
|
2012-02-10 23:44:04 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testMethodAsValueField() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = Team::get()->sort('Title');
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map($list, 'ID', 'MyTitle');
|
2012-03-15 05:42:42 +01:00
|
|
|
$this->assertEquals(array(
|
|
|
|
'Team Subteam 1',
|
|
|
|
'Team Subteam 2',
|
|
|
|
'Team Subteam 3',
|
|
|
|
'Team Team 1',
|
|
|
|
'Team Team 2',
|
|
|
|
'Team Team 3'
|
|
|
|
), $map->values());
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUnshift() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = TeamComment::get();
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map($list, 'Name', 'Comment');
|
2011-10-29 03:04:17 +02:00
|
|
|
|
|
|
|
$map->unshift(-1, '(All)');
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
-1 => "(All)",
|
|
|
|
"Joe" => "This is a team comment by Joe",
|
|
|
|
"Bob" => "This is a team comment by Bob",
|
|
|
|
"Phil" => "Phil is a unique guy, and comments on team2"), $map->toArray());
|
|
|
|
|
|
|
|
$map->unshift(0, '(Select)');
|
2016-05-25 07:09:29 +02:00
|
|
|
|
2011-10-29 03:04:17 +02:00
|
|
|
$this->assertEquals('(All)', $map[-1]);
|
|
|
|
$this->assertEquals('(Select)', $map[0]);
|
2016-05-25 07:09:29 +02:00
|
|
|
|
2011-10-29 03:04:17 +02:00
|
|
|
$this->assertEquals(array(
|
|
|
|
0 => "(Select)",
|
|
|
|
-1 => "(All)",
|
|
|
|
"Joe" => "This is a team comment by Joe",
|
|
|
|
"Bob" => "This is a team comment by Bob",
|
|
|
|
"Phil" => "Phil is a unique guy, and comments on team2"), $map->toArray());
|
|
|
|
|
|
|
|
$map->unshift("Bob","Replaced");
|
|
|
|
$this->assertEquals(array(
|
|
|
|
"Bob" => "Replaced",
|
|
|
|
0 => "(Select)",
|
|
|
|
-1 => "(All)",
|
|
|
|
"Joe" => "This is a team comment by Joe",
|
|
|
|
"Phil" => "Phil is a unique guy, and comments on team2"), $map->toArray());
|
|
|
|
|
|
|
|
$map->unshift("Phil","Replaced as well");
|
|
|
|
$this->assertEquals(array(
|
|
|
|
"Phil" => "Replaced as well",
|
|
|
|
"Bob" => "Replaced",
|
|
|
|
0 => "(Select)",
|
|
|
|
-1 => "(All)",
|
|
|
|
"Joe" => "This is a team comment by Joe"), $map->toArray());
|
|
|
|
|
|
|
|
$map->unshift("Joe","Replaced the last one");
|
|
|
|
$this->assertEquals(array(
|
|
|
|
"Joe" => "Replaced the last one",
|
|
|
|
"Phil" => "Replaced as well",
|
|
|
|
"Bob" => "Replaced",
|
|
|
|
0 => "(Select)",
|
|
|
|
-1 => "(All)"), $map->toArray());
|
2013-11-16 01:05:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPush() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = TeamComment::get();
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map($list, 'Name', 'Comment');
|
2011-10-29 03:04:17 +02:00
|
|
|
|
2013-11-16 01:05:30 +01:00
|
|
|
$map->push(1, '(All)');
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
"Joe" => "This is a team comment by Joe",
|
|
|
|
"Bob" => "This is a team comment by Bob",
|
|
|
|
"Phil" => "Phil is a unique guy, and comments on team2",
|
|
|
|
1 => "(All)"
|
|
|
|
), $map->toArray());
|
2011-10-29 03:04:17 +02:00
|
|
|
}
|
2016-05-25 07:09:29 +02:00
|
|
|
|
2013-11-16 01:05:30 +01:00
|
|
|
public function testCount() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = TeamComment::get();
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map($list, 'Name', 'Comment');
|
2013-11-16 01:05:30 +01:00
|
|
|
|
|
|
|
$this->assertEquals(3, $map->count());
|
|
|
|
|
|
|
|
// pushing a new item should update the count
|
|
|
|
$map->push(1, 'Item pushed');
|
|
|
|
$this->assertEquals(4, $map->count());
|
|
|
|
|
|
|
|
$map->unshift(2, 'Item shifted');
|
|
|
|
$this->assertEquals(5, $map->count());
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map(new ArrayList());
|
2013-11-16 01:05:30 +01:00
|
|
|
$map->unshift('1', 'shifted');
|
|
|
|
|
|
|
|
$this->assertEquals(1, $map->count());
|
|
|
|
|
|
|
|
unset($map[1]);
|
|
|
|
$this->assertEquals(0, $map->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIterationWithUnshift() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = TeamComment::get()->sort('ID');
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map($list, 'Name', 'Comment');
|
2013-11-16 01:05:30 +01:00
|
|
|
$map->unshift(1, 'Unshifted');
|
|
|
|
|
|
|
|
$text = "";
|
|
|
|
|
|
|
|
foreach($map as $k => $v) {
|
|
|
|
$text .= "$k: $v\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals("1: Unshifted\n"
|
|
|
|
. "Joe: This is a team comment by Joe\n"
|
|
|
|
. "Bob: This is a team comment by Bob\n"
|
|
|
|
. "Phil: Phil is a unique guy, and comments on team2\n", $text
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIterationWithPush() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = TeamComment::get()->sort('ID');
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map($list, 'Name', 'Comment');
|
2013-11-16 01:05:30 +01:00
|
|
|
$map->push(1, 'Pushed');
|
2016-05-25 07:09:29 +02:00
|
|
|
|
2013-11-16 01:05:30 +01:00
|
|
|
$text = "";
|
|
|
|
|
|
|
|
foreach($map as $k => $v) {
|
|
|
|
$text .= "$k: $v\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals("Joe: This is a team comment by Joe\n"
|
|
|
|
. "Bob: This is a team comment by Bob\n"
|
|
|
|
. "Phil: Phil is a unique guy, and comments on team2\n"
|
|
|
|
. "1: Pushed\n", $text
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIterationWithEmptyListUnshifted() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map(new ArrayList());
|
2013-11-16 01:05:30 +01:00
|
|
|
$map->unshift('1', 'unshifted');
|
|
|
|
|
|
|
|
$text = "";
|
|
|
|
|
|
|
|
foreach($map as $k => $v) {
|
|
|
|
$text .= "$k: $v\n";
|
|
|
|
}
|
2016-05-25 07:09:29 +02:00
|
|
|
|
2013-11-16 01:05:30 +01:00
|
|
|
$this->assertEquals("1: unshifted\n", $text);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIterationWithEmptyListPushed() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$map = new Map(new ArrayList());
|
2013-11-16 01:05:30 +01:00
|
|
|
$map->push('1', 'pushed');
|
|
|
|
|
|
|
|
$text = "";
|
|
|
|
|
|
|
|
foreach($map as $k => $v) {
|
|
|
|
$text .= "$k: $v\n";
|
|
|
|
}
|
2016-05-25 07:09:29 +02:00
|
|
|
|
2013-11-16 01:05:30 +01:00
|
|
|
$this->assertEquals("1: pushed\n", $text);
|
|
|
|
}
|
2012-03-15 05:42:42 +01:00
|
|
|
}
|