1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Kami\ApiCoreBundle\Tests\RequestProcessor\Step\Common; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
8
|
|
|
use Doctrine\ORM\EntityManager; |
9
|
|
|
use Kami\ApiCoreBundle\RequestProcessor\Step\Common\PersistStep; |
10
|
|
|
use Kami\ApiCoreBundle\Tests\Entity\MyModel; |
11
|
|
|
use Kami\Component\RequestProcessor\Artifact; |
12
|
|
|
use Kami\Component\RequestProcessor\ArtifactCollection; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
16
|
|
|
|
17
|
|
|
class PersistStepTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
public function testCanBeConstructed() |
20
|
|
|
{ |
21
|
|
|
$registryMock = $this->createMock(Registry::class); |
22
|
|
|
$step = new PersistStep($registryMock); |
23
|
|
|
|
24
|
|
|
$this->assertInstanceOf(PersistStep::class, $step); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testGetRequiredArtifacts() |
28
|
|
|
{ |
29
|
|
|
$registryMock = $this->createMock(Registry::class); |
30
|
|
|
$step = new PersistStep($registryMock); |
31
|
|
|
|
32
|
|
|
$this->assertEquals(['validation', 'entity', 'access_granted'], $step->getRequiredArtifacts()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testExecute() |
36
|
|
|
{ |
37
|
|
|
$emMock = $this->createMock(EntityManager::class); |
38
|
|
|
$registryMock = $this->createMock(Registry::class); |
39
|
|
|
$registryMock->expects($this->any())->method('getManager')->willReturn($emMock); |
40
|
|
|
$step = new PersistStep($registryMock); |
41
|
|
|
|
42
|
|
|
$step->setArtifacts(new ArtifactCollection([ |
43
|
|
|
new Artifact('entity', new MyModel()), |
44
|
|
|
new Artifact('validation', true) |
45
|
|
|
])); |
46
|
|
|
|
47
|
|
|
$response = $step->execute(new Request()); |
48
|
|
|
$this->assertInstanceOf(ArtifactCollection::class, $response); |
49
|
|
|
$this->assertInstanceOf(MyModel::class, $response->get('response_data')->getValue()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testExecuteFailure() |
53
|
|
|
{ |
54
|
|
|
$emMock = $this->createMock(EntityManager::class); |
55
|
|
|
$emMock->expects($this->any())->method('persist')->will($this->throwException(new BadRequestHttpException())); |
56
|
|
|
$registryMock = $this->createMock(Registry::class); |
57
|
|
|
$registryMock->expects($this->any())->method('getManager')->willReturn($emMock); |
58
|
|
|
$step = new PersistStep($registryMock); |
59
|
|
|
|
60
|
|
|
$step->setArtifacts(new ArtifactCollection([ |
61
|
|
|
new Artifact('entity', new MyModel()), |
62
|
|
|
new Artifact('validation', true) |
63
|
|
|
])); |
64
|
|
|
|
65
|
|
|
$this->expectException(BadRequestHttpException::class); |
66
|
|
|
$step->execute(new Request()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testExecuteFailureValidation() |
70
|
|
|
{ |
71
|
|
|
$registryMock = $this->createMock(Registry::class); |
72
|
|
|
$step = new PersistStep($registryMock); |
73
|
|
|
|
74
|
|
|
$step->setArtifacts(new ArtifactCollection([ |
75
|
|
|
new Artifact('entity', new MyModel()), |
76
|
|
|
new Artifact('validation', false) |
77
|
|
|
])); |
78
|
|
|
|
79
|
|
|
$response = $step->execute(new Request()); |
80
|
|
|
|
81
|
|
|
$this->assertEquals(new ArtifactCollection(), $response); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |