|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Tests\Integration; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\AccountClient; |
| 6 | +use Algolia\AlgoliaSearch\Response\MultiResponse; |
| 7 | +use Algolia\AlgoliaSearch\SearchIndex; |
| 8 | +use Algolia\AlgoliaSearch\Tests\TestHelper; |
| 9 | + |
| 10 | +class AccountTest extends BaseTest |
| 11 | +{ |
| 12 | + protected $secondaryIndexes = array(); |
| 13 | + |
| 14 | + public function testCopyIndex() |
| 15 | + { |
| 16 | + $this->indexes['copy_index'] = TestHelper::getTestIndexName('copy_index'); |
| 17 | + $this->indexes['copy_index_2'] = TestHelper::getTestIndexName('copy_index_2'); |
| 18 | + |
| 19 | + /** @var SearchIndex $copyIndex */ |
| 20 | + $copyIndex = TestHelper::getClient()->initIndex($this->indexes['copy_index']); |
| 21 | + $secondaryIndex = TestHelper::getClient()->initIndex($this->indexes['copy_index_2']); |
| 22 | + |
| 23 | + try { |
| 24 | + AccountClient::copyIndex($copyIndex, $secondaryIndex)->wait(); |
| 25 | + } catch (\Exception $e) { |
| 26 | + $this->assertInstanceOf('\InvalidArgumentException', $e); |
| 27 | + $this->assertEquals( |
| 28 | + 'If both index are on the same app, please use SearchClient::copyIndex method instead.', |
| 29 | + $e->getMessage() |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + $secondaryConfig = array( |
| 34 | + 'appId' => getenv('ALGOLIA_APPLICATION_ID_2'), |
| 35 | + 'apiKey' => getenv('ALGOLIA_ADMIN_KEY_2'), |
| 36 | + ); |
| 37 | + |
| 38 | + $secondaryClient = TestHelper::getClient($secondaryConfig); |
| 39 | + $this->secondaryIndexes['copy_index_2'] = TestHelper::getTestIndexName('copy_index_2'); |
| 40 | + |
| 41 | + /** @var SearchIndex $secondaryIndex */ |
| 42 | + $secondaryIndex = $secondaryClient->initIndex($this->secondaryIndexes['copy_index_2']); |
| 43 | + |
| 44 | + $responses = array(); |
| 45 | + $responses[] = $copyIndex->saveObject( |
| 46 | + array('objectID' => 'one'), |
| 47 | + array('autoGenerateObjectIDIfNotExist' => true) |
| 48 | + ); |
| 49 | + |
| 50 | + $rule = array( |
| 51 | + 'objectID' => 'one', |
| 52 | + 'condition' => array( |
| 53 | + 'anchoring' => 'is', |
| 54 | + 'pattern' => 'pattern', |
| 55 | + ), |
| 56 | + 'consequence' => array( |
| 57 | + 'params' => array( |
| 58 | + 'query' => array( |
| 59 | + 'edits' => array( |
| 60 | + array( |
| 61 | + 'type' => 'remove', |
| 62 | + 'delete' => 'pattern', |
| 63 | + ), |
| 64 | + ), |
| 65 | + ), |
| 66 | + ), |
| 67 | + ), |
| 68 | + ); |
| 69 | + |
| 70 | + $responses[] = $copyIndex->saveRule($rule); |
| 71 | + |
| 72 | + $synonym = array( |
| 73 | + 'objectID' => 'one', |
| 74 | + 'type' => 'synonym', |
| 75 | + 'synonyms' => array('one', 'two'), |
| 76 | + ); |
| 77 | + |
| 78 | + $responses[] = $copyIndex->saveSynonym($synonym); |
| 79 | + |
| 80 | + $responses[] = $copyIndex->setSettings(array('searchableAttributes' => array('objectID'))); |
| 81 | + |
| 82 | + /* Wait all collected task to terminate */ |
| 83 | + $multiResponse = new MultiResponse($responses); |
| 84 | + $multiResponse->wait(); |
| 85 | + |
| 86 | + AccountClient::copyIndex($copyIndex, $secondaryIndex)->wait(); |
| 87 | + |
| 88 | + $result = $secondaryIndex->getObject('one'); |
| 89 | + $this->assertEquals('one', $result['objectID']); |
| 90 | + |
| 91 | + $result = $secondaryIndex->getSettings(); |
| 92 | + $this->assertEquals(array('objectID'), $result['searchableAttributes']); |
| 93 | + |
| 94 | + $result = $secondaryIndex->getRule($rule['objectID']); |
| 95 | + $this->assertEquals('one', $result['objectID']); |
| 96 | + |
| 97 | + $result = $secondaryIndex->getSynonym($synonym['objectID']); |
| 98 | + $this->assertEquals('one', $result['objectID']); |
| 99 | + |
| 100 | + try { |
| 101 | + AccountClient::copyIndex($copyIndex, $secondaryIndex)->wait(); |
| 102 | + } catch (\Exception $e) { |
| 103 | + $this->assertInstanceOf('\InvalidArgumentException', $e); |
| 104 | + $this->assertEquals( |
| 105 | + 'Destination index already exists. Please delete it before copying index across applications.', |
| 106 | + $e->getMessage() |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments