CloneListenerTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 120
Duplicated Lines 10.83 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 8
dl 13
loc 120
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 33 1
A testClonePagePart() 13 13 1
A testClonePageTemplate() 0 39 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 Kunstmaan\PagePartBundle\Tests\EventListener;
4
5
use Kunstmaan\AdminBundle\Event\DeepCloneAndSaveEvent;
6
use Kunstmaan\PagePartBundle\Entity\PagePartRef;
7
use Kunstmaan\PagePartBundle\Entity\PageTemplateConfiguration;
8
use Kunstmaan\PagePartBundle\EventListener\CloneListener;
9
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
10
use Kunstmaan\PagePartBundle\Helper\HasPageTemplateInterface;
11
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdminConfigurator;
12
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdminConfiguratorInterface;
13
use Kunstmaan\PagePartBundle\PagePartConfigurationReader\PagePartConfigurationReaderInterface;
14
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateConfigurationService;
15
use Kunstmaan\PagePartBundle\Repository\PagePartRefRepository;
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * Class CloneListenerTest
20
 */
21
class CloneListenerTest extends TestCase
22
{
23
    /**
24
     * @var \Doctrine\ORM\EntityManager|\PHPUnit_Framework_MockObject_MockObject
25
     */
26
    private $em;
27
28
    /**
29
     * @var PagePartAdminConfiguratorInterface
30
     */
31
    private $configurator;
32
33
    /**
34
     * @var \Doctrine\ORM\EntityRepository|\PHPUnit_Framework_MockObject_MockObject
35
     */
36
    private $repo;
37
38
    /**
39
     * @var CloneListener
40
     */
41
    private $object;
42
43
    /**
44
     * @var PagePartConfigurationReaderInterface|\PHPUnit_Framework_MockObject_MockObject
45
     */
46
    private $reader;
47
48
    /**
49
     * @var PageTemplateConfigurationService|\PHPUnit_Framework_MockObject_MockObject
50
     */
51
    private $templateService;
52
53
    protected function setUp(): void
54
    {
55
        $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('D...onstructor()->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\ORM\Enti..._MockObject_MockObject> of property $em.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
            ->disableOriginalConstructor()
57
            ->getMock();
58
59
        $this->repo = $this->getMockBuilder(PagePartRefRepository::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\K...onstructor()->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\ORM\Enti..._MockObject_MockObject> of property $repo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60
            ->disableOriginalConstructor()
61
            ->getMock();
62
63
        $this->em->expects($this->any())
64
            ->method('getRepository')
65
            ->with($this->equalTo(PagePartRef::class))
66
            ->willReturn($this->repo);
67
68
        $this->configurator = new PagePartAdminConfigurator();
69
        $this->configurator->setContext('main');
70
71
        $this->reader = $this->createMock(PagePartConfigurationReaderInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Kunst...ReaderInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Kunstmaan\PagePar..._MockObject_MockObject> of property $reader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
        $this->reader
73
            ->expects($this->any())
74
            ->method('getPagePartAdminConfigurators')
75
            ->willReturn([$this->configurator]);
76
77
        $this->reader
78
            ->expects($this->any())
79
            ->method('getPagePartContexts')
80
            ->willReturn([$this->configurator->getContext()]);
81
82
        $this->templateService = $this->getMockBuilder(PageTemplateConfigurationService::class)->disableOriginalConstructor()->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\K...onstructor()->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Kunstmaan\PagePar..._MockObject_MockObject> of property $templateService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
83
84
        $this->object = new CloneListener($this->em, $this->reader, $this->templateService);
0 ignored issues
show
Documentation introduced by
$this->em is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->reader is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\PagePar...urationReaderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->templateService is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\PagePar...teConfigurationService>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
    }
86
87 View Code Duplication
    public function testClonePagePart()
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...
88
    {
89
        $entity = $this->createMock(HasPagePartsInterface::class);
90
91
        $clone = clone $entity;
92
93
        $this->repo->expects($this->once())
94
            ->method('copyPageParts')
95
            ->with($this->em, $entity, $clone, 'main');
96
97
        $event = new DeepCloneAndSaveEvent($entity, $clone);
98
        $this->object->postDeepCloneAndSave($event);
99
    }
100
101
    public function testClonePageTemplate()
102
    {
103
        $entity = $this->createMock(HasPageTemplateInterface::class);
104
105
        /** @var HasPageTemplateInterface|\PHPUnit_Framework_MockObject_MockObject $clone */
106
        $clone = clone $entity;
107
108
        $entity->expects($this->any())
109
            ->method('getId')
110
            ->willReturn(1);
111
112
        $clone->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Kunstmaan\PagePar...sPageTemplateInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
            ->method('getId')
114
            ->willReturn(2);
115
116
        $this->repo->expects($this->once())
117
            ->method('copyPageParts')
118
            ->with($this->em, $entity, $clone, 'main');
119
120
        $configuration = new PageTemplateConfiguration();
121
        $configuration->setId(1);
122
        $configuration->setPageId(1);
123
124
        $this->templateService->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Kunstmaan\PagePar...teConfigurationService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
            ->method('findOrCreateFor')
126
            ->with($this->identicalTo($entity))
127
            ->willReturn($configuration);
128
129
        $newConfiguration = clone $configuration;
130
        $newConfiguration->setId(null);
131
        $newConfiguration->setPageId($clone->getId());
132
133
        $this->em->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ORM\EntityManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
            ->method('persist')
135
            ->with($newConfiguration);
136
137
        $event = new DeepCloneAndSaveEvent($entity, $clone);
138
        $this->object->postDeepCloneAndSave($event);
139
    }
140
}
141