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