/**
* Ticket #2478 from Damon Jones (dljones)
*/
public function testAddPersistRetrieve()
{
$f1 = new ECommerceFeature();
$f1->setDescription('AC-3');
$f2 = new ECommerceFeature();
$f2->setDescription('DTS');
$p = new ECommerceProduct();
$p->addFeature($f1);
$p->addfeature($f2);
$this->_em->persist($p);
$this->_em->flush();
$this->assertEquals(2, count($p->getFeatures()));
$this->assertTrue($p->getFeatures() instanceof \Doctrine\ORM\PersistentCollection);
$q = $this->_em->createQuery('SELECT p, f
FROM Doctrine\\Tests\\Models\\ECommerce\\ECommerceProduct p
JOIN p.features f');
$res = $q->getResult();
$this->assertEquals(2, count($p->getFeatures()));
$this->assertTrue($p->getFeatures() instanceof \Doctrine\ORM\PersistentCollection);
// Check that the features are the same instances still
foreach ($p->getFeatures() as $feature) {
if ($feature->getDescription() == 'AC-3') {
$this->assertTrue($feature === $f1);
} else {
$this->assertTrue($feature === $f2);
}
}
// Now we test how Hydrator affects IdentityMap
// (change from ArrayCollection to PersistentCollection)
$f3 = new ECommerceFeature();
$f3->setDescription('XVID');
$p->addFeature($f3);
// Now we persist the Feature #3
$this->_em->persist($p);
$this->_em->flush();
$q = $this->_em->createQuery('SELECT p, f
FROM Doctrine\\Tests\\Models\\ECommerce\\ECommerceProduct p
JOIN p.features f');
$res = $q->getResult();
// Persisted Product now must have 3 Feature items
$this->assertEquals(3, count($res[0]->getFeatures()));
}