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

api-platform / core / 7142557150

08 Dec 2023 02:28PM UTC coverage: 36.003% (-1.4%) from 37.36%
7142557150

push

github

web-flow
fix(jsonld): remove link to ApiDocumentation when doc is disabled (#6029)

0 of 1 new or added line in 1 file covered. (0.0%)

2297 existing lines in 182 files now uncovered.

9992 of 27753 relevant lines covered (36.0%)

147.09 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\CacheableSupportsMethodInterface;
23
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
24
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
25
use Symfony\Component\Serializer\Serializer;
26
use Symfony\Component\Serializer\SerializerAwareInterface;
27
use Symfony\Component\Serializer\SerializerInterface;
28

29
final class ItemNormalizerTest extends TestCase
30
{
31
    use ProphecyTrait;
32

33
    private $normalizerProphecy;
34
    private ItemNormalizer $itemNormalizer;
35

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

43
        if (!method_exists(Serializer::class, 'getSupportedTypes')) {
×
44
            $this->normalizerProphecy->willImplement(CacheableSupportsMethodInterface::class);
×
45
        }
46

47
        $this->itemNormalizer = new ItemNormalizer($this->normalizerProphecy->reveal());
×
48
    }
49

50
    public function testConstruct(): void
51
    {
52
        self::assertInstanceOf(NormalizerInterface::class, $this->itemNormalizer);
×
53
        self::assertInstanceOf(DenormalizerInterface::class, $this->itemNormalizer);
×
54
        self::assertInstanceOf(SerializerAwareInterface::class, $this->itemNormalizer);
×
55
    }
56

57
    /**
58
     * @group legacy
59
     */
60
    public function testHasCacheableSupportsMethod(): void
61
    {
62
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
63
            $this->markTestSkipped('Symfony Serializer >= 6.3');
×
64
        }
65

66
        $this->normalizerProphecy->hasCacheableSupportsMethod()->willReturn(true)->shouldBeCalledOnce();
×
67

68
        self::assertTrue($this->itemNormalizer->hasCacheableSupportsMethod());
×
69
    }
70

71
    public function testDenormalize(): void
72
    {
73
        $this->normalizerProphecy->denormalize('foo', 'string', 'json', ['groups' => 'foo'])->willReturn('foo')->shouldBeCalledOnce();
×
74

75
        self::assertEquals('foo', $this->itemNormalizer->denormalize('foo', 'string', 'json', ['groups' => 'foo']));
×
76
    }
77

78
    public function testSupportsDenormalization(): void
79
    {
80
        $this->normalizerProphecy->supportsDenormalization('foo', 'string', 'json', Argument::type('array'))->willReturn(true)->shouldBeCalledOnce();
×
81
        $this->normalizerProphecy->supportsDenormalization('foo', 'string', DocumentNormalizer::FORMAT, Argument::type('array'))->shouldNotBeCalled();
×
82

83
        self::assertTrue($this->itemNormalizer->supportsDenormalization('foo', 'string', 'json'));
×
84
        self::assertFalse($this->itemNormalizer->supportsDenormalization('foo', 'string', DocumentNormalizer::FORMAT));
×
85
    }
86

87
    public function testNormalize(): void
88
    {
89
        $this->normalizerProphecy->normalize($object = (object) ['foo'], 'json', ['groups' => 'foo'])->willReturn(['foo'])->shouldBeCalledOnce();
×
90

91
        self::assertEquals(['foo'], $this->itemNormalizer->normalize($object, 'json', ['groups' => 'foo']));
×
92
    }
93

94
    public function testSupportsNormalization(): void
95
    {
96
        $this->normalizerProphecy->supportsNormalization($object = (object) ['foo'], 'json')->willReturn(true)->shouldBeCalledOnce();
×
97
        $this->normalizerProphecy->supportsNormalization($object, DocumentNormalizer::FORMAT)->shouldNotBeCalled();
×
98

99
        self::assertTrue($this->itemNormalizer->supportsNormalization($object, 'json'));
×
100
        self::assertFalse($this->itemNormalizer->supportsNormalization($object, DocumentNormalizer::FORMAT));
×
101
    }
102

103
    public function testSetSerializer(): void
104
    {
105
        $this->normalizerProphecy->setSerializer($serializer = $this->prophesize(SerializerInterface::class)->reveal())->shouldBeCalledOnce();
×
106

107
        $this->itemNormalizer->setSerializer($serializer);
×
108
    }
109

110
    /**
111
     * @group legacy
112
     */
113
    public function testHasCacheableSupportsMethodWithDecoratedNormalizerNotAnInstanceOfCacheableSupportsMethodInterface(): void
114
    {
115
        $this->expectException(LogicException::class);
×
116
        $this->expectExceptionMessage(sprintf('The decorated normalizer must be an instance of "%s".', CacheableSupportsMethodInterface::class));
×
117

118
        (new ItemNormalizer($this->prophesize(NormalizerInterface::class)->reveal()))->hasCacheableSupportsMethod();
×
119
    }
120

121
    public function testDenormalizeWithDecoratedNormalizerNotAnInstanceOfDenormalizerInterface(): void
122
    {
123
        $this->expectException(LogicException::class);
×
124
        $this->expectExceptionMessage(sprintf('The decorated normalizer must be an instance of "%s".', DenormalizerInterface::class));
×
125

126
        (new ItemNormalizer($this->prophesize(NormalizerInterface::class)->reveal()))->denormalize('foo', 'string');
×
127
    }
128

129
    public function testSupportsDenormalizationWithDecoratedNormalizerNotAnInstanceOfDenormalizerInterface(): void
130
    {
131
        $this->expectException(LogicException::class);
×
132
        $this->expectExceptionMessage(sprintf('The decorated normalizer must be an instance of "%s".', DenormalizerInterface::class));
×
133

134
        (new ItemNormalizer($this->prophesize(NormalizerInterface::class)->reveal()))->supportsDenormalization('foo', 'string');
×
135
    }
136

137
    public function testSetSerializerWithDecoratedNormalizerNotAnInstanceOfSerializerAwareInterface(): void
138
    {
139
        $this->expectException(LogicException::class);
×
140
        $this->expectExceptionMessage(sprintf('The decorated normalizer must be an instance of "%s".', SerializerAwareInterface::class));
×
141

142
        (new ItemNormalizer($this->prophesize(NormalizerInterface::class)->reveal()))->setSerializer($this->prophesize(SerializerInterface::class)->reveal());
×
143
    }
144

145
    public function testGetSupportedTypes(): void
146
    {
147
        if (!method_exists(Serializer::class, 'getSupportedTypes')) {
×
148
            $this->markTestSkipped('Symfony Serializer < 6.3');
×
149
        }
150

151
        // TODO: use prophecy when getSupportedTypes() will be added to the interface
152
        $this->itemNormalizer = new ItemNormalizer(new class() implements NormalizerInterface {
×
UNCOV
153
            public function normalize(mixed $object, string $format = null, array $context = []): \ArrayObject|array|string|int|float|bool|null
×
154
            {
155
                return null;
×
156
            }
157

UNCOV
158
            public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
×
159
            {
160
                return true;
×
161
            }
162

163
            public function getSupportedTypes(?string $format): array
164
            {
165
                return ['*' => true];
×
166
            }
167
        });
×
168

169
        $this->assertEmpty($this->itemNormalizer->getSupportedTypes($this->itemNormalizer::FORMAT));
×
170
        $this->assertSame(['*' => true], $this->itemNormalizer->getSupportedTypes('json'));
×
171
    }
172
}
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