66use Orisai \Exceptions \Logic \InvalidArgument ;
77use Orisai \ObjectMapper \MappedObject ;
88use Orisai \ObjectMapper \Meta \Source \AnnotationsMetaSource ;
9+ use Orisai \ObjectMapper \Meta \Source \AttributesMetaSource ;
910use Orisai \ObjectMapper \Meta \Source \ReflectorMetaSource ;
1011use Orisai \ReflectionMeta \Structure \StructureBuilder ;
1112use Orisai \ReflectionMeta \Structure \StructureFlattener ;
1213use Orisai \ReflectionMeta \Structure \StructureGroup ;
1314use Orisai \ReflectionMeta \Structure \StructureGrouper ;
1415use PHPUnit \Framework \TestCase ;
1516use ReflectionClass ;
17+ use Tests \Orisai \ObjectMapper \Doubles \Invalid \DefinitionAboveConstantVO ;
18+ use Tests \Orisai \ObjectMapper \Doubles \Invalid \DefinitionAboveMethodVO ;
19+ use Tests \Orisai \ObjectMapper \Doubles \Invalid \DefinitionAboveParameterVO ;
1620use Tests \Orisai \ObjectMapper \Doubles \Invalid \RuleAboveClassChildVO ;
1721use Tests \Orisai \ObjectMapper \Doubles \Invalid \RuleAboveClassVO ;
1822use Tests \Orisai \ObjectMapper \Doubles \Invalid \UnsupportedClassDefinitionVO ;
1923use Tests \Orisai \ObjectMapper \Doubles \Invalid \UnsupportedPropertyDefinitionVO ;
24+ use const PHP_VERSION_ID ;
2025
2126final class ReflectorMetaSourceTest extends TestCase
2227{
2328
24- private ReflectorMetaSource $ source ;
29+ private ReflectorMetaSource $ annotationsSource ;
30+
31+ private ReflectorMetaSource $ attributesSource ;
2532
2633 protected function setUp (): void
2734 {
28- $ this ->source = new AnnotationsMetaSource ();
35+ $ this ->annotationsSource = new AnnotationsMetaSource ();
36+
37+ if (PHP_VERSION_ID >= 8_00_00 ) {
38+ $ this ->attributesSource = new AttributesMetaSource ();
39+ }
2940 }
3041
3142 /**
@@ -40,6 +51,62 @@ private function createStructureGroup(ReflectionClass $class): StructureGroup
4051 );
4152 }
4253
54+ /**
55+ * @param class-string<MappedObject> $class
56+ *
57+ * @dataProvider provideUnsupportedDefinition
58+ */
59+ public function testUnsupportedDefinitionLocation (string $ class , string $ errorMessage ): void
60+ {
61+ if (PHP_VERSION_ID < 8_00_00 ) {
62+ self ::markTestSkipped ('Attributes are supported on PHP 8.0+ ' );
63+ }
64+
65+ $ reflector = new ReflectionClass ($ class );
66+ $ group = $ this ->createStructureGroup ($ reflector );
67+
68+ $ this ->expectException (InvalidArgument::class);
69+ $ this ->expectExceptionMessage ($ errorMessage );
70+
71+ $ this ->attributesSource ->load ($ reflector , $ group );
72+ }
73+
74+ public function provideUnsupportedDefinition (): Generator
75+ {
76+ yield [
77+ DefinitionAboveConstantVO::class,
78+ <<<'MSG'
79+ Context: Resolving metadata of mapped object
80+ 'Tests\Orisai\ObjectMapper\Doubles\Invalid\DefinitionAboveConstantVO'.
81+ Problem: Definitions are not allowed on constants.
82+ Solution: Remove definition from
83+ 'Tests\Orisai\ObjectMapper\Doubles\Invalid\DefinitionAboveConstantVO::Test'.
84+ MSG,
85+ ];
86+
87+ yield [
88+ DefinitionAboveMethodVO::class,
89+ <<<'MSG'
90+ Context: Resolving metadata of mapped object
91+ 'Tests\Orisai\ObjectMapper\Doubles\Invalid\DefinitionAboveMethodVO'.
92+ Problem: Definitions are not allowed on methods.
93+ Solution: Remove definition from
94+ 'Tests\Orisai\ObjectMapper\Doubles\Invalid\DefinitionAboveMethodVO->test()'.
95+ MSG,
96+ ];
97+
98+ yield [
99+ DefinitionAboveParameterVO::class,
100+ <<<'MSG'
101+ Context: Resolving metadata of mapped object
102+ 'Tests\Orisai\ObjectMapper\Doubles\Invalid\DefinitionAboveParameterVO'.
103+ Problem: Definitions are not allowed on parameters.
104+ Solution: Remove definition from
105+ 'Tests\Orisai\ObjectMapper\Doubles\Invalid\DefinitionAboveParameterVO->test(test)'.
106+ MSG,
107+ ];
108+ }
109+
43110 /**
44111 * @param class-string<MappedObject> $class
45112 *
@@ -60,7 +127,7 @@ public function testUnsupportedDefinitionType(string $class): void
60127 . "'Orisai\ObjectMapper\Rules\RuleDefinition'. " ,
61128 );
62129
63- $ this ->source ->load ($ reflector , $ group );
130+ $ this ->annotationsSource ->load ($ reflector , $ group );
64131 }
65132
66133 public function provideUnsupportedDefinitionType (): Generator
@@ -90,7 +157,7 @@ public function testRuleAboveClassRelativeName(): void
90157MSG,
91158 );
92159
93- $ this ->source ->load ($ reflector , $ group );
160+ $ this ->annotationsSource ->load ($ reflector , $ group );
94161 }
95162
96163 public function testRuleAboveClassFullName (): void
@@ -111,7 +178,7 @@ public function testRuleAboveClassFullName(): void
111178MSG,
112179 );
113180
114- $ this ->source ->load ($ reflector , $ group );
181+ $ this ->annotationsSource ->load ($ reflector , $ group );
115182 }
116183
117184}
0 commit comments