|
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\Exception\NotDeserializableException; |
|
10
|
|
|
use Gorynych\Http\Exception\BadRequestHttpException; |
|
11
|
|
|
use Gorynych\Http\Exception\UnsupportedMediaTypeHttpException; |
|
12
|
|
|
use Gorynych\Operation\ControllerTrait; |
|
13
|
|
|
use Gorynych\Resource\Exception\InvalidEntityException; |
|
14
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
|
|
18
|
|
|
class ControllerTraitTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var SerializerAdapter|MockObject */ |
|
21
|
|
|
private $serializerMock; |
|
22
|
|
|
/** @var ValidatorAdapter|MockObject */ |
|
23
|
|
|
private $validatorMock; |
|
24
|
|
|
|
|
25
|
|
|
public function setUp(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->serializerMock = $this->createMock(SerializerAdapter::class); |
|
28
|
|
|
$this->validatorMock = $this->createMock(ValidatorAdapter::class); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testThrowsUnsupportedMediaType(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$this->expectException(UnsupportedMediaTypeHttpException::class); |
|
34
|
|
|
|
|
35
|
|
|
$requestMock = $this->createMock(Request::class); |
|
36
|
|
|
$requestMock->method('getContent')->willReturn(json_encode(['foo' => 'bar'])); |
|
37
|
|
|
|
|
38
|
|
|
$this->serializerMock |
|
39
|
|
|
->expects($this->once()) |
|
40
|
|
|
->method('setup') |
|
41
|
|
|
->with(null) |
|
42
|
|
|
->willReturn($this->serializerMock); |
|
43
|
|
|
$this->serializerMock |
|
44
|
|
|
->expects($this->once()) |
|
45
|
|
|
->method('deserialize') |
|
46
|
|
|
->with($requestMock->getContent(), 'Test', 'json', []) |
|
47
|
|
|
->willThrowException(new NotDeserializableException()); |
|
48
|
|
|
|
|
49
|
|
|
$this->setUpController()->deserializeBody($requestMock, 'Test'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testThrowsBadRequest(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->expectException(BadRequestHttpException::class); |
|
55
|
|
|
|
|
56
|
|
|
$this->validatorMock |
|
57
|
|
|
->expects($this->once()) |
|
58
|
|
|
->method('setup') |
|
59
|
|
|
->with('Test') |
|
60
|
|
|
->willReturn($this->validatorMock); |
|
61
|
|
|
$this->validatorMock |
|
62
|
|
|
->expects($this->once()) |
|
63
|
|
|
->method('validate') |
|
64
|
|
|
->with(new \stdClass()) |
|
65
|
|
|
->willThrowException(new InvalidEntityException()); |
|
66
|
|
|
|
|
67
|
|
|
$this->setUpController()->validate(new \stdClass(), 'Test'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function setUpController(): object |
|
71
|
|
|
{ |
|
72
|
|
|
$controller = new class { |
|
73
|
|
|
use ControllerTrait { |
|
74
|
|
|
deserializeBody as public; |
|
75
|
|
|
validate as public; |
|
76
|
|
|
} |
|
77
|
|
|
}; |
|
78
|
|
|
|
|
79
|
|
|
$controller->setSerializer($this->serializerMock); |
|
80
|
|
|
$controller->setValidator($this->validatorMock); |
|
81
|
|
|
|
|
82
|
|
|
return $controller; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|