37
37
use ApiPlatform \Tests \Metadata \Extractor \Adapter \YamlResourceAdapter ;
38
38
use PHPUnit \Framework \AssertionFailedError ;
39
39
use PHPUnit \Framework \TestCase ;
40
+ use Symfony \Component \Serializer \NameConverter \CamelCaseToSnakeCaseNameConverter ;
40
41
41
42
/**
42
43
* Ensures XML and YAML mappings are fully compatible with ApiResource.
@@ -49,6 +50,9 @@ final class ResourceMetadataCompatibilityTest extends TestCase
49
50
50
51
private const RESOURCE_CLASS = Comment::class;
51
52
private const SHORT_NAME = 'Comment ' ;
53
+ private const DEFAULTS = [
54
+ 'route_prefix ' => '/v1 '
55
+ ];
52
56
private const FIXTURES = [
53
57
null ,
54
58
[
@@ -208,6 +212,10 @@ final class ResourceMetadataCompatibilityTest extends TestCase
208
212
'priority ' => 200 ,
209
213
'extraProperties ' => [
210
214
'foo ' => 'bar ' ,
215
+ 'custom_property ' => 'Lorem ipsum dolor sit amet ' ,
216
+ 'another_custom_property ' => [
217
+ 'Lorem ipsum ' => 'Dolor sit amet ' ,
218
+ ],
211
219
],
212
220
],
213
221
],
@@ -329,6 +337,10 @@ final class ResourceMetadataCompatibilityTest extends TestCase
329
337
'priority ' => 200 ,
330
338
'extraProperties ' => [
331
339
'foo ' => 'bar ' ,
340
+ 'custom_property ' => 'Lorem ipsum dolor sit amet ' ,
341
+ 'another_custom_property ' => [
342
+ 'Lorem ipsum ' => 'Dolor sit amet ' ,
343
+ ],
332
344
],
333
345
],
334
346
[
@@ -420,7 +432,7 @@ public function testValidMetadata(string $extractorClass, ResourceAdapterInterfa
420
432
421
433
try {
422
434
$ extractor = new $ extractorClass ($ adapter (self ::RESOURCE_CLASS , $ parameters , self ::FIXTURES ));
423
- $ factory = new ExtractorResourceMetadataCollectionFactory ($ extractor );
435
+ $ factory = new ExtractorResourceMetadataCollectionFactory ($ extractor, null , self :: DEFAULTS );
424
436
$ collection = $ factory ->create (self ::RESOURCE_CLASS );
425
437
} catch (\Exception $ exception ) {
426
438
throw new AssertionFailedError ('Failed asserting that the schema is valid according to ' .ApiResource::class, 0 , $ exception );
@@ -595,6 +607,7 @@ private function withGraphQlOperations(array $values, ?array $fixtures): array
595
607
596
608
private function getOperationWithDefaults (ApiResource $ resource , HttpOperation $ operation ): HttpOperation
597
609
{
610
+ // Inherit from resource defaults
598
611
foreach (get_class_methods ($ resource ) as $ methodName ) {
599
612
if (0 !== strpos ($ methodName , 'get ' )) {
600
613
continue ;
@@ -611,6 +624,64 @@ private function getOperationWithDefaults(ApiResource $resource, HttpOperation $
611
624
$ operation = $ operation ->{'with ' .substr ($ methodName , 3 )}($ value );
612
625
}
613
626
627
+ $ operation = $ operation ->withExtraProperties (array_merge (
628
+ $ resource ->getExtraProperties (),
629
+ $ operation ->getExtraProperties ()
630
+ ));
631
+
632
+ // Add global defaults attributes to the operation
633
+ $ operation = $ this ->addGlobalDefaults ($ operation );
634
+
635
+ if ($ operation ->getRouteName ()) {
636
+ /** @var HttpOperation $operation */
637
+ $ operation = $ operation ->withName ($ operation ->getRouteName ());
638
+ }
639
+
640
+ // Check for name conflict
641
+ if ($ operation ->getName () && null !== ($ operations = $ resource ->getOperations ())) {
642
+ if (!$ operations ->has ($ operation ->getName ())) {
643
+ return $ operation ;
644
+ }
645
+
646
+ /** @var HttpOperation $operation */
647
+ $ operation = $ operation ->withName ('' );
648
+ }
649
+
614
650
return $ operation ;
615
651
}
652
+
653
+ private function addGlobalDefaults (HttpOperation $ operation ): HttpOperation
654
+ {
655
+ if (!$ this ->camelCaseToSnakeCaseNameConverter ) {
656
+ $ this ->camelCaseToSnakeCaseNameConverter = new CamelCaseToSnakeCaseNameConverter ();
657
+ }
658
+
659
+ $ extraProperties = [];
660
+ foreach (self ::DEFAULTS as $ key => $ value ) {
661
+ $ upperKey = ucfirst ($ this ->camelCaseToSnakeCaseNameConverter ->denormalize ($ key ));
662
+ $ getter = 'get ' .$ upperKey ;
663
+
664
+ if (!method_exists ($ operation , $ getter )) {
665
+ if (!isset ($ extraProperties [$ key ])) {
666
+ $ extraProperties [$ key ] = $ value ;
667
+ }
668
+
669
+ continue ;
670
+ }
671
+
672
+ $ currentValue = $ operation ->{$ getter }();
673
+
674
+ if (\is_array ($ currentValue ) && $ currentValue ) {
675
+ $ operation = $ operation ->{'with ' .$ upperKey }(array_merge ($ value , $ currentValue ));
676
+ }
677
+
678
+ if (null !== $ currentValue ) {
679
+ continue ;
680
+ }
681
+
682
+ $ operation = $ operation ->{'with ' .$ upperKey }($ value );
683
+ }
684
+
685
+ return $ operation ->withExtraProperties (array_merge ($ extraProperties , $ operation ->getExtraProperties ()));
686
+ }
616
687
}
0 commit comments