3 namespace Doctrine\Tests\ORM\Functional;
5 use Doctrine\Tests\Models\ECommerce\ECommerceCart,
6 Doctrine\Tests\Models\ECommerce\ECommerceFeature,
7 Doctrine\Tests\Models\ECommerce\ECommerceCustomer,
8 Doctrine\Tests\Models\ECommerce\ECommerceProduct;
10 use Doctrine\ORM\Mapping\AssociationMapping;
12 require_once __DIR__ . '/../../TestInit.php';
15 * Tests capabilities of the persister.
16 * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
18 class StandardEntityPersisterTest extends \Doctrine\Tests\OrmFunctionalTestCase
20 protected function setUp()
22 $this->useModelSet('ecommerce');
26 public function testAcceptsForeignKeysAsCriteria()
28 $customer = new ECommerceCustomer();
29 $customer->setName('John Doe');
30 $cart = new ECommerceCart();
31 $cart->setPayment('Credit card');
32 $customer->setCart($cart);
33 $this->_em->persist($customer);
36 $cardId = $cart->getId();
39 $class = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCart');
41 $persister = $this->_em->getUnitOfWork()->getEntityPersister('Doctrine\Tests\Models\ECommerce\ECommerceCart');
42 $newCart = new ECommerceCart();
43 $this->_em->getUnitOfWork()->registerManaged($newCart, array('id' => $cardId), array());
44 $persister->load(array('customer_id' => $customer->getId()), $newCart, $class->associationMappings['customer']);
45 $this->assertEquals('Credit card', $newCart->getPayment());
49 * Ticket #2478 from Damon Jones (dljones)
51 public function testAddPersistRetrieve()
53 $f1 = new ECommerceFeature;
54 $f1->setDescription('AC-3');
56 $f2 = new ECommerceFeature;
57 $f2->setDescription('DTS');
59 $p = new ECommerceProduct;
62 $this->_em->persist($p);
66 $this->assertEquals(2, count($p->getFeatures()));
67 $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $p->getFeatures());
69 $q = $this->_em->createQuery(
71 FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p
75 $res = $q->getResult();
77 $this->assertEquals(2, count($p->getFeatures()));
78 $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $p->getFeatures());
80 // Check that the features are the same instances still
81 foreach ($p->getFeatures() as $feature) {
82 if ($feature->getDescription() == 'AC-3') {
83 $this->assertTrue($feature === $f1);
85 $this->assertTrue($feature === $f2);
89 // Now we test how Hydrator affects IdentityMap
90 // (change from ArrayCollection to PersistentCollection)
91 $f3 = new ECommerceFeature();
92 $f3->setDescription('XVID');
95 // Now we persist the Feature #3
96 $this->_em->persist($p);
99 $q = $this->_em->createQuery(
101 FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p
105 $res = $q->getResult();
107 // Persisted Product now must have 3 Feature items
108 $this->assertEquals(3, count($res[0]->getFeatures()));