From d02cba60a66510c26a805a7482cd1d816219616a Mon Sep 17 00:00:00 2001 From: Hayden Date: Mon, 24 Jun 2024 08:46:59 +0700 Subject: [PATCH] feat: collection schema cloning --- src/Collections.php | 5 +++-- tests/Feature/CollectionsTest.php | 21 ++++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Collections.php b/src/Collections.php index 2a224ace..b2a769ae 100644 --- a/src/Collections.php +++ b/src/Collections.php @@ -55,13 +55,14 @@ public function __get($collectionName) /** * @param array $schema + * @param array $options * * @return array * @throws TypesenseClientError|HttpClientException */ - public function create(array $schema): array + public function create(array $schema, array $options = []): array { - return $this->apiCall->post(static::RESOURCE_PATH, $schema); + return $this->apiCall->post(static::RESOURCE_PATH, $schema, true, $options); } /** diff --git a/tests/Feature/CollectionsTest.php b/tests/Feature/CollectionsTest.php index 0fe4fdb0..d719d9df 100644 --- a/tests/Feature/CollectionsTest.php +++ b/tests/Feature/CollectionsTest.php @@ -3,11 +3,13 @@ namespace Feature; use Tests\TestCase; +use Typesense\Collection; use Typesense\Exceptions\ObjectNotFound; class CollectionsTest extends TestCase { private $createCollectionRes = null; + private ?Collection $testCollection = null; protected function setUp(): void @@ -15,7 +17,9 @@ protected function setUp(): void parent::setUp(); $schema = $this->getSchema('books'); + $this->createCollectionRes = $this->client()->collections->create($schema); + $this->testCollection = $this->client()->collections['books']; } public function testCanCreateACollection(): void @@ -25,7 +29,7 @@ public function testCanCreateACollection(): void public function testCanRetrieveACollection(): void { - $response = $this->client()->collections['books']->retrieve(); + $response = $this->testCollection->retrieve(); $this->assertEquals('books', $response['name']); } @@ -39,20 +43,20 @@ public function testCanUpdateACollection(): void ] ] ]; - $response = $this->client()->collections['books']->update($update_schema); + $response = $this->testCollection->update($update_schema); $this->assertEquals('isbn', $response['fields'][0]['name']); $this->assertArrayHasKey('drop', $response['fields'][0]); - $response = $this->client()->collections['books']->retrieve(); + $response = $this->testCollection->retrieve(); $this->assertEquals(5, count($response['fields'])); } public function testCanDeleteACollection(): void { - $this->client()->collections['books']->delete(); + $this->testCollection->delete(); $this->expectException(ObjectNotFound::class); - $this->client()->collections['books']->retrieve(); + $this->testCollection->retrieve(); } public function testCanRetrieveAllCollections(): void @@ -60,4 +64,11 @@ public function testCanRetrieveAllCollections(): void $response = $this->client()->collections->retrieve(); $this->assertCount(1, $response); } + + public function testCanCloneACollectionSchema(): void + { + $response = $this->client()->collections->create(['name' => 'books_v2'], ["src_name" => "books"]); + $this->assertEquals('books_v2', $response['name']); + $this->assertEquals($this->createCollectionRes['fields'], $response['fields']); + } }