Skip to content

Commit

Permalink
Merge pull request #67 from phiHero/master
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
jasonbosco authored Jun 21, 2024
2 parents d0ce46c + 0b36de4 commit 523db65
Show file tree
Hide file tree
Showing 23 changed files with 816 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
coverage: xdebug
- uses: php-actions/composer@v6
- name: Run tests
run: vendor/bin/phpunit --coverage-text
run: vendor/bin/phpunit --coverage-text --testdox
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"Composer\\Config::disableProcessTimeout",
"docker-compose up"
],
"test": "vendor/bin/phpunit",
"test": "vendor/bin/phpunit --testdox",
"lint": "phpcs -v",
"lint:fix": "phpcbf"
}
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
failOnWarning="true"
failOnNotice="true"
failOnRisky="true"
executionOrder="random"
>
<testsuites>
<testsuite name="feature">
Expand Down
60 changes: 60 additions & 0 deletions tests/Feature/AliasesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Feature;

use Tests\TestCase;
use Typesense\Exceptions\ObjectNotFound;

class AliasesTest extends TestCase
{
private $sampleAliasResponse = [
"name" => "companies",
"collection_name" => "companies_june11",
];
private $upsertResponse = null;

protected function setUp(): void
{
parent::setUp();

$aliasedCollection = [
'collection_name' => 'companies_june11'
];
$this->upsertResponse = $this->client()->aliases->upsert('companies', $aliasedCollection);
}

protected function tearDown(): void
{
$aliases = $this->client()->aliases->retrieve();
foreach ($aliases['aliases'] as $alias) {
$this->client()->aliases[$alias['name']]->delete();
}
}

public function testCanUpsertAnAlias(): void
{
$this->assertEquals($this->sampleAliasResponse, $this->upsertResponse);
}

public function testCanRetrieveAlias(): void
{
$response = $this->client()->aliases['companies']->retrieve();
$this->assertEquals($this->sampleAliasResponse, $response);
}

public function testCanDeleteAlias(): void
{
$response = $this->client()->aliases['companies']->delete();
$this->assertEquals($this->sampleAliasResponse, $response);

$this->expectException(ObjectNotFound::class);
$this->client()->aliases['companies']->retrieve();
}

public function testCanRetrieveAllAliases(): void
{
$response = $this->client()->aliases->retrieve();

$this->assertEquals(['aliases' => [0 => $this->sampleAliasResponse]], $response);
}
}
22 changes: 22 additions & 0 deletions tests/Feature/AnalyticsEventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Feature;

use Tests\TestCase;

class AnalyticsEventsTest extends TestCase
{
//* there is no method for sending events
// public function testCanCreateAnEvent(): void
// {
// $returnData = $this->client()->analytics->rules()->even
// $this->assertEquals($returnData['name'], $this->ruleName);
// }

public function testNeedImplementationForAnalyticsEvents(): void
{
$this->markTestIncomplete(
'This test has not been implemented yet.',
);
}
}
57 changes: 57 additions & 0 deletions tests/Feature/AnalyticsRulesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Feature;

use Tests\TestCase;
use Typesense\Exceptions\ObjectNotFound;

class AnalyticsRulesTest extends TestCase
{
private $ruleName = 'product_queries_aggregation';
private $ruleConfiguration = [
"type" => "popular_queries",
"params" => [
"source" => [
"collections" => ["products"]
],
"destination" => [
"collection" => "product_queries"
],
"expand_query" => false,
"limit" => 1000
]
];
private $ruleUpsertResponse = null;

protected function setUp(): void
{
parent::setUp();
$this->ruleUpsertResponse = $this->client()->analytics->rules()->upsert($this->ruleName, $this->ruleConfiguration);
}

public function testCanUpsertARule(): void
{
$this->assertEquals($this->ruleName, $this->ruleUpsertResponse['name']);
}

public function testCanRetrieveARule(): void
{
$returnData = $this->client()->analytics->rules()->{$this->ruleName}->retrieve();
$this->assertEquals($returnData['name'], $this->ruleName);
}

public function testCanDeleteARule(): void
{
$returnData = $this->client()->analytics->rules()->{$this->ruleName}->delete();
$this->assertEquals($returnData['name'], $this->ruleName);

$this->expectException(ObjectNotFound::class);
$this->client()->analytics->rules()->{$this->ruleName}->retrieve();
}

public function testCanRetrieveAllRules(): void
{
$returnData = $this->client()->analytics->rules()->retrieve();
$this->assertCount(1, $returnData['rules']);
}
}
34 changes: 34 additions & 0 deletions tests/Feature/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Feature;

use Tests\TestCase;
use Typesense\Collections;
use Typesense\Stopwords;
use Typesense\Aliases;
use Typesense\Keys;
use Typesense\Debug;
use Typesense\Metrics;
use Typesense\Health;
use Typesense\Operations;
use Typesense\MultiSearch;
use Typesense\Presets;
use Typesense\Analytics;

class ClientTest extends TestCase
{
public function testCanCreateAClient(): void
{
$this->assertInstanceOf(Collections::class, $this->client()->collections);
$this->assertInstanceOf(Stopwords::class, $this->client()->stopwords);
$this->assertInstanceOf(Aliases::class, $this->client()->aliases);
$this->assertInstanceOf(Keys::class, $this->client()->keys);
$this->assertInstanceOf(Debug::class, $this->client()->debug);
$this->assertInstanceOf(Metrics::class, $this->client()->metrics);
$this->assertInstanceOf(Health::class, $this->client()->health);
$this->assertInstanceOf(Operations::class, $this->client()->operations);
$this->assertInstanceOf(MultiSearch::class, $this->client()->multiSearch);
$this->assertInstanceOf(Presets::class, $this->client()->presets);
$this->assertInstanceOf(Analytics::class, $this->client()->analytics);
}
}
40 changes: 0 additions & 40 deletions tests/Feature/CollectionTest.php

This file was deleted.

63 changes: 63 additions & 0 deletions tests/Feature/CollectionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Feature;

use Tests\TestCase;
use Typesense\Exceptions\ObjectNotFound;

class CollectionsTest extends TestCase
{
private $createCollectionRes = null;


protected function setUp(): void
{
parent::setUp();

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

public function testCanCreateACollection(): void
{
$this->assertEquals('books', $this->createCollectionRes['name']);
}

public function testCanRetrieveACollection(): void
{
$response = $this->client()->collections['books']->retrieve();
$this->assertEquals('books', $response['name']);
}

public function testCanUpdateACollection(): void
{
$update_schema = [
'fields' => [
[
'name' => 'isbn',
'drop' => true
]
]
];
$response = $this->client()->collections['books']->update($update_schema);
$this->assertEquals('isbn', $response['fields'][0]['name']);
$this->assertArrayHasKey('drop', $response['fields'][0]);

$response = $this->client()->collections['books']->retrieve();
$this->assertEquals(5, count($response['fields']));
}

public function testCanDeleteACollection(): void
{
$this->client()->collections['books']->delete();

$this->expectException(ObjectNotFound::class);
$this->client()->collections['books']->retrieve();
}

public function testCanRetrieveAllCollections(): void
{
$response = $this->client()->collections->retrieve();
$this->assertCount(1, $response);
}
}
14 changes: 14 additions & 0 deletions tests/Feature/DebugTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Feature;

use Tests\TestCase;

class DebugTest extends TestCase
{
public function testCanRetrieveDebugInformation(): void
{
$returnData = $this->client()->debug->retrieve();
$this->assertArrayHasKey('version', $returnData);
}
}
59 changes: 59 additions & 0 deletions tests/Feature/DocumentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Feature;

use Tests\TestCase;
use Typesense\Exceptions\ObjectNotFound;

class DocumentTest extends TestCase
{
private $documentId = '1';
private $testDocument = null;

protected function setUp(): void
{
parent::setUp();
$this->setUpCollection('books');
$this->setUpDocuments('books');

$this->testDocument = $this->client()->collections['books']->documents[$this->documentId];
}

public function testCanRetrieveADocumentById(): void
{
$response = $this->testDocument->retrieve();
$this->assertEquals($this->documentId, $response['id']);
}

public function testCanUpdateADocumentById(): void
{
$partialDocument = [
"title" => "hello there :D",
];
$response = $this->testDocument->update($partialDocument);
$this->assertEquals("hello there :D", $response['title']);
}

public function testCanUpdateADocumentWithDirtyValuesById(): void
{
$partialDocument = [
"title" => 1,
];
$response = $this->testDocument->update(
$partialDocument,
[
"dirty_values" => "coerce_or_reject",
]
);
$this->assertIsString($response['title']);
}

public function testCanDeleteADocumentById(): void
{
$response = $this->testDocument->delete();
$this->assertEquals($this->documentId, $response['id']);

$this->expectException(ObjectNotFound::class);
$this->testDocument->retrieve();
}
}
Loading

0 comments on commit 523db65

Please sign in to comment.