1
0
mirror of synced 2025-01-18 22:41:43 +03:00
doctrine2/tests/Doctrine/Tests/ORM/Functional/OneToOneAssociationTest.php

71 lines
2.2 KiB
PHP
Raw Normal View History

2009-07-01 09:18:08 +00:00
<?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);
}
}