Skip to content

Commit c78634e

Browse files
authored
Fix: exists method (#582)
* fix(index): add requestOptions to exist method * fix(exists-method): add unit tests
1 parent a1aa7b9 commit c78634e

File tree

4 files changed

+89
-46
lines changed

4 files changed

+89
-46
lines changed

src/SearchIndex.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,17 @@ public function delete($requestOptions = array())
624624
return new IndexingResponse($response, $this);
625625
}
626626

627-
public function exists()
627+
/**
628+
* Check whether an index exists or not.
629+
*
630+
* @param array<string, int|string|array>|RequestOptions $requestOptions array of options or RequestOptions object
631+
*
632+
* @return bool
633+
*/
634+
public function exists($requestOptions = array())
628635
{
629636
try {
630-
$this->getSettings();
637+
$this->getSettings($requestOptions);
631638
} catch (NotFoundException $exception) {
632639
return false;
633640
}

tests/Integration/ExistsTest.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/Integration/SearchIndexTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,33 @@ protected function setUp()
1515
}
1616
}
1717

18+
public function testIndexDoesNotExist()
19+
{
20+
$index = static::getClient()->initIndex(static::$indexes['main']);
21+
22+
self::assertFalse($index->exists());
23+
}
24+
25+
public function testIndexExists()
26+
{
27+
$index = static::getClient()->initIndex(static::$indexes['main']);
28+
29+
$index
30+
->saveObject(
31+
array(
32+
'firstname' => 'Jimmie',
33+
),
34+
array('autoGenerateObjectIDIfNotExist' => true)
35+
)
36+
->wait();
37+
38+
self::assertTrue($index->exists());
39+
}
40+
1841
public function testFindObject()
1942
{
2043
$index = static::getClient()->initIndex(static::$indexes['main']);
44+
$index->clearObjects();
2145
$index->saveObjects($this->companies);
2246

2347
$res = $index->search('Algolia');

tests/Unit/SearchIndexTest.php

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@
1010

1111
class 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

Comments
 (0)