/
BookPersistProcessorTest.php
145 lines (136 loc) · 5.15 KB
/
BookPersistProcessorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace App\Tests\State\Processor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\Book;
use App\State\Processor\BookPersistProcessor;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
final class BookPersistProcessorTest extends TestCase
{
private MockObject|ProcessorInterface $persistProcessorMock;
private MockObject|ProcessorInterface $mercureProcessorMock;
private HttpClientInterface|MockObject $clientMock;
private MockObject|ResponseInterface $responseMock;
private DecoderInterface|MockObject $decoderMock;
private Book|MockObject $objectMock;
private MockObject|Operation $operationMock;
private BookPersistProcessor $processor;
protected function setUp(): void
{
$this->persistProcessorMock = $this->createMock(ProcessorInterface::class);
$this->mercureProcessorMock = $this->createMock(ProcessorInterface::class);
$this->clientMock = $this->createMock(HttpClientInterface::class);
$this->responseMock = $this->createMock(ResponseInterface::class);
$this->decoderMock = $this->createMock(DecoderInterface::class);
$this->objectMock = $this->createMock(Book::class);
$this->objectMock->book = 'https://openlibrary.org/books/OL2055137M.json';
$this->operationMock = $this->createMock(Operation::class);
$this->processor = new BookPersistProcessor(
$this->persistProcessorMock,
$this->mercureProcessorMock,
$this->clientMock,
$this->decoderMock
);
}
#[Test]
public function itUpdatesBookDataBeforeSaveAndSendMercureUpdates(): void
{
$expectedData = $this->objectMock;
$expectedData->title = 'Foundation';
$expectedData->author = 'Dan Simmons';
$this->clientMock
->expects($this->exactly(2))
->method('request')
// ->withConsecutive(
// [
// Request::METHOD_GET, 'https://openlibrary.org/books/OL2055137M.json', [
// 'headers' => [
// 'Accept' => 'application/json',
// ],
// ],
// ],
// [
// Request::METHOD_GET, 'https://openlibrary.org/authors/OL34221A.json', [
// 'headers' => [
// 'Accept' => 'application/json',
// ],
// ],
// ],
// )
->willReturnOnConsecutiveCalls($this->responseMock, $this->responseMock)
;
$this->responseMock
->expects($this->exactly(2))
->method('getContent')
->willReturnOnConsecutiveCalls(
json_encode([
'title' => 'Foundation',
'authors' => [
['key' => '/authors/OL34221A'],
],
]),
json_encode([
'name' => 'Dan Simmons',
]),
)
;
$this->decoderMock
->expects($this->exactly(2))
->method('decode')
// ->withConsecutive(
// [
// json_encode([
// 'title' => 'Foundation',
// 'authors' => [
// ['key' => '/authors/OL34221A'],
// ],
// ]),
// 'json',
// ],
// [
// json_encode([
// 'name' => 'Dan Simmons',
// ]),
// 'json',
// ],
// )
->willReturnOnConsecutiveCalls(
[
'title' => 'Foundation',
'authors' => [
['key' => '/authors/OL34221A'],
],
],
[
'name' => 'Dan Simmons',
],
)
;
$this->persistProcessorMock
->expects($this->once())
->method('process')
->with($expectedData, $this->operationMock, [], [])
->willReturn($expectedData)
;
$this->mercureProcessorMock
->expects($this->exactly(2))
->method('process')
// ->withConsecutive(
// [$expectedData, $this->operationMock, [], ['item_uri_template' => '/admin/books/{id}{._format}']],
// [$expectedData, $this->operationMock, [], ['item_uri_template' => '/books/{id}{._format}']],
// )
->willReturnOnConsecutiveCalls(
$expectedData,
$expectedData,
)
;
$this->assertEquals($expectedData, $this->processor->process($this->objectMock, $this->operationMock));
}
}