Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC735Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 use Doctrine\Common\Collections\ArrayCollection;
6
7 require_once __DIR__ . '/../../../TestInit.php';
8
9 class DDC735Test extends \Doctrine\Tests\OrmFunctionalTestCase
10 {
11     protected function setUp()
12     {
13         parent::setUp();
14         try {
15             $this->_schemaTool->createSchema(array(
16                 $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC735Product'),
17                 $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC735Review')
18             ));
19         } catch(\Exception $e) {
20
21         }
22     }
23
24     public function testRemoveElement_AppliesOrphanRemoval()
25     {
26         // Create a product and its first review
27         $product = new DDC735Product;
28         $review  = new DDC735Review($product);
29
30         // Persist and flush
31         $this->_em->persist($product);
32         $this->_em->flush();
33
34         // Now you see it
35         $this->assertEquals(1, count($product->getReviews()));
36
37         // Remove the review
38         $reviewId = $review->getId();
39         $product->removeReview($review);
40         $this->_em->flush();
41
42         // Now you don't
43         $this->assertEquals(0, count($product->getReviews()), 'count($reviews) should be 0 after removing its only Review');
44
45         // Refresh
46         $this->_em->refresh($product);
47
48         // It should still be 0
49         $this->assertEquals(0, count($product->getReviews()), 'count($reviews) should still be 0 after the refresh');
50
51         // Review should also not be available anymore
52         $this->assertNull($this->_em->find(__NAMESPACE__.'\DDC735Review', $reviewId));
53     }
54 }
55
56 /**
57  * @Entity
58  */
59 class DDC735Product
60 {
61     /**
62      * @Id @Column(type="integer") @GeneratedValue
63      */
64     protected $id;
65
66     /**
67      * @OneToMany(
68      *   targetEntity="DDC735Review",
69      *   mappedBy="product",
70      *   cascade={"persist"},
71      *   orphanRemoval=true
72      * )
73      */
74     protected $reviews;
75
76     public function __construct()
77     {
78         $this->reviews = new ArrayCollection;
79     }
80
81     public function getReviews()
82     {
83         return $this->reviews;
84     }
85
86     public function addReview(DDC735Review $review)
87     {
88         $this->reviews->add($review);
89     }
90
91     public function removeReview(DDC735Review $review)
92     {
93         $this->reviews->removeElement($review);
94     }
95 }
96
97 /**
98  * @Entity
99  */
100 class DDC735Review
101 {
102     /**
103      * @Id @Column(type="integer") @GeneratedValue
104      */
105     protected $id;
106
107     /**
108      * @ManyToOne(targetEntity="DDC735Product", inversedBy="reviews")
109      */
110     protected $product;
111
112     public function __construct(DDC735Product $product)
113     {
114         $this->product = $product;
115         $product->addReview($this);
116     }
117
118     public function getId()
119     {
120         return $this->id;
121     }
122 }