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

UpdateControllerTest::createProcessor()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
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