Commit 1e058992 authored by piccoloprincipe's avatar piccoloprincipe

[2.0] added new bidirectional one-many association test (affects #2276)

parent 01147039
<?php
namespace Doctrine\Tests\Models\ECommerce;
/**
* Describes a product feature.
*
* @author Giorgio Sironi
* @Entity
* @Table(name="ecommerce_features")
*/
class ECommerceFeature
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @Column(type="string", length=50)
*/
private $description;
/**
* @ManyToOne(targetEntity="ECommerceProduct")
* @JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
public function getId() {
return $this->id;
}
public function getDescription() {
return $this->description;
}
public function setDescription($description) {
$this->description = $description;
}
public function setProduct(ECommerceProduct $product) {
if ($this->product !== $product) {
$this->product = $product;
$product->addFeature($this);
}
}
public function removeProduct() {
if ($this->product !== null) {
$product = $this->product;
$this->product = null;
$product->removeFeature($this);
}
}
public function getProduct() {
return $this->product;
}
}
...@@ -24,6 +24,17 @@ class ECommerceProduct ...@@ -24,6 +24,17 @@ class ECommerceProduct
*/ */
private $name; private $name;
/**
* @OneToOne(targetEntity="ECommerceShipping", cascade={"save"})
* @JoinColumn(name="shipping_id", referencedColumnName="id")
*/
private $shipping;
/**
* @OneToMany(targetEntity="ECommerceFeature", mappedBy="product", cascade={"save"})
*/
private $features;
/** /**
* @ManyToMany(targetEntity="ECommerceCategory", cascade={"save"}) * @ManyToMany(targetEntity="ECommerceCategory", cascade={"save"})
* @JoinTable(name="ecommerce_products_categories", * @JoinTable(name="ecommerce_products_categories",
...@@ -32,12 +43,6 @@ class ECommerceProduct ...@@ -32,12 +43,6 @@ class ECommerceProduct
private $categories; private $categories;
*/ */
/**
* @OneToOne(targetEntity="ECommerceShipping", cascade={"save"})
* @JoinColumn(name="shipping_id", referencedColumnName="id")
*/
private $shipping;
public function getId() public function getId()
{ {
return $this->id; return $this->id;
...@@ -53,16 +58,6 @@ class ECommerceProduct ...@@ -53,16 +58,6 @@ class ECommerceProduct
$this->name = $name; $this->name = $name;
} }
public function getPrice()
{
return $this->price;
}
public function setPrice($price)
{
$this->price = $price;
}
public function getShipping() public function getShipping()
{ {
return $this->shipping; return $this->shipping;
...@@ -77,4 +72,30 @@ class ECommerceProduct ...@@ -77,4 +72,30 @@ class ECommerceProduct
{ {
$this->shipping = null; $this->shipping = null;
} }
public function getFeatures()
{
return $this->features;
}
public function addFeature(ECommerceFeature $feature) {
$this->features[] = $feature;
$feature->setProduct($this);
}
/** does not set the owning side */
public function brokenAddFeature(ECommerceFeature $feature) {
$this->features[] = $feature;
}
public function removeFeature(ECommerceFeature $feature) {
foreach ($this->features as $index => $current) {
if ($current === $feature) {
unset($this->features[$index]);
$current->removeProduct();
return true;
}
}
return false;
}
} }
<?php
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
require_once __DIR__ . '/../../TestInit.php';
/**
* Tests a bidirectional one-to-one association mapping (without inheritance).
*/
class OneToManyBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
private $product;
private $firstFeature;
protected function setUp()
{
$this->useModelSet('ecommerce');
parent::setUp();
$this->product = new ECommerceProduct();
$this->product->setName('Doctrine Cookbook');
$this->firstFeature = new ECommerceFeature();
$this->firstFeature->setDescription('Model writing tutorial');
$this->secondFeature = new ECommerceFeature();
$this->secondFeature->setDescription('Annotations examples');
}
public function testSavesAOneToManyAssociationWithCascadeSaveSet() {
$this->product->addFeature($this->firstFeature);
$this->product->addFeature($this->secondFeature);
$this->_em->save($this->product);
$this->assertFeatureForeignKeyIs($this->product->getId(), $this->firstFeature);
$this->assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature);
}
public function testDoesNotSaveAnInverseSideSet() {
$this->product->brokenAddFeature($this->firstFeature);
$this->_em->save($this->product);
$this->assertFeatureForeignKeyIs(null, $this->firstFeature);
}
public function testRemovesOneToOneAssociation()
{
$this->product->addFeature($this->firstFeature);
$this->product->addFeature($this->secondFeature);
$this->_em->save($this->product);
$this->product->removeFeature($this->firstFeature);
$this->_em->flush();
$this->assertFeatureForeignKeyIs(null, $this->firstFeature);
$this->assertFeatureForeignKeyIs($this->product->getId(), $this->secondFeature);
}
public function testEagerLoadsOneToManyAssociation()
{
$this->product->addFeature($this->firstFeature);
$this->product->addFeature($this->secondFeature);
$this->_em->save($this->product);
$this->_em->flush();
$this->_em->clear();
$query = $this->_em->createQuery('select p, f from Doctrine\Tests\Models\ECommerce\ECommerceProduct p join p.features f');
$result = $query->getResultList();
$product = $result[0];
$features = $product->getFeatures();
$this->assertTrue($features[0] instanceof ECommerceFeature);
$this->assertSame($product, $features[0]->getProduct());
$this->assertEquals('Model writing tutorial', $features[0]->getDescription());
$this->assertTrue($features[1] instanceof ECommerceFeature);
$this->assertSame($product, $features[1]->getProduct());
$this->assertEquals('Annotations examples', $features[1]->getDescription());
}
/* TODO: not yet implemented
public function testLazyLoad() {
}*/
public function assertFeatureForeignKeyIs($value, ECommerceFeature $feature) {
$foreignKey = $this->_em->getConnection()->execute('SELECT product_id FROM ecommerce_features WHERE id=?', array($feature->getId()))->fetchColumn();
$this->assertEquals($value, $foreignKey);
}
}
...@@ -48,7 +48,8 @@ class OrmFunctionalTestCase extends OrmTestCase ...@@ -48,7 +48,8 @@ class OrmFunctionalTestCase extends OrmTestCase
'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\ECommerceProduct',
'Doctrine\Tests\Models\ECommerce\ECommerceShipping' 'Doctrine\Tests\Models\ECommerce\ECommerceShipping',
'Doctrine\Tests\Models\ECommerce\ECommerceFeature'
), ),
'generic' => array( 'generic' => array(
'Doctrine\Tests\Models\Generic\DateTimeModel' 'Doctrine\Tests\Models\Generic\DateTimeModel'
...@@ -79,6 +80,7 @@ class OrmFunctionalTestCase extends OrmTestCase ...@@ -79,6 +80,7 @@ class OrmFunctionalTestCase extends OrmTestCase
$conn->exec('DELETE FROM ecommerce_customers'); $conn->exec('DELETE FROM ecommerce_customers');
$conn->exec('DELETE FROM ecommerce_products'); $conn->exec('DELETE FROM ecommerce_products');
$conn->exec('DELETE FROM ecommerce_shippings'); $conn->exec('DELETE FROM ecommerce_shippings');
$conn->exec('DELETE FROM ecommerce_features');
} }
if (isset($this->_usedModelSets['company'])) { if (isset($this->_usedModelSets['company'])) {
$conn->exec('DELETE FROM company_persons_friends'); $conn->exec('DELETE FROM company_persons_friends');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment