PageTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 131
Duplicated Lines 32.82 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 43
loc 131
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDummyPage() 0 14 1
B testOneHomepage() 0 24 1
B testLifecycleCallbacks() 0 34 2
B testRemoval() 43 43 1
A testPageSlugIsTransliterated() 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\Page;
17
18
class PageTest extends AbstractTestCase
19
{
20
    public function getDummyPage(): Page
21
    {
22
        $page = new Page();
23
24
        $page
25
            ->setHomepage(true)
26
            ->setSlug('home')
27
            ->setTitle('My homepage')
28
            ->setHost('localhost')
29
            ->setContent('Hello world!')
30
        ;
31
32
        return $page;
33
    }
34
35
    public function testOneHomepage()
36
    {
37
        $homepage = $this->getDummyPage();
38
39
        $kernel = static::getKernel();
40
41
        /** @var EntityManager $em */
42
        $em = $kernel->getContainer()->get('doctrine')->getManager();
43
        $em->persist($homepage);
44
        $em->flush();
45
46
        /** @var Page $homepage */
47
        $homepage = $em->getRepository(get_class($homepage))->find($homepage->getId());
48
49
        static::assertEquals($homepage->getTitle(), (string) $homepage);
50
51
        static::assertFalse($homepage->isEnabled()); // Base value in entity
52
        static::assertTrue($homepage->isHomepage());
53
        static::assertEquals('localhost', $homepage->getHost());
54
        static::assertInstanceOf('DateTime', $homepage->getCreatedAt());
55
56
        $homepage->setParent($homepage);
57
        static::assertNull($homepage->getParent());
58
    }
59
60
    public function testLifecycleCallbacks()
61
    {
62
        $homepage = $this->getDummyPage();
63
64
        $child = $this->getDummyPage()->setSlug('child');
65
66
        $homepage->addChild($child);
67
        $child->setParent($homepage);
68
69
        $kernel = static::getKernel();
70
71
        /** @var EntityManager $em */
72
        $em = $kernel->getContainer()->get('doctrine')->getManager();
73
        $em->persist($homepage);
74
        $em->persist($child);
75
        $em->flush();
76
77
        static::assertEquals([$child], $homepage->getChildren()->toArray());
78
79
        /** @var Page $homepage */
80
        $homepage = $em->getRepository(get_class($homepage))->findOneBy(['id' => $homepage->getId()]);
81
82
        static::assertNotNull($homepage);
83
84
        if (null !== $homepage) {
85
            $em->remove($homepage);
86
            $em->flush();
87
        }
88
89
        $homepage = $em->getRepository(get_class($homepage))->findOneBy(['id' => $homepage->getId()]);
90
91
        static::assertNull($homepage);
92
        static::assertNull($child->getParent());
93
    }
94
95 View Code Duplication
    public function testRemoval()
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...
96
    {
97
        $page = new Page();
98
        $page
99
            ->setTitle('Default page')
100
            ->setSlug('default')
101
            ->setEnabled(true)
102
        ;
103
104
        $child = new Page();
105
        $child
106
            ->setTitle('Child page')
107
            ->setSlug('child')
108
            ->setEnabled(true)
109
            ->setParent($page)
110
        ;
111
        $page->addChild($child);
112
113
        $kernel = static::getKernel();
114
115
        /** @var EntityManager $em */
116
        $em = $kernel->getContainer()->get('doctrine')->getManager();
117
        $em->persist($page);
118
        $em->persist($child);
119
        $em->flush();
120
121
        $page = $em->getRepository(get_class($page))->find($page->getId());
122
123
        $children = $page->getChildren();
124
        /** @var Page $first */
125
        $first = $children[0];
126
        static::assertEquals($child->getId(), $first->getId());
127
128
        $page->removeChild($child);
129
        $child->setParent(null);
130
131
        $em->remove($page);
0 ignored issues
show
Bug introduced by
It seems like $page defined by $em->getRepository(get_c...)->find($page->getId()) on line 121 can also be of type null; however, Doctrine\ORM\EntityManager::remove() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
132
        $em->flush();
133
134
        $child = $em->getRepository(get_class($child))->find($child->getId());
135
136
        static::assertNull($child->getParent());
137
    }
138
139
    public function testPageSlugIsTransliterated()
140
    {
141
        $page = new Page();
142
        $page->setTitle('Default page');
143
144
        $page->updateSlug();
145
146
        static::assertEquals('default-page', $page->getSlug());
147
    }
148
}
149