14
14
namespace ApiPlatform \Core \Tests \DataProvider ;
15
15
16
16
use ApiPlatform \Core \DataProvider \ChainSubresourceDataProvider ;
17
+ use ApiPlatform \Core \DataProvider \RestrictedDataProviderInterface ;
17
18
use ApiPlatform \Core \DataProvider \SubresourceDataProviderInterface ;
18
19
use ApiPlatform \Core \Exception \ResourceClassNotSupportedException ;
19
20
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \Dummy ;
@@ -33,20 +34,80 @@ public function testGetSubresource()
33
34
34
35
$ context = ['identifiers ' => ['id ' => Dummy::class], 'property ' => 'relatedDummies ' ];
35
36
$ firstDataProvider = $ this ->prophesize (SubresourceDataProviderInterface::class);
37
+ $ firstDataProvider ->willImplement (RestrictedDataProviderInterface::class);
38
+ $ firstDataProvider ->supports (Dummy::class, 'get ' , $ context )->willReturn (false );
36
39
$ firstDataProvider ->getSubresource (Dummy::class, ['id ' => 1 ], $ context , 'get ' )->willReturn ([$ dummy , $ dummy2 ])->willThrow (ResourceClassNotSupportedException::class);
37
40
38
41
$ secondDataProvider = $ this ->prophesize (SubresourceDataProviderInterface::class);
42
+ $ secondDataProvider ->willImplement (RestrictedDataProviderInterface::class);
43
+ $ secondDataProvider ->supports (Dummy::class, 'get ' , $ context )->willReturn (true );
39
44
$ secondDataProvider ->getSubresource (Dummy::class, ['id ' => 1 ], $ context , 'get ' )->willReturn ([$ dummy , $ dummy2 ]);
40
45
41
46
$ thirdDataProvider = $ this ->prophesize (SubresourceDataProviderInterface::class);
47
+ $ thirdDataProvider ->willImplement (RestrictedDataProviderInterface::class);
48
+ $ thirdDataProvider ->supports (Dummy::class, 'get ' , $ context )->willReturn (true );
42
49
$ thirdDataProvider ->getSubresource (Dummy::class, ['id ' => 1 ], $ context , 'get ' )->willReturn ([$ dummy ]);
43
50
44
51
$ chainSubresourceDataProvider = new ChainSubresourceDataProvider ([$ firstDataProvider ->reveal (), $ secondDataProvider ->reveal (), $ thirdDataProvider ->reveal ()]);
45
52
46
53
$ this ->assertEquals ([$ dummy , $ dummy2 ], $ chainSubresourceDataProvider ->getSubresource (Dummy::class, ['id ' => 1 ], $ context , 'get ' ));
47
54
}
48
55
49
- public function testGetCollectionExeptions ()
56
+ public function testGetSubresourceExceptionsItem ()
57
+ {
58
+ $ context = ['collection ' => false ];
59
+ $ firstDataProvider = $ this ->prophesize (SubresourceDataProviderInterface::class);
60
+ $ firstDataProvider ->willImplement (RestrictedDataProviderInterface::class);
61
+ $ firstDataProvider ->supports (Dummy::class, 'get ' , $ context )->willReturn (false );
62
+
63
+ $ chainSubresourceDataProvider = new ChainSubresourceDataProvider ([$ firstDataProvider ->reveal ()]);
64
+
65
+ $ this ->assertNull ($ chainSubresourceDataProvider ->getSubresource (Dummy::class, ['id ' => 1 ], $ context , 'get ' ));
66
+ }
67
+
68
+ public function testGetSubresourceExceptionsCollection ()
69
+ {
70
+ $ context = ['collection ' => true ];
71
+ $ firstDataProvider = $ this ->prophesize (SubresourceDataProviderInterface::class);
72
+ $ firstDataProvider ->willImplement (RestrictedDataProviderInterface::class);
73
+ $ firstDataProvider ->supports (Dummy::class, 'get ' , $ context )->willReturn (false );
74
+
75
+ $ chainSubresourceDataProvider = new ChainSubresourceDataProvider ([$ firstDataProvider ->reveal ()]);
76
+
77
+ $ this ->assertEquals ([], $ chainSubresourceDataProvider ->getSubresource (Dummy::class, ['id ' => 1 ], $ context , 'get ' ));
78
+ }
79
+
80
+ /**
81
+ * @group legacy
82
+ * @expectedDeprecation Throwing a "ApiPlatform\Core\Exception\ResourceClassNotSupportedException" in a data provider is deprecated in favor of implementing "ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface"
83
+ */
84
+ public function testLegacyGetSubresource ()
85
+ {
86
+ $ dummy = new Dummy ();
87
+ $ dummy ->setName ('Rosa ' );
88
+ $ dummy2 = new Dummy ();
89
+ $ dummy2 ->setName ('Parks ' );
90
+
91
+ $ context = ['identifiers ' => ['id ' => Dummy::class], 'property ' => 'relatedDummies ' ];
92
+ $ firstDataProvider = $ this ->prophesize (SubresourceDataProviderInterface::class);
93
+ $ firstDataProvider ->getSubresource (Dummy::class, ['id ' => 1 ], $ context , 'get ' )->willReturn ([$ dummy , $ dummy2 ])->willThrow (ResourceClassNotSupportedException::class);
94
+
95
+ $ secondDataProvider = $ this ->prophesize (SubresourceDataProviderInterface::class);
96
+ $ secondDataProvider ->getSubresource (Dummy::class, ['id ' => 1 ], $ context , 'get ' )->willReturn ([$ dummy , $ dummy2 ]);
97
+
98
+ $ thirdDataProvider = $ this ->prophesize (SubresourceDataProviderInterface::class);
99
+ $ thirdDataProvider ->getSubresource (Dummy::class, ['id ' => 1 ], $ context , 'get ' )->willReturn ([$ dummy ]);
100
+
101
+ $ chainSubresourceDataProvider = new ChainSubresourceDataProvider ([$ firstDataProvider ->reveal (), $ secondDataProvider ->reveal (), $ thirdDataProvider ->reveal ()]);
102
+
103
+ $ this ->assertEquals ([$ dummy , $ dummy2 ], $ chainSubresourceDataProvider ->getSubresource (Dummy::class, ['id ' => 1 ], $ context , 'get ' ));
104
+ }
105
+
106
+ /**
107
+ * @group legacy
108
+ * @expectedDeprecation Throwing a "ApiPlatform\Core\Exception\ResourceClassNotSupportedException" in a data provider is deprecated in favor of implementing "ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface"
109
+ */
110
+ public function testLegacyGetCollectionExeptions ()
50
111
{
51
112
$ firstDataProvider = $ this ->prophesize (SubresourceDataProviderInterface::class);
52
113
$ firstDataProvider ->getSubresource ('notfound ' , ['id ' => 1 ], [], 'get ' )->willThrow (ResourceClassNotSupportedException::class);
0 commit comments