X-Git-Url: http://git.inspyration.org/?a=blobdiff_plain;f=vendor%2Fdoctrine%2Form%2Ftests%2FDoctrine%2FTests%2FORM%2FFunctional%2FOneToOneBidirectionalAssociationTest.php;fp=vendor%2Fdoctrine%2Form%2Ftests%2FDoctrine%2FTests%2FORM%2FFunctional%2FOneToOneBidirectionalAssociationTest.php;h=ced524c45c265c914ae1a65cde931da9235d5e54;hb=8b04b2d11798dee4f3e1358e4f43e97a6df851f6;hp=0000000000000000000000000000000000000000;hpb=73568cf05a785a45f94ca3f2351d9e07bf917958;p=zf2.biz%2Fapplication_blanche.git diff --git a/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Functional/OneToOneBidirectionalAssociationTest.php b/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Functional/OneToOneBidirectionalAssociationTest.php new file mode 100644 index 0000000..ced524c --- /dev/null +++ b/vendor/doctrine/orm/tests/Doctrine/Tests/ORM/Functional/OneToOneBidirectionalAssociationTest.php @@ -0,0 +1,151 @@ +useModelSet('ecommerce'); + parent::setUp(); + $this->customer = new ECommerceCustomer(); + $this->customer->setName('John Doe'); + $this->cart = new ECommerceCart(); + $this->cart->setPayment('Credit card'); + } + + public function testSavesAOneToOneAssociationWithCascadeSaveSet() { + $this->customer->setCart($this->cart); + $this->_em->persist($this->customer); + $this->_em->flush(); + + $this->assertCartForeignKeyIs($this->customer->getId()); + } + + public function testDoesNotSaveAnInverseSideSet() { + $this->customer->brokenSetCart($this->cart); + $this->_em->persist($this->customer); + $this->_em->flush(); + + $this->assertCartForeignKeyIs(null); + } + + public function testRemovesOneToOneAssociation() + { + $this->customer->setCart($this->cart); + $this->_em->persist($this->customer); + $this->customer->removeCart(); + + $this->_em->flush(); + + $this->assertCartForeignKeyIs(null); + } + + public function testEagerLoad() + { + $this->_createFixture(); + + $query = $this->_em->createQuery('select c, ca from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c join c.cart ca'); + $result = $query->getResult(); + $customer = $result[0]; + + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCart', $customer->getCart()); + $this->assertEquals('paypal', $customer->getCart()->getPayment()); + } + + public function testLazyLoadsObjectsOnTheOwningSide() { + $this->_createFixture(); + $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCart'); + $metadata->associationMappings['customer']['fetchMode'] = ClassMetadata::FETCH_LAZY; + + $query = $this->_em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCart c'); + $result = $query->getResult(); + $cart = $result[0]; + + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCustomer', $cart->getCustomer()); + $this->assertEquals('Giorgio', $cart->getCustomer()->getName()); + } + + public function testInverseSideIsNeverLazy() + { + $this->_createFixture(); + $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCustomer'); + $metadata->associationMappings['mentor']['fetch'] = ClassMetadata::FETCH_EAGER; + + $query = $this->_em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c'); + $result = $query->getResult(); + $customer = $result[0]; + + $this->assertNull($customer->getMentor()); + $this->assertInstanceOF('Doctrine\Tests\Models\ECommerce\ECommerceCart', $customer->getCart()); + $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $customer->getCart()); + $this->assertEquals('paypal', $customer->getCart()->getPayment()); + } + + public function testUpdateWithProxyObject() + { + $cust = new ECommerceCustomer; + $cust->setName('Roman'); + $cart = new ECommerceCart; + $cart->setPayment('CARD'); + $cust->setCart($cart); + + $this->_em->persist($cust); + $this->_em->flush(); + $this->_em->clear(); + + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCart', $cust->getCart()); + $this->assertEquals('Roman', $cust->getName()); + $this->assertSame($cust, $cart->getCustomer()); + + $query = $this->_em->createQuery('select ca from Doctrine\Tests\Models\ECommerce\ECommerceCart ca where ca.id =?1'); + $query->setParameter(1, $cart->getId()); + + $cart2 = $query->getSingleResult(); + + $cart2->setPayment('CHEQUE'); + + $this->_em->flush(); + $this->_em->clear(); + + $query2 = $this->_em->createQuery('select ca, c from Doctrine\Tests\Models\ECommerce\ECommerceCart ca left join ca.customer c where ca.id =?1'); + $query2->setParameter(1, $cart->getId()); + + $cart3 = $query2->getSingleResult(); + + $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCustomer', $cart3->getCustomer()); + $this->assertEquals('Roman', $cart3->getCustomer()->getName()); + } + + protected function _createFixture() + { + $customer = new ECommerceCustomer; + $customer->setName('Giorgio'); + $cart = new ECommerceCart; + $cart->setPayment('paypal'); + $customer->setCart($cart); + + $this->_em->persist($customer); + + $this->_em->flush(); + $this->_em->clear(); + } + + public function assertCartForeignKeyIs($value) { + $foreignKey = $this->_em->getConnection()->executeQuery('SELECT customer_id FROM ecommerce_carts WHERE id=?', array($this->cart->getId()))->fetchColumn(); + $this->assertEquals($value, $foreignKey); + } +}