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

api-platform / core / 7358252084

29 Dec 2023 04:32PM UTC coverage: 37.248% (-0.1%) from 37.395%
7358252084

push

github

soyuka
Merge 3.2

6 of 8 new or added lines in 6 files covered. (75.0%)

44 existing lines in 2 files now uncovered.

10347 of 27779 relevant lines covered (37.25%)

28.62 hits per line

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

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

16
use ApiPlatform\Elasticsearch\Metadata\Document\Factory\DocumentMetadataFactoryInterface;
17
use ApiPlatform\Elasticsearch\Serializer\DocumentNormalizer;
18
use ApiPlatform\Elasticsearch\State\ItemProvider;
19
use ApiPlatform\Elasticsearch\Tests\Fixtures\Foo;
20
use ApiPlatform\Metadata\Get;
21
use Elastic\Elasticsearch\ClientInterface;
22
use PHPUnit\Framework\TestCase;
23
use Prophecy\PhpUnit\ProphecyTrait;
24
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
25
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
26

27
/**
28
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
29
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
30
 */
31
final class ItemProviderTest extends TestCase
32
{
33
    use ProphecyTrait;
34

35
    public function testConstruct(): void
36
    {
37
        if (interface_exists(ClientInterface::class)) {
×
38
            $this->markTestSkipped('Can not test using Elasticsearch 8.');
×
39
        }
40

41
        self::assertInstanceOf(
×
42
            ItemProvider::class,
×
43
            new ItemProvider(
×
44
                $this->prophesize(\Elasticsearch\Client::class)->reveal(),
×
45
                $this->prophesize(DocumentMetadataFactoryInterface::class)->reveal(),
×
46
                $this->prophesize(DenormalizerInterface::class)->reveal()
×
47
            )
×
48
        );
×
49
    }
50

51
    public function testGetItem(): void
52
    {
53
        if (interface_exists(ClientInterface::class)) {
×
54
            $this->markTestSkipped('Can not test using Elasticsearch 8.');
×
55
        }
56

57
        $documentMetadataFactoryProphecy = $this->prophesize(DocumentMetadataFactoryInterface::class);
×
58

59
        $document = [
×
60
            '_index' => 'foo',
×
61
            '_type' => '_doc',
×
62
            '_id' => '1',
×
63
            'found' => true,
×
64
            '_source' => [
×
65
                'id' => 1,
×
66
                'name' => 'Rossinière',
×
67
                'bar' => 'erèinissor',
×
68
            ],
×
69
        ];
×
70

71
        $foo = new Foo();
×
72
        $foo->setName('Rossinière');
×
73
        $foo->setBar('erèinissor');
×
74

75
        $clientProphecy = $this->prophesize(\Elasticsearch\Client::class);
×
76
        $clientProphecy->get(['index' => 'foo', 'id' => '1'])->willReturn($document)->shouldBeCalled();
×
77

78
        $denormalizerProphecy = $this->prophesize(DenormalizerInterface::class);
×
79
        $denormalizerProphecy->denormalize($document, Foo::class, DocumentNormalizer::FORMAT, [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true])->willReturn($foo)->shouldBeCalled();
×
80

81
        $itemDataProvider = new ItemProvider($clientProphecy->reveal(), $documentMetadataFactoryProphecy->reveal(), $denormalizerProphecy->reveal());
×
82

83
        self::assertSame($foo, $itemDataProvider->provide((new Get())->withClass(Foo::class)->withShortName('foo'), ['id' => 1]));
×
84
    }
85

86
    public function testGetInexistantItem(): void
87
    {
88
        if (interface_exists(ClientInterface::class)) {
×
89
            $this->markTestSkipped('Can not test using Elasticsearch 8.');
×
90
        }
91

92
        $documentMetadataFactoryProphecy = $this->prophesize(DocumentMetadataFactoryInterface::class);
×
93

NEW
94
        $clientClass = class_exists(\Elasticsearch\Client::class) ? \Elasticsearch\Client::class : ClientInterface::class;
×
95

96
        $clientProphecy = $this->prophesize($clientClass);
×
97
        $clientProphecy->get(['index' => 'foo', 'id' => '404'])->willReturn([
×
98
            '_index' => 'foo',
×
99
            '_type' => '_doc',
×
100
            '_id' => '404',
×
101
            'found' => false,
×
102
        ])->shouldBeCalled();
×
103

104
        $itemDataProvider = new ItemProvider($clientProphecy->reveal(), $documentMetadataFactoryProphecy->reveal(), $this->prophesize(DenormalizerInterface::class)->reveal());
×
105

106
        self::assertNull($itemDataProvider->provide((new Get())->withClass(Foo::class)->withShortName('foo'), ['id' => 404]));
×
107
    }
108
}
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