21
21
use ApiPlatform \Core \Bridge \Elasticsearch \Metadata \Document \Factory \DocumentMetadataFactoryInterface ;
22
22
use ApiPlatform \Core \Bridge \Elasticsearch \Serializer \ItemNormalizer ;
23
23
use ApiPlatform \Core \DataProvider \ItemDataProviderInterface ;
24
+ use ApiPlatform \Core \Exception \ResourceClassNotFoundException ;
25
+ use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
26
+ use ApiPlatform \Core \Metadata \Resource \ResourceMetadata ;
24
27
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \CompositeRelation ;
25
28
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \Dummy ;
29
+ use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \DummyCar ;
30
+ use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \DummyCarColor ;
26
31
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \Foo ;
27
32
use Elasticsearch \Client ;
28
33
use Elasticsearch \Common \Exceptions \Missing404Exception ;
@@ -40,7 +45,8 @@ public function testConstruct()
40
45
$ this ->prophesize (Client::class)->reveal (),
41
46
$ this ->prophesize (DocumentMetadataFactoryInterface::class)->reveal (),
42
47
$ this ->prophesize (IdentifierExtractorInterface::class)->reveal (),
43
- $ this ->prophesize (DenormalizerInterface::class)->reveal ()
48
+ $ this ->prophesize (DenormalizerInterface::class)->reveal (),
49
+ $ this ->prophesize (ResourceMetadataFactoryInterface::class)->reveal ()
44
50
)
45
51
);
46
52
}
@@ -56,16 +62,26 @@ public function testSupports()
56
62
$ identifierExtractorProphecy ->getIdentifierFromResourceClass (Foo::class)->willReturn ('id ' )->shouldBeCalled ();
57
63
$ identifierExtractorProphecy ->getIdentifierFromResourceClass (CompositeRelation::class)->willThrow (new NonUniqueIdentifierException ())->shouldBeCalled ();
58
64
65
+ $ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
66
+ $ resourceMetadataFactoryProphecy ->create (Foo::class)->shouldBeCalled ()->willReturn (new ResourceMetadata ());
67
+ $ resourceMetadataFactoryProphecy ->create (Dummy::class)->shouldBeCalled ()->willReturn (new ResourceMetadata ());
68
+ $ resourceMetadataFactoryProphecy ->create (CompositeRelation::class)->shouldBeCalled ()->willReturn (new ResourceMetadata ());
69
+ $ resourceMetadataFactoryProphecy ->create (DummyCar::class)->shouldBeCalled ()->willReturn ((new ResourceMetadata ())->withAttributes (['elasticsearch ' => false ]));
70
+ $ resourceMetadataFactoryProphecy ->create (DummyCarColor::class)->shouldBeCalled ()->willThrow (new ResourceClassNotFoundException ());
71
+
59
72
$ itemDataProvider = new ItemDataProvider (
60
73
$ this ->prophesize (Client::class)->reveal (),
61
74
$ documentMetadataFactoryProphecy ->reveal (),
62
75
$ identifierExtractorProphecy ->reveal (),
63
- $ this ->prophesize (DenormalizerInterface::class)->reveal ()
76
+ $ this ->prophesize (DenormalizerInterface::class)->reveal (),
77
+ $ resourceMetadataFactoryProphecy ->reveal ()
64
78
);
65
79
66
80
self ::assertTrue ($ itemDataProvider ->supports (Foo::class));
67
81
self ::assertFalse ($ itemDataProvider ->supports (Dummy::class));
68
82
self ::assertFalse ($ itemDataProvider ->supports (CompositeRelation::class));
83
+ self ::assertFalse ($ itemDataProvider ->supports (DummyCar::class));
84
+ self ::assertFalse ($ itemDataProvider ->supports (DummyCarColor::class));
69
85
}
70
86
71
87
public function testGetItem ()
@@ -99,7 +115,9 @@ public function testGetItem()
99
115
$ denormalizerProphecy = $ this ->prophesize (DenormalizerInterface::class);
100
116
$ denormalizerProphecy ->denormalize ($ document , Foo::class, ItemNormalizer::FORMAT , [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true ])->willReturn ($ foo )->shouldBeCalled ();
101
117
102
- $ itemDataProvider = new ItemDataProvider ($ clientProphecy ->reveal (), $ documentMetadataFactoryProphecy ->reveal (), $ identifierExtractorProphecy ->reveal (), $ denormalizerProphecy ->reveal ());
118
+ $ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
119
+
120
+ $ itemDataProvider = new ItemDataProvider ($ clientProphecy ->reveal (), $ documentMetadataFactoryProphecy ->reveal (), $ identifierExtractorProphecy ->reveal (), $ denormalizerProphecy ->reveal (), $ resourceMetadataFactoryProphecy ->reveal ());
103
121
104
122
self ::assertSame ($ foo , $ itemDataProvider ->getItem (Foo::class, ['id ' => 1 ]));
105
123
}
@@ -115,7 +133,9 @@ public function testGetItemWithMissing404Exception()
115
133
$ clientProphecy = $ this ->prophesize (Client::class);
116
134
$ clientProphecy ->get (['index ' => 'foo ' , 'type ' => DocumentMetadata::DEFAULT_TYPE , 'id ' => '404 ' ])->willThrow (new Missing404Exception ())->shouldBeCalled ();
117
135
118
- $ itemDataProvider = new ItemDataProvider ($ clientProphecy ->reveal (), $ documentMetadataFactoryProphecy ->reveal (), $ identifierExtractorProphecy ->reveal (), $ this ->prophesize (DenormalizerInterface::class)->reveal ());
136
+ $ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
137
+
138
+ $ itemDataProvider = new ItemDataProvider ($ clientProphecy ->reveal (), $ documentMetadataFactoryProphecy ->reveal (), $ identifierExtractorProphecy ->reveal (), $ this ->prophesize (DenormalizerInterface::class)->reveal (), $ resourceMetadataFactoryProphecy ->reveal ());
119
139
120
140
self ::assertNull ($ itemDataProvider ->getItem (Foo::class, ['id ' => 404 ]));
121
141
}
0 commit comments