From b7b01fcc2555e4fc8e52dacb87b9658d49c2f199 Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Tue, 6 Aug 2019 14:25:11 +1000 Subject: [PATCH] Fixing pagination problem and namespace issues --- src/Engine.php | 27 +++++++++---------- ...{SwiftypeDelete.php => DeleteDocument.php} | 4 +-- .../{SwiftypeSync.php => SyncDocument.php} | 4 +-- ...ypeDocument.php => IsSwiftypeDocument.php} | 21 ++++++++------- tests/Feature/SwiftypeEngineFacadeTest.php | 20 +++----------- tests/Models/TestModel.php | 7 +++-- 6 files changed, 35 insertions(+), 48 deletions(-) rename src/Jobs/{SwiftypeDelete.php => DeleteDocument.php} (90%) rename src/Jobs/{SwiftypeSync.php => SyncDocument.php} (90%) rename src/Traits/{ExistsAsSwiftypeDocument.php => IsSwiftypeDocument.php} (56%) diff --git a/src/Engine.php b/src/Engine.php index 54db241..7745859 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -129,11 +129,13 @@ public function deleteDocuments($documentIds) public function listDocuments($page = 1, $pageSize = self::MAX_PAGE_SIZE) { $response = $this->client->get('documents/list', [ - 'page' => [ - 'current' => $page, - // enforce 100 max - 'size' => min(self::MAX_PAGE_SIZE, $pageSize), - ], + 'json' => [ + 'page' => [ + 'current' => $page, + // enforce 100 max + 'size' => min(self::MAX_PAGE_SIZE, $pageSize), + ], + ] ]); return json_decode($response->getBody()->getContents(), true); @@ -150,15 +152,12 @@ public function listAllDocumentsByPages(callable $action, $page = 1, $pageSize = { // start with page 1 $currentPage = $page; - do { + $finalPage = 1; + while($currentPage <= $finalPage) { + // Swiftype paginates results 100 per page - $chunkResult = $this->listDocuments([ - 'page' => [ - 'current' => $currentPage, - // enforce self::MAX_PAGE_SIZE max - 'size' => min(self::MAX_PAGE_SIZE, $pageSize), - ], - ]); + $chunkResult = $this->listDocuments($currentPage, $pageSize); + // pagination data $finalPage = $chunkResult['meta']['page']['total_pages']; $currentPage = $chunkResult['meta']['page']['current']; @@ -171,7 +170,7 @@ public function listAllDocumentsByPages(callable $action, $page = 1, $pageSize = $action($chunkResult['results'], $currentPage, $finalPage); $currentPage++; - } while ($currentPage <= $finalPage); + } } /** diff --git a/src/Jobs/SwiftypeDelete.php b/src/Jobs/DeleteDocument.php similarity index 90% rename from src/Jobs/SwiftypeDelete.php rename to src/Jobs/DeleteDocument.php index 5480b96..a013155 100644 --- a/src/Jobs/SwiftypeDelete.php +++ b/src/Jobs/DeleteDocument.php @@ -1,6 +1,6 @@ getModelSwiftypeTransformed(); + $data = $model->getSwiftypeAttributes(); if (! empty($data)) { - dispatch(new SwiftypeSync($data)); + dispatch(new SyncDocument($data)); } }); static::created(function ($model) { - $data = $model->getModelSwiftypeTransformed(); + $data = $model->getSwiftypeAttributes(); if (! empty($data)) { - dispatch(new SwiftypeSync($data)); + dispatch(new SyncDocument($data)); } }); static::deleted(function ($model) { - dispatch(new SwiftypeDelete($model->getKey())); + dispatch(new DeleteDocument($model->getKey())); }); } - public function getModelSwiftypeTransformed() + public function getSwiftypeAttributes() { $attributes = $this->getAttributes(); // make sure that there is an id field set for swiftype diff --git a/tests/Feature/SwiftypeEngineFacadeTest.php b/tests/Feature/SwiftypeEngineFacadeTest.php index 54abd35..7ffb28c 100644 --- a/tests/Feature/SwiftypeEngineFacadeTest.php +++ b/tests/Feature/SwiftypeEngineFacadeTest.php @@ -13,7 +13,7 @@ public function testSeedDocuments() $this->log('Starting testSeedDocuments'); $modelData = []; for ($i = 0; $i < 5; $i++) { - $modelData[] = (new TestModel())->getModelSwiftypeTransformed(); + $modelData[] = (new TestModel())->getSwiftypeAttributes(); } $results = SwiftypeEngine::createOrUpdateDocuments($modelData); $this->assertCount(5, $results, 'All ids are returned'); @@ -52,9 +52,10 @@ public function testListAllDocumentsWorks() */ public function testSearchWorks() { - $results = SwiftypeEngine::listDocuments(); + $results = SwiftypeEngine::listDocuments(1, 1); + $this->assertCount(1, $results['results'], 'Only one result'); - $query = $results['results'][0]['first_name']; + $query = $results['results'][0]['advisor_name']; $this->log('Starting testSearchWorks'); $documents = SwiftypeEngine::searchWithQuery($query); @@ -62,17 +63,4 @@ public function testSearchWorks() $this->log('Search for '.$query.': ', count($documents['results'])); } - /** - * A basic test example. - * - * @return void - */ - public function testPurgeDocuments() - { - $this->log('Starting testPurgeDocuments'); - $documentIds = SwiftypeEngine::purgeAllDocuments(); - - $this->assertNotEmpty($documentIds, 'Purge documents returned a valid response'); - $this->log('Deleted documents', count($documentIds)); - } } diff --git a/tests/Models/TestModel.php b/tests/Models/TestModel.php index a2dfb89..443fe8a 100644 --- a/tests/Models/TestModel.php +++ b/tests/Models/TestModel.php @@ -3,19 +3,18 @@ namespace Loonpwn\Swiftype\Tests\Models; use Ramsey\Uuid\Uuid; -use Loonpwn\Swiftype\Traits\ExistsAsSwiftypeDocument; +use Loonpwn\Swiftype\Traits\IsSwiftypeDocument; class TestModel { - use ExistsAsSwiftypeDocument; + use IsSwiftypeDocument; public function getAttributes() { $faker = \Faker\Factory::create(); return [ - 'first_name' => $faker->firstName, - 'last_name' => $faker->lastName, + 'advisor_name' => $faker->name, 'location' => $faker->address, 'created_at' => $faker->dateTime(), ];