Completed
Push — master ( 8aadd3...9420d2 )
by Alex
14:17
created

CategoryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 145
Duplicated Lines 29.66 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 9
Bugs 4 Features 1
Metric Value
wmc 6
c 9
b 4
f 1
lcom 1
cbo 9
dl 43
loc 145
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testIdenticalParent() 0 11 1
B testCategory() 0 35 1
B testLifecycleCallbacks() 0 40 2
B testRemoval() 43 43 1
A testCategorySlugIsTransliterated() 0 9 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
/*
4
* This file is part of the OrbitaleCmsBundle package.
5
*
6
* (c) Alexandre Rock Ancelet <[email protected]>
7
*
8
* For the full copyright and license information, please view the LICENSE
9
* file that was distributed with this source code.
10
*/
11
12
namespace Orbitale\Bundle\CmsBundle\Tests\Entity;
13
14
use Doctrine\ORM\EntityManager;
15
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\AbstractTestCase;
16
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\TestBundle\Entity\Category;
17
use Orbitale\Bundle\CmsBundle\Tests\Fixtures\TestBundle\Entity\Page;
18
19
class CategoryTest extends AbstractTestCase
20
{
21
    public function testCategory()
22
    {
23
        $homepage = new Page();
24
        $homepage
25
            ->setHomepage(true)
26
            ->setEnabled(false)
27
            ->setSlug('home')
28
            ->setTitle('My homepage')
29
            ->setHost('localhost')
30
            ->setContent('Hello world!')
31
        ;
32
33
        $category = new Category();
34
        $category
35
            ->setName('Default category')
36
            ->setSlug('default')
37
        ;
38
39
        $homepage->setCategory($category);
40
41
        $kernel = static::getKernel();
42
43
        /** @var EntityManager $em */
44
        $em = $kernel->getContainer()->get('doctrine')->getManager();
45
        $em->persist($homepage);
46
        $em->persist($category);
47
        $em->flush();
48
49
        /** @var Page $homepage */
50
        $homepage = $em->getRepository(get_class($homepage))->find($homepage->getId());
51
52
        static::assertEquals($homepage->getCategory(), $category);
53
        static::assertEquals($category->getName(), (string) $category);
54
        static::assertFalse($category->isEnabled()); // Base value
55
    }
56
57
    public function testIdenticalParent()
58
    {
59
        $category = new Category();
60
        $category
61
            ->setName('Default category')
62
            ->setSlug('default')
63
            ->setEnabled(true)
64
        ;
65
        $category->setParent($category);
66
        static::assertNull($category->getParent());
67
    }
68
69
    public function testLifecycleCallbacks()
70
    {
71
        $category = new Category();
72
        $category
73
            ->setName('Default category')
74
            ->setSlug('default')
75
            ->setEnabled(true)
76
        ;
77
78
        $child = clone $category;
79
        $child->setSlug('child');
80
81
        $category->addChild($child);
82
        $child->setParent($category);
83
84
        $kernel = static::getKernel();
85
86
        /** @var EntityManager $em */
87
        $em = $kernel->getContainer()->get('doctrine')->getManager();
88
        $em->persist($category);
89
        $em->persist($child);
90
        $em->flush();
91
92
        static::assertEquals(array($child), $category->getChildren()->toArray());
93
94
        /** @var Category $category */
95
        $category = $em->getRepository(get_class($category))->findOneBy(array('id' => $category->getId()));
96
97
        static::assertNotNull($category);
98
99
        if (null !== $category) {
100
            $em->remove($category);
101
            $em->flush();
102
        }
103
104
        $category = $em->getRepository(get_class($category))->findOneBy(array('id' => $category->getId()));
105
106
        static::assertNull($category);
107
        static::assertNull($child->getParent());
108
    }
109
110 View Code Duplication
    public function testRemoval()
111
    {
112
        $category = new Category();
113
        $category
114
            ->setName('Default category')
115
            ->setSlug('default')
116
            ->setEnabled(true)
117
        ;
118
119
        $child = new Category();
120
        $child
121
            ->setName('Child category')
122
            ->setSlug('child')
123
            ->setEnabled(true)
124
            ->setParent($category)
125
        ;
126
        $category->addChild($child);
127
128
        $kernel = static::getKernel();
129
130
        /** @var EntityManager $em */
131
        $em = $kernel->getContainer()->get('doctrine')->getManager();
132
        $em->persist($category);
133
        $em->persist($child);
134
        $em->flush();
135
136
        /** @var Category $category */
137
        $category = $em->getRepository(get_class($category))->find($category->getId());
138
139
        $children = $category->getChildren();
140
        $first = $children[0];
141
        static::assertEquals($child->getId(), $first->getId());
142
143
        $category->removeChild($child);
144
        $child->setParent(null);
145
146
        $em->remove($category);
147
        $em->flush();
148
149
        $child = $em->getRepository(get_class($child))->find($child->getId());
150
151
        static::assertNull($child->getParent());
152
    }
153
154
    public function testCategorySlugIsTransliterated()
155
    {
156
        $category = new Category();
157
        $category->setName('Default category');
158
159
        $category->updateSlug();
160
161
        static::assertEquals('default-category', $category->getSlug());
162
    }
163
}
164