Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
regnerisch committed Jun 16, 2024
1 parent be1db86 commit 6c8d735
Show file tree
Hide file tree
Showing 39 changed files with 267 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea
.tmp
.phpunit.result.cache
/composer.lock
phpunit.xml
vendor
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ Run Typesense Server:
composer run-script typesenseServer
```

Run tests:

```shell script
docker compose up
cp phpunit.xml.dist phpunit.xml
composer run-script test
```

## Credits

This client was originally developed by [Abdullah Al-Faqeir](https://github.org/abdullahfaqeir) from
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
"Typesense\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"require": {
"php": ">=7.4",
"ext-json": "*",
Expand All @@ -42,6 +47,7 @@
"psr/http-factory": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^11.2",
"squizlabs/php_codesniffer": "3.*",
"symfony/http-client": "^5.2"
},
Expand All @@ -57,6 +63,7 @@
"Composer\\Config::disableProcessTimeout",
"docker-compose up"
],
"test": "vendor/bin/phpunit",
"lint": "phpcs -v",
"lint:fix": "phpcbf"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/collection_operations.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
echo "--------Delete Document-------\n";
echo "\n";
echo "--------Import Documents-------\n";
$docsToImport = [];
$docsToImport = [];
$exportedDocStrsArray = explode('\n', $exportedDocStrs);
foreach ($exportedDocStrsArray as $exportedDocStr) {
$docsToImport[] = json_decode($exportedDocStr, true);
Expand Down
3 changes: 2 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<file>src</file>
<file>examples</file>
<file>tests/</file>

<exclude-pattern>*/src/Standards/*/Tests/*\.(inc|css|js)$</exclude-pattern>
<exclude-pattern>*/tests/Core/*/*\.(inc|css|js)$</exclude-pattern>
Expand Down Expand Up @@ -42,4 +43,4 @@
</property>
</properties>
</rule>
</ruleset>
</ruleset>
18 changes: 18 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
>
<testsuites>
<testsuite name="feature">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>

<php>
<env name="TYPESENSE_NODE_HOST" value="host.docker.internal" />
<env name="TYPESENSE_NODE_PORT" value="8108" />
<env name="TYPESENSE_NODE_PROTOCOL" value="http" />
<env name="TYPESENSE_API_KEY" value="xyz" />
</php>
</phpunit>
1 change: 0 additions & 1 deletion src/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class Alias
{

/**
* @var string
*/
Expand Down
1 change: 0 additions & 1 deletion src/Aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class Aliases implements \ArrayAccess
{

public const RESOURCE_PATH = '/aliases';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/AnalyticsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AnalyticsRule
public function __construct(string $ruleName, ApiCall $apiCall)
{
$this->ruleName = $ruleName;
$this->apiCall = $apiCall;
$this->apiCall = $apiCall;
}

public function retrieve()
Expand Down
1 change: 0 additions & 1 deletion src/ApiCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
*/
class ApiCall
{

private const API_KEY_HEADER_NAME = 'X-TYPESENSE-API-KEY';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function __construct(array $config)
$this->apiCall = new ApiCall($this->config);

$this->collections = new Collections($this->apiCall);
$this->stopwords = new Stopwords($this->apiCall);
$this->stopwords = new Stopwords($this->apiCall);
$this->aliases = new Aliases($this->apiCall);
$this->keys = new Keys($this->apiCall);
$this->debug = new Debug($this->apiCall);
Expand Down
1 change: 0 additions & 1 deletion src/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class Collections implements \ArrayAccess
{

public const RESOURCE_PATH = '/collections';

/**
Expand Down
1 change: 0 additions & 1 deletion src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class Document
{

/**
* @var string
*/
Expand Down
1 change: 0 additions & 1 deletion src/Documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class Documents implements \ArrayAccess
{

public const RESOURCE_PATH = 'documents';

/**
Expand Down
1 change: 0 additions & 1 deletion src/Exceptions/ConfigError.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
*/
class ConfigError extends TypesenseClientError
{

}
1 change: 0 additions & 1 deletion src/Exceptions/HTTPStatus0Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@
*/
class HTTPStatus0Error extends TypesenseClientError
{

}
1 change: 0 additions & 1 deletion src/Exceptions/ObjectAlreadyExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
*/
class ObjectAlreadyExists extends TypesenseClientError
{

}
1 change: 0 additions & 1 deletion src/Exceptions/ObjectNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
*/
class ObjectNotFound extends TypesenseClientError
{

}
1 change: 0 additions & 1 deletion src/Exceptions/ObjectUnprocessable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
*/
class ObjectUnprocessable extends TypesenseClientError
{

}
1 change: 0 additions & 1 deletion src/Exceptions/RequestMalformed.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
*/
class RequestMalformed extends TypesenseClientError
{

}
1 change: 0 additions & 1 deletion src/Exceptions/RequestUnauthorized.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
*/
class RequestUnauthorized extends TypesenseClientError
{

}
1 change: 0 additions & 1 deletion src/Exceptions/ServerError.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
*/
class ServerError extends TypesenseClientError
{

}
1 change: 0 additions & 1 deletion src/Exceptions/ServiceUnavailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
*/
class ServiceUnavailable extends TypesenseClientError
{

}
1 change: 0 additions & 1 deletion src/Exceptions/Timeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
*/
class Timeout extends TypesenseClientError
{

}
1 change: 0 additions & 1 deletion src/Exceptions/TypesenseClientError.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
class TypesenseClientError extends Exception
{

public function setMessage(string $message): TypesenseClientError
{
$this->message = $message;
Expand Down
1 change: 0 additions & 1 deletion src/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class Key
{

/**
* @var ApiCall
*/
Expand Down
10 changes: 5 additions & 5 deletions src/Keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class Keys implements \ArrayAccess
{

public const RESOURCE_PATH = '/keys';

/**
Expand Down Expand Up @@ -59,15 +58,16 @@ public function generateScopedSearchKey(
string $searchKey,
array $parameters
): string {
$paramStr = json_encode($parameters, JSON_THROW_ON_ERROR);
$digest = base64_encode(
$paramStr = json_encode($parameters, JSON_THROW_ON_ERROR);
$digest = base64_encode(
hash_hmac(
'sha256',
mb_convert_encoding($paramStr, 'UTF-8', 'ISO-8859-1'),
mb_convert_encoding($searchKey, 'UTF-8', 'ISO-8859-1'),
true)
true
)
);
$keyPrefix = substr($searchKey, 0, 4);
$keyPrefix = substr($searchKey, 0, 4);
$rawScopedKey = sprintf(
'%s%s%s',
mb_convert_encoding($digest, 'ISO-8859-1', 'UTF-8'),
Expand Down
5 changes: 2 additions & 3 deletions src/Lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
class Configuration
{

/**
* @var Node[]
*/
Expand Down Expand Up @@ -219,8 +218,8 @@ public function getClient(): ClientInterface
{
return new HttpMethodsClient(
$this->client ?? Psr18ClientDiscovery::find(),
Psr17FactoryDiscovery::findRequestFactory(),
Psr17FactoryDiscovery::findStreamFactory(),
Psr17FactoryDiscovery::findRequestFactory(),
Psr17FactoryDiscovery::findStreamFactory(),
);
}
}
1 change: 0 additions & 1 deletion src/Lib/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
class Node
{

/**
* @var string
*/
Expand Down
1 change: 0 additions & 1 deletion src/Override.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class Override
{

/**
* @var string
*/
Expand Down
1 change: 0 additions & 1 deletion src/Overrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class Overrides implements \ArrayAccess
{

public const RESOURCE_PATH = 'overrides';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Presets.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function get()
*/
public function put(array $options = [])
{
$presetName = $options['preset_name'];
$presetName = $options['preset_name'];
$presetsData = $options['preset_data'];

return $this->apiCall->put($this->endpointPath($presetName), $presetsData);
Expand Down
1 change: 0 additions & 1 deletion src/Synonym.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
class Synonym
{

/**
* @var string
*/
Expand Down
1 change: 0 additions & 1 deletion src/Synonyms.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
class Synonyms implements \ArrayAccess
{

public const RESOURCE_PATH = 'synonyms';

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/Feature/CollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Feature;

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

class CollectionTest extends TestCase
{
public function testCanCreateCollection(): void
{
$schema = $this->getSchema('books');

$response = $this->client()->collections->create($schema);

$this->assertEquals('books', $response['name']);
}

public function testCanRetrieveCollection(): void
{
$schema = $this->getSchema('books');
$this->client()->collections->create($schema);

$response = $this->client()->collections['books']->retrieve();

$this->assertEquals('books', $response['name']);
}

public function testCanDeleteCollection(): void
{
$schema = $this->getSchema('books');
$this->client()->collections->create($schema);

$this->client()->collections['books']->delete();

$this->expectException(ObjectNotFound::class);

$this->client()->collections['books']->retrieve();
}
}
Loading

0 comments on commit 6c8d735

Please sign in to comment.