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

[2.0] added new unidirectional one-one association test

This commit is contained in:
piccoloprincipe 2009-07-01 12:03:41 +00:00
parent b592e44cf6
commit 8ccb7df1bb
7 changed files with 219 additions and 2 deletions

View File

@ -29,6 +29,14 @@ class ECommerceCart
* @JoinColumn(name="customer_id", referencedColumnName="id") * @JoinColumn(name="customer_id", referencedColumnName="id")
*/ */
private $customer; private $customer;
/**
* @ManyToMany(targetEntity="ECommerceProduct", cascade={"save"})
* @JoinTable(name="ecommerce_carts_products",
joinColumns={{"name"="cart_id", "referencedColumnName"="id"}},
inverseJoinColumns={{"name"="product_id", "referencedColumnName"="id"}})
*/
private $products;
public function getId() { public function getId() {
return $this->id; return $this->id;
@ -62,4 +70,5 @@ class ECommerceCart
public function getCustomer() { public function getCustomer() {
return $this->customer; return $this->customer;
} }
} }

View File

@ -41,6 +41,11 @@ class ECommerceCustomer
$this->name = $name; $this->name = $name;
} }
/**
* @OneToMany(targetEntity="ECommerceProduct")
public $watched;
*/
public function setCart(ECommerceCart $cart) public function setCart(ECommerceCart $cart)
{ {
if ($this->cart !== $cart) { if ($this->cart !== $cart) {

View File

@ -0,0 +1,80 @@
<?php
namespace Doctrine\Tests\Models\ECommerce;
/**
* ECommerceProduct
* Represents a type of product of a shopping application.
*
* @author Giorgio Sironi
* @Entity
* @Table(name="ecommerce_products")
*/
class ECommerceProduct
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @Column(type="string", length=50)
*/
private $name;
/**
* @ManyToMany(targetEntity="ECommerceCategory", cascade={"save"})
* @JoinTable(name="ecommerce_products_categories",
joinColumns={{"name"="product_id", "referencedColumnName"="id"}},
inverseJoinColumns={{"name"="category_id", "referencedColumnName"="id"}})
private $categories;
*/
/**
* @OneToOne(targetEntity="ECommerceShipping", cascade={"save"})
* @JoinColumn(name="shipping_id", referencedColumnName="id")
*/
private $shipping;
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getPrice()
{
return $this->price;
}
public function setPrice($price)
{
$this->price = $price;
}
public function getShipping()
{
return $this->shipping;
}
public function setShipping(ECommerceShipping $shipping)
{
$this->shipping = $shipping;
}
public function removeShipping()
{
$this->shipping = null;
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Doctrine\Tests\Models\ECommerce;
/**
* ECommerceShipping
* Represents a shipping method.
*
* @author Giorgio Sironi
* @Entity
* @Table(name="ecommerce_shippings")
*/
class ECommerceShipping
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @Column(type="integer")
*/
private $days;
public function getId()
{
return $this->id;
}
public function getDays()
{
return $this->days;
}
public function setDays($days)
{
$this->days = $days;
}
}

View File

@ -26,6 +26,7 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\DetachedEntityTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\DetachedEntityTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\QueryCacheTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\QueryCacheTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\QueryTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\QueryTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneUnidirectionalAssociationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneBidirectionalAssociationTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Functional\OneToOneBidirectionalAssociationTest');
return $suite; return $suite;
@ -34,4 +35,4 @@ class AllTests
if (PHPUnit_MAIN_METHOD == 'Orm_Functional_AllTests::main') { if (PHPUnit_MAIN_METHOD == 'Orm_Functional_AllTests::main') {
AllTests::main(); AllTests::main();
} }

View File

@ -0,0 +1,77 @@
<?php
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
use Doctrine\Tests\Models\ECommerce\ECommerceShipping;
require_once __DIR__ . '/../../TestInit.php';
/**
* Tests a unidirectional one-to-one association mapping (without inheritance).
* Inverse side is not present.
*/
class OneToOneUnidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
private $product;
private $shipping;
protected function setUp()
{
$this->useModelSet('ecommerce');
parent::setUp();
$this->product = new ECommerceProduct();
$this->product->setName('Doctrine 2 Manual');
$this->shipping = new ECommerceShipping();
$this->shipping->setDays('5');
}
public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
$this->product->setShipping($this->shipping);
$this->_em->save($this->product);
$this->assertForeignKeyIs($this->shipping->getId());
}
public function testRemovesOneToOneAssociation()
{
$this->product->setShipping($this->shipping);
$this->_em->save($this->product);
$this->product->removeShipping();
$this->_em->flush();
$this->assertForeignKeyIs(null);
}
public function testEagerLoad()
{
$product = new ECommerceProduct;
$product->setName('Php manual');
$shipping = new ECommerceShipping;
$shipping->setDays('1');
$product->setShipping($shipping);
$this->_em->save($product);
$this->_em->flush();
$this->_em->clear();
$query = $this->_em->createQuery('select p, s from Doctrine\Tests\Models\ECommerce\ECommerceProduct p left join p.shipping s');
$result = $query->getResultList();
$product = $result[0];
$this->assertTrue($product->getShipping() instanceof ECommerceShipping);
$this->assertEquals(1, $product->getShipping()->getDays());
}
/* TODO: not yet implemented
public function testLazyLoad() {
}*/
public function assertForeignKeyIs($value) {
$foreignKey = $this->_em->getConnection()->execute('SELECT shipping_id FROM ecommerce_products WHERE id=?', array($this->product->getId()))->fetchColumn();
$this->assertEquals($value, $foreignKey);
}
}

View File

@ -46,7 +46,9 @@ class OrmFunctionalTestCase extends OrmTestCase
), ),
'ecommerce' => array( 'ecommerce' => array(
'Doctrine\Tests\Models\ECommerce\ECommerceCart', 'Doctrine\Tests\Models\ECommerce\ECommerceCart',
'Doctrine\Tests\Models\ECommerce\ECommerceCustomer' 'Doctrine\Tests\Models\ECommerce\ECommerceCustomer',
'Doctrine\Tests\Models\ECommerce\ECommerceProduct',
'Doctrine\Tests\Models\ECommerce\ECommerceShipping'
), ),
'generic' => array( 'generic' => array(
'Doctrine\Tests\Models\Generic\DateTimeModel' 'Doctrine\Tests\Models\Generic\DateTimeModel'
@ -75,6 +77,8 @@ class OrmFunctionalTestCase extends OrmTestCase
if (isset($this->_usedModelSets['ecommerce'])) { if (isset($this->_usedModelSets['ecommerce'])) {
$conn->exec('DELETE FROM ecommerce_carts'); $conn->exec('DELETE FROM ecommerce_carts');
$conn->exec('DELETE FROM ecommerce_customers'); $conn->exec('DELETE FROM ecommerce_customers');
$conn->exec('DELETE FROM ecommerce_products');
$conn->exec('DELETE FROM ecommerce_shippings');
} }
if (isset($this->_usedModelSets['company'])) { if (isset($this->_usedModelSets['company'])) {
$conn->exec('DELETE FROM company_persons_friends'); $conn->exec('DELETE FROM company_persons_friends');