44
55namespace Consistence \Sentry \Metadata ;
66
7+ use Generator ;
78use PHPUnit \Framework \Assert ;
89
910class BidirectionalAssociationTest extends \PHPUnit \Framework \TestCase
@@ -30,45 +31,88 @@ public function testCreate(): void
3031 Assert::assertTrue ($ bidirectionalAssociation ->getTargetType ()->equalsValue (BidirectionalAssociationType::ONE ));
3132 }
3233
33- public function testGetTargetMethodForType (): void
34+ /**
35+ * @return mixed[][]|\Generator
36+ */
37+ public function getTargetMethodForTypeDataProvider (): Generator
3438 {
35- $ sentryMethods = [
36- new SentryMethod (
37- new SentryAccess ('get ' ),
38- 'getFoo ' ,
39- Visibility::get (Visibility::VISIBILITY_PUBLIC )
40- ),
41- new SentryMethod (
42- new SentryAccess ('set ' ),
43- 'setFoo ' ,
44- Visibility::get (Visibility::VISIBILITY_PUBLIC )
45- ),
39+ yield 'two public methods with different SentryAccess type ' => [
40+ 'sentryMethods ' => [
41+ new SentryMethod (
42+ new SentryAccess ('get ' ),
43+ 'getFoo ' ,
44+ Visibility::get (Visibility::VISIBILITY_PUBLIC )
45+ ),
46+ new SentryMethod (
47+ new SentryAccess ('set ' ),
48+ 'setFoo ' ,
49+ Visibility::get (Visibility::VISIBILITY_PUBLIC )
50+ ),
51+ ],
52+ 'visibility ' => Visibility::get (Visibility::VISIBILITY_PUBLIC ),
53+ 'expectedMethodName ' => 'setFoo ' ,
4654 ];
47- $ bidirectionalAssociation = new BidirectionalAssociation (
48- 'FooClass ' ,
49- 'fooProperty ' ,
50- BidirectionalAssociationType::get (BidirectionalAssociationType::ONE ),
51- $ sentryMethods
52- );
5355
54- $ targetMethod = $ bidirectionalAssociation ->getTargetMethodForType (
55- new SentryAccess ('set ' ),
56- Visibility::get (Visibility::VISIBILITY_PUBLIC )
57- );
58- Assert::assertSame ('setFoo ' , $ targetMethod ->getMethodName ());
59- Assert::assertTrue ($ targetMethod ->getMethodVisibility ()->equalsValue (Visibility::VISIBILITY_PUBLIC ));
60- Assert::assertTrue ($ targetMethod ->getSentryAccess ()->equals (new SentryAccess ('set ' )));
56+ yield 'single public method, looser visibility ' => [
57+ 'sentryMethods ' => [
58+ new SentryMethod (
59+ new SentryAccess ('set ' ),
60+ 'setFoo ' ,
61+ Visibility::get (Visibility::VISIBILITY_PUBLIC )
62+ ),
63+ ],
64+ 'visibility ' => Visibility::get (Visibility::VISIBILITY_PRIVATE ),
65+ 'expectedMethodName ' => 'setFoo ' ,
66+ ];
67+
68+ yield 'one public and one private method with same SentryAccess type, pick by visibility ' => [
69+ 'sentryMethods ' => [
70+ new SentryMethod (
71+ new SentryAccess ('set ' ),
72+ 'setPrivate ' ,
73+ Visibility::get (Visibility::VISIBILITY_PRIVATE )
74+ ),
75+ new SentryMethod (
76+ new SentryAccess ('set ' ),
77+ 'setPublic ' ,
78+ Visibility::get (Visibility::VISIBILITY_PUBLIC )
79+ ),
80+ ],
81+ 'visibility ' => Visibility::get (Visibility::VISIBILITY_PUBLIC ),
82+ 'expectedMethodName ' => 'setPublic ' ,
83+ ];
84+
85+ yield 'two public methods with same SentryAccess type, pick by order ' => [
86+ 'sentryMethods ' => [
87+ new SentryMethod (
88+ new SentryAccess ('set ' ),
89+ 'setFirst ' ,
90+ Visibility::get (Visibility::VISIBILITY_PUBLIC )
91+ ),
92+ new SentryMethod (
93+ new SentryAccess ('set ' ),
94+ 'setSecond ' ,
95+ Visibility::get (Visibility::VISIBILITY_PUBLIC )
96+ ),
97+ ],
98+ 'visibility ' => Visibility::get (Visibility::VISIBILITY_PUBLIC ),
99+ 'expectedMethodName ' => 'setFirst ' ,
100+ ];
61101 }
62102
63- public function testGetTargetMethodForTypeLooserVisibility (): void
103+ /**
104+ * @dataProvider getTargetMethodForTypeDataProvider
105+ *
106+ * @param \Consistence\Sentry\Metadata\SentryMethod[] $sentryMethods
107+ * @param \Consistence\Sentry\Metadata\Visibility $visibility
108+ * @param string $expectedMethodName
109+ */
110+ public function testGetTargetMethodForType (
111+ array $ sentryMethods ,
112+ Visibility $ visibility ,
113+ string $ expectedMethodName
114+ ): void
64115 {
65- $ sentryMethods = [
66- new SentryMethod (
67- new SentryAccess ('set ' ),
68- 'setFoo ' ,
69- Visibility::get (Visibility::VISIBILITY_PUBLIC )
70- ),
71- ];
72116 $ bidirectionalAssociation = new BidirectionalAssociation (
73117 'FooClass ' ,
74118 'fooProperty ' ,
@@ -78,9 +122,9 @@ public function testGetTargetMethodForTypeLooserVisibility(): void
78122
79123 $ targetMethod = $ bidirectionalAssociation ->getTargetMethodForType (
80124 new SentryAccess ('set ' ),
81- Visibility:: get (Visibility:: VISIBILITY_PRIVATE )
125+ $ visibility
82126 );
83- Assert::assertSame (' setFoo ' , $ targetMethod ->getMethodName ());
127+ Assert::assertSame ($ expectedMethodName , $ targetMethod ->getMethodName ());
84128 Assert::assertTrue ($ targetMethod ->getMethodVisibility ()->equalsValue (Visibility::VISIBILITY_PUBLIC ));
85129 Assert::assertTrue ($ targetMethod ->getSentryAccess ()->equals (new SentryAccess ('set ' )));
86130 }
@@ -114,64 +158,4 @@ public function testGetTargetMethodForTypeRequiredVisibilityNotFound(): void
114158 }
115159 }
116160
117- public function testGetTargetMethodForTypePickByVisibility (): void
118- {
119- $ sentryMethods = [
120- new SentryMethod (
121- new SentryAccess ('set ' ),
122- 'setPrivate ' ,
123- Visibility::get (Visibility::VISIBILITY_PRIVATE )
124- ),
125- new SentryMethod (
126- new SentryAccess ('set ' ),
127- 'setPublic ' ,
128- Visibility::get (Visibility::VISIBILITY_PUBLIC )
129- ),
130- ];
131- $ bidirectionalAssociation = new BidirectionalAssociation (
132- 'FooClass ' ,
133- 'fooProperty ' ,
134- BidirectionalAssociationType::get (BidirectionalAssociationType::ONE ),
135- $ sentryMethods
136- );
137-
138- $ targetMethod = $ bidirectionalAssociation ->getTargetMethodForType (
139- new SentryAccess ('set ' ),
140- Visibility::get (Visibility::VISIBILITY_PUBLIC )
141- );
142- Assert::assertSame ('setPublic ' , $ targetMethod ->getMethodName ());
143- Assert::assertTrue ($ targetMethod ->getMethodVisibility ()->equalsValue (Visibility::VISIBILITY_PUBLIC ));
144- Assert::assertTrue ($ targetMethod ->getSentryAccess ()->equals (new SentryAccess ('set ' )));
145- }
146-
147- public function testGetTargetMethodForTypeMultipleSentryAccessPickByOrder (): void
148- {
149- $ sentryMethods = [
150- new SentryMethod (
151- new SentryAccess ('set ' ),
152- 'setFirst ' ,
153- Visibility::get (Visibility::VISIBILITY_PUBLIC )
154- ),
155- new SentryMethod (
156- new SentryAccess ('set ' ),
157- 'setSecond ' ,
158- Visibility::get (Visibility::VISIBILITY_PUBLIC )
159- ),
160- ];
161- $ bidirectionalAssociation = new BidirectionalAssociation (
162- 'FooClass ' ,
163- 'fooProperty ' ,
164- BidirectionalAssociationType::get (BidirectionalAssociationType::ONE ),
165- $ sentryMethods
166- );
167-
168- $ targetMethod = $ bidirectionalAssociation ->getTargetMethodForType (
169- new SentryAccess ('set ' ),
170- Visibility::get (Visibility::VISIBILITY_PUBLIC )
171- );
172- Assert::assertSame ('setFirst ' , $ targetMethod ->getMethodName ());
173- Assert::assertTrue ($ targetMethod ->getMethodVisibility ()->equalsValue (Visibility::VISIBILITY_PUBLIC ));
174- Assert::assertTrue ($ targetMethod ->getSentryAccess ()->equals (new SentryAccess ('set ' )));
175- }
176-
177161}
0 commit comments