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