AbstractMappingDriverTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 143
Duplicated Lines 23.08 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 33
loc 143
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
loadDriver() 0 1 ?
A testLoadMapping() 0 11 1
A testFieldMappings() 0 11 1
A testStringFieldMappings() 0 6 1
A testIdentifier() 0 7 1
A testManyToOneAssociationMapping() 16 16 1
A testManyToManyAssociationMapping() 17 17 1
A testAttachmentMapping() 0 7 1
A testEmbeddedMapping() 0 11 1
A testVersionMapping() 0 16 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Doctrine\Tests\ODM\CouchDB\Mapping;
4
5
use Doctrine\ODM\CouchDB\Mapping\ClassMetadata,
6
    Doctrine\ODM\CouchDB\Mapping\Driver\XmlDriver,
7
    Doctrine\ODM\CouchDB\Mapping\Driver\YamlDriver;
8
9
abstract class AbstractMappingDriverTest extends \PHPUnit_Framework_TestCase
10
{
11
    abstract protected function loadDriver();
12
13
    public function testLoadMapping()
14
    {
15
        $className = 'Doctrine\Tests\Models\CMS\CmsUser';
16
        $mappingDriver = $this->loadDriver();
17
18
        $class = new ClassMetadata($className);
19
        $class->namespace = 'Doctrine\Tests\Models\CMS';
20
        $mappingDriver->loadMetadataForClass($className, $class);
21
22
        return $class;
23
    }
24
25
    /**
26
     * @depends testLoadMapping
27
     * @param ClassMetadata $class
28
     */
29
    public function testFieldMappings($class)
30
    {
31
        $this->assertCount(5, $class->fieldMappings);
32
        $this->assertArrayHasKey('id', $class->fieldMappings);
33
        $this->assertArrayHasKey('name', $class->fieldMappings);
34
        $this->assertArrayHasKey('username', $class->fieldMappings);
35
        $this->assertArrayHasKey('status', $class->fieldMappings);
36
        $this->assertArrayHasKey('address', $class->fieldMappings);
37
38
        return $class;
39
    }
40
41
    /**
42
     * @depends testFieldMappings
43
     * @param ClassMetadata $class
44
     */
45
    public function testStringFieldMappings($class)
46
    {
47
        $this->assertEquals('string', $class->fieldMappings['name']['type']);
48
49
        return $class;
50
    }
51
52
    /**
53
     * @depends testFieldMappings
54
     * @param ClassMetadata $class
55
     */
56
    public function testIdentifier($class)
57
    {
58
        $this->assertEquals('id', $class->identifier);
59
        $this->assertEquals(ClassMetadata::IDGENERATOR_UUID, $class->idGenerator);
60
61
        return $class;
62
    }
63
64
    /**
65
     * @depends testFieldMappings
66
     * @param ClassMetadata $class
67
     */
68 View Code Duplication
    public function testManyToOneAssociationMapping($class)
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...
69
    {
70
        $this->assertArrayHasKey('rights', $class->associationsMappings);
71
72
        $this->assertEquals(array(
73
            'fieldName' => 'rights',
74
            'cascade' => 0,
75
            'jsonName' => 'rights',
76
            'targetDocument' => 'Doctrine\Tests\Models\CMS\CmsUserRights',
77
            'sourceDocument' => 'Doctrine\Tests\Models\CMS\CmsUser',
78
            'isOwning' => true,
79
            'type' => ClassMetadata::MANY_TO_ONE,
80
        ), $class->associationsMappings['rights']);
81
82
        return $class;
83
    }
84
85
    /**
86
     * @depends testFieldMappings
87
     * @param ClassMetadata $class
88
     */
89 View Code Duplication
    public function testManyToManyAssociationMapping($class)
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...
90
    {
91
        $this->assertArrayHasKey('groups', $class->associationsMappings);
92
93
        $this->assertEquals(array(
94
            'fieldName' => 'groups',
95
            'cascade' => 0,
96
            'mappedBy' => null,
97
            'targetDocument' => 'Doctrine\Tests\Models\CMS\CmsGroup',
98
            'jsonName' => 'groups',
99
            'sourceDocument' => 'Doctrine\Tests\Models\CMS\CmsUser',
100
            'isOwning' => true,
101
            'type' => ClassMetadata::MANY_TO_MANY,
102
        ), $class->associationsMappings['groups']);
103
104
        return $class;
105
    }
106
107
    /**
108
     * @depends testManyToManyAssociationMapping
109
     * @param ClassMetadata $class
110
     */
111
    public function testAttachmentMapping($class)
112
    {
113
        $this->assertTrue($class->hasAttachments);
114
        $this->assertEquals('attachments', $class->attachmentField);
115
116
        return $class;
117
    }
118
    
119
    /**
120
     * @depends testManyToManyAssociationMapping
121
     * @param ClassMetadata $class
122
     */
123
    public function testEmbeddedMapping($class)
124
    {
125
        $this->assertArrayHasKey('address', $class->fieldMappings);
126
        $this->assertEquals(array(
127
            'fieldName' => 'address',
128
            'jsonName' => 'address',
129
            'embedded' => 'one',
130
            'targetDocument' => null,
131
            'type' => 'mixed',
132
        ), $class->fieldMappings['address']);
133
    }
134
135
    public function testVersionMapping()
136
    {
137
        $className = 'Doctrine\Tests\Models\CMS\CmsArticle';
138
        $mappingDriver = $this->loadDriver();
139
140
        $class = new ClassMetadata($className);
141
        $class->namespace = 'Doctrine\Tests\Models\CMS';
142
        $mappingDriver->loadMetadataForClass($className, $class);
143
144
        $this->assertEquals(array(
145
            'fieldName' => 'version',
146
            'jsonName' => '_rev',
147
            'type' => 'string',
148
            'isVersionField' => true,
149
        ), $class->fieldMappings['version']);
150
    }
151
}
152