• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10315659289

09 Aug 2024 07:49AM UTC coverage: 7.841% (-0.006%) from 7.847%
10315659289

push

github

soyuka
style: cs fixes

70 of 529 new or added lines in 176 files covered. (13.23%)

160 existing lines in 58 files now uncovered.

12688 of 161818 relevant lines covered (7.84%)

26.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Elasticsearch/Tests/Serializer/ItemNormalizerTest.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Elasticsearch\Tests\Serializer;
15

16
use ApiPlatform\Elasticsearch\Serializer\DocumentNormalizer;
17
use ApiPlatform\Elasticsearch\Serializer\ItemNormalizer;
18
use PHPUnit\Framework\TestCase;
19
use Prophecy\Argument;
20
use Prophecy\PhpUnit\ProphecyTrait;
21
use Symfony\Component\Serializer\Exception\LogicException;
22
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
23
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
24
use Symfony\Component\Serializer\SerializerAwareInterface;
25
use Symfony\Component\Serializer\SerializerInterface;
26

27
final class ItemNormalizerTest extends TestCase
28
{
29
    use ProphecyTrait;
30

31
    private $normalizerProphecy;
32
    private ItemNormalizer $itemNormalizer;
33

34
    protected function setUp(): void
35
    {
36
        $this->normalizerProphecy = $this
×
37
            ->prophesize(NormalizerInterface::class)
×
38
            ->willImplement(DenormalizerInterface::class)
×
39
            ->willImplement(SerializerAwareInterface::class);
×
40

41
        $this->itemNormalizer = new ItemNormalizer($this->normalizerProphecy->reveal());
×
42
    }
43

44
    public function testConstruct(): void
45
    {
46
        self::assertInstanceOf(NormalizerInterface::class, $this->itemNormalizer);
×
47
        self::assertInstanceOf(DenormalizerInterface::class, $this->itemNormalizer);
×
48
        self::assertInstanceOf(SerializerAwareInterface::class, $this->itemNormalizer);
×
49
    }
50

51
    public function testDenormalize(): void
52
    {
53
        $this->normalizerProphecy->denormalize('foo', 'string', 'json', ['groups' => 'foo'])->willReturn('foo')->shouldBeCalledOnce();
×
54

55
        self::assertEquals('foo', $this->itemNormalizer->denormalize('foo', 'string', 'json', ['groups' => 'foo']));
×
56
    }
57

58
    public function testSupportsDenormalization(): void
59
    {
60
        $this->normalizerProphecy->supportsDenormalization('foo', 'string', 'json', Argument::type('array'))->willReturn(true)->shouldBeCalledOnce();
×
61
        $this->normalizerProphecy->supportsDenormalization('foo', 'string', DocumentNormalizer::FORMAT, Argument::type('array'))->shouldNotBeCalled();
×
62

63
        self::assertTrue($this->itemNormalizer->supportsDenormalization('foo', 'string', 'json'));
×
64
        self::assertFalse($this->itemNormalizer->supportsDenormalization('foo', 'string', DocumentNormalizer::FORMAT));
×
65
    }
66

67
    public function testNormalize(): void
68
    {
69
        $this->normalizerProphecy->normalize($object = (object) ['foo'], 'json', ['groups' => 'foo'])->willReturn(['foo'])->shouldBeCalledOnce();
×
70

71
        self::assertEquals(['foo'], $this->itemNormalizer->normalize($object, 'json', ['groups' => 'foo']));
×
72
    }
73

74
    public function testSupportsNormalization(): void
75
    {
76
        $this->normalizerProphecy->supportsNormalization($object = (object) ['foo'], 'json')->willReturn(true)->shouldBeCalledOnce();
×
77
        $this->normalizerProphecy->supportsNormalization($object, DocumentNormalizer::FORMAT)->shouldNotBeCalled();
×
78

79
        self::assertTrue($this->itemNormalizer->supportsNormalization($object, 'json'));
×
80
        self::assertFalse($this->itemNormalizer->supportsNormalization($object, DocumentNormalizer::FORMAT));
×
81
    }
82

83
    public function testSetSerializer(): void
84
    {
85
        $this->normalizerProphecy->setSerializer($serializer = $this->prophesize(SerializerInterface::class)->reveal())->shouldBeCalledOnce();
×
86

87
        $this->itemNormalizer->setSerializer($serializer);
×
88
    }
89

90
    public function testDenormalizeWithDecoratedNormalizerNotAnInstanceOfDenormalizerInterface(): void
91
    {
92
        $this->expectException(LogicException::class);
×
NEW
93
        $this->expectExceptionMessage(\sprintf('The decorated normalizer must be an instance of "%s".', DenormalizerInterface::class));
×
94

95
        (new ItemNormalizer($this->prophesize(NormalizerInterface::class)->reveal()))->denormalize('foo', 'string');
×
96
    }
97

98
    public function testSupportsDenormalizationWithDecoratedNormalizerNotAnInstanceOfDenormalizerInterface(): void
99
    {
100
        $this->expectException(LogicException::class);
×
NEW
101
        $this->expectExceptionMessage(\sprintf('The decorated normalizer must be an instance of "%s".', DenormalizerInterface::class));
×
102

103
        (new ItemNormalizer($this->prophesize(NormalizerInterface::class)->reveal()))->supportsDenormalization('foo', 'string');
×
104
    }
105

106
    public function testSetSerializerWithDecoratedNormalizerNotAnInstanceOfSerializerAwareInterface(): void
107
    {
108
        $this->expectException(LogicException::class);
×
NEW
109
        $this->expectExceptionMessage(\sprintf('The decorated normalizer must be an instance of "%s".', SerializerAwareInterface::class));
×
110

111
        (new ItemNormalizer($this->prophesize(NormalizerInterface::class)->reveal()))->setSerializer($this->prophesize(SerializerInterface::class)->reveal());
×
112
    }
113

114
    public function testGetSupportedTypes(): void
115
    {
116
        // TODO: use prophecy when getSupportedTypes() will be added to the interface
NEW
117
        $this->itemNormalizer = new ItemNormalizer(new class implements NormalizerInterface {
×
118
            public function normalize(mixed $object, ?string $format = null, array $context = []): \ArrayObject|array|string|int|float|bool|null
119
            {
120
                return null;
×
121
            }
122

123
            public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
124
            {
125
                return true;
×
126
            }
127

128
            public function getSupportedTypes(?string $format): array
129
            {
130
                return ['*' => true];
×
131
            }
132
        });
×
133

134
        $this->assertEmpty($this->itemNormalizer->getSupportedTypes($this->itemNormalizer::FORMAT));
×
135
        $this->assertSame(['*' => true], $this->itemNormalizer->getSupportedTypes('json'));
×
136
    }
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc