From 2ab1d04b033df12907de46ea64c1acedbefc103f Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Tue, 8 Mar 2022 10:13:26 +1300 Subject: [PATCH] chore: update unit tests --- src/Extensions/AlgoliaObjectExtension.php | 10 +++++----- src/Jobs/AlgoliaReindexAllJob.php | 2 +- src/Service/AlgoliaIndexer.php | 13 +++++++++---- src/Service/AlgoliaService.php | 2 +- src/Tasks/AlgoliaReindex.php | 4 +--- tests/AlgoliaIndexerTest.php | 2 +- tests/AlgoliaObjectExtensionTest.php | 13 +++++++++---- tests/AlgoliaQuerierTest.php | 2 +- tests/AlgoliaTestObject.php | 1 + tests/TestAlgoliaService.php | 10 +++++++++- tests/TestAlgoliaServiceClient.php | 5 +++-- tests/TestAlgoliaServiceIndex.php | 21 +++++++++++++++++++-- tests/TestAlgoliaServiceResponse.php | 13 +++++++++++++ 13 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 tests/TestAlgoliaServiceResponse.php diff --git a/src/Extensions/AlgoliaObjectExtension.php b/src/Extensions/AlgoliaObjectExtension.php index bbe7a0a..139c20c 100644 --- a/src/Extensions/AlgoliaObjectExtension.php +++ b/src/Extensions/AlgoliaObjectExtension.php @@ -56,19 +56,19 @@ public function indexEnabled(): bool return $this->config('enable_indexer') ? true : false; } - - + + /** * @param FieldList */ public function updateCMSFields(FieldList $fields) - { + { $fields->removeByName('AlgoliaIndexed'); $fields->removeByName('AlgoliaUUID'); $fields->removeByName('AlgoliaError'); } - - + + /** * @param FieldList */ diff --git a/src/Jobs/AlgoliaReindexAllJob.php b/src/Jobs/AlgoliaReindexAllJob.php index 9fed261..dd2e670 100644 --- a/src/Jobs/AlgoliaReindexAllJob.php +++ b/src/Jobs/AlgoliaReindexAllJob.php @@ -81,7 +81,7 @@ public function setup() $this->totalSteps++; } - $this->addMessage('Indexing '. $count . ' '. $candidate . ' instances with filters '. $filter ); + $this->addMessage('Indexing '. $count . ' '. $candidate . ' instances with filters '. $filter); } } } diff --git a/src/Service/AlgoliaIndexer.php b/src/Service/AlgoliaIndexer.php index 4067e23..1c68d84 100644 --- a/src/Service/AlgoliaIndexer.php +++ b/src/Service/AlgoliaIndexer.php @@ -18,7 +18,6 @@ use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\ORM\Map; use SilverStripe\ORM\RelationList; -use stdClass; /** * Handles all the index management and communication with Algolia. Note that @@ -64,16 +63,21 @@ public function indexItem($item) $searchIndexes = $this->getService()->initIndexes($item); $fields = $this->exportAttributesFromObject($item); + if (method_exists($fields, 'toArray')) { + $fields = $fields->toArray(); + } + if ($searchIndexes) { + $output = true; foreach ($searchIndexes as $searchIndex) { - $result = $searchIndex->saveObject($fields->toArray()); + $result = $searchIndex->saveObject($fields); if (!$result->valid()) { - return false; + $output = false; } } - return true; + return $output; } return false; @@ -356,6 +360,7 @@ public function getObject($item) foreach ($indexes as $index) { try { $output = $index->getObject($item); + if ($output) { return $output; } diff --git a/src/Service/AlgoliaService.php b/src/Service/AlgoliaService.php index d70e161..b9dcd19 100644 --- a/src/Service/AlgoliaService.php +++ b/src/Service/AlgoliaService.php @@ -56,6 +56,7 @@ public function getIndexes($excludeReplicas = true) return $this->indexes; } + $replicas = []; $output = []; @@ -109,7 +110,6 @@ public function initIndexes($item = null, $excludeReplicas = true) return []; } - if (!$item) { $indexes = $this->getIndexes($excludeReplicas); diff --git a/src/Tasks/AlgoliaReindex.php b/src/Tasks/AlgoliaReindex.php index 773f648..257fd8e 100644 --- a/src/Tasks/AlgoliaReindex.php +++ b/src/Tasks/AlgoliaReindex.php @@ -173,7 +173,7 @@ public function indexItems($targetClass, $filter = '', $items = null, $output = if ($output) { echo sprintf( - 'Found %s %s remaining to index which match filter (%s), will export in batches of %s, %s batches total %s', + 'Found %s %s remaining to index which match filter (%s), export in batches of %s, %s batches total %s', $total, $targetClass, $filter, @@ -285,7 +285,6 @@ public function indexBatch($items): bool $indexes = Injector::inst()->create(AlgoliaService::class)->initIndexes($items[0]); try { - foreach ($indexes as $index) { $result = $index->saveObjects($items, [ 'autoGenerateObjectIDIfNotExist' => true @@ -327,5 +326,4 @@ public function clearErrors() return $this; } - } diff --git a/tests/AlgoliaIndexerTest.php b/tests/AlgoliaIndexerTest.php index 5298c32..c21070f 100644 --- a/tests/AlgoliaIndexerTest.php +++ b/tests/AlgoliaIndexerTest.php @@ -23,7 +23,7 @@ class AlgoliaIndexerTest extends SapphireTest ] ]; - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); diff --git a/tests/AlgoliaObjectExtensionTest.php b/tests/AlgoliaObjectExtensionTest.php index 3806d13..c582442 100644 --- a/tests/AlgoliaObjectExtensionTest.php +++ b/tests/AlgoliaObjectExtensionTest.php @@ -23,7 +23,7 @@ class AlgoliaObjectExtensionTest extends SapphireTest ] ]; - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); @@ -38,15 +38,20 @@ public function testIndexInAlgolia() $object->Active = false; $object->write(); - $this->assertFalse($object->canIndexInAlgolia(), 'Objects with canIndexInAlgolia() set to false should not index'); + $this->assertFalse( + min($object->invokeWithExtensions('canIndexInAlgolia')), + 'Objects with canIndexInAlgolia() false should not index' + ); $object->Active = true; $object->write(); - $this->assertTrue($object->canIndexInAlgolia(), 'Objects with canIndexInAlgolia() set to true should index'); + $this->assertTrue( + min($object->invokeWithExtensions('canIndexInAlgolia')), + 'Objects with canIndexInAlgolia() set to true should index' + ); $index = $object->indexInAlgolia(); - $this->assertTrue($index, 'Indexed in Algolia'); } diff --git a/tests/AlgoliaQuerierTest.php b/tests/AlgoliaQuerierTest.php index 231b585..16252f4 100644 --- a/tests/AlgoliaQuerierTest.php +++ b/tests/AlgoliaQuerierTest.php @@ -25,7 +25,7 @@ class AlgoliaQuerierTest extends SapphireTest ] ]; - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); diff --git a/tests/AlgoliaTestObject.php b/tests/AlgoliaTestObject.php index 967fc77..5b9db03 100644 --- a/tests/AlgoliaTestObject.php +++ b/tests/AlgoliaTestObject.php @@ -36,6 +36,7 @@ class AlgoliaTestObject extends DataObject implements TestOnly private static $table_name = 'AlgoliaTestObject'; + public function AbsoluteLink() { return Director::absoluteBaseURL(); diff --git a/tests/TestAlgoliaService.php b/tests/TestAlgoliaService.php index 1e22920..cd4ee7e 100644 --- a/tests/TestAlgoliaService.php +++ b/tests/TestAlgoliaService.php @@ -7,8 +7,16 @@ class TestAlgoliaService extends AlgoliaService implements TestOnly { + public $indexes = [ + 'testIndex' => [ + 'includeClasses' => [ + AlgoliaTestObject::class + ] + ] + ]; + public function getClient() { - return new TestAlgoliaServiceClient(); + return TestAlgoliaServiceClient::create('ABC', '123'); } } diff --git a/tests/TestAlgoliaServiceClient.php b/tests/TestAlgoliaServiceClient.php index 3ac26d8..0f76de1 100644 --- a/tests/TestAlgoliaServiceClient.php +++ b/tests/TestAlgoliaServiceClient.php @@ -2,12 +2,13 @@ namespace Wilr\SilverStripe\Algolia\Tests; +use Algolia\AlgoliaSearch\SearchClient; use SilverStripe\Dev\TestOnly; -class TestAlgoliaServiceClient implements TestOnly +class TestAlgoliaServiceClient extends SearchClient implements TestOnly { public function initIndex($name) { - return new TestAlgoliaServiceIndex(); + return new TestAlgoliaServiceIndex($name, $this->api, $this->config); } } diff --git a/tests/TestAlgoliaServiceIndex.php b/tests/TestAlgoliaServiceIndex.php index 94c3ac1..def0023 100644 --- a/tests/TestAlgoliaServiceIndex.php +++ b/tests/TestAlgoliaServiceIndex.php @@ -2,11 +2,14 @@ namespace Wilr\SilverStripe\Algolia\Tests; +use Algolia\AlgoliaSearch\SearchIndex; use SilverStripe\Dev\TestOnly; -class TestAlgoliaServiceIndex implements TestOnly +class TestAlgoliaServiceIndex extends SearchIndex implements TestOnly { - public function setSettings($settings) + private $objects = []; + + public function setSettings($settings, $requestOptions = array()) { return $settings; } @@ -20,4 +23,18 @@ public function search($query, $requestOptions = array()) 'hitsPerPage' => 10 ]; } + + public function deleteObject($objectId, $requestOptions = array()) + { + if (isset($this->objects[$objectId])) { + unset($this->objects[$objectId]); + } + } + + public function saveObject($object, $requestOptions = array()) + { + $this->objects[$object['objectID']] = $object; + + return new TestAlgoliaServiceResponse(); + } } diff --git a/tests/TestAlgoliaServiceResponse.php b/tests/TestAlgoliaServiceResponse.php new file mode 100644 index 0000000..322f052 --- /dev/null +++ b/tests/TestAlgoliaServiceResponse.php @@ -0,0 +1,13 @@ +