1313namespace Ecn \FeatureToggleBundle \Tests \Voters ;
1414
1515use Ecn \FeatureToggleBundle \Voters \RatioVoter ;
16+ use InvalidArgumentException ;
1617use PHPUnit \Framework \MockObject \MockObject ;
1718use PHPUnit \Framework \TestCase ;
1819use Symfony \Component \HttpFoundation \Session \Session ;
20+ use Symfony \Component \HttpFoundation \Session \SessionInterface ;
1921
2022/**
2123 * @author Pierre Groth <pierre@elbcoast.net>
@@ -32,71 +34,66 @@ public function testLowRatioVoterPass(): void
3234 $ this ->assertLessThan (100 - $ hits , $ hits );
3335 }
3436
35- protected function getRatioVoter ( $ ratio , $ sticky = false ): RatioVoter
37+ public function testStickyRatioVoterPassOnNullSession ( ): void
3638 {
37- // Create service stub
38- /** @var Session&MockObject $session */
39- $ session = $ this ->getMockBuilder (Session::class)
40- ->disableOriginalConstructor ()
41- ->getMock ();
39+ $ this ->expectException (InvalidArgumentException::class);
4240
43- $ session ->method ('get ' )->willReturnCallback ([$ this , 'getStickyCallback ' ]);
44- $ session ->method ('has ' )->willReturnCallback ([$ this , 'hasStickyCallback ' ]);
45-
46- $ params = ['ratio ' => $ ratio , 'sticky ' => $ sticky ];
47-
48- $ voter = new RatioVoter ($ session );
49- $ voter ->setFeature ('ratiotest ' );
50- $ voter ->setParams ($ params );
51-
52- return $ voter ;
41+ $ voter = $ this ->getRatioVoter (0.5 , true , false );
42+ $ voter ->pass ();
5343 }
5444
5545 /**
56- * Executes the tests n time returning the number of passes
46+ * @dataProvider dataProvider
5747 *
58- * @param RatioVoter $ratioVoter
59- * @param int $iterationCount
60- *
61- * @return int
48+ * @param bool $hasSession
6249 */
63- private function executeTestIteration ( RatioVoter $ ratioVoter , $ iterationCount = 100 ): int
50+ public function testHighRatioVoterPass ( bool $ hasSession ): void
6451 {
65- $ hits = 0 ;
66-
67- for ($ i = 1 ; $ i <= $ iterationCount ; $ i ++) {
68- if ($ ratioVoter ->pass ()) {
69- $ hits ++;
70- }
71- }
72-
73- return $ hits ;
74- }
75-
76- public function testHighRatioVoterPass (): void
77- {
78- $ voter = $ this ->getRatioVoter (0.9 );
52+ $ voter = $ this ->getRatioVoter (0.9 , false , $ hasSession );
7953 $ hits = $ this ->executeTestIteration ($ voter );
8054
8155 $ this ->assertGreaterThan (100 - $ hits , $ hits );
8256 }
8357
84- public function testZeroRatioVoterPass (): void
58+ /**
59+ * @dataProvider dataProvider
60+ *
61+ * @param bool $hasSession
62+ */
63+ public function testZeroRatioVoterPass (bool $ hasSession ): void
8564 {
86- $ voter = $ this ->getRatioVoter (0 );
65+ $ voter = $ this ->getRatioVoter (0 , false , $ hasSession );
8766 $ hits = $ this ->executeTestIteration ($ voter );
8867
8968 $ this ->assertEquals (0 , $ hits );
9069 }
9170
92- public function testOneRatioVoterPass (): void
71+ /**
72+ * @dataProvider dataProvider
73+ *
74+ * @param bool $hasSession
75+ */
76+ public function testOneRatioVoterPass (bool $ hasSession ): void
9377 {
94- $ voter = $ this ->getRatioVoter (1 );
78+ $ voter = $ this ->getRatioVoter (1 , false , $ hasSession );
9579 $ hits = $ this ->executeTestIteration ($ voter );
9680
9781 $ this ->assertEquals (100 , $ hits );
9882 }
9983
84+ /**
85+ * Simple data provider for $hasSession
86+ *
87+ * @return array
88+ */
89+ public function dataProvider (): array
90+ {
91+ return [
92+ [true ],
93+ [false ],
94+ ];
95+ }
96+
10097 public function testStickyRatioVoterPass (): void
10198 {
10299 $ voter = $ this ->getRatioVoter (0.5 , true );
@@ -135,4 +132,48 @@ public function getStickyCallback($key)
135132 {
136133 return $ this ->stickyValues [$ key ] ?? null ;
137134 }
135+
136+ protected function getRatioVoter ($ ratio , $ sticky = false , $ hasSession = true ): RatioVoter
137+ {
138+ $ session = null ;
139+
140+ if ($ hasSession )
141+ {
142+ // Create service stub
143+ /** @var Session&MockObject $session */
144+ $ session = $ this ->createMock (SessionInterface::class);
145+
146+ $ session ->method ('get ' )->willReturnCallback ([$ this , 'getStickyCallback ' ]);
147+ $ session ->method ('has ' )->willReturnCallback ([$ this , 'hasStickyCallback ' ]);
148+ }
149+
150+ $ params = ['ratio ' => $ ratio , 'sticky ' => $ sticky ];
151+
152+ $ voter = new RatioVoter ($ session );
153+ $ voter ->setFeature ('ratiotest ' );
154+ $ voter ->setParams ($ params );
155+
156+ return $ voter ;
157+ }
158+
159+ /**
160+ * Executes the tests n time returning the number of passes
161+ *
162+ * @param RatioVoter $ratioVoter
163+ * @param int $iterationCount
164+ *
165+ * @return int
166+ */
167+ private function executeTestIteration (RatioVoter $ ratioVoter , $ iterationCount = 100 ): int
168+ {
169+ $ hits = 0 ;
170+
171+ for ($ i = 1 ; $ i <= $ iterationCount ; $ i ++) {
172+ if ($ ratioVoter ->pass ()) {
173+ $ hits ++;
174+ }
175+ }
176+
177+ return $ hits ;
178+ }
138179}
0 commit comments