Completed
Push — master ( 707688...6da863 )
by Mike
04:47 queued 02:08
created

PreUpdateSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Doctrine\Tests\ODM\CouchDB\Functional;
3
4
use Doctrine\Tests\Models\Embedded\Embedded;
5
use Doctrine\Tests\Models\Embedded\Embedder;
6
use Doctrine\Tests\Models\Embedded\Nested;
7
8
class EmbedManyTest extends \Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCase
9
{
10
    private $dm;
11
12
    public function setUp()
13
    {
14
        $this->type = 'Doctrine\Tests\Models\Embedded\Embedder';
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
        $this->embeddedType = 'Doctrine\Tests\Models\Embedded\Embedded';
0 ignored issues
show
Bug introduced by
The property embeddedType does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
        $this->dm = $this->createDocumentManager();
17
18
        $document = new Embedder();
19
        $document->id = 1;
20
        $embedded1 = new Embedded();
21
        $embedded1->name = 'embedded 1';
22
        $embedded2 = new Embedded();
23
        $embedded2->name = 'embedded 2';
24
25
        $document->embeds[] = $embedded1;
26
        $document->embeds[] = $embedded2;
27
28
        $this->dm->persist($document);
29
        $this->dm->flush();
30
    }
31
32
    public function testFind()
33
    {
34
        $embedder = $this->dm->find($this->type, 1);
35
        $this->assertInstanceOf($this->type, $embedder);
36
        $this->assertEquals(2, count($embedder->embeds));
37
        $this->assertEquals('embedded 1', $embedder->embeds[0]->name);
38
        $this->assertEquals('embedded 2', $embedder->embeds[1]->name);
39
    }
40
41
    public function testShouldNotSaveUnchanged()
42
    {
43
        $listener = new PreUpdateSubscriber;
44
        $this->dm->getEventManager()->addEventListener('preUpdate', $listener);
45
46
        $embedder = $this->dm->find($this->type, 1);
0 ignored issues
show
Unused Code introduced by
$embedder is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
        $this->dm->flush();
48
49
        $this->assertEquals(0, count($listener->eventArgs));
50
    }
51
52
    public function testSave()
53
    {
54
        $embedder = $this->dm->find($this->type, 1);
55
        // change the first element
56
        $embedder->embeds[0]->name = 'changed 1';
57
        // add another one
58
        $newOne = new Embedded;
59
        $newOne->name = 'new one';
60
        $embedder->embeds[] = $newOne;
61
        $this->dm->flush();
62
        $this->dm->clear();
63
64
        $embedder = $this->dm->find($this->type, 1);
65
        $this->assertEquals(3, count($embedder->embeds));
66
        $this->assertEquals('new one', $embedder->embeds[2]->name);
67
68
        $embedder->embeds[0]->name = 'changed';
69
        $embedder->name = 'foo';
70
        $embedder->embeds[0]->arrayField[] = 'bar';
71
        $this->dm->flush();
72
        $this->dm->clear();
73
74
        $embedder = $this->dm->find($this->type, 1);
75
        $this->assertEquals(3, count($embedder->embeds));
76
        $this->assertEquals('foo', $embedder->name);
77
        $this->assertEquals('changed', $embedder->embeds[0]->name);
78
        $this->assertEquals(1, count($embedder->embeds[0]->arrayField));
79
        $this->assertEquals('bar', $embedder->embeds[0]->arrayField[0]);
80
81
    }
82
83 View Code Duplication
    public function testCreate()
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...
84
    {
85
        $newOne = new Embedder;
86
        $newOne->id = '2';
87
88
        $embedded1 = new Embedded;
89
        $embedded1->name = 'newly embedded 1';
90
        $embedded2 = new Embedded;
91
        $embedded2->name = 'newly embedded 2';
92
        $newOne->embeds[] = $embedded1;
93
        $newOne->embeds[] = $embedded2;
94
95
        $this->dm->persist($newOne);
96
        $this->dm->flush();
97
        $this->dm->clear();
98
99
        $newOne = null;
100
        $this->assertNull($newOne);
101
        $newOne = $this->dm->find($this->type, 2);
102
        $this->assertNotNull($newOne);
103
        $this->assertEquals(2, count($newOne->embeds));
104
        $this->assertEquals('newly embedded 1', $newOne->embeds[0]->name);
105
        $this->assertEquals('newly embedded 2', $newOne->embeds[1]->name);
106
    }
107
108 View Code Duplication
    public function testAssocCreate()
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...
109
    {
110
        $newOne = new Embedder;
111
        $newOne->id = '2';
112
113
        $embedded1 = new Embedded;
114
        $embedded1->name = 'newly embedded 1';
115
        $embedded2 = new Embedded;
116
        $embedded2->name = 'newly embedded 2';
117
        $newOne->embeds['one'] = $embedded1;
118
        $newOne->embeds['two'] = $embedded2;
119
120
        $this->dm->persist($newOne);
121
        $this->dm->flush();
122
        $this->dm->clear();
123
124
        $newOne = null;
125
        $this->assertNull($newOne);
126
        $newOne = $this->dm->find($this->type, 2);
127
        $this->assertNotNull($newOne);
128
        $this->assertEquals(2, count($newOne->embeds));
129
        $this->assertEquals('newly embedded 1', $newOne->embeds['one']->name);
130
        $this->assertEquals('newly embedded 2', $newOne->embeds['two']->name);
131
    }
132
133
    public function testMetadataMapping()
134
    {
135
        $metadata = $this->dm->getClassMetadata($this->type);
136
        $this->assertArrayHasKey('embeds', $metadata->fieldMappings);
137
        $mapping = $metadata->fieldMappings['embeds'];
138
        $this->assertEquals('mixed', $mapping['type']);
139
        $this->assertEquals('many', $mapping['embedded']);
140
        $this->assertEquals($this->embeddedType, $mapping['targetDocument']);
141
    }
142
143
    // TODO testEmbeddedWithNonMappedData
144
}
145
146
147
148 View Code Duplication
class PreUpdateSubscriber implements \Doctrine\Common\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...
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
149
{
150
    public $eventArgs = array();
151
    public function getSubscribedEvents()
152
    {
153
        return array(\Doctrine\ODM\CouchDB\Event::preUpdate);
154
    }
155
156
    public function preUpdate(\Doctrine\ODM\CouchDB\Event\LifecycleEventArgs $args)
157
    {
158
        $this->eventArgs[] = $args;
159
    }
160
161
}
162