Skip to content

Commit

Permalink
throw on unknown options
Browse files Browse the repository at this point in the history
  • Loading branch information
tsterker committed Nov 27, 2023
1 parent 4620d2f commit 43a5bd9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/CollectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TSterker\SolariumCollectionManager;

use InvalidArgumentException;
use Solarium\Client;
use Solarium\Core\Client\State\CollectionState;
use Solarium\Core\Query\Result\ResultInterface;
Expand Down Expand Up @@ -85,7 +86,7 @@ public function ensureCollection(string $name): void
*/
public function create(string $name, array $options = []): ResultInterface
{
$options = array_merge([
$defaults = [
'num_shards' => 1,
'router_name' => 'compositeId',
'nrt_replicas' => 1, // alias: replication_factor
Expand All @@ -95,7 +96,15 @@ public function create(string $name, array $options = []): ResultInterface
// NOTE: maxShardsPerNode has been removed in Solr 9.0
// @see https://solr.apache.org/guide/solr/latest/upgrade-notes/major-changes-in-solr-9.html
// 'max_shards_per_node' => 1,
], $options);
];

$unknownKeys = array_diff_key($options, $defaults);

if (count($unknownKeys) > 0) {
throw new InvalidArgumentException("Unknown options: " . json_encode(array_keys($unknownKeys)));
}

$options = array_merge($defaults, $options);

$q = $this->client->createCollections();

Expand Down
19 changes: 17 additions & 2 deletions tests/Unit/CollectionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TSterker\SolariumCollectionManager\Tests\Unit;

use InvalidArgumentException;
use Mockery;
use Mockery\MockInterface;
use Solarium\Client;
Expand Down Expand Up @@ -64,6 +65,21 @@ public function it_accepts_options()

$this->assertEquals(['dummy' => 'data'], $data->getData());
}

/** @test */
public function it_throws_on_unknown_options()
{
$manager = new CollectionManager(Mockery::mock(Client::class));

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Unknown options: [\"foo\",\"bar\"]");

$data = $manager->create('foo', [
'num_shards' => 111,
'foo' => 'xxx',
'bar' => 'yyy',
]);
}
}

class ClientMockBuilder
Expand All @@ -80,10 +96,9 @@ public function __construct()

$this->query = Mockery::mock(Query::class);

$this->client->shouldReceive('createCollections')->once()->andReturn($this->query);
$this->client->shouldReceive('createCollections')->andReturn($this->query);

$this->client->shouldReceive('collections')
->once()
->with($this->query)
->andReturn(Mockery::mock(
ClusterStatusResult::class,
Expand Down

0 comments on commit 43a5bd9

Please sign in to comment.