1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gorynych\Tests\Operation; |
6
|
|
|
|
7
|
|
|
use Gorynych\Adapter\SerializerAdapter; |
8
|
|
|
use Gorynych\Adapter\ValidatorAdapter; |
9
|
|
|
use Gorynych\Operation\AbstractOperation; |
10
|
|
|
use Gorynych\Tests\Operation\Resource\TestInput; |
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
class AbstractOperationTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** @var SerializerAdapter|MockObject */ |
18
|
|
|
private $serializerMock; |
19
|
|
|
/** @var ValidatorAdapter|MockObject */ |
20
|
|
|
private $validatorMock; |
21
|
|
|
|
22
|
|
|
public function setUp(): void |
23
|
|
|
{ |
24
|
|
|
$this->serializerMock = $this->createMock(SerializerAdapter::class); |
25
|
|
|
$this->validatorMock = $this->createMock(ValidatorAdapter::class); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testHandlesRequest(): void |
29
|
|
|
{ |
30
|
|
|
$requestMock = $this->createMock(Request::class); |
31
|
|
|
$requestMock->method('getContentType')->willReturn('json'); |
32
|
|
|
$requestMock->method('getContent')->willReturn(json_encode(['foo' => 'bar'])); |
33
|
|
|
|
34
|
|
|
$this->serializerMock |
35
|
|
|
->expects($this->exactly(2)) |
36
|
|
|
->method('setup') |
37
|
|
|
->with(null) |
38
|
|
|
->willReturn($this->serializerMock); |
39
|
|
|
$this->serializerMock |
40
|
|
|
->expects($this->once()) |
41
|
|
|
->method('canDeserialize') |
42
|
|
|
->with(TestInput::class) |
43
|
|
|
->willReturn(true); |
44
|
|
|
$this->serializerMock |
45
|
|
|
->expects($this->once()) |
46
|
|
|
->method('deserialize') |
47
|
|
|
->with($requestMock->getContent(), TestInput::class, 'json', []) |
48
|
|
|
->willReturn(new TestInput('bar')); |
49
|
|
|
|
50
|
|
|
$this->validatorMock |
51
|
|
|
->expects($this->once()) |
52
|
|
|
->method('setup') |
53
|
|
|
->with('TestInput') |
54
|
|
|
->willReturn($this->validatorMock); |
55
|
|
|
$this->validatorMock |
56
|
|
|
->expects($this->once()) |
57
|
|
|
->method('validate') |
58
|
|
|
->with(new TestInput('bar')); |
59
|
|
|
|
60
|
|
|
$this->serializerMock |
61
|
|
|
->expects($this->once()) |
62
|
|
|
->method('canNormalize') |
63
|
|
|
->with(new \stdClass()) |
64
|
|
|
->willReturn(true); |
65
|
|
|
$this->serializerMock |
66
|
|
|
->expects($this->once()) |
67
|
|
|
->method('normalize') |
68
|
|
|
->with(new \stdClass(), []) |
69
|
|
|
->willReturn(['foo' => 'bar']); |
70
|
|
|
|
71
|
|
|
$output = $this->setUpOperation()->handle($requestMock); |
72
|
|
|
|
73
|
|
|
$this->assertIsArray($output); |
74
|
|
|
$this->assertSame($output['foo'], 'bar'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function setUpOperation(): AbstractOperation |
78
|
|
|
{ |
79
|
|
|
$operation = new class extends AbstractOperation { |
80
|
|
|
public function getMethod(): string |
81
|
|
|
{ |
82
|
|
|
return AbstractOperation::GET_METHOD; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getPath(): string |
86
|
|
|
{ |
87
|
|
|
return '/'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function __invoke(TestInput $input): object |
91
|
|
|
{ |
92
|
|
|
return new \stdClass(); |
93
|
|
|
} |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
$operation->setSerializer($this->serializerMock); |
97
|
|
|
$operation->setValidator($this->validatorMock); |
98
|
|
|
|
99
|
|
|
return $operation; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|