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

api-platform / core / 10014117656

19 Jul 2024 08:44PM UTC coverage: 7.856% (-56.3%) from 64.185%
10014117656

push

github

soyuka
Merge branch 'sf/remove-flag'

0 of 527 new or added lines in 83 files covered. (0.0%)

10505 existing lines in 362 files now uncovered.

12705 of 161727 relevant lines covered (7.86%)

26.85 hits per line

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

0.0
/src/Serializer/Tests/Filter/PropertyFilterTest.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\Serializer\Tests\Filter;
15

16
use ApiPlatform\OpenApi\Model\Parameter;
17
use ApiPlatform\Serializer\Filter\PropertyFilter;
18
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\DummyProperty;
19
use ApiPlatform\Serializer\Tests\Fixtures\Serializer\NameConverter\CustomConverter;
20
use PHPUnit\Framework\TestCase;
21
use Symfony\Component\HttpFoundation\Request;
22

23
/**
24
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
25
 */
26
class PropertyFilterTest extends TestCase
27
{
28
    public function testApply(): void
29
    {
30
        $request = new Request(['properties' => ['foo', 'bar', 'baz']]);
×
31
        $context = ['attributes' => ['foo', 'qux']];
×
32

33
        $propertyFilter = new PropertyFilter();
×
34
        $propertyFilter->apply($request, true, [], $context);
×
35

36
        $this->assertEquals(['attributes' => ['foo', 'qux', 'foo', 'bar', 'baz']], $context);
×
37
    }
38

39
    public function testApplyWithOverriding(): void
40
    {
41
        $request = new Request(['custom_properties' => ['foo', 'bar', 'baz']]);
×
42
        $context = ['attributes' => ['foo', 'qux']];
×
43

44
        $propertyFilter = new PropertyFilter('custom_properties', true);
×
45
        $propertyFilter->apply($request, false, [], $context);
×
46

47
        $this->assertEquals(['attributes' => ['foo', 'bar', 'baz']], $context);
×
48
    }
49

50
    public function testApplyWithoutPropertiesInRequest(): void
51
    {
52
        $context = ['attributes' => ['foo', 'bar']];
×
53

54
        $propertyFilter = new PropertyFilter();
×
55
        $propertyFilter->apply(new Request(), false, [], $context);
×
56

57
        $this->assertEquals(['attributes' => ['foo', 'bar']], $context);
×
58
    }
59

60
    public function testApplyWithPropertiesWhitelist(): void
61
    {
62
        $request = new Request(['properties' => ['foo', 'bar', 'baz']]);
×
63
        $context = ['attributes' => ['qux']];
×
64

65
        $propertyFilter = new PropertyFilter('properties', false, ['bar', 'fuz', 'foo']);
×
66
        $propertyFilter->apply($request, true, [], $context);
×
67

68
        $this->assertEquals(['attributes' => ['qux', 'foo', 'bar']], $context);
×
69
    }
70

71
    public function testApplyWithPropertiesWhitelistAndNestedProperty(): void
72
    {
73
        $request = new Request(['properties' => ['foo', 'bar', 'group' => ['baz' => ['baz', 'qux'], 'qux']]]);
×
74
        $context = ['attributes' => ['qux']];
×
75

76
        $propertyFilter = new PropertyFilter('properties', false, ['foo' => null, 'group' => ['baz' => ['qux']]]);
×
77
        $propertyFilter->apply($request, true, [], $context);
×
78

79
        $this->assertEquals(['attributes' => ['qux', 'foo', 'group' => ['baz' => ['qux']]]], $context);
×
80
    }
81

82
    public function testApplyWithPropertiesWhitelistNotMatchingAnyProperty(): void
83
    {
84
        $request = new Request(['properties' => ['foo', 'bar', 'group' => ['baz' => ['baz', 'qux'], 'qux']]]);
×
85
        $context = ['attributes' => ['qux']];
×
86

87
        $propertyFilter = new PropertyFilter('properties', false, ['fuz', 'fiz']);
×
88
        $propertyFilter->apply($request, true, [], $context);
×
89

90
        $this->assertEquals(['attributes' => ['qux']], $context);
×
91
    }
92

93
    public function testApplyWithPropertiesWhitelistAndOverriding(): void
94
    {
95
        $request = new Request(['properties' => ['foo', 'bar', 'baz']]);
×
96
        $context = ['attributes' => ['qux']];
×
97

98
        $propertyFilter = new PropertyFilter('properties', true, ['foo', 'baz']);
×
99
        $propertyFilter->apply($request, true, [], $context);
×
100

101
        $this->assertEquals(['attributes' => ['foo', 'baz']], $context);
×
102
    }
103

104
    public function testApplyWithPropertiesInPropertyFilterAttribute(): void
105
    {
106
        $request = new Request(['properties' => ['foo', 'bar', 'baz']], [], ['_api_filters' => ['properties' => ['fooz']]]);
×
107
        $context = ['attributes' => ['foo', 'qux']];
×
108

109
        $propertyFilter = new PropertyFilter();
×
110
        $propertyFilter->apply($request, true, [], $context);
×
111

112
        $this->assertEquals(['attributes' => ['foo', 'qux', 'fooz']], $context);
×
113
    }
114

115
    public function testApplyWithInvalidPropertiesInRequest(): void
116
    {
117
        $request = new Request(['properties' => 'foo,bar,baz']);
×
118
        $context = ['attributes' => ['foo', 'bar']];
×
119

120
        $propertyFilter = new PropertyFilter();
×
121
        $propertyFilter->apply($request, true, [], $context);
×
122

123
        $this->assertEquals(['attributes' => ['foo', 'bar']], $context);
×
124
    }
125

126
    public function testApplyWithNameConverter(): void
127
    {
128
        $request = new Request(['properties' => ['foo', 'bar', 'name_converted']]);
×
129
        $context = ['attributes' => ['foo', 'name_converted']];
×
130

131
        $propertyFilter = new PropertyFilter('properties', false, null, new CustomConverter());
×
132
        $propertyFilter->apply($request, true, [], $context);
×
133

134
        $this->assertEquals(['attributes' => ['foo', 'name_converted', 'foo', 'bar', 'nameConverted']], $context);
×
135
    }
136

137
    public function testApplyWithOverridingAndNameConverter(): void
138
    {
139
        $request = new Request(['custom_properties' => ['foo', 'bar', 'name_converted']]);
×
140
        $context = ['attributes' => ['foo', 'qux']];
×
141

142
        $propertyFilter = new PropertyFilter('custom_properties', true, null, new CustomConverter());
×
143
        $propertyFilter->apply($request, false, [], $context);
×
144

145
        $this->assertEquals(['attributes' => ['foo', 'bar', 'nameConverted']], $context);
×
146
    }
147

148
    public function testApplyWithoutPropertiesInRequestAndNameConverter(): void
149
    {
150
        $context = ['attributes' => ['foo', 'name_converted']];
×
151

152
        $propertyFilter = new PropertyFilter('properties', false, null, new CustomConverter());
×
153
        $propertyFilter->apply(new Request(), false, [], $context);
×
154

155
        $this->assertEquals(['attributes' => ['foo', 'name_converted']], $context);
×
156
    }
157

158
    public function testApplyWithPropertiesWhitelistAndNameConverter(): void
159
    {
160
        $request = new Request(['properties' => ['foo', 'name_converted', 'baz']]);
×
161
        $context = ['attributes' => ['qux']];
×
162

163
        $propertyFilter = new PropertyFilter('properties', false, ['nameConverted', 'fuz', 'foo'], new CustomConverter());
×
164
        $propertyFilter->apply($request, true, [], $context);
×
165

166
        $this->assertEquals(['attributes' => ['qux', 'foo', 'nameConverted']], $context);
×
167
    }
168

169
    public function testApplyWithPropertiesWhitelistWithNestedPropertyAndNameConverter(): void
170
    {
171
        $request = new Request(['properties' => ['foo', 'bar', 'name_converted' => ['baz' => ['baz', 'name_converted'], 'qux']]]);
×
172
        $context = ['attributes' => ['qux']];
×
173

174
        $propertyFilter = new PropertyFilter('properties', false, ['foo' => null, 'nameConverted' => ['baz' => ['nameConverted']]], new CustomConverter());
×
175
        $propertyFilter->apply($request, true, [], $context);
×
176

177
        $this->assertEquals(['attributes' => ['qux', 'foo', 'nameConverted' => ['baz' => ['nameConverted']]]], $context);
×
178
    }
179

180
    public function testApplyWithPropertiesWhitelistNotMatchingAnyPropertyAndNameConverter(): void
181
    {
182
        $request = new Request(['properties' => ['foo', 'bar', 'name_converted' => ['baz' => ['baz', 'name_converted'], 'qux']]]);
×
183
        $context = ['attributes' => ['qux']];
×
184

185
        $propertyFilter = new PropertyFilter('properties', false, ['fuz', 'fiz'], new CustomConverter());
×
186
        $propertyFilter->apply($request, true, [], $context);
×
187

188
        $this->assertEquals(['attributes' => ['qux']], $context);
×
189
    }
190

191
    public function testApplyWithPropertiesWhitelistAndOverridingAndNameConverter(): void
192
    {
193
        $request = new Request(['properties' => ['foo', 'bar', 'name_converted']]);
×
194
        $context = ['attributes' => ['qux']];
×
195

196
        $propertyFilter = new PropertyFilter('properties', true, ['foo', 'nameConverted'], new CustomConverter());
×
197
        $propertyFilter->apply($request, true, [], $context);
×
198

199
        $this->assertEquals(['attributes' => ['foo', 'nameConverted']], $context);
×
200
    }
201

202
    public function testApplyWithPropertiesInPropertyFilterAttributeAndNameConverter(): void
203
    {
204
        $request = new Request(['properties' => ['foo', 'bar', 'baz']], [], ['_api_filters' => ['properties' => ['name_converted']]]);
×
205
        $context = ['attributes' => ['foo', 'qux']];
×
206

207
        $propertyFilter = new PropertyFilter('properties', false, null, new CustomConverter());
×
208
        $propertyFilter->apply($request, true, [], $context);
×
209

210
        $this->assertEquals(['attributes' => ['foo', 'qux', 'nameConverted']], $context);
×
211
    }
212

213
    public function testGetDescription(): void
214
    {
215
        $propertyFilter = new PropertyFilter('custom_properties');
×
216
        $expectedDescription = [
×
217
            'custom_properties[]' => [
×
218
                'type' => 'string',
×
219
                'is_collection' => true,
×
220
                'required' => false,
×
221
                'description' => 'Allows you to reduce the response to contain only the properties you need. If your desired property is nested, you can address it using nested arrays. Example: custom_properties[]={propertyName}&custom_properties[]={anotherPropertyName}&custom_properties[{nestedPropertyParent}][]={nestedProperty}',
×
NEW
222
                'openapi' => new Parameter(
×
NEW
223
                    in: 'query',
×
NEW
224
                    description: 'Allows you to reduce the response to contain only the properties you need. If your desired property is nested, you can address it using nested arrays. Example: custom_properties[]={propertyName}&custom_properties[]={anotherPropertyName}&custom_properties[{nestedPropertyParent}][]={nestedProperty}',
×
NEW
225
                    name: 'custom_properties[]',
×
NEW
226
                    schema: [
×
227
                        'type' => 'array',
×
228
                        'items' => [
×
229
                            'type' => 'string',
×
230
                        ],
×
NEW
231
                    ]
×
NEW
232
                ),
×
233
            ],
×
234
        ];
×
235

NEW
236
        $this->assertEquals($expectedDescription, $propertyFilter->getDescription(DummyProperty::class));
×
237
    }
238
}
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