Skip to content

Commit

Permalink
Getting the unit tests to run a little cleaner.
Browse files Browse the repository at this point in the history
  • Loading branch information
tehnorm committed Apr 25, 2023
1 parent aa5c6c8 commit 4b8a40b
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 73 deletions.
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
"autoload": {
"psr-0": { "": "src/" },
"psr-4": {
"Supabase\\PostgrestPhp\\": "src/"
},
"classmap": [
"src/"
]
"Supabase\\Postgrest\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Supabase\\Test\\": "tests/"
"Supabase\\Postgrest\\Test\\": "tests/"
}
},
"authors": [
Expand All @@ -38,6 +35,7 @@
},
"require": {
"guzzlehttp/guzzle": "7.5",
"vlucas/phpdotenv": "5.5"
"vlucas/phpdotenv": "5.5",
"spatie/url": "2.2"
}
}
10 changes: 1 addition & 9 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="./vendor/autoload.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
cacheResult ="false" processUncoveredFilesFromWhitelist="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheResult ="false">
<testsuites>
<testsuite name="test-unit">
<directory>./tests/unit</directory>
Expand All @@ -11,11 +8,6 @@ cacheResult ="false" processUncoveredFilesFromWhitelist="false">
<directory>./tests/integration</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
<php>
<env name="REFERENCE_ID" value=""/>
<env name="API_KEY" value=""/>
Expand Down
17 changes: 9 additions & 8 deletions src/Postgrest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ class Postgrest
public $headers;
private $body;
private $schema;
private $fetch;
private $shouldThrowOnError;
private $signal;
public $allowEmpty;
private $reference_id;
private $api_key;

public function __construct($url, $reference_id, $api_key, $opts = [], $domain = '.supabase.co', $scheme = 'https://', $path = '/rest/v1/')
public function __construct($url, $opts = [])
{
$this->method = (isset($opts['method']) && in_array($opts['method'], ['GET', 'HEAD', 'POST', 'PATCH', 'PUT', 'DELETE'])) ? $opts['method'] : null;
$this->url = $url;
Expand All @@ -27,10 +26,6 @@ public function __construct($url, $reference_id, $api_key, $opts = [], $domain =
$this->allowEmpty = isset($opts['allowEmpty']) && $opts['allowEmpty'];
$this->fetch = isset($opts) && isset($opts->fetch) && $opts->fetch;
$this->body = isset($opts['body']) ? $opts['body'] : [];
$this->reference_id = $reference_id;
$this->api_key = $api_key;
$this->domain = $domain;
$this->path = $path;
}

public function execute()
Expand Down Expand Up @@ -142,7 +137,13 @@ public function execute()

class PostgrestResponse
{
public function __construct($data = '', $error, $count = 0, $status = 0, $statusText = '')
public mixed $data;
public mixed $error;
public int$count;
public int $status;
public string $statusText;

public function __construct($data = '', $error = null, $count = 0, $status = 0, $statusText = '')
{
$this->data = $data;
$this->error = $error;
Expand Down
2 changes: 1 addition & 1 deletion src/PostgrestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct($reference_id, $api_key, $opts = [], $domain = '.sup
$this->url = Url::fromString($scheme.$reference_id.$domain);
$headers = ['Authorization' => "Bearer {$api_key}", 'apikey'=>$api_key];
$this->headers = array_merge(Constants::getDefaultHeaders(), $headers);

$this->schema = (isset($opts) && isset($opts['schema'])) && $opts['schema'];
$this->fetch = isset($opts) && isset($opts->fetch) && $opts->fetch;
$this->reference_id = $reference_id;
Expand Down Expand Up @@ -57,7 +58,6 @@ public function rpc($fn, $args = [], $opts = [])
}

return new PostgrestFilter($url, $this->reference_id, [
'url' => $url,
'headers' => $this->headers,
'schema' => $this->schema,
'fetch' => $this->fetch,
Expand Down
2 changes: 1 addition & 1 deletion src/PostgrestFilter.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$FilterOperators = ['eq', 'neq', 'gt', 'gte', 'lt', 'lte', 'like', 'ilike', 'is', 'in', 'cs', 'cd', 'sl', 'sr', 'nxr', 'nxl', 'adj', 'ov', 'fts', 'plfts', 'phfts', 'wfts'];
class PostgrestFilter2 extends PostgrestTransform
class PostgrestFilter extends PostgrestTransform
{
public function eq($column, $value)
{
Expand Down
40 changes: 40 additions & 0 deletions src/Util/EnvSetup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Supabase\Util;

use Dotenv\Dotenv;

class EnvSetup
{
public static function env($path): array
{
// If the env vars are set and not empty use them
$apiKey = getenv('API_KEY');
$refId = getenv('REFERENCE_ID');

// else check try to load the .env file in the $path
if (empty($apiKey) || empty($refId)) {
$loaded = Dotenv::createArrayBacked($path)->safeLoad();
if (key_exists('API_KEY', $loaded)) {
$apiKey = $loaded['API_KEY'];
}

if (key_exists('REFERENCE_ID', $loaded)) {
$refId = $loaded['REFERENCE_ID'];
}
}

if (empty($apiKey)) {
throw new \Exception('Could not load API_KEY');
}

if (empty($refId)) {
throw new \Exception('Could not load REFERENCE_ID');
}

return [
'API_KEY' => $apiKey,
'REFERENCE_ID' => $refId,
];
}
}
4 changes: 0 additions & 4 deletions tests/unit/PostgrestClientTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<?php

declare(strict_types=1);
require 'vendor/autoload.php';
use PHPUnit\Framework\TestCase;

use function PHPUnit\Framework\assertSame;

final class PostgrestClientTest extends TestCase
{
private $client;
Expand Down
61 changes: 24 additions & 37 deletions tests/unit/PostgrestFilterTest.php
Original file line number Diff line number Diff line change
@@ -1,162 +1,149 @@
<?php

declare(strict_types=1);
require 'vendor/autoload.php';
use PHPUnit\Framework\TestCase;
use Spatie\Url\Url;

use function PHPUnit\Framework\assertEquals;

require __DIR__.'/../../src/PostgrestFilter.php';

final class PostgrestFilterTest extends TestCase
{
private $filter;

public function setup(): void
{
parent::setUp();
\Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv = \Dotenv\Dotenv::createUnsafeImmutable(__DIR__, '/../../.env.test');
$dotenv->load();
$api_key = getenv('API_KEY');
$reference_id = getenv('REFERENCE_ID');
$url = Url::fromString('https://'.$reference_id.'.supabase.co'.'/rest/v1/');
$this->filter = new PostgrestFilter($url, $reference_id, $api_key);
$url = Url::fromString('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/');
$this->filter = new PostgrestFilter($url);
}
//should look like this https://gpdefvsxamnscceccczu.supabase.co/rest/v1/countries?name=eq.Algeria&select=*

public function testEq(): void
{
$result = $this->filter->eq('name', 'Algeria');
print_r((string) $result->url);
ob_flush();
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=eq.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=eq.Algeria', (string) $result->url);
}

public function testNeq(): void
{
$result = $this->filter->neq('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=neq.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=neq.Algeria', (string) $result->url);
}

public function testGt(): void
{
$result = $this->filter->gt('id', '1');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?id=gt.1', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?id=gt.1', (string) $result->url);
}

public function testGte(): void
{
$result = $this->filter->gte('id', '1');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?id=gte.1', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?id=gte.1', (string) $result->url);
}

public function testLt(): void
{
$result = $this->filter->lt('id', '1');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?id=lt.1', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?id=lt.1', (string) $result->url);
}

public function testLte(): void
{
$result = $this->filter->lte('id', '1');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?id=lte.1', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?id=lte.1', (string) $result->url);
}

public function testLike(): void
{
$result = $this->filter->like('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=like.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=like.Algeria', (string) $result->url);
}

public function testiLike(): void
{
$result = $this->filter->ilike('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=ilike.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=ilike.Algeria', (string) $result->url);
}

public function testIs(): void
{
$result = $this->filter->is('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=is.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=is.Algeria', (string) $result->url);
}

public function testIn(): void
{
$result = $this->filter->in('Algeria', ['countries', 'id']);
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?Algeria=in.%28countries%2Cid%29', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?Algeria=in.%28countries%2Cid%29', (string) $result->url);
}

public function testContains(): void
{
$result = $this->filter->contains('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=cs.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=cs.Algeria', (string) $result->url);
}

public function testContainedBy(): void
{
$result = $this->filter->containedBy('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=cd.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=cd.Algeria', (string) $result->url);
}

public function testRangeGt(): void
{
$result = $this->filter->rangeGt('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=sr.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=sr.Algeria', (string) $result->url);
}

public function testRangeGte(): void
{
$result = $this->filter->rangeGte('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=nxl.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=nxl.Algeria', (string) $result->url);
}

public function testRangeLt(): void
{
$result = $this->filter->rangeLt('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=sl.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=sl.Algeria', (string) $result->url);
}

public function testRangeLte(): void
{
$result = $this->filter->rangeLte('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=nxr.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=nxr.Algeria', (string) $result->url);
}

public function testRangeAdjacent(): void
{
$result = $this->filter->rangeAdjacent('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=adj.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=adj.Algeria', (string) $result->url);
}

public function testOverlaps(): void
{
$result = $this->filter->overlaps('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=ov.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=ov.Algeria', (string) $result->url);
}

public function testMatch(): void
{
$result = $this->filter->match('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?', (string) $result->url);
}

public function testNot(): void
{
$result = $this->filter->not('name', 'IS', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=not.IS.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=not.IS.Algeria', (string) $result->url);
}

public function testOr(): void
{
$result = $this->filter->or('name', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?0=or&1=%28name%29', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?0=or&1=%28name%29', (string) $result->url);
}

public function testFilter(): void
{
$result = $this->filter->filter('name', 'IS', 'Algeria');
assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=IS.Algeria', (string) $result->url);
$this->assertEquals('https://gpdefvsxamnscceccczu.supabase.co/rest/v1/?name=IS.Algeria', (string) $result->url);
}
}
6 changes: 0 additions & 6 deletions tests/unit/PostgrestQueryTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
<?php

declare(strict_types=1);
require 'vendor/autoload.php';

use PHPUnit\Framework\TestCase;

use function PHPUnit\Framework\assertEquals;

require __DIR__.'/../../src/PostgrestFilter.php';

final class PostgrestQueryTest extends TestCase
{
private $query;
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/PostgrestTrasformTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class PostgrestTransformTest extends TestCase
{
private $query;
public $url;

public function setup(): void
{
parent::setUp();
\Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv = \Dotenv\Dotenv::createUnsafeImmutable(__DIR__, '/../../.env.test');
$dotenv->load();
$api_key = getenv('API_KEY');
$reference_id = getenv('REFERENCE_ID');
$this->query = new \Supabase\Postgrest\PostgrestQuery($reference_id, $api_key, $opts = []);
}


}

0 comments on commit 4b8a40b

Please sign in to comment.