Skip to content

Commit

Permalink
chore: update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Mar 7, 2022
1 parent 724075c commit 2ab1d04
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/Extensions/AlgoliaObjectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/AlgoliaReindexAllJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/Service/AlgoliaIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -356,6 +360,7 @@ public function getObject($item)
foreach ($indexes as $index) {
try {
$output = $index->getObject($item);

if ($output) {
return $output;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Service/AlgoliaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function getIndexes($excludeReplicas = true)
return $this->indexes;
}


$replicas = [];
$output = [];

Expand Down Expand Up @@ -109,7 +110,6 @@ public function initIndexes($item = null, $excludeReplicas = true)

return [];
}

if (!$item) {
$indexes = $this->getIndexes($excludeReplicas);

Expand Down
4 changes: 1 addition & 3 deletions src/Tasks/AlgoliaReindex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -327,5 +326,4 @@ public function clearErrors()

return $this;
}

}
2 changes: 1 addition & 1 deletion tests/AlgoliaIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AlgoliaIndexerTest extends SapphireTest
]
];

public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

Expand Down
13 changes: 9 additions & 4 deletions tests/AlgoliaObjectExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AlgoliaObjectExtensionTest extends SapphireTest
]
];

public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

Expand All @@ -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');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/AlgoliaQuerierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AlgoliaQuerierTest extends SapphireTest
]
];

public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

Expand Down
1 change: 1 addition & 0 deletions tests/AlgoliaTestObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class AlgoliaTestObject extends DataObject implements TestOnly

private static $table_name = 'AlgoliaTestObject';


public function AbsoluteLink()
{
return Director::absoluteBaseURL();
Expand Down
10 changes: 9 additions & 1 deletion tests/TestAlgoliaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
5 changes: 3 additions & 2 deletions tests/TestAlgoliaServiceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
21 changes: 19 additions & 2 deletions tests/TestAlgoliaServiceIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
}
}
13 changes: 13 additions & 0 deletions tests/TestAlgoliaServiceResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Wilr\SilverStripe\Algolia\Tests;

use SilverStripe\Dev\TestOnly;

class TestAlgoliaServiceResponse implements TestOnly
{
public function valid()
{
return true;
}
}

0 comments on commit 2ab1d04

Please sign in to comment.