diff --git a/src/CollectionManager.php b/src/CollectionManager.php index 68dc7fd..5dd1e9f 100644 --- a/src/CollectionManager.php +++ b/src/CollectionManager.php @@ -2,6 +2,7 @@ namespace TSterker\SolariumCollectionManager; +use InvalidArgumentException; use Solarium\Client; use Solarium\Core\Client\State\CollectionState; use Solarium\Core\Query\Result\ResultInterface; @@ -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 @@ -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(); diff --git a/tests/Unit/CollectionManagerTest.php b/tests/Unit/CollectionManagerTest.php index ec630b6..4ca53fb 100644 --- a/tests/Unit/CollectionManagerTest.php +++ b/tests/Unit/CollectionManagerTest.php @@ -2,6 +2,7 @@ namespace TSterker\SolariumCollectionManager\Tests\Unit; +use InvalidArgumentException; use Mockery; use Mockery\MockInterface; use Solarium\Client; @@ -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 @@ -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,