Source of file SwiftypeClientTest.php
Size: 5,870 Bytes - Last Modified: 2021-12-23T10:02:56+00:00
/var/www/docs.ssmods.com/process/src/tests/php/SwiftypeClientTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 | <?php namespace Marcz\Swiftype\Tests; use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\TestOnly; use SilverStripe\Core\Environment; use Marcz\Swiftype\SwiftypeClient; use GuzzleHttp\Ring\Client\CurlHandler; use GuzzleHttp\Ring\Client\MockHandler; use GuzzleHttp\Stream\Stream; class SwiftypeClientTest extends SapphireTest { protected function setUp() { parent::setUp(); Environment::setEnv('SS_SWIFTYPE_END_POINT', 'http://api.swiftype.com/api/v1/'); Environment::setEnv('SS_SWIFTYPE_AUTH_TOKEN', 'SS_SWIFTYPE_AUTH_TOKEN'); } protected function fetchMockedResponse($data = [], $status = 200) { return new MockHandler( [ 'status' => $status, 'body' => Stream::factory(new FakeStreamArray($data)) ] ); } public function testCreateClient() { $client = new SwiftypeClient; $curlClient = $client->createClient(); $this->assertInstanceOf(CurlHandler::class, $client->createClient()); $this->assertEquals($curlClient, $client->createClient()); } public function testInitIndex() { $client = new SwiftypeClient; $rawQuery = $client->initIndex('index_name'); $expected = [ 'http_method' => 'GET', 'uri' => '/api/v1/', 'headers' => [ 'host' => ['api.swiftype.com'], 'Content-Type' => ['application/json'], ], 'client' => [ 'curl' => [ CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => false ] ] ]; $this->assertEquals($expected, $rawQuery); } public function testCreateIndex() { $client = new SwiftypeClient; $client->setClientAPI($this->fetchMockedResponse([['name' => 'myproducts']])); $this->assertTrue($client->createIndex('MyProducts')); } public function testHasEngine() { $client = new SwiftypeClient; $data = ['auth_token' => Environment::getEnv('SS_SWIFTYPE_AUTH_TOKEN')]; $expected = [ 'http_method' => 'GET', 'uri' => '/api/v1/engines.json', 'headers' => [ 'host' => ['api.swiftype.com'], 'Content-Type' => ['application/json'], ], 'client' => [ 'curl' => [ CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => false ] ], 'body' => json_encode($data, JSON_PRESERVE_ZERO_FRACTION) ]; $client->setClientAPI($this->fetchMockedResponse([['name' => 'myproducts']])); $this->assertTrue($client->hasEngine('MyProducts')); $this->assertEquals($expected, $client->sql()); } public function testGetDocumentTypes() { $client = new SwiftypeClient; $data = ['auth_token' => Environment::getEnv('SS_SWIFTYPE_AUTH_TOKEN')]; $expected = [ 'http_method' => 'GET', 'uri' => '/api/v1/engines/myproducts/document_types.json', 'headers' => [ 'host' => ['api.swiftype.com'], 'Content-Type' => ['application/json'], ], 'client' => [ 'curl' => [ CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => false ] ], 'body' => json_encode($data, JSON_PRESERVE_ZERO_FRACTION) ]; $client->setClientAPI($this->fetchMockedResponse([['name' => 'myproducts']])); $this->assertEquals( [['name' => strtolower('MyProducts')]], $client->getDocumentTypes('MyProducts') ); $this->assertEquals($expected, $client->sql()); } public function testCreateEngine() { $client = new SwiftypeClient; $data = [ 'auth_token' => 'SS_SWIFTYPE_AUTH_TOKEN', 'engine' => ['name' => 'myproducts'] ]; $expected = [ 'http_method' => 'POST', 'uri' => '/api/v1/engines.json', 'headers' => [ 'host' => ['api.swiftype.com'], 'Content-Type' => ['application/json'], ], 'client' => [ 'curl' => [ CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => false ] ], 'body' => json_encode($data, JSON_PRESERVE_ZERO_FRACTION) ]; $client->setClientAPI($this->fetchMockedResponse([['name' => 'myproducts']])); $this->assertTrue($client->createEngine('MyProducts')); $this->assertEquals($expected, $client->sql()); } public function testCreateDocumentType() { $client = new SwiftypeClient; $data = [ 'auth_token' => 'SS_SWIFTYPE_AUTH_TOKEN', 'document_type' => ['name' => 'myproducts'] ]; $expected = [ 'http_method' => 'POST', 'uri' => '/api/v1/engines/myproducts/document_types.json', 'headers' => [ 'host' => ['api.swiftype.com'], 'Content-Type' => ['application/json'], ], 'client' => [ 'curl' => [ CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => false ] ], 'body' => json_encode($data, JSON_PRESERVE_ZERO_FRACTION) ]; $client->setClientAPI($this->fetchMockedResponse([['name' => 'myproducts']])); $this->assertTrue($client->createDocumentType('MyProducts', 'MyProducts')); $this->assertEquals($expected, $client->sql()); } } |