Skip to content

Commit 9a04673

Browse files
authored
Merge pull request #68 from phiHero/master
#54: add collection schema cloning
2 parents 523db65 + d02cba6 commit 9a04673

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/Collections.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ public function __get($collectionName)
5555

5656
/**
5757
* @param array $schema
58+
* @param array $options
5859
*
5960
* @return array
6061
* @throws TypesenseClientError|HttpClientException
6162
*/
62-
public function create(array $schema): array
63+
public function create(array $schema, array $options = []): array
6364
{
64-
return $this->apiCall->post(static::RESOURCE_PATH, $schema);
65+
return $this->apiCall->post(static::RESOURCE_PATH, $schema, true, $options);
6566
}
6667

6768
/**

tests/Feature/CollectionsTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@
33
namespace Feature;
44

55
use Tests\TestCase;
6+
use Typesense\Collection;
67
use Typesense\Exceptions\ObjectNotFound;
78

89
class CollectionsTest extends TestCase
910
{
1011
private $createCollectionRes = null;
12+
private ?Collection $testCollection = null;
1113

1214

1315
protected function setUp(): void
1416
{
1517
parent::setUp();
1618

1719
$schema = $this->getSchema('books');
20+
1821
$this->createCollectionRes = $this->client()->collections->create($schema);
22+
$this->testCollection = $this->client()->collections['books'];
1923
}
2024

2125
public function testCanCreateACollection(): void
@@ -25,7 +29,7 @@ public function testCanCreateACollection(): void
2529

2630
public function testCanRetrieveACollection(): void
2731
{
28-
$response = $this->client()->collections['books']->retrieve();
32+
$response = $this->testCollection->retrieve();
2933
$this->assertEquals('books', $response['name']);
3034
}
3135

@@ -39,25 +43,32 @@ public function testCanUpdateACollection(): void
3943
]
4044
]
4145
];
42-
$response = $this->client()->collections['books']->update($update_schema);
46+
$response = $this->testCollection->update($update_schema);
4347
$this->assertEquals('isbn', $response['fields'][0]['name']);
4448
$this->assertArrayHasKey('drop', $response['fields'][0]);
4549

46-
$response = $this->client()->collections['books']->retrieve();
50+
$response = $this->testCollection->retrieve();
4751
$this->assertEquals(5, count($response['fields']));
4852
}
4953

5054
public function testCanDeleteACollection(): void
5155
{
52-
$this->client()->collections['books']->delete();
56+
$this->testCollection->delete();
5357

5458
$this->expectException(ObjectNotFound::class);
55-
$this->client()->collections['books']->retrieve();
59+
$this->testCollection->retrieve();
5660
}
5761

5862
public function testCanRetrieveAllCollections(): void
5963
{
6064
$response = $this->client()->collections->retrieve();
6165
$this->assertCount(1, $response);
6266
}
67+
68+
public function testCanCloneACollectionSchema(): void
69+
{
70+
$response = $this->client()->collections->create(['name' => 'books_v2'], ["src_name" => "books"]);
71+
$this->assertEquals('books_v2', $response['name']);
72+
$this->assertEquals($this->createCollectionRes['fields'], $response['fields']);
73+
}
6374
}

0 commit comments

Comments
 (0)