[2.0] added new bidirectional one-many association test (affects #2276)
This commit is contained in:
parent
01147039d4
commit
1e0589928a
62
tests/Doctrine/Tests/Models/ECommerce/ECommerceFeature.php
Normal file
62
tests/Doctrine/Tests/Models/ECommerce/ECommerceFeature.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?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
|
||||
*/
|
||||
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"})
|
||||
* @JoinTable(name="ecommerce_products_categories",
|
||||
@ -32,12 +43,6 @@ class ECommerceProduct
|
||||
private $categories;
|
||||
*/
|
||||
|
||||
/**
|
||||
* @OneToOne(targetEntity="ECommerceShipping", cascade={"save"})
|
||||
* @JoinColumn(name="shipping_id", referencedColumnName="id")
|
||||
*/
|
||||
private $shipping;
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
@ -53,16 +58,6 @@ class ECommerceProduct
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function getPrice()
|
||||
{
|
||||
return $this->price;
|
||||
}
|
||||
|
||||
public function setPrice($price)
|
||||
{
|
||||
$this->price = $price;
|
||||
}
|
||||
|
||||
public function getShipping()
|
||||
{
|
||||
return $this->shipping;
|
||||
@ -77,4 +72,30 @@ class ECommerceProduct
|
||||
{
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,90 @@
|
||||
<?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
|
||||
'Doctrine\Tests\Models\ECommerce\ECommerceCart',
|
||||
'Doctrine\Tests\Models\ECommerce\ECommerceCustomer',
|
||||
'Doctrine\Tests\Models\ECommerce\ECommerceProduct',
|
||||
'Doctrine\Tests\Models\ECommerce\ECommerceShipping'
|
||||
'Doctrine\Tests\Models\ECommerce\ECommerceShipping',
|
||||
'Doctrine\Tests\Models\ECommerce\ECommerceFeature'
|
||||
),
|
||||
'generic' => array(
|
||||
'Doctrine\Tests\Models\Generic\DateTimeModel'
|
||||
@ -79,6 +80,7 @@ class OrmFunctionalTestCase extends OrmTestCase
|
||||
$conn->exec('DELETE FROM ecommerce_customers');
|
||||
$conn->exec('DELETE FROM ecommerce_products');
|
||||
$conn->exec('DELETE FROM ecommerce_shippings');
|
||||
$conn->exec('DELETE FROM ecommerce_features');
|
||||
}
|
||||
if (isset($this->_usedModelSets['company'])) {
|
||||
$conn->exec('DELETE FROM company_persons_friends');
|
||||
|
Loading…
x
Reference in New Issue
Block a user