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

api-platform / core / 6183243940

14 Sep 2023 09:02AM UTC coverage: 36.924% (-0.08%) from 36.999%
6183243940

push

github

web-flow
feat(elasticsearch): filtering on nested fields (#5820)

* chore(elasticsearch): define a valid, known type for 'baz'

* fix(elasticsearch): fix nested object paths (api-platform/api-platform#1375)

* fix(elasticsearch): support additional nesting within collections (#5642)

---------

Co-authored-by: Colin O'Dell <colinodell@gmail.com>

57 of 57 new or added lines in 4 files covered. (100.0%)

10101 of 27356 relevant lines covered (36.92%)

19.94 hits per line

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

0.0
/src/Elasticsearch/Tests/Util/FieldDatatypeTraitTest.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\Util;
15

16
use ApiPlatform\Elasticsearch\Tests\Fixtures\Foo;
17
use ApiPlatform\Elasticsearch\Util\FieldDatatypeTrait;
18
use ApiPlatform\Metadata\ApiProperty;
19
use ApiPlatform\Metadata\Exception\PropertyNotFoundException;
20
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21
use ApiPlatform\Metadata\ResourceClassResolverInterface;
22
use PHPUnit\Framework\TestCase;
23
use Prophecy\PhpUnit\ProphecyTrait;
24
use Symfony\Component\PropertyInfo\Type;
25

26
class FieldDatatypeTraitTest extends TestCase
27
{
28
    use ProphecyTrait;
29

30
    public function testGetNestedFieldPath(): void
31
    {
32
        $fieldDatatype = $this->getValidFieldDatatype();
×
33

34
        self::assertSame('foo.bar', $fieldDatatype->getNestedFieldPath(Foo::class, 'foo.bar.baz'));
×
35
        self::assertSame('foo', $fieldDatatype->getNestedFieldPath(Foo::class, 'foo.baz'));
×
36
        self::assertNull($fieldDatatype->getNestedFieldPath(Foo::class, 'baz'));
×
37
    }
38

39
    public function testGetNestedFieldInNestedCollection(): void
40
    {
41
        $fieldDatatype = $this->getValidFieldDatatype();
×
42

43
        self::assertSame('bar.foo', $fieldDatatype->getNestedFieldPath(Foo::class, 'bar.foo.baz'));
×
44
        self::assertSame('bar', $fieldDatatype->getNestedFieldPath(Foo::class, 'bar.foo'));
×
45
        self::assertNull($fieldDatatype->getNestedFieldPath(Foo::class, 'baz'));
×
46
    }
47

48
    public function testGetNestedFieldPathWithPropertyNotFound(): void
49
    {
50
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
51
        $propertyMetadataFactoryProphecy->create(Foo::class, 'foo')->willThrow(new PropertyNotFoundException())->shouldBeCalled();
×
52

53
        $fieldDatatype = self::createFieldDatatypeInstance($propertyMetadataFactoryProphecy->reveal(), $this->prophesize(ResourceClassResolverInterface::class)->reveal());
×
54

55
        self::assertNull($fieldDatatype->getNestedFieldPath(Foo::class, 'foo.bar'));
×
56
    }
57

58
    public function testGetNestedFieldPathWithPropertyWithoutType(): void
59
    {
60
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
61
        $propertyMetadataFactoryProphecy->create(Foo::class, 'foo')->willReturn(new ApiProperty())->shouldBeCalled();
×
62

63
        $fieldDatatype = self::createFieldDatatypeInstance($propertyMetadataFactoryProphecy->reveal(),
×
64
            $this->prophesize(ResourceClassResolverInterface::class)->reveal());
×
65

66
        self::assertNull($fieldDatatype->getNestedFieldPath(Foo::class, 'foo.bar'));
×
67
    }
68

69
    public function testGetNestedFieldPathWithInvalidCollectionType(): void
70
    {
71
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
72
        $propertyMetadataFactoryProphecy->create(Foo::class, 'foo')->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)]))->shouldBeCalled();
×
73

74
        $fieldDatatype = self::createFieldDatatypeInstance($propertyMetadataFactoryProphecy->reveal(),
×
75
            $this->prophesize(ResourceClassResolverInterface::class)->reveal());
×
76

77
        self::assertNull($fieldDatatype->getNestedFieldPath(Foo::class, 'foo.bar'));
×
78
    }
79

80
    public function testIsNestedField(): void
81
    {
82
        $fieldDatatype = $this->getValidFieldDatatype();
×
83

84
        self::assertTrue($fieldDatatype->isNestedField(Foo::class, 'foo.bar.baz'));
×
85
        self::assertTrue($fieldDatatype->isNestedField(Foo::class, 'foo.baz'));
×
86
        self::assertFalse($fieldDatatype->isNestedField(Foo::class, 'baz'));
×
87
    }
88

89
    private function getValidFieldDatatype()
90
    {
91
        $fooType = new Type(Type::BUILTIN_TYPE_OBJECT, false, Foo::class);
×
92
        $barType = new Type(Type::BUILTIN_TYPE_ARRAY, false, Foo::class, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, Foo::class));
×
93
        $bazType = new Type(Type::BUILTIN_TYPE_STRING, false, Foo::class);
×
94

95
        $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
×
96
        $propertyMetadataFactoryProphecy->create(Foo::class, 'foo')->willReturn((new ApiProperty())->withBuiltinTypes([$fooType]))->shouldBeCalled();
×
97
        $propertyMetadataFactoryProphecy->create(Foo::class, 'bar')->willReturn((new ApiProperty())->withBuiltinTypes([$barType]))->shouldBeCalled();
×
98
        $propertyMetadataFactoryProphecy->create(Foo::class, 'baz')->willReturn((new ApiProperty())->withBuiltinTypes([$bazType]));
×
99

100
        $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
×
101
        $resourceClassResolverProphecy->isResourceClass(Foo::class)->willReturn(true)->shouldBeCalled();
×
102

103
        return self::createFieldDatatypeInstance($propertyMetadataFactoryProphecy->reveal(),
×
104
            $resourceClassResolverProphecy->reveal());
×
105
    }
106

107
    private static function createFieldDatatypeInstance(PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver)
108
    {
109
        return new class($propertyMetadataFactory, $resourceClassResolver) {
×
110
            use FieldDatatypeTrait {
111
                isNestedField as public;
112
                getNestedFieldPath as public;
113
            }
114

115
            public function __construct(PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver)
116
            {
117
                $this->propertyMetadataFactory = $propertyMetadataFactory;
×
118
                $this->resourceClassResolver = $resourceClassResolver;
×
119
            }
120
        };
×
121
    }
122
}
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