Completed
Push — master ( 0f15f1...06dce9 )
by Pavel
09:44
created

UpdateControllerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 10.67 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 8
loc 75
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testUpdating() 0 20 1
A createRepository() 0 7 1
createProcessor() 0 1 ?
A createEntityManager() 0 7 1
A createEvm() 8 8 1
A createConstructorFactory() 0 4 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 ScayTrase\Api\Cruds\Tests\Unit\Controller;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use Prophecy\Argument;
8
use ScayTrase\Api\Cruds\Controller\UpdateController;
9
use ScayTrase\Api\Cruds\EntityProcessorInterface;
10
use ScayTrase\Api\Cruds\Event\CollectionCrudEvent;
11
use ScayTrase\Api\Cruds\Event\CrudEvents;
12
use ScayTrase\Api\Cruds\ReflectionConstructorFactory;
13
use ScayTrase\Api\Cruds\Tests\Fixtures\AbcClass;
14
use Symfony\Component\EventDispatcher\Event;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
17
abstract class UpdateControllerTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testUpdating()
20
    {
21
        $id       = 241;
22
        $original = new AbcClass();
23
24
        $controller = new UpdateController(
25
            $this->createRepository($id, $original),
26
            $this->createProcessor(AbcClass::class),
27
            $this->createEntityManager(),
28
            $this->createEvm()
29
        );
30
31
        /** @var AbcClass $entity */
32
        $entity = $controller->patchAction($id, ['a' => 1, 'b' => 'b', 'c' => [1, 2, 3], 'd' => null]);
33
        self::assertSame($original, $entity);
34
        self::assertSame(1, $entity->a);
35
        self::assertSame('b', $entity->b);
36
        self::assertSame([1, 2, 3], $entity->c);
37
        self::assertNull(null, $entity->d);
38
    }
39
40
    /**
41
     * @param mixed  $id
42
     * @param object $entity
43
     *
44
     * @return ObjectRepository
45
     */
46
    protected function createRepository($id, $entity)
47
    {
48
        $repository = $this->prophesize(ObjectRepository::class);
49
        $repository->find(Argument::exact($id))->willReturn($entity)->shouldBeCalled();
50
51
        return $repository->reveal();
52
    }
53
54
    /**
55
     * @param string $fqcn
56
     *
57
     * @return EntityProcessorInterface
58
     */
59
    abstract protected function createProcessor($fqcn);
60
61
    /**
62
     * @return ObjectManager
63
     */
64
    protected function createEntityManager()
65
    {
66
        $manager = $this->prophesize(ObjectManager::class);
67
        $manager->flush()->shouldBeCalled();
68
69
        return $manager->reveal();
70
    }
71
72
    /**
73
     * @return EventDispatcherInterface
74
     */
75 View Code Duplication
    protected function createEvm()
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...
76
    {
77
        $evmProphecy = $this->prophesize(EventDispatcherInterface::class);
78
        $evmProphecy->dispatch(CrudEvents::READ, Argument::type(CollectionCrudEvent::class))->shouldBeCalled();
79
        $evmProphecy->dispatch(CrudEvents::UPDATE, Argument::type(Event::class))->shouldBeCalled();
80
81
        return $evmProphecy->reveal();
82
    }
83
84
    /**
85
     * @return ReflectionConstructorFactory
86
     */
87
    protected function createConstructorFactory()
88
    {
89
        return new ReflectionConstructorFactory(AbcClass::class);
90
    }
91
}
92