1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Tests\Unit\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
6
|
|
|
use Prophecy\Argument; |
7
|
|
|
use ScayTrase\Api\Cruds\Controller\CreateController; |
8
|
|
|
use ScayTrase\Api\Cruds\EntityProcessorInterface; |
9
|
|
|
use ScayTrase\Api\Cruds\Event\CollectionCrudEvent; |
10
|
|
|
use ScayTrase\Api\Cruds\Event\CrudEvents; |
11
|
|
|
use ScayTrase\Api\Cruds\ReflectionConstructorFactory; |
12
|
|
|
use ScayTrase\Api\Cruds\Tests\Fixtures\AbcClass; |
13
|
|
|
use Symfony\Component\EventDispatcher\Event; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
15
|
|
|
|
16
|
|
|
abstract class CreateControllerTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
public function testEntityCreation() |
19
|
|
|
{ |
20
|
|
|
$evm = $this->createEvm(); |
21
|
|
|
$factory = $this->createConstructorFactory(); |
22
|
|
|
$processor = $this->createProcessor(AbcClass::class); |
23
|
|
|
$manager = $this->createEntityManager(); |
24
|
|
|
|
25
|
|
|
$controller = new CreateController($processor, $manager, $factory, $evm); |
26
|
|
|
|
27
|
|
|
/** @var AbcClass $entity */ |
28
|
|
|
$entity = $controller->createAction(['a' => 1, 'b' => 'b', 'c' => [1, 2, 3], 'd' => null]); |
29
|
|
|
self::assertInstanceOf(AbcClass::class, $entity); |
30
|
|
|
self::assertSame(1, $entity->a); |
31
|
|
|
self::assertSame('b', $entity->b); |
32
|
|
|
self::assertSame([1, 2, 3], $entity->c); |
33
|
|
|
self::assertNull(null, $entity->d); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return EventDispatcherInterface |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
protected function createEvm() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$evmProphecy = $this->prophesize(EventDispatcherInterface::class); |
42
|
|
|
$evmProphecy->dispatch(CrudEvents::READ, Argument::type(CollectionCrudEvent::class))->shouldBeCalled(); |
43
|
|
|
$evmProphecy->dispatch(CrudEvents::CREATE, Argument::type(Event::class))->shouldBeCalled(); |
44
|
|
|
|
45
|
|
|
return $evmProphecy->reveal(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return ReflectionConstructorFactory |
50
|
|
|
*/ |
51
|
|
|
protected function createConstructorFactory() |
52
|
|
|
{ |
53
|
|
|
return new ReflectionConstructorFactory(AbcClass::class); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $fqcn |
58
|
|
|
* |
59
|
|
|
* @return EntityProcessorInterface |
60
|
|
|
*/ |
61
|
|
|
abstract protected function createProcessor($fqcn); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return ObjectManager |
65
|
|
|
*/ |
66
|
|
|
protected function createEntityManager() |
67
|
|
|
{ |
68
|
|
|
$manager = $this->prophesize(ObjectManager::class); |
69
|
|
|
$manager->persist(Argument::type(AbcClass::class))->shouldBeCalled(); |
70
|
|
|
$manager->flush()->shouldBeCalled(); |
71
|
|
|
|
72
|
|
|
return $manager->reveal(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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.