Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC767Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 use Doctrine\Common\Collections\ArrayCollection;
6 use Doctrine\Tests\Models\CMS\CmsUser;
7 use Doctrine\Tests\Models\CMS\CmsGroup;
8
9 require_once __DIR__ . '/../../../TestInit.php';
10
11 class DDC767Test extends \Doctrine\Tests\OrmFunctionalTestCase
12 {
13     protected function setUp()
14     {
15         $this->useModelSet('cms');
16         parent::setUp();
17     }
18
19     /**
20      * @group DDC-767
21      */
22     public function testCollectionChangesInsideTransaction()
23     {
24         $user = new CmsUser();
25         $user->name = "beberlei";
26         $user->status = "active";
27         $user->username = "beberlei";
28
29         $group1 = new CmsGroup();
30         $group1->name = "foo";
31
32         $group2 = new CmsGroup();
33         $group2->name = "bar";
34
35         $group3 = new CmsGroup();
36         $group3->name = "baz";
37
38         $user->addGroup($group1);
39         $user->addGroup($group2);
40
41         $this->_em->persist($user);
42         $this->_em->persist($group1);
43         $this->_em->persist($group2);
44         $this->_em->persist($group3);
45
46         $this->_em->flush();
47         $this->_em->clear();
48
49         /* @var $pUser CmsUser */
50         $pUser = $this->_em->find(get_class($user), $user->id);
51
52         $this->assertNotNull($pUser, "User not retrieved from database.");
53
54         $groups = array(2, 3);
55
56         try {
57             $this->_em->beginTransaction();
58
59             $pUser->groups->clear();
60
61             $this->_em->flush();
62
63             // Add new
64             foreach ($groups as $groupId) {
65                 $pUser->addGroup($this->_em->find(get_class($group1), $groupId));
66             }
67
68             $this->_em->flush();
69             $this->_em->commit();
70         } catch(\Exception $e) {
71             $this->_em->rollback();
72         }
73     }
74 }