/
BookNormalizerTest.php
102 lines (90 loc) · 3.63 KB
/
BookNormalizerTest.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
<?php
declare(strict_types=1);
namespace App\Tests\Serializer;
use App\Entity\Book;
use App\Enum\BookCondition;
use App\Repository\ReviewRepository;
use App\Serializer\BookNormalizer;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Uid\Uuid;
final class BookNormalizerTest extends TestCase
{
private MockObject|NormalizerInterface $normalizerMock;
private MockObject|RouterInterface $routerMock;
private MockObject|ReviewRepository $repositoryMock;
private Book|MockObject $objectMock;
private BookNormalizer $normalizer;
protected function setUp(): void
{
$this->normalizerMock = $this->createMock(NormalizerInterface::class);
$this->routerMock = $this->createMock(RouterInterface::class);
$this->repositoryMock = $this->createMock(ReviewRepository::class);
$this->objectMock = $this->createMock(Book::class);
$this->normalizer = new BookNormalizer($this->routerMock, $this->repositoryMock);
$this->normalizer->setNormalizer($this->normalizerMock);
}
#[Test]
public function itDoesNotSupportInvalidObjectClass(): void
{
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
}
#[Test]
public function itDoesNotSupportInvalidContext(): void
{
$this->assertFalse($this->normalizer->supportsNormalization($this->objectMock, null, [BookNormalizer::class => true]));
}
#[Test]
public function itSupportsValidObjectClassAndContext(): void
{
$this->assertTrue($this->normalizer->supportsNormalization($this->objectMock));
}
#[Test]
public function itNormalizesData(): void
{
$expectedObject = $this->objectMock;
$expectedObject->reviews = '/books/a528046c-7ba1-4acc-bff2-b5390ab17d41/reviews';
$expectedObject->rating = 3;
$this->objectMock
->expects($this->once())
->method('getId')
->willReturn(Uuid::fromString('a528046c-7ba1-4acc-bff2-b5390ab17d41'))
;
$this->routerMock
->expects($this->once())
->method('generate')
->with('_api_/books/{bookId}/reviews{._format}_get_collection', ['bookId' => 'a528046c-7ba1-4acc-bff2-b5390ab17d41'])
->willReturn('/books/a528046c-7ba1-4acc-bff2-b5390ab17d41/reviews')
;
$this->repositoryMock
->expects($this->once())
->method('getAverageRating')
->with($this->objectMock)
->willReturn(3)
;
$this->normalizerMock
->expects($this->once())
->method('normalize')
->with($expectedObject, null, [BookNormalizer::class => true])
->willReturn([
'book' => 'https://openlibrary.org/books/OL2055137M.json',
'title' => 'Foundation',
'author' => 'Dan Simmons',
'condition' => BookCondition::NewCondition->value,
'reviews' => '/books/a528046c-7ba1-4acc-bff2-b5390ab17d41/reviews',
'rating' => 3,
])
;
$this->assertEquals([
'book' => 'https://openlibrary.org/books/OL2055137M.json',
'title' => 'Foundation',
'author' => 'Dan Simmons',
'condition' => BookCondition::NewCondition->value,
'reviews' => '/books/a528046c-7ba1-4acc-bff2-b5390ab17d41/reviews',
'rating' => 3,
], $this->normalizer->normalize($this->objectMock));
}
}