|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Tests\Unit; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Config\SearchConfig; |
| 6 | +use Algolia\AlgoliaSearch\RequestOptions\RequestOptionsFactory; |
| 7 | +use Algolia\AlgoliaSearch\SearchClient; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use PHPUnit_Framework_Assert as Assert; |
| 10 | + |
| 11 | +class SearchIndexTest extends TestCase |
| 12 | +{ |
| 13 | + public function testFindObject() |
| 14 | + { |
| 15 | + $config = SearchConfig::create('foo', 'bar'); |
| 16 | + $requestOptionsFactory = new RequestOptionsFactory($config); |
| 17 | + |
| 18 | + // Test without requestOptions |
| 19 | + $apiWrapperMock = $this->getMock('Algolia\AlgoliaSearch\RetryStrategy\ApiWrapperInterface'); |
| 20 | + $apiWrapperMock->method('read') |
| 21 | + ->with($this->anything(), $this->anything(), $this->callback(function ($requestOptions) { |
| 22 | + Assert::assertInstanceOf('Algolia\AlgoliaSearch\RequestOptions\RequestOptions', $requestOptions); |
| 23 | + Assert::assertEquals($requestOptions->getBody(), array('page' => 0, 'query' => '')); |
| 24 | + |
| 25 | + return $requestOptions; |
| 26 | + })) |
| 27 | + ->willReturn(array( |
| 28 | + 'hits' => array(array('foo' => 'bar')), |
| 29 | + 'nbPages' => 1, |
| 30 | + )); |
| 31 | + |
| 32 | + $client = new SearchClient($apiWrapperMock, $config); |
| 33 | + $client->initIndex('foo')->findObject( |
| 34 | + function () { return true; } |
| 35 | + ); |
| 36 | + |
| 37 | + // Test with requestOptions as an array |
| 38 | + $apiWrapperMock = $this->getMock('Algolia\AlgoliaSearch\RetryStrategy\ApiWrapperInterface'); |
| 39 | + $apiWrapperMock->method('read') |
| 40 | + ->with($this->anything(), $this->anything(), $this->callback(function ($requestOptions) { |
| 41 | + Assert::assertInstanceOf('Algolia\AlgoliaSearch\RequestOptions\RequestOptions', $requestOptions); |
| 42 | + Assert::assertEquals($requestOptions->getBody(), array('page' => 0, 'query' => 'foo', 'hitsPerPage' => 5)); |
| 43 | + |
| 44 | + return $requestOptions; |
| 45 | + })) |
| 46 | + ->willReturn(array( |
| 47 | + 'hits' => array(array('foo' => 'bar')), |
| 48 | + 'nbPages' => 1, |
| 49 | + )); |
| 50 | + |
| 51 | + $client = new SearchClient($apiWrapperMock, $config); |
| 52 | + $client->initIndex('foo')->findObject( |
| 53 | + function () { return true; }, |
| 54 | + array('query' => 'foo', 'hitsPerPage' => 5) |
| 55 | + ); |
| 56 | + |
| 57 | + // Test with requestOptions as a RequestOptions object |
| 58 | + $apiWrapperMock = $this->getMock('Algolia\AlgoliaSearch\RetryStrategy\ApiWrapperInterface'); |
| 59 | + $apiWrapperMock->method('read') |
| 60 | + ->with($this->anything(), $this->anything(), $this->callback(function ($requestOptions) { |
| 61 | + Assert::assertInstanceOf('Algolia\AlgoliaSearch\RequestOptions\RequestOptions', $requestOptions); |
| 62 | + Assert::assertEquals($requestOptions->getBody(), array('page' => 0, 'query' => '')); |
| 63 | + Assert::assertArraySubset(array('User-Agent' => 'blabla'), $requestOptions->getHeaders()); |
| 64 | + |
| 65 | + return $requestOptions; |
| 66 | + })) |
| 67 | + ->willReturn(array( |
| 68 | + 'hits' => array(array('foo' => 'bar')), |
| 69 | + 'nbPages' => 1, |
| 70 | + )); |
| 71 | + |
| 72 | + $client = new SearchClient($apiWrapperMock, $config); |
| 73 | + $client->initIndex('foo')->findObject( |
| 74 | + function () { return true; }, |
| 75 | + $requestOptionsFactory->create(array('User-Agent' => 'blabla')) |
| 76 | + ); |
| 77 | + } |
| 78 | +} |
0 commit comments