first version, no prod ready

This commit is contained in:
Jonathan Martz
2025-06-24 21:43:34 +02:00
parent 81e8486870
commit a21e474749
22 changed files with 566 additions and 693 deletions

View File

@@ -1,55 +0,0 @@
<?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);
}
}

View File

@@ -1,28 +0,0 @@
<?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.');
}
}

View File

@@ -1,76 +0,0 @@
<?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.');
}
}

View File

@@ -1,50 +0,0 @@
<?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);
}
}

174
tests/CollectionTest.php Normal file
View File

@@ -0,0 +1,174 @@
<?php
use PHPUnit\Framework\TestCase;
use PocketBase\ViewModel\CustomRecordViewModel;
use PocketBase\ViewModel\RecordListViewModel;
use PocketBase\ViewModel\RecordViewModel as RecordViewModelAlias;
use PocketBase\Client as Client;
use PocketBase\Collection as Collection;
use PocketBase\HttpClient as HttpClient;
final class CollectionTest extends TestCase
{
private Collection $collection;
private ?Client $client = null;
public ?HttpClient $http = null;
public string $name = '';
public function test_getFullList_empty()
{
$this->collection = new Collection(null, 'users');
$this->collection
->setClient(new Client('http://localhost:7090'));
$this->collection->setPath("/api/collections/" . $this->collection->getName() . "/records");
$data = $this->collection
->getFullList(10, []);
$this->assertEquals(RecordListViewModel::class, get_class($data));
$this->assertCount(0, $data->getItems());
}
public function test_getFullList_ownUser()
{
$this->collection = new Collection(null, 'users');
$token = $this->collection->authAsUser('admin@jonathan-martz.de', 'Password123');
$this->client = new Client('http://localhost:7090');
$this->client->setToken($token);
$this->collection->setPath('/api/collections/' . $this->collection->getName() . '/records');
$this->collection->setClient($this->client);
$data = $this->collection->getFullList(10, []);
$this->assertEquals(RecordListViewModel::class, get_class($data));
$this->assertCount(1, $data->getItems());
}
public function test_getFullList_ownAdmin()
{
$this->collection = new Collection(null, 'users');
$token = $this->collection->authAsAdmin('admin@jonathan-martz.de', 'Password123');
$this->client = new Client('http://localhost:7090');
$this->client->setToken($token);
$this->collection->setPath('/api/collections/' . $this->collection->getName() . '/records');
$this->collection->setClient($this->client);
$data = $this->collection->getFullList(10, []);
$this->assertEquals(RecordListViewModel::class, get_class($data));
$this->assertCount(2, $data->getItems());
}
public function test_create_record(): void
{
$this->client = new \PocketBase\Client(null);
$this->collection = $this->client->collection('users');
$this->collection->setPath('/api/collections/users/records');
$record = $this->collection->create([
'email' => 'admin@jonathan-martz.de',
'password' => 'Password123',
'passwordConfirm' => 'Password123',
]);
$this->assertEquals(CustomRecordViewModel::class, get_class($record));
}
public function test_update_record(): void
{
$this->client = new \PocketBase\Client(null);
$this->collection = $this->client->collection('users');
$token = $this->collection->authAsUser('admin@jonathan-martz.de', 'Password123');
$this->client->setToken($token);
$this->collection = $this->client->collection('test');
$this->collection->setClient($this->client);
$this->collection->setPath('/api/collections/test/records/');
try {
$record = $this->collection->update('ss05u9mnegvplds', [
'name' => 'Test123456'
], $token);
} catch (Exception $exception) {
$this->fail('Exception: ' . $exception->getMessage());
}
$this->assertEquals(RecordViewModelAlias::class, get_class($record));
}
public function test_getone_record(): void
{
$this->client = new \PocketBase\Client(null);
$this->collection = $this->client->collection('users');
$token = $this->collection->authAsUser('admin@jonathan-martz.de', 'Password123');
$this->client->setToken($token);
$this->collection = $this->client->collection('test');
$this->collection->setClient($this->client);
$this->collection->setPath('/api/collections/test/records/');
try {
$record = $this->collection->getOne('ss05u9mnegvplds', $token);
} catch (Exception $exception) {
$this->fail('Exception: ' . $exception->getMessage());
}
$this->assertEquals(CustomRecordViewModel::class, get_class($record));
}
public function test_getlist_record(): void
{
$this->client = new \PocketBase\Client(null);
$this->collection = $this->client->collection('users');
$token = $this->collection->authAsUser('admin@jonathan-martz.de', 'Password123');
$this->client->setToken($token);
$this->collection = $this->client->collection('test');
$this->collection->setClient($this->client);
$this->collection->setPath('/api/collections/users/records/');
try {
$record = $this->collection->getList(1, 10);
} catch (Exception $exception) {
$this->fail('Exception: ' . $exception->getMessage());
}
$this->assertEquals(RecordListViewModel::class, get_class($record));
}
public function test_delete_record(): void
{
$this->client = new \PocketBase\Client(null);
$this->collection = $this->client->collection('users');
$token = $this->collection->authAsUser('admin@jonathan-martz.de', 'Password123');
$this->client->setToken($token);
$this->collection = $this->client->collection('test');
$this->collection->setClient($this->client);
$this->collection->setPath('/api/collections/test/records/');
try {
$this->collection->delete('65uahqp4wicc88l');
} catch (Exception $exception) {
$this->fail('Exception: ' . $exception->getMessage());
}
$this->assertTrue(true);
}
public function test_upload_record(): void
{
$this->markTestSkipped('todo');
$this->client = new \PocketBase\Client(null);
$this->collection = $this->client->collection('users');
$token = $this->collection->authAsUser('admin@jonathan-martz.de', 'Password123');
$this->client->setToken($token);
$this->collection = $this->client->collection('test');
$this->collection->setPath('/api/collections/users/records/');
$this->collection->setClient($this->client);
try {
$record = $this->collection->getList(1, 10);
} catch (Exception $exception) {
$this->fail('Exception: ' . $exception->getMessage());
}
$this->assertEquals(RecordListViewModel::class, get_class($record));
}
}