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

api-platform / core / 6545986262

17 Oct 2023 10:35AM UTC coverage: 37.305% (+0.02%) from 37.285%
6545986262

push

github

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

* feat(elasticsearch): filtering on nested fields

* Add additional checks and function doc for less confusion

* Fixing tests

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

10266 of 27519 relevant lines covered (37.31%)

19.99 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::assertNull($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('foo.foo.bar', $fieldDatatype->getNestedFieldPath(Foo::class, 'foo.foo.bar.baz'));
×
44
        self::assertNull($fieldDatatype->getNestedFieldPath(Foo::class, 'foo.foo.baz'));
×
45
        self::assertSame('foo.bar', $fieldDatatype->getNestedFieldPath(Foo::class, 'foo.bar.foo.baz'));
×
46
        self::assertSame('bar', $fieldDatatype->getNestedFieldPath(Foo::class, 'bar.foo'));
×
47
        self::assertNull($fieldDatatype->getNestedFieldPath(Foo::class, 'baz'));
×
48
    }
49

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

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

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

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

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

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

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

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

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

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

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

91
    private function getValidFieldDatatype()
92
    {
93
        $fooType = new Type(Type::BUILTIN_TYPE_OBJECT, false, Foo::class);
×
94
        $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));
×
95
        $bazType = new Type(Type::BUILTIN_TYPE_STRING, false, Foo::class);
×
96

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

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

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

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

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

© 2025 Coveralls, Inc