33namespace Algolia \AlgoliaSearch \Tests \Integration ;
44
55use Algolia \AlgoliaSearch \AnalyticsClient ;
6- use Algolia \AlgoliaSearch \SearchIndex ;
6+ use Algolia \AlgoliaSearch \Config \AnalyticsConfig ;
7+ use Algolia \AlgoliaSearch \Http \HttpClientInterface ;
8+ use Algolia \AlgoliaSearch \Http \Psr7 \Response ;
9+ use Algolia \AlgoliaSearch \RetryStrategy \ApiWrapper ;
10+ use Algolia \AlgoliaSearch \RetryStrategy \ClusterHosts ;
711use Algolia \AlgoliaSearch \Tests \TestHelper ;
812use DateTime ;
13+ use Psr \Http \Message \RequestInterface ;
914
10- class AnalyticsClientTest extends BaseTest
15+ class AnalyticsClientTest extends BaseTest implements HttpClientInterface
1116{
12- public function testAbTesting ()
17+ /**
18+ * @var RequestInterface[]
19+ */
20+ private $ recordedRequests = [];
21+
22+ protected function assertRequests (array $ requests )
1323 {
14- $ this ->indexes ['ab_testing ' ] = TestHelper::getTestIndexName ('ab_testing ' );
15- $ this ->indexes ['ab_testing_dev ' ] = TestHelper::getTestIndexName ('ab_testing_dev ' );
24+ $ this ->assertGreaterThan (0 , count ($ requests ));
25+ $ this ->assertEquals (count ($ requests ), count ($ this ->recordedRequests ));
26+
27+ foreach ($ requests as $ i => $ request ) {
28+ $ recordedRequest = $ this ->recordedRequests [$ i ];
1629
17- /** @var SearchIndex $index */
18- $ index = TestHelper::getClient ()->initIndex ($ this ->indexes ['ab_testing ' ]);
30+ $ this ->assertEquals ($ request ['method ' ], $ recordedRequest ->getMethod ());
31+ $ this ->assertEquals ($ request ['path ' ], $ recordedRequest ->getUri ()->getPath ());
32+ $ this ->assertEquals ($ request ['body ' ], $ recordedRequest ->getBody ()->getContents ());
33+ }
34+ }
1935
20- /** @var SearchIndex $indexDev */
21- $ indexDev = TestHelper::getClient ()->initIndex ($ this ->indexes ['ab_testing_dev ' ]);
36+ protected function getClient ()
37+ {
38+ $ api = new ApiWrapper ($ this , AnalyticsConfig::create (), ClusterHosts::create ('127.0.0.1 ' ));
39+ $ config = AnalyticsConfig::create ('foo ' , 'bar ' );
40+
41+ return new AnalyticsClient ($ api , $ config );
42+ }
2243
23- $ responses = [];
44+ public function sendRequest (RequestInterface $ request , $ timeout , $ connectTimeout )
45+ {
46+ $ this ->recordedRequests [] = $ request ;
2447
25- $ object = ['objectID ' => 'one ' ];
48+ return new Response (200 , [], '{} ' );
49+ }
2650
27- $ index ->saveObject ($ object , ['autoGenerateObjectIDIfNotExist ' => true ])->wait ();
28- $ indexDev ->saveObject ($ object , ['autoGenerateObjectIDIfNotExist ' => true ])->wait ();
51+ public function testAbTesting ()
52+ {
53+ $ this ->indexes ['ab_testing ' ] = TestHelper::getTestIndexName ('ab_testing ' );
54+ $ this ->indexes ['ab_testing_dev ' ] = TestHelper::getTestIndexName ('ab_testing_dev ' );
55+ $ this ->indexes ['aa_testing ' ] = TestHelper::getTestIndexName ('aa_testing ' );
2956
3057 $ dateTime = new DateTime ('tomorrow ' );
3158 $ abTestName = $ this ->indexes ['ab_testing ' ];
59+ $ aaTestName = $ this ->indexes ['aa_testing ' ];
60+ $ date = $ dateTime ->format ('Y-m-d\TH:i:s\Z ' );
3261
3362 $ abTest = [
3463 'name ' => $ abTestName ,
3564 'variants ' => [
3665 [
37- 'index ' => $ this -> indexes [ ' ab_testing ' ] ,
66+ 'index ' => $ abTestName ,
3867 'trafficPercentage ' => 60 ,
3968 'description ' => 'a description ' ,
4069 ],
@@ -43,162 +72,62 @@ public function testAbTesting()
4372 'trafficPercentage ' => 40 ,
4473 ],
4574 ],
46- 'endAt ' => $ dateTime -> format ( ' Y-m-d\TH:i:s\Z ' ) ,
75+ 'endAt ' => $ date ,
4776 ];
4877
49- $ analyticsClient = AnalyticsClient::create (
50- getenv ('ALGOLIA_APPLICATION_ID_1 ' ),
51- getenv ('ALGOLIA_ADMIN_KEY_1 ' )
52- );
53-
54- $ cpt = 0 ;
55- do {
56- if ($ cpt >= 10 ) {
57- break ;
58- }
59- $ index ->exists () && $ indexDev ->exists ();
60- sleep (1 );
61- $ cpt ++;
62- } while (false );
63-
64- $ response = TestHelper::retry (function () use ($ analyticsClient , $ abTest ) {
65- return $ analyticsClient ->addABTest ($ abTest );
66- }, 0.1 , 40 );
67-
68- $ abTestId = $ response ['abTestID ' ];
69- $ index ->waitTask ($ response ['taskID ' ]);
70-
71- $ result = $ analyticsClient ->getABTest ($ abTestId );
72-
73- $ this ->assertSame ($ abTest ['name ' ], $ result ['name ' ]);
74- $ this ->assertSame ($ abTest ['endAt ' ], $ result ['endAt ' ]);
75- $ this ->assertSame ($ abTest ['variants ' ][0 ]['index ' ], $ result ['variants ' ][0 ]['index ' ]);
76- $ this ->assertSame ($ abTest ['variants ' ][0 ]['trafficPercentage ' ], $ result ['variants ' ][0 ]['trafficPercentage ' ]);
77- $ this ->assertSame ($ abTest ['variants ' ][0 ]['description ' ], $ result ['variants ' ][0 ]['description ' ]);
78- $ this ->assertSame ($ abTest ['variants ' ][1 ]['index ' ], $ result ['variants ' ][1 ]['index ' ]);
79- $ this ->assertSame ($ abTest ['variants ' ][1 ]['trafficPercentage ' ], $ result ['variants ' ][1 ]['trafficPercentage ' ]);
80- $ this ->assertNotEquals ('stopped ' , $ result ['status ' ]);
81-
82- $ results = $ analyticsClient ->getABTests ();
83- $ found = false ;
84-
85- foreach ($ results ['abtests ' ] as $ fetchedAbTest ) {
86- if ($ fetchedAbTest ['name ' ] != $ abTest ['name ' ]) {
87- continue ;
88- }
89- $ this ->assertSame ($ abTest ['name ' ], $ fetchedAbTest ['name ' ]);
90- $ this ->assertSame ($ abTest ['endAt ' ], $ fetchedAbTest ['endAt ' ]);
91- $ this ->assertSame ($ abTest ['variants ' ][0 ]['index ' ], $ fetchedAbTest ['variants ' ][0 ]['index ' ]);
92- $ this ->assertSame (
93- $ abTest ['variants ' ][0 ]['trafficPercentage ' ],
94- $ fetchedAbTest ['variants ' ][0 ]['trafficPercentage ' ]
95- );
96- $ this ->assertSame ($ abTest ['variants ' ][0 ]['description ' ], $ fetchedAbTest ['variants ' ][0 ]['description ' ]);
97- $ this ->assertSame ($ abTest ['variants ' ][1 ]['index ' ], $ fetchedAbTest ['variants ' ][1 ]['index ' ]);
98- $ this ->assertSame (
99- $ abTest ['variants ' ][1 ]['trafficPercentage ' ],
100- $ fetchedAbTest ['variants ' ][1 ]['trafficPercentage ' ]
101- );
102- $ this ->assertNotEquals ('stopped ' , $ fetchedAbTest ['status ' ]);
103- $ found = true ;
104- }
105-
106- $ this ->assertTrue ($ found );
107-
108- $ response = $ analyticsClient ->stopABTest ($ abTestId );
109- $ index ->waitTask ($ response ['taskID ' ]);
110-
111- $ result = $ analyticsClient ->getABTest ($ abTestId );
112- $ this ->assertEquals ('stopped ' , $ result ['status ' ]);
113-
114- $ response = $ analyticsClient ->deleteABTest ($ abTestId );
115- $ index ->waitTask ($ response ['taskID ' ]);
116-
117- try {
118- $ result = $ analyticsClient ->getABTest ($ abTestId );
119- } catch (\Exception $ e ) {
120- $ this ->assertInstanceOf ('Algolia\AlgoliaSearch\Exceptions\NotFoundException ' , $ e );
121- $ this ->assertEquals (404 , $ e ->getCode ());
122- $ this ->assertEquals ('ABTestID not found ' , $ e ->getMessage ());
123- }
124- }
125-
126- public function testAaTesting ()
127- {
128- $ this ->indexes ['aa_testing ' ] = TestHelper::getTestIndexName ('aa_testing ' );
129-
130- /** @var SearchIndex $index */
131- $ index = TestHelper::getClient ()->initIndex ($ this ->indexes ['aa_testing ' ]);
132-
133- $ analyticsClient = AnalyticsClient::create (
134- getenv ('ALGOLIA_APPLICATION_ID_1 ' ),
135- getenv ('ALGOLIA_ADMIN_KEY_1 ' )
136- );
137-
138- $ object = ['objectID ' => 'one ' ];
139- $ res = $ index ->saveObject ($ object , ['autoGenerateObjectIDIfNotExist ' => true ])->wait ();
140- $ dateTime = new DateTime ('tomorrow ' );
141- $ abTestName = $ this ->indexes ['aa_testing ' ];
142-
14378 $ aaTest = [
144- 'name ' => $ abTestName ,
79+ 'name ' => $ aaTestName ,
14580 'variants ' => [
146- ['index ' => $ this -> indexes [ ' aa_testing ' ] , 'trafficPercentage ' => 90 ],
81+ ['index ' => $ aaTestName , 'trafficPercentage ' => 90 ],
14782 [
148- 'index ' => $ this -> indexes [ ' aa_testing ' ] ,
83+ 'index ' => $ aaTestName ,
14984 'trafficPercentage ' => 10 ,
15085 'customSearchParameters ' => ['ignorePlurals ' => true ],
15186 ],
15287 ],
153- 'endAt ' => $ dateTime -> format ( ' Y-m-d\TH:i:s\Z ' ) ,
88+ 'endAt ' => $ date ,
15489 ];
15590
156- $ cpt = 0 ;
157- do {
158- if ($ cpt >= 10 ) {
159- break ;
160- }
161- $ index ->exists ();
162- sleep (1 );
163- $ cpt ++;
164- } while (false );
165-
166- $ response = TestHelper::retry (function () use ($ analyticsClient , $ aaTest ) {
167- return $ analyticsClient ->addABTest ($ aaTest );
168- }, 0.1 , 40 );
169-
170- $ aaTestId = $ response ['abTestID ' ];
171- TestHelper::getClient ()->waitTask ($ this ->indexes ['aa_testing ' ], $ response ['taskID ' ]);
172-
173- $ fetchedAbTest = $ analyticsClient ->getABTest ($ aaTestId );
174-
175- $ this ->assertSame ($ aaTest ['name ' ], $ fetchedAbTest ['name ' ]);
176- $ this ->assertSame ($ aaTest ['endAt ' ], $ fetchedAbTest ['endAt ' ]);
177- $ this ->assertSame ($ aaTest ['variants ' ][0 ]['index ' ], $ fetchedAbTest ['variants ' ][0 ]['index ' ]);
178- $ this ->assertSame (
179- $ aaTest ['variants ' ][0 ]['trafficPercentage ' ],
180- $ fetchedAbTest ['variants ' ][0 ]['trafficPercentage ' ]
181- );
182- $ this ->assertSame ($ aaTest ['variants ' ][1 ]['index ' ], $ fetchedAbTest ['variants ' ][1 ]['index ' ]);
183- $ this ->assertSame (
184- $ aaTest ['variants ' ][1 ]['trafficPercentage ' ],
185- $ fetchedAbTest ['variants ' ][1 ]['trafficPercentage ' ]
186- );
187- $ this ->assertSame (
188- $ aaTest ['variants ' ][1 ]['customSearchParameters ' ],
189- $ fetchedAbTest ['variants ' ][1 ]['customSearchParameters ' ]
190- );
191- $ this ->assertNotEquals ('stopped ' , $ fetchedAbTest ['status ' ]);
192-
193- $ response = $ analyticsClient ->deleteABTest ($ aaTestId );
194- $ index ->waitTask ($ response ['taskID ' ]);
195-
196- try {
197- $ result = $ analyticsClient ->getABTest ($ aaTestId );
198- } catch (\Exception $ e ) {
199- $ this ->assertInstanceOf ('Algolia\AlgoliaSearch\Exceptions\NotFoundException ' , $ e );
200- $ this ->assertEquals (404 , $ e ->getCode ());
201- $ this ->assertEquals ('ABTestID not found ' , $ e ->getMessage ());
202- }
91+ $ analyticsClient = $ this ->getClient ();
92+ // Test AB Testing format
93+ $ analyticsClient ->addABTest ($ abTest );
94+ // Test AA Testing format
95+ $ analyticsClient ->addABTest ($ aaTest );
96+
97+ $ abTestId = 'myAbTestID ' ;
98+ // Test Stop AB test
99+ $ analyticsClient ->stopABTest ($ abTestId );
100+ // Test get AB test
101+ $ analyticsClient ->getABTest ($ abTestId );
102+ // Test delete AB test
103+ $ analyticsClient ->deleteABTest ($ abTestId );
104+
105+ $ this ->assertRequests ([
106+ [
107+ 'path ' => '/2/abtests ' ,
108+ 'method ' => 'POST ' ,
109+ 'body ' => '{"name":" ' .$ abTestName .'","variants":[{"index":" ' .$ abTestName .'","trafficPercentage":60,"description":"a description"},{"index":" ' .$ this ->indexes ['ab_testing_dev ' ].'","trafficPercentage":40}],"endAt":" ' .$ date .'"} ' ,
110+ ],
111+ [
112+ 'path ' => '/2/abtests ' ,
113+ 'method ' => 'POST ' ,
114+ 'body ' => '{"name":" ' .$ aaTestName .'","variants":[{"index":" ' .$ aaTestName .'","trafficPercentage":90},{"index":" ' .$ aaTestName .'","trafficPercentage":10,"customSearchParameters":{"ignorePlurals":true}}],"endAt":" ' .$ date .'"} ' ,
115+ ],
116+ [
117+ 'path ' => '/2/abtests/myAbTestID/stop ' ,
118+ 'method ' => 'POST ' ,
119+ 'body ' => '' ,
120+ ],
121+ [
122+ 'path ' => '/2/abtests/myAbTestID ' ,
123+ 'method ' => 'GET ' ,
124+ 'body ' => '' ,
125+ ],
126+ [
127+ 'path ' => '/2/abtests/myAbTestID ' ,
128+ 'method ' => 'DELETE ' ,
129+ 'body ' => '' ,
130+ ],
131+ ]);
203132 }
204133}
0 commit comments