mirror of
https://github.com/jonathan-martz/pocketbase-php-sdk.git
synced 2026-04-03 07:27:42 +00:00
refactoring collection a little bit, adjust token from collection manual,write some test for the simple logic, fixing superusers authAdmin
This commit is contained in:
46
src/AuthTest.php
Normal file
46
src/AuthTest.php
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pb;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
final class AuthTest extends TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
private Collection $collection;
|
||||||
|
private ?string $url;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev';
|
||||||
|
$this->collection = new Collection($this->url, 'users');
|
||||||
|
}
|
||||||
|
public function testAuthUser(): void
|
||||||
|
{
|
||||||
|
$actual = $this->collection->authAsUser('support@jonathan-martz.de', 'rockt');
|
||||||
|
$expected = '{"data":{},"message":"Failed to authenticate.","status":400}';
|
||||||
|
|
||||||
|
$this->assertEquals($expected, trim($actual, PHP_EOL));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAuthSuperUser(): void
|
||||||
|
{
|
||||||
|
$this->collection = new Collection($this->url, '_superusers');
|
||||||
|
$actual = $this->collection->authAsAdmin('admin@jonathan-martz.de', 'rockt');
|
||||||
|
$expected = '{"data":{},"message":"Failed to authenticate.","status":400}';
|
||||||
|
|
||||||
|
$this->assertEquals($expected, trim($actual, PHP_EOL));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAuthSuperUser2(): void
|
||||||
|
{
|
||||||
|
$this->collection = new Collection($this->url, '_superusers');
|
||||||
|
|
||||||
|
$actual = $this->collection->authAsAdmin('admin@jonathan-martz.de', 'rockt');
|
||||||
|
|
||||||
|
$data = json_decode($actual,true);
|
||||||
|
$this->assertArrayHasKey('record',$data);
|
||||||
|
$this->assertArrayHasKey('token',$data);
|
||||||
|
$this->assertCount(8,$data['record']);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ namespace Pb;
|
|||||||
class Client
|
class Client
|
||||||
{
|
{
|
||||||
private string $url;
|
private string $url;
|
||||||
private string $token = '';
|
public string $token = '';
|
||||||
|
|
||||||
public function __construct(string $url)
|
public function __construct(string $url)
|
||||||
{
|
{
|
||||||
@@ -26,4 +26,9 @@ class Client
|
|||||||
{
|
{
|
||||||
$this->token = $token;
|
$this->token = $token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAuthToken(): string
|
||||||
|
{
|
||||||
|
return $this->token;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,12 +27,14 @@ class Collection
|
|||||||
* @param string $collection
|
* @param string $collection
|
||||||
* @param string $token
|
* @param string $token
|
||||||
*/
|
*/
|
||||||
public function __construct(string $url, string $collection, string $token)
|
public function __construct(string $url, string $collection, string $token = '')
|
||||||
{
|
{
|
||||||
$this->url = $url;
|
$this->url = $url;
|
||||||
$this->collection = $collection;
|
$this->collection = $collection;
|
||||||
|
if (!empty($token)) {
|
||||||
self::$token = $token;
|
self::$token = $token;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $start
|
* @param int $start
|
||||||
@@ -79,12 +81,13 @@ class Collection
|
|||||||
* @param string $password
|
* @param string $password
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function authAsUser(string $email, string $password)
|
public function authAsUser(string $email, string $password): string
|
||||||
{
|
{
|
||||||
$result = $this->doRequest($this->url . "/api/collections/users/auth-with-password", 'POST', ['identity' => $email, 'password' => $password]);
|
$result = $this->doRequest($this->url . "/api/collections/users/auth-with-password", 'POST', ['identity' => $email, 'password' => $password]);
|
||||||
if (!empty($result['token'])) {
|
if (!empty($result['token'])) {
|
||||||
self::$token = $result['token'];
|
self::$token = $result['token'];
|
||||||
}
|
}
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -121,7 +124,7 @@ class Collection
|
|||||||
*/
|
*/
|
||||||
public function create(array $bodyParams = [], array $queryParams = []): string
|
public function create(array $bodyParams = [], array $queryParams = []): string
|
||||||
{
|
{
|
||||||
return $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records", 'POST', json_encode($bodyParams));
|
return $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records", 'POST', $bodyParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -133,7 +136,7 @@ class Collection
|
|||||||
public function update(string $recordId, array $bodyParams = [], array $queryParams = []): void
|
public function update(string $recordId, array $bodyParams = [], array $queryParams = []): void
|
||||||
{
|
{
|
||||||
// Todo bodyParams equals json, currently workaround
|
// Todo bodyParams equals json, currently workaround
|
||||||
$this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'PATCH', json_encode($bodyParams));
|
$this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'PATCH', $bodyParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -154,6 +157,8 @@ class Collection
|
|||||||
*/
|
*/
|
||||||
public function doRequest(string $url, string $method, $bodyParams = []): string
|
public function doRequest(string $url, string $method, $bodyParams = []): string
|
||||||
{
|
{
|
||||||
|
// TODO move doRequestIntoService ?
|
||||||
|
// TODO replace curl with HttpClient
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
|
|
||||||
if (self::$token != '') {
|
if (self::$token != '') {
|
||||||
@@ -193,11 +198,16 @@ class Collection
|
|||||||
* @param string $password
|
* @param string $password
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function authAsAdmin(string $email, string $password): void
|
public function authAsAdmin(string $email, string $password): string
|
||||||
{
|
{
|
||||||
$bodyParams['identity'] = $email;
|
$bodyParams['identity'] = $email;
|
||||||
$bodyParams['password'] = $password;
|
$bodyParams['password'] = $password;
|
||||||
$output = $this->doRequest($this->url . "/api/admins/auth-with-password", 'POST', $bodyParams);
|
$output = $this->doRequest($this->url . "/api/collections/_superusers/auth-with-password", 'POST', $bodyParams);
|
||||||
self::$token = json_decode($output, true)['token'];
|
$token = json_decode($output, true)['token'];
|
||||||
|
if ($token) {
|
||||||
|
self::$token = $token;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class Settings
|
|||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string $token
|
* @param string $token
|
||||||
*/
|
*/
|
||||||
public function __construct(string $url, string $token)
|
public function __construct(string $url, string $token = '')
|
||||||
{
|
{
|
||||||
$this->url = $url;
|
$this->url = $url;
|
||||||
self::$token = $token;
|
self::$token = $token;
|
||||||
@@ -31,16 +31,10 @@ class Settings
|
|||||||
{
|
{
|
||||||
$bodyParams['identity'] = $email;
|
$bodyParams['identity'] = $email;
|
||||||
$bodyParams['password'] = $password;
|
$bodyParams['password'] = $password;
|
||||||
$output = $this->doRequest($this->url . "/api/admins/auth-with-password", 'POST', $bodyParams);
|
$output = $this->doRequest($this->url . "/api/collections/_superusers/auth-with-password", 'POST', $bodyParams);
|
||||||
self::$token = json_decode($output, true)['token'];
|
self::$token = json_decode($output, true)['token'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $recordId
|
|
||||||
* @param string $url
|
|
||||||
* @param string $method
|
|
||||||
* @return bool|string
|
|
||||||
*/
|
|
||||||
public function doRequest(string $url, string $method, $bodyParams = []): string
|
public function doRequest(string $url, string $method, $bodyParams = []): string
|
||||||
{
|
{
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
@@ -66,9 +60,6 @@ class Settings
|
|||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function getAll():array
|
public function getAll():array
|
||||||
{
|
{
|
||||||
return json_decode($this->doRequest($this->url . '/api/settings', 'GET', []), true);
|
return json_decode($this->doRequest($this->url . '/api/settings', 'GET', []), true);
|
||||||
|
|||||||
8
src/SuperUser.php
Normal file
8
src/SuperUser.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pb;
|
||||||
|
|
||||||
|
class SuperUser
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
18
src/User.php
Normal file
18
src/User.php
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pb;
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
|
class User
|
||||||
|
{
|
||||||
|
public string $avatar;
|
||||||
|
public string $collectionName;
|
||||||
|
public string $collectionId;
|
||||||
|
public bool $emailVisibility;
|
||||||
|
public DateTime $created;
|
||||||
|
public string $id;
|
||||||
|
public string $name;
|
||||||
|
public DateTime $updated;
|
||||||
|
public bool $verified;
|
||||||
|
}
|
||||||
14
src/viewModels/CollectionItem.php
Normal file
14
src/viewModels/CollectionItem.php
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pb\viewModels;
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
|
class CollectionItem
|
||||||
|
{
|
||||||
|
public string $collectionName;
|
||||||
|
public string $collectionId;
|
||||||
|
public DateTime $created;
|
||||||
|
public string $id;
|
||||||
|
public DateTime $updated;
|
||||||
|
}
|
||||||
12
src/viewModels/CollectionResponse.php
Normal file
12
src/viewModels/CollectionResponse.php
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pb\viewModels;
|
||||||
|
|
||||||
|
class CollectionResponse
|
||||||
|
{
|
||||||
|
public array $items;
|
||||||
|
public int $page;
|
||||||
|
public int $perPage;
|
||||||
|
public int $totalItems;
|
||||||
|
public int $totalPages;
|
||||||
|
}
|
||||||
48
tests/ClientTest.php
Normal file
48
tests/ClientTest.php
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Pb\Client;
|
||||||
|
use Pb\Collection;
|
||||||
|
use Pb\Settings;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
final class ClientTest extends TestCase
|
||||||
|
{
|
||||||
|
private Collection $collection;
|
||||||
|
private ?string $url;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev';
|
||||||
|
$this->collection = new Collection($this->url, 'users');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_collection(): void
|
||||||
|
{
|
||||||
|
$pb = new Client($this->url);
|
||||||
|
|
||||||
|
$actual = $pb->collection('users');
|
||||||
|
|
||||||
|
$this->assertEquals($actual, $this->collection);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_settings(): void
|
||||||
|
{
|
||||||
|
$pb = new Client($this->url);
|
||||||
|
|
||||||
|
$actual = $pb->settings();
|
||||||
|
|
||||||
|
$this->assertEquals($actual, new Settings($this->url));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_setToken(){
|
||||||
|
$token = 'test123';
|
||||||
|
|
||||||
|
$pb = new Client($this->url);
|
||||||
|
$pb->setAuthToken($token);
|
||||||
|
|
||||||
|
$this->assertEquals($token, $pb->getAuthToken());
|
||||||
|
$this->assertEquals($token, $pb->token);
|
||||||
|
$pb->token = 'test456';
|
||||||
|
$this->assertEquals('test456', $pb->getAuthToken());
|
||||||
|
}
|
||||||
|
}
|
||||||
55
tests/CollectionGetFirstListItemTest.php
Normal file
55
tests/CollectionGetFirstListItemTest.php
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Pb\Collection;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
final class CollectionGetFirstListItemTest extends TestCase
|
||||||
|
{
|
||||||
|
private Collection $collection;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev';
|
||||||
|
$this->collection = new Collection($url, 'users');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_getOne(): void
|
||||||
|
{
|
||||||
|
$id = '6588yk36406qqv1';
|
||||||
|
$actual = $this->collection->getFirstListItem('id="'.$id.'"');
|
||||||
|
$expected = [
|
||||||
|
'avatar' => '',
|
||||||
|
'collectionId' => '_pb_users_auth_',
|
||||||
|
'collectionName' => 'users',
|
||||||
|
'created' => '2025-01-21 21:22:47.002Z',
|
||||||
|
'emailVisibility' => false,
|
||||||
|
'id' => '6588yk36406qqv1',
|
||||||
|
'name' => 'Jonathan Martz',
|
||||||
|
'updated' => '2025-01-21 21:22:47.002Z',
|
||||||
|
'verified' => true
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $actual);
|
||||||
|
$this->assertCount(9, $actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_getOneWrongId(): void
|
||||||
|
{
|
||||||
|
$id = '6588yk36406qqva';
|
||||||
|
$actual = $this->collection->getFirstListItem('id="'.$id.'"');
|
||||||
|
$expected = [
|
||||||
|
'avatar' => '',
|
||||||
|
'collectionId' => '_pb_users_auth_',
|
||||||
|
'collectionName' => 'users',
|
||||||
|
'created' => '2025-01-21 21:22:47.002Z',
|
||||||
|
'emailVisibility' => false,
|
||||||
|
'id' => '6588yk36406qqv1',
|
||||||
|
'name' => 'Jonathan Martz',
|
||||||
|
'updated' => '2025-01-21 21:22:47.002Z',
|
||||||
|
'verified' => true
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $actual);
|
||||||
|
$this->assertCount(9,$actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
28
tests/CollectionGetFullListTest.php
Normal file
28
tests/CollectionGetFullListTest.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Pb\Collection;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
final class CollectionGetFullListTest extends TestCase
|
||||||
|
{
|
||||||
|
private string $url;
|
||||||
|
private Collection $collection;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev';
|
||||||
|
$this->collection = new Collection($this->url, 'users');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_getFullList_gettingArrayWithOneUser(): void
|
||||||
|
{
|
||||||
|
$actual = $this->collection->getFullList([],100);
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('items', $actual, 'Key "items" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('page', $actual, 'Key "page" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('perPage', $actual, 'Key "perPage" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('totalItems', $actual, 'Key "totalItems" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('totalPages', $actual, 'Key "totalPages" does not exist in the response.');
|
||||||
|
$this->assertCount(1, $actual['items'], 'Expected no items in the response.');
|
||||||
|
}
|
||||||
|
}
|
||||||
76
tests/CollectionGetListTest.php
Normal file
76
tests/CollectionGetListTest.php
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
use Pb\Collection;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
final class CollectionGetListTest extends TestCase
|
||||||
|
{
|
||||||
|
private string $url;
|
||||||
|
private Collection $collection;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev';
|
||||||
|
$this->collection = new Collection($this->url, 'users');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_getList_gettingArrayWithOneItem(): void
|
||||||
|
{
|
||||||
|
$actual = $this->collection->getList(1, 10);
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('items', $actual, 'Key "items" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('page', $actual, 'Key "page" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('perPage', $actual, 'Key "perPage" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('totalItems', $actual, 'Key "totalItems" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('totalPages', $actual, 'Key "totalPages" does not exist in the response.');
|
||||||
|
$this->assertCount(1, $actual['items'], 'Expected no items in the response.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_getListWithContainsMartz(): void
|
||||||
|
{
|
||||||
|
$actual = $this->collection->getList(1, 10, ['filter'=>'name~"%Martz%"']);
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('items', $actual, 'Key "items" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('page', $actual, 'Key "page" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('perPage', $actual, 'Key "perPage" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('totalItems', $actual, 'Key "totalItems" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('totalPages', $actual, 'Key "totalPages" does not exist in the response.');
|
||||||
|
$this->assertCount(1, $actual['items'], 'Expected no items in the response.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_getListStartsWithMartz(): void
|
||||||
|
{
|
||||||
|
$actual = $this->collection->getList(1, 10, ['filter'=>'name~"Martz%"']);
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('items', $actual, 'Key "items" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('page', $actual, 'Key "page" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('perPage', $actual, 'Key "perPage" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('totalItems', $actual, 'Key "totalItems" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('totalPages', $actual, 'Key "totalPages" does not exist in the response.');
|
||||||
|
$this->assertCount(0, $actual['items'], 'Expected no items in the response.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_getListStartWithJonathan(): void
|
||||||
|
{
|
||||||
|
$actual = $this->collection->getList(1, 10, ['filter'=>'name~"Jonathan%"']);
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('items', $actual, 'Key "items" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('page', $actual, 'Key "page" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('perPage', $actual, 'Key "perPage" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('totalItems', $actual, 'Key "totalItems" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('totalPages', $actual, 'Key "totalPages" does not exist in the response.');
|
||||||
|
$this->assertCount(1, $actual['items'], 'Expected no items in the response.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_getListCheckingDifferentName(): void
|
||||||
|
{
|
||||||
|
$actual = $this->collection->getList(1, 10, ['filter'=>'name~"%Sibylle%"']);
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('items', $actual, 'Key "items" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('page', $actual, 'Key "page" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('perPage', $actual, 'Key "perPage" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('totalItems', $actual, 'Key "totalItems" does not exist in the response.');
|
||||||
|
$this->assertArrayHasKey('totalPages', $actual, 'Key "totalPages" does not exist in the response.');
|
||||||
|
$this->assertCount(0, $actual['items'], 'Expected no items in the response.');
|
||||||
|
}
|
||||||
|
}
|
||||||
50
tests/CollectionGetOneTest.php
Normal file
50
tests/CollectionGetOneTest.php
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Pb\Collection;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
final class CollectionGetOneTest extends TestCase
|
||||||
|
{
|
||||||
|
private string $url;
|
||||||
|
private Collection $collection;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev';
|
||||||
|
$this->collection = new Collection($this->url, 'users');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_getOne(): void
|
||||||
|
{
|
||||||
|
$id = '6588yk36406qqv1';
|
||||||
|
$actual = $this->collection->getOne($id);
|
||||||
|
$expected = [
|
||||||
|
'avatar' => '',
|
||||||
|
'collectionId' => '_pb_users_auth_',
|
||||||
|
'collectionName' => 'users',
|
||||||
|
'created' => '2025-01-21 21:22:47.002Z',
|
||||||
|
'emailVisibility' => false,
|
||||||
|
'id' => '6588yk36406qqv1',
|
||||||
|
'name' => 'Jonathan Martz',
|
||||||
|
'updated' => '2025-01-21 21:22:47.002Z',
|
||||||
|
'verified' => true
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $actual);
|
||||||
|
$this->assertCount(9, $actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_getOneWrongId(): void
|
||||||
|
{
|
||||||
|
$id = '6588yk36406qqva';
|
||||||
|
$actual = $this->collection->getOne($id);
|
||||||
|
$expected = [
|
||||||
|
'data' => [],
|
||||||
|
'message' => "The requested resource wasn't found.",
|
||||||
|
'status' => 404,
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $actual);
|
||||||
|
$this->assertCount(3,$actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
<?php declare(strict_types=1);
|
|
||||||
use PHPUnit\Framework\TestCase;
|
|
||||||
|
|
||||||
final class CollectionTest extends TestCase
|
|
||||||
{
|
|
||||||
private $url;
|
|
||||||
|
|
||||||
protected function setUp(): void
|
|
||||||
{
|
|
||||||
$this->url = getenv('POCKETBASE_URL') ?: 'https://admin.pocketbase.dev';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test_givenUrlAndCollectionNameUsers_thenCheckEmptyResult(): void
|
|
||||||
{
|
|
||||||
$collection = new \Pb\Collection($this->url, 'users');
|
|
||||||
$actual = $collection->getList(1, 10);
|
|
||||||
|
|
||||||
$this->assertTrue(array_key_exists('items', $actual), 'Key "items" does not exist in the response.');
|
|
||||||
$this->assertTrue(array_key_exists('page', $actual), 'Key "page" does not exist in the response.');
|
|
||||||
$this->assertTrue(array_key_exists('perPage', $actual), 'Key "perPage" does not exist in the response.');
|
|
||||||
$this->assertTrue(array_key_exists('totalItems', $actual), 'Key "totalItems" does not exist in the response.');
|
|
||||||
$this->assertTrue(array_key_exists('totalPages', $actual), 'Key "totalPages" does not exist in the response.');
|
|
||||||
$this->assertCount(0, $actual['items'], 'Expected no items in the response.');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test_givenFilterByName_thenCheckEmptyResult(): void
|
|
||||||
{
|
|
||||||
$collection = new \Pb\Collection($this->url, 'users', [
|
|
||||||
'filter' => 'name ~ "%Jonathan%"'
|
|
||||||
]);
|
|
||||||
$actual = $collection->getList(1, 10);
|
|
||||||
|
|
||||||
$this->assertTrue(array_key_exists('items', $actual), 'Key "items" does not exist in the response.');
|
|
||||||
$this->assertTrue(array_key_exists('page', $actual), 'Key "page" does not exist in the response.');
|
|
||||||
$this->assertTrue(array_key_exists('perPage', $actual), 'Key "perPage" does not exist in the response.');
|
|
||||||
$this->assertTrue(array_key_exists('totalItems', $actual), 'Key "totalItems" does not exist in the response.');
|
|
||||||
$this->assertTrue(array_key_exists('totalPages', $actual), 'Key "totalPages" does not exist in the response.');
|
|
||||||
$this->assertCount(1, $actual['items'], 'Expected no items in the response.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user