/** * Test has many relationships against polymorphic has_one fields * - Test getComponents() gets the ComponentSet of the other side of the relation * - Test the IDs on the DataObjects are set correctly */ public function testHasManyPolymorphicRelationships() { $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1'); // Test getComponents() gets the ComponentSet of the other side of the relation $this->assertTrue($team1->Fans()->Count() == 2); // Test the IDs/Classes on the DataObjects are set correctly foreach ($team1->Fans() as $fan) { $this->assertEquals($team1->ID, $fan->FavouriteID, 'Fan has the correct FavouriteID'); $this->assertEquals('DataObjectTest_Team', $fan->FavouriteClass, 'Fan has the correct FavouriteClass'); } // Test that we can add and remove items that already exist in the database $newFan = new DataObjectTest_Fan(); $newFan->Name = "New fan"; $newFan->write(); $team1->Fans()->add($newFan); $this->assertEquals($team1->ID, $newFan->FavouriteID, 'Newly created fan has the correct FavouriteID'); $this->assertEquals('DataObjectTest_Team', $newFan->FavouriteClass, 'Newly created fan has the correct FavouriteClass'); $fan1 = $this->objFromFixture('DataObjectTest_Fan', 'fan1'); $fan3 = $this->objFromFixture('DataObjectTest_Fan', 'fan3'); $team1->Fans()->remove($fan3); $team1FanIDs = $team1->Fans()->sort('ID')->column('ID'); $this->assertEquals(array($fan1->ID, $newFan->ID), $team1FanIDs); // 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'); $player1 = $this->objFromFixture('DataObjectTest_Player', 'player1'); $player1->Fans()->remove($fan1); $team1FanIDs = $team1->Fans()->sort('ID')->column('ID'); $this->assertEquals(array($fan1->ID, $newFan->ID), $team1FanIDs); }