1
0
mirror of synced 2024-12-13 06:46:03 +03:00

[2.0] references #2276

This commit is contained in:
piccoloprincipe 2009-07-01 09:18:08 +00:00
parent 455f877486
commit 4e50792a8d
4 changed files with 154 additions and 2 deletions

View File

@ -0,0 +1,32 @@
<?php
namespace Doctrine\Tests\Models\ECommerce;
/**
* ECommerceCart
* Represents a typical cart of a shopping application.
*
* @author Giorgio Sironi
* @Entity
* @Table(name="ecommerce_carts")
*/
class ECommerceCart
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @Column(type="string", length=50)
*/
public $payment;
/**
* @OneToOne(targetEntity="ECommerceCustomer")
* @JoinColumn(name="customer_id", referencedColumnName="id")
*/
public $customer;
}

View File

@ -0,0 +1,43 @@
<?php
namespace Doctrine\Tests\Models\ECommerce;
/**
* ECommerceCustomer
* Represents a registered user of a shopping application.
*
* @author Giorgio Sironi
* @Entity
* @Table(name="ecommerce_customers")
*/
class ECommerceCustomer
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @Column(type="string", length=50)
*/
public $name;
/**
* @OneToOne(targetEntity="ECommerceCart", mappedBy="customer", cascade={"save"})
*/
public $cart;
public function setCart(ECommerceCart $cart)
{
$this->cart = $cart;
$cart->customer = $this;
}
public function removeCart()
{
$this->cart->customer = null;
$this->cart = null;
}
}

View File

@ -0,0 +1,70 @@
<?php
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
require_once __DIR__ . '/../../TestInit.php';
/**
* Tests association mapping for ECommerceCustomer and ECommerceCart.
* The latter is the owning side of the relation.
*/
class OneToOneAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
public $customer;
public $cart;
protected function setUp()
{
$this->useModelSet('ecommerce');
parent::setUp();
$this->customer = new ECommerceCustomer();
$this->customer->name = 'John Doe';
$this->cart = new ECommerceCart();
$this->cart->payment = 'Credit card';
}
public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
$this->customer->setCart($this->cart);
$this->_em->save($this->customer);
$this->assertCartForeignKeyIs($this->customer->id);
}
public function testDoesNotSaveAnInverseSideSet() {
$this->customer->cart = $this->cart;
$this->_em->save($this->customer);
$this->assertCartForeignKeyIs(null);
}
public function testRemovesOneToOneAssociation()
{
$this->customer->setCart($this->cart);
$this->_em->save($this->customer);
$this->customer->removeCart();
$this->_em->flush();
$this->assertCartForeignKeyIs(null);
}
public function testLoadsAnAssociation()
{
$conn = $this->_em->getConnection();
$conn->execute('INSERT INTO ecommerce_customers (name) VALUES ("Giorgio")');
$customerId = $conn->lastInsertId();
$conn->execute("INSERT INTO ecommerce_carts (customer_id, payment) VALUES ('$customerId', 'paypal')");
$customer = $this->_em->find("Doctrine\Tests\Models\ECommerce\ECommerceCustomer", $customerId);
$this->assertEquals('paypal', $customer->cart->payment);
}
public function assertCartForeignKeyIs($value) {
$foreignKey = $this->_em->getConnection()->execute('SELECT customer_id FROM ecommerce_carts WHERE id=?', array($this->cart->id))->fetchColumn();
$this->assertEquals($value, $foreignKey);
}
}

View File

@ -44,7 +44,10 @@ class OrmFunctionalTestCase extends OrmTestCase
'Doctrine\Tests\Models\Company\CompanyEmployee', 'Doctrine\Tests\Models\Company\CompanyEmployee',
'Doctrine\Tests\Models\Company\CompanyManager' 'Doctrine\Tests\Models\Company\CompanyManager'
), ),
'ecommerce' => array(), 'ecommerce' => array(
'Doctrine\Tests\Models\ECommerce\ECommerceCart',
'Doctrine\Tests\Models\ECommerce\ECommerceCustomer'
),
'generic' => array( 'generic' => array(
'Doctrine\Tests\Models\Generic\DateTimeModel' 'Doctrine\Tests\Models\Generic\DateTimeModel'
) )
@ -69,6 +72,10 @@ class OrmFunctionalTestCase extends OrmTestCase
$conn->exec('DELETE FROM cms_articles'); $conn->exec('DELETE FROM cms_articles');
$conn->exec('DELETE FROM cms_users'); $conn->exec('DELETE FROM cms_users');
} }
if (isset($this->_usedModelSets['ecommerce'])) {
$conn->exec('DELETE FROM ecommerce_carts');
$conn->exec('DELETE FROM ecommerce_customers');
}
if (isset($this->_usedModelSets['company'])) { if (isset($this->_usedModelSets['company'])) {
$conn->exec('DELETE FROM company_persons_friends'); $conn->exec('DELETE FROM company_persons_friends');
$conn->exec('DELETE FROM company_managers'); $conn->exec('DELETE FROM company_managers');
@ -142,4 +149,4 @@ class OrmFunctionalTestCase extends OrmTestCase
return \Doctrine\ORM\EntityManager::create($conn, $config, $eventManager); return \Doctrine\ORM\EntityManager::create($conn, $config, $eventManager);
} }
} }