14
14
namespace ApiPlatform \Core \Tests \DataProvider ;
15
15
16
16
use ApiPlatform \Core \DataProvider \ChainItemDataProvider ;
17
+ use ApiPlatform \Core \DataProvider \DenormalizedIdentifiersAwareItemDataProviderInterface ;
17
18
use ApiPlatform \Core \DataProvider \ItemDataProviderInterface ;
18
19
use ApiPlatform \Core \DataProvider \RestrictedDataProviderInterface ;
19
20
use ApiPlatform \Core \Exception \ResourceClassNotSupportedException ;
21
+ use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \CompositePrimitiveItem ;
20
22
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \Dummy ;
21
23
use PHPUnit \Framework \TestCase ;
22
24
@@ -32,14 +34,42 @@ public function testGetItem()
32
34
$ dummy = new Dummy ();
33
35
$ dummy ->setName ('Lucie ' );
34
36
37
+ $ firstDataProvider = $ this ->prophesize (DenormalizedIdentifiersAwareItemDataProviderInterface::class);
38
+ $ firstDataProvider ->willImplement (RestrictedDataProviderInterface::class);
39
+ $ firstDataProvider ->supports (Dummy::class, null , [])->willReturn (false );
40
+
41
+ $ secondDataProvider = $ this ->prophesize (DenormalizedIdentifiersAwareItemDataProviderInterface::class);
42
+ $ secondDataProvider ->willImplement (RestrictedDataProviderInterface::class);
43
+ $ secondDataProvider ->supports (Dummy::class, null , [])->willReturn (true );
44
+ $ secondDataProvider ->getItem (Dummy::class, ['id ' => 1 ], null , [])->willReturn ($ dummy );
45
+
46
+ $ thirdDataProvider = $ this ->prophesize (DenormalizedIdentifiersAwareItemDataProviderInterface::class);
47
+ $ thirdDataProvider ->willImplement (RestrictedDataProviderInterface::class);
48
+ $ thirdDataProvider ->supports (Dummy::class, null , [])->willReturn (true );
49
+ $ thirdDataProvider ->getItem (Dummy::class, ['id ' => 1 ], null , [])->willReturn (new \stdClass ());
50
+
51
+ $ chainItemDataProvider = new ChainItemDataProvider ([
52
+ $ firstDataProvider ->reveal (),
53
+ $ secondDataProvider ->reveal (),
54
+ $ thirdDataProvider ->reveal (),
55
+ ]);
56
+
57
+ $ this ->assertEquals ($ dummy , $ chainItemDataProvider ->getItem (Dummy::class, ['id ' => 1 ]));
58
+ }
59
+
60
+ public function testGetItemWithoutDenormalizedIdentifiers ()
61
+ {
62
+ $ dummy = new Dummy ();
63
+ $ dummy ->setName ('Lucie ' );
64
+
35
65
$ firstDataProvider = $ this ->prophesize (ItemDataProviderInterface::class);
36
66
$ firstDataProvider ->willImplement (RestrictedDataProviderInterface::class);
37
67
$ firstDataProvider ->supports (Dummy::class, null , [])->willReturn (false );
38
68
39
69
$ secondDataProvider = $ this ->prophesize (ItemDataProviderInterface::class);
40
70
$ secondDataProvider ->willImplement (RestrictedDataProviderInterface::class);
41
71
$ secondDataProvider ->supports (Dummy::class, null , [])->willReturn (true );
42
- $ secondDataProvider ->getItem (Dummy::class, 1 , null , [])->willReturn ($ dummy );
72
+ $ secondDataProvider ->getItem (Dummy::class, ' 1 ' , null , [])->willReturn ($ dummy );
43
73
44
74
$ thirdDataProvider = $ this ->prophesize (ItemDataProviderInterface::class);
45
75
$ thirdDataProvider ->willImplement (RestrictedDataProviderInterface::class);
@@ -52,7 +82,7 @@ public function testGetItem()
52
82
$ thirdDataProvider ->reveal (),
53
83
]);
54
84
55
- $ this ->assertEquals ($ dummy , $ chainItemDataProvider ->getItem (Dummy::class, 1 ));
85
+ $ this ->assertEquals ($ dummy , $ chainItemDataProvider ->getItem (Dummy::class, [ ' id ' => 1 ] ));
56
86
}
57
87
58
88
public function testGetItemExceptions ()
@@ -86,7 +116,27 @@ public function testLegacyGetItem()
86
116
87
117
$ chainItemDataProvider = new ChainItemDataProvider ([$ firstDataProvider ->reveal (), $ secondDataProvider ->reveal (), $ thirdDataProvider ->reveal ()]);
88
118
89
- $ this ->assertEquals ($ dummy , $ chainItemDataProvider ->getItem (Dummy::class, 1 ));
119
+ $ chainItemDataProvider ->getItem (Dummy::class, 1 );
120
+ }
121
+
122
+ /**
123
+ * @group legacy
124
+ * @expectedDeprecation Receiving "$id" as non-array in an item data provider is deprecated in 2.3 in favor of implementing "ApiPlatform\Core\DataProvider\DenormalizedIdentifiersAwareItemDataProviderInterface".
125
+ */
126
+ public function testLegacyGetItemWithoutDenormalizedIdentifiersAndCompositeIdentifier ()
127
+ {
128
+ $ dummy = new CompositePrimitiveItem ('Lucie ' , 1984 );
129
+
130
+ $ dataProvider = $ this ->prophesize (ItemDataProviderInterface::class);
131
+ $ dataProvider ->willImplement (RestrictedDataProviderInterface::class);
132
+ $ dataProvider ->supports (CompositePrimitiveItem::class, null , [])->willReturn (true );
133
+ $ dataProvider ->getItem (CompositePrimitiveItem::class, 'name=Lucie;year=1984 ' , null , [])->willReturn ($ dummy );
134
+
135
+ $ chainItemDataProvider = new ChainItemDataProvider ([
136
+ $ dataProvider ->reveal (),
137
+ ]);
138
+
139
+ $ this ->assertEquals ($ dummy , $ chainItemDataProvider ->getItem (CompositePrimitiveItem::class, ['name ' => 'Lucie ' , 'year ' => 1984 ]));
90
140
}
91
141
92
142
/**
0 commit comments