PreUpdateSubscriber2::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ODM\CouchDB\Functional;
4
5
use Doctrine\Common\EventSubscriber;
6
7
class EmbeddedAssociationTest extends \Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCase
8
{
9
    private $userId;
10
    private $groupIds = array();
11
    private $dm;
12
13
    public function setUp()
14
    {
15
        $this->dm = $this->createDocumentManager();
16
17
        $user1 = new \Doctrine\Tests\Models\CMS\CmsUser();
18
        $user1->username = "beberlei";
19
        $user1->status = "active";
20
        $user1->name = "Benjamin";
21
22
        $user2 = new \Doctrine\Tests\Models\CMS\CmsUser();
23
        $user2->username = "lsmith";
24
        $user2->status = "active";
25
        $user2->name = "Lukas";
26
27
        $address1 = new \Doctrine\Tests\Models\CMS\CmsAddress();
28
        $address1->country = "Hungary";
29
        $address1->zip = "1122";
30
        $address1->city = "Budapest";
31
        $address1->mainAddress = true;
32
33
        $user1->setAddress($address1);
34
35
        $this->dm->persist($user1);
36
        $this->dm->persist($user2);
37
38
        $this->dm->flush();
39
        $this->dm->clear();
40
41
        $this->userId = $user1->id;
42
    }
43
44
    public function testShouldNotSaveUnchanged()
45
    {
46
        $listener = new PreUpdateSubscriber;
47
        $this->dm->getEventManager()->addEventListener('preUpdate', $listener);
48
49
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
50
        $this->assertInstanceOf('\Doctrine\Tests\Models\CMS\CmsAddress', $user->address);
51
        $this->assertEquals('Hungary', $user->address->country);
52
        $this->assertEquals('1122', $user->address->zip);
53
        $this->assertEquals('Budapest', $user->address->city);
54
55
        $this->dm->flush();
56
57
        $this->assertCount(0, $listener->eventArgs);
58
    }
59
60
    public function testSaveModifiedEmbedded()
61
    {
62
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
63
        $this->assertInstanceOf('\Doctrine\Tests\Models\CMS\CmsAddress', $user->address);
64
        $this->assertEquals('Hungary', $user->address->country);
65
        $this->assertEquals('1122', $user->address->zip);
66
        $this->assertEquals('Budapest', $user->address->city);
67
68
        $user->address->country = "Spain";
69
        $user->address->zip = "1234";
70
        $user->address->city = "Cartagena";
71
72
        $this->dm->flush();
73
        $this->dm->clear();
74
75
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
76
        $this->assertEquals('Spain', $user->address->country);
77
        $this->assertEquals('1234', $user->address->zip);
78
        $this->assertEquals('Cartagena', $user->address->city);
79
80
    }
81
82
    public function testSaveEmbedded()
83
    {
84
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
85
        $this->assertInstanceOf('\Doctrine\Tests\Models\CMS\CmsAddress', $user->address);
86
        $this->assertEquals('Hungary', $user->address->country);
87
        $this->assertEquals('1122', $user->address->zip);
88
        $this->assertEquals('Budapest', $user->address->city);
89
90
        $address3 = new \Doctrine\Tests\Models\CMS\CmsAddress();
91
        $address3->country = "Spain";
92
        $address3->zip = "1234";
93
        $address3->city = "Cartagena";
94
        $user->setAddress($address3);
95
96
        $this->dm->flush();
97
        $this->dm->clear();
98
99
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
100
        $this->assertEquals('Spain', $user->address->country);
101
        $this->assertEquals('1234', $user->address->zip);
102
        $this->assertEquals('Cartagena', $user->address->city);
103
    }
104
105
    /**
106
     * @group GH-80
107
     */
108
    public function testUpdateBooleanToFalseInEmbeddedDocument()
109
    {
110
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
111
112
        $this->assertTrue($user->address->mainAddress);
113
114
        $user->address->mainAddress = false;
115
116
        $this->dm->flush();
117
        $this->dm->clear();
118
119
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
120
121
        $this->assertFalse($user->address->mainAddress);
122
    }
123
124
    /**
125
     * @group GH-80
126
     */
127 View Code Duplication
    public function testEmptyStringInEmbeddedDocument()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
    {
129
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $this->userId);
130
        $user->address->street = "";
131
132
        $this->dm->flush();
133
        $this->dm->clear();
134
135
        $user = $this->dm->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
136
137
        $this->assertEquals("", $user->address->street);
138
    }
139
}
140
141 View Code Duplication
class PreUpdateSubscriber2 implements EventSubscriber
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
{
143
    public $eventArgs = array();
144
    public function getSubscribedEvents()
145
    {
146
        return array(\Doctrine\ODM\CouchDB\Event::preUpdate);
147
    }
148
149
    public function preUpdate(\Doctrine\ODM\CouchDB\Event\LifecycleEventArgs $args)
150
    {
151
        $this->eventArgs[] = $args;
152
    }
153
154
}
155