Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add facets settings routes #36

Merged
merged 1 commit into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,25 @@ public function updateAcceptNewFields($accept_new_fields)
return $this->httpPost('/indexes/'.$this->uid.'/settings/accept-new-fields', $accept_new_fields);
}

// Settings - Attributes for faceting

public function getAttributesForFaceting()
{
return $this->httpGet('/indexes/'.$this->uid.'/settings/attributes-for-faceting');
}

public function updateAttributesForFaceting($attributes_for_faceting)
{
return $this->httpPost('/indexes/'.$this->uid.'/settings/attributes-for-faceting', $attributes_for_faceting);
}

public function resetAttributesForFaceting()
{
return $this->httpDelete('/indexes/'.$this->uid.'/settings/attributes-for-faceting');
}

// PRIVATE

private function flattenOptions(array $options)
{
return array_map(function ($entry) {
Expand Down
59 changes: 59 additions & 0 deletions tests/Settings/AttributesForFacetingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Tests\Settings;

use Tests\TestCase;

class AttributesForFacetingTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->client->deleteAllIndexes();
}

public function testGetDefaultAttributesForFaceting()
{
$index = $this->client->createIndex('index');

$attributes = $index->getAttributesForFaceting();

$this->assertIsArray($attributes);
$this->assertEmpty($attributes);
}

public function testUpdateAttributesForFaceting()
{
$newAttributes = ['title'];
$index = $this->client->createIndex('index');

$promise = $index->updateAttributesForFaceting($newAttributes);

$this->assertIsValidPromise($promise);
$index->waitForPendingUpdate($promise['updateId']);

$attributesForFaceting = $index->getAttributesForFaceting();

$this->assertIsArray($attributesForFaceting);
$this->assertEquals($newAttributes, $attributesForFaceting);
}

public function testResetAttributesForFaceting()
{
$index = $this->client->createIndex('index');
$newAttributes = ['title'];

$promise = $index->updateAttributesForFaceting($newAttributes);
$index->waitForPendingUpdate($promise['updateId']);

$promise = $index->resetAttributesForFaceting();

$this->assertIsValidPromise($promise);

$index->waitForPendingUpdate($promise['updateId']);

$attributesForFaceting = $index->getAttributesForFaceting();
$this->assertIsArray($attributesForFaceting);
$this->assertEmpty($attributesForFaceting);
}
}