1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kami\ApiCoreBundle\Tests\RequestProcessor\Step\Common; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\Serializer; |
6
|
|
|
use Kami\ApiCoreBundle\RequestProcessor\Step\Common\PersistStep; |
7
|
|
|
use Kami\ApiCoreBundle\RequestProcessor\Step\Common\TrimResponseStep; |
8
|
|
|
use Kami\ApiCoreBundle\Security\AccessManager; |
9
|
|
|
use Kami\ApiCoreBundle\Tests\Entity\MyModel; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Kami\ApiCoreBundle\RequestProcessor\ProcessorResponse; |
13
|
|
|
|
14
|
|
|
class TrimResponseStepTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
public function testCanBeConstructed() |
18
|
|
|
{ |
19
|
|
|
$accessManagerMock = $this->createMock(AccessManager::class); |
20
|
|
|
|
21
|
|
|
$step = new TrimResponseStep($accessManagerMock); |
22
|
|
|
$this->assertInstanceOf(TrimResponseStep::class, $step); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testRequiresBefore() |
26
|
|
|
{ |
27
|
|
|
$accessManagerMock = $this->createMock(AccessManager::class); |
28
|
|
|
|
29
|
|
|
$step = new TrimResponseStep($accessManagerMock); |
30
|
|
|
$this->assertEquals([PersistStep::class], $step->requiresBefore()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testExecuteNoAccess() |
34
|
|
|
{ |
35
|
|
|
$accessManagerMock = $this->createMock(AccessManager::class); |
36
|
|
|
$accessManagerMock->expects($this->any())->method('canAccessProperty')->willReturn(false); |
37
|
|
|
|
38
|
|
|
$step = new TrimResponseStep($accessManagerMock); |
39
|
|
|
$request = new Request(); |
40
|
|
|
$step->setRequest($request); |
41
|
|
|
$step->setPreviousResponse(new ProcessorResponse($request, [ |
42
|
|
|
'reflection' => new \ReflectionClass(MyModel::class), |
43
|
|
|
'response_data' => new MyModel() |
44
|
|
|
])); |
45
|
|
|
|
46
|
|
|
$response = $step->execute(); |
47
|
|
|
$this->assertInstanceOf(ProcessorResponse::class, $response); |
48
|
|
|
$this->assertEquals([], $response->getData()['response_data']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testExecute() |
52
|
|
|
{ |
53
|
|
|
$accessManagerMock = $this->createMock(AccessManager::class); |
54
|
|
|
$accessManagerMock->expects($this->any())->method('canAccessProperty')->willReturn(true); |
55
|
|
|
|
56
|
|
|
$step = new TrimResponseStep($accessManagerMock); |
57
|
|
|
$request = new Request(); |
58
|
|
|
$step->setRequest($request); |
59
|
|
|
$step->setPreviousResponse(new ProcessorResponse($request, [ |
60
|
|
|
'reflection' => new \ReflectionClass(MyModel::class), |
61
|
|
|
'response_data' => new MyModel() |
62
|
|
|
])); |
63
|
|
|
|
64
|
|
|
$response = $step->execute(); |
65
|
|
|
$this->assertInstanceOf(ProcessorResponse::class, $response); |
66
|
|
|
$this->assertArrayHasKey('id', $response->getData()['response_data']); |
67
|
|
|
$this->assertArrayHasKey('title', $response->getData()['response_data']); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|