1010
1111class SearchIndexTest extends TestCase
1212{
13- public function testFindObject ()
13+ /** @var SearchConfig */
14+ protected $ config ;
15+
16+ /** @var RequestOptionsFactory */
17+ protected $ requestOptionsFactory ;
18+
19+ public function setUp ()
1420 {
15- $ config = SearchConfig::create ('foo ' , 'bar ' );
16- $ requestOptionsFactory = new RequestOptionsFactory ($ config );
21+ $ this ->config = SearchConfig::create ('foo ' , 'bar ' );
22+ $ this ->requestOptionsFactory = new RequestOptionsFactory ($ this ->config );
23+ }
1724
25+ public function testFindObject ()
26+ {
1827 // Test without requestOptions
1928 $ apiWrapperMock = $ this ->getMock ('Algolia\AlgoliaSearch\RetryStrategy\ApiWrapperInterface ' );
2029 $ apiWrapperMock ->method ('read ' )
@@ -29,7 +38,7 @@ public function testFindObject()
2938 'nbPages ' => 1 ,
3039 ));
3140
32- $ client = new SearchClient ($ apiWrapperMock , $ config );
41+ $ client = new SearchClient ($ apiWrapperMock , $ this -> config );
3342 $ client ->initIndex ('foo ' )->findObject (
3443 function () { return true ; }
3544 );
@@ -48,7 +57,7 @@ function () { return true; }
4857 'nbPages ' => 1 ,
4958 ));
5059
51- $ client = new SearchClient ($ apiWrapperMock , $ config );
60+ $ client = new SearchClient ($ apiWrapperMock , $ this -> config );
5261 $ client ->initIndex ('foo ' )->findObject (
5362 function () { return true ; },
5463 array ('query ' => 'foo ' , 'hitsPerPage ' => 5 )
@@ -69,10 +78,50 @@ function () { return true; },
6978 'nbPages ' => 1 ,
7079 ));
7180
72- $ client = new SearchClient ($ apiWrapperMock , $ config );
81+ $ client = new SearchClient ($ apiWrapperMock , $ this -> config );
7382 $ client ->initIndex ('foo ' )->findObject (
7483 function () { return true ; },
75- $ requestOptionsFactory ->create (array ('User-Agent ' => 'blabla ' ))
84+ $ this -> requestOptionsFactory ->create (array ('User-Agent ' => 'blabla ' ))
7685 );
7786 }
87+
88+ public function testExistsWithRequestOptions ()
89+ {
90+ $ requestOptions = $ this ->requestOptionsFactory ->create (array ('X-Algolia-User-ID ' => 'foo ' ));
91+ $ apiWrapperMock = $ this ->getMock ('Algolia\AlgoliaSearch\RetryStrategy\ApiWrapperInterface ' );
92+
93+ $ apiWrapperMock ->method ('read ' )
94+ ->with ($ this ->anything (), $ this ->anything (), $ this ->callback (function ($ requestOptions ) {
95+ Assert::assertInstanceOf ('Algolia\AlgoliaSearch\RequestOptions\RequestOptions ' , $ requestOptions );
96+ Assert::assertArraySubset (array ('X-Algolia-User-ID ' => 'foo ' ), $ requestOptions ->getHeaders ());
97+
98+ return $ requestOptions ;
99+ }))
100+ ->willReturn (array (
101+ 'hitsPerPage ' => 31 ,
102+ 'userData ' => 'API SearchClient copy test ' ,
103+ ));
104+ $ client = new SearchClient ($ apiWrapperMock , $ this ->config );
105+ $ client ->initIndex ('foo ' )->exists ($ requestOptions );
106+ }
107+
108+ public function testExistsWithoutRequestOptions ()
109+ {
110+ $ apiWrapperMock = $ this ->getMock ('Algolia\AlgoliaSearch\RetryStrategy\ApiWrapperInterface ' );
111+
112+ // getVersion is added by default in requestOptions
113+ $ apiWrapperMock ->method ('read ' )
114+ ->with ($ this ->anything (), $ this ->anything (), $ this ->callback (function ($ requestOptions ) {
115+ Assert::assertInternalType ('array ' , $ requestOptions );
116+ Assert::assertArraySubset (array ('getVersion ' => 2 ), $ requestOptions );
117+
118+ return $ requestOptions ;
119+ }))
120+ ->willReturn (array (
121+ 'hitsPerPage ' => 31 ,
122+ 'userData ' => 'API SearchClient copy test ' ,
123+ ));
124+ $ client = new SearchClient ($ apiWrapperMock , $ this ->config );
125+ $ client ->initIndex ('foo ' )->exists ();
126+ }
78127}
0 commit comments