Skip to content

Commit

Permalink
Merge pull request #68 from phiHero/master
Browse files Browse the repository at this point in the history
#54: add collection schema cloning
  • Loading branch information
jasonbosco authored Jun 24, 2024
2 parents 523db65 + d02cba6 commit 9a04673
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
21 changes: 16 additions & 5 deletions tests/Feature/CollectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
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
{
parent::setUp();

$schema = $this->getSchema('books');

$this->createCollectionRes = $this->client()->collections->create($schema);
$this->testCollection = $this->client()->collections['books'];
}

public function testCanCreateACollection(): void
Expand All @@ -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']);
}

Expand All @@ -39,25 +43,32 @@ 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
{
$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']);
}
}

0 comments on commit 9a04673

Please sign in to comment.