function testUnshift() { $list = DataObjectTest_TeamComment::get(); $map = new SS_Map($list, 'Name', 'Comment'); $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)'); $this->assertEquals('(All)', $map[-1]); $this->assertEquals('(Select)', $map[0]); $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()); }
public function testIterationWithPush() { $list = DataObjectTest_TeamComment::get()->sort('ID'); $map = new SS_Map($list, 'Name', 'Comment'); $map->push(1, 'Pushed'); $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 testReverse() { $list = DataObjectTest_TeamComment::get(); $list = $list->sort('Name'); $list = $list->reverse(); $this->assertEquals('Bob', $list->last()->Name, 'Last comment should be from Bob'); $this->assertEquals('Phil', $list->first()->Name, 'First comment should be from Phil'); }
/** * Test has many relationships * - Test getComponents() gets the ComponentSet of the other side of the relation * - Test the IDs on the DataObjects are set correctly */ public function testHasManyRelationships() { $team = $this->objFromFixture('DataObjectTest_Team', 'team1'); // Test getComponents() gets the ComponentSet of the other side of the relation $this->assertTrue($team->Comments()->Count() == 2); // Test the IDs on the DataObjects are set correctly foreach ($team->Comments() as $comment) { $this->assertEquals($team->ID, $comment->TeamID); } // Test that we can add and remove items that already exist in the database $newComment = new DataObjectTest_TeamComment(); $newComment->Name = "Automated commenter"; $newComment->Comment = "This is a new comment"; $newComment->write(); $team->Comments()->add($newComment); $this->assertEquals($team->ID, $newComment->TeamID); $comment1 = $this->fixture->objFromFixture('DataObjectTest_TeamComment', 'comment1'); $comment2 = $this->fixture->objFromFixture('DataObjectTest_TeamComment', 'comment2'); $team->Comments()->remove($comment2); $commentIDs = $team->Comments()->sort('ID')->column('ID'); $this->assertEquals(array($comment1->ID, $newComment->ID), $commentIDs); }
/** * Test has many relationships * - Test getComponents() gets the ComponentSet of the other side of the relation * - Test the IDs on the DataObjects are set correctly */ public function testHasManyRelationships() { $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1'); // Test getComponents() gets the ComponentSet of the other side of the relation $this->assertTrue($team1->Comments()->Count() == 2); $team1Comments = [['Comment' => 'This is a team comment by Joe'], ['Comment' => 'This is a team comment by Bob']]; // Test the IDs on the DataObjects are set correctly $this->assertDOSEquals($team1Comments, $team1->Comments()); // Test that has_many can be infered from the has_one via getNonReciprocalComponent $this->assertDOSEquals($team1Comments, $team1->inferReciprocalComponent('DataObjectTest_TeamComment', 'Team')); // Test that we can add and remove items that already exist in the database $newComment = new DataObjectTest_TeamComment(); $newComment->Name = "Automated commenter"; $newComment->Comment = "This is a new comment"; $newComment->write(); $team1->Comments()->add($newComment); $this->assertEquals($team1->ID, $newComment->TeamID); $comment1 = $this->objFromFixture('DataObjectTest_TeamComment', 'comment1'); $comment2 = $this->objFromFixture('DataObjectTest_TeamComment', 'comment2'); $team1->Comments()->remove($comment2); $team1CommentIDs = $team1->Comments()->sort('ID')->column('ID'); $this->assertEquals(array($comment1->ID, $newComment->ID), $team1CommentIDs); // Test that removing an item from a list doesn't remove it from the same // relation belonging to a different object $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1'); $team2 = $this->objFromFixture('DataObjectTest_Team', 'team2'); $team2->Comments()->remove($comment1); $team1CommentIDs = $team1->Comments()->sort('ID')->column('ID'); $this->assertEquals(array($comment1->ID, $newComment->ID), $team1CommentIDs); }