4 Commits

Author SHA1 Message Date
Jonathan Martz
b0a0cf6f1f continue on collection create logic 2025-01-27 20:09:15 +01:00
Jonathan Martz
d90cda9613 remove php docs from collection, add methods for token url collection 2025-01-26 22:50:10 +01:00
Jonathan Martz
0d7255d77e start with create collection item test 2025-01-26 22:46:57 +01:00
Jonathan Martz
40db76d3ed refactoring code, add doRequest via httpClient 2025-01-26 21:54:56 +01:00
10 changed files with 862 additions and 364 deletions

View File

@@ -12,11 +12,16 @@
"src/Client.php", "src/Client.php",
"src/Settings.php", "src/Settings.php",
"src/Collection.php" "src/Collection.php"
] ],
"psr-4": {
"Pb\\": "src/",
"Pb\\Exception\\": "src/exception"
}
}, },
"require": { "require": {
"php": "^8.1|^8.2", "php": "^8.1|^8.2",
"ext-curl": "*" "ext-curl": "*",
"guzzlehttp/guzzle": "^7.9"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^11.5", "phpunit/phpunit": "^11.5",

882
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

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

View File

@@ -2,6 +2,8 @@
namespace Pb; namespace Pb;
use GuzzleHttp\Exception\GuzzleException;
/** /**
* *
*/ */
@@ -27,7 +29,10 @@ 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;
@@ -36,12 +41,6 @@ class Collection
} }
} }
/**
* @param int $start
* @param int $end
* @param array $queryParams
* @return array
*/
public function getList(int $start = 1, int $end = 50, array $queryParams = []): array public function getList(int $start = 1, int $end = 50, array $queryParams = []): array
{ {
$queryParams['perPage'] = $end; $queryParams['perPage'] = $end;
@@ -51,12 +50,6 @@ class Collection
return json_decode($response, JSON_FORCE_OBJECT); return json_decode($response, JSON_FORCE_OBJECT);
} }
/**
* @param string $recordId
* @param string $field
* @param string $filepath
* @return void
*/
public function upload(string $recordId, string $field, string $filepath): void public function upload(string $recordId, string $field, string $filepath): void
{ {
$ch = curl_init($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId); $ch = curl_init($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId);
@@ -76,25 +69,16 @@ class Collection
$response = curl_exec($ch); $response = curl_exec($ch);
} }
/** public function authAsUser(string $email, string $password): array
* @param string $email
* @param string $password
* @return void
*/
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]);
$result = json_decode($result, JSON_FORCE_OBJECT);
if (!empty($result['token'])) { if (!empty($result['token'])) {
self::$token = $result['token']; self::$token = $result['token'];
} }
return $result; return $result;
} }
/**
* @param int $batch
* @param array $queryParams
* @return array
*/
public function getFullList(array $queryParams, int $batch = 200): array public function getFullList(array $queryParams, int $batch = 200): array
{ {
$queryParams = [... $queryParams, 'perPage' => $batch]; $queryParams = [... $queryParams, 'perPage' => $batch];
@@ -104,104 +88,72 @@ class Collection
return json_decode($response, JSON_FORCE_OBJECT); return json_decode($response, JSON_FORCE_OBJECT);
} }
/**
* @param string $filter
* @param array $queryParams
* @return array
*/
public function getFirstListItem(string $filter, array $queryParams = []): array public function getFirstListItem(string $filter, array $queryParams = []): array
{ {
// TODO filter
$queryParams['perPage'] = 1; $queryParams['perPage'] = 1;
$queryParams['filter'] = $filter;
$getParams = !empty($queryParams) ? http_build_query($queryParams) : ""; $getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
$response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET'); $response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET');
return json_decode($response, JSON_FORCE_OBJECT)['items'][0];
$data = json_decode($response, JSON_FORCE_OBJECT);
if (empty($data['items']) || count($data['items']) < 1) {
throw new exception\FirstListItemNotFoundException('First doesnt exists');
}
return $data['items'][0] ?? [];
} }
/**
* @param array $bodyParams
* @param array $queryParams
* @return void
*/
public function create(array $bodyParams = [], array $queryParams = []): string public function create(array $bodyParams = [], array $queryParams = []): string
{ {
// TODO query params ?
return $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records", 'POST', $bodyParams); return $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records", 'POST', $bodyParams);
} }
/**
* @param string $recordId
* @param array $bodyParams
* @param array $queryParams
* @return void
*/
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', $bodyParams); $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'PATCH', $bodyParams);
} }
/**
* @param string $recordId
* @param array $queryParams
* @return void
*/
public function delete(string $recordId, array $queryParams = []): void public function delete(string $recordId, array $queryParams = []): void
{ {
// TODO params ?
$this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'DELETE'); $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'DELETE');
} }
/** /**
* @param string $recordId * @throws GuzzleException
* @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
{ {
// TODO move doRequestIntoService ? $tmp = $bodyParams;
// TODO replace curl with HttpClient $bodyParams = [];
$ch = curl_init(); $bodyParams['json'] = $tmp;
$bodyParams['headers']['Content-Type'] = 'application/json';
if (self::$token != '') { if (self::$token != '') {
$headers = array( $bodyParams['headers']['Authorization'] = 'Bearer ' . self::$token;
'Authorization: ' . self::$token
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
} }
if ($bodyParams) { $client = new \GuzzleHttp\Client();
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyParams); $response = $client->request($method, $url, $bodyParams);
} return $response->getBody()->getContents() ?? '';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
$output = curl_exec($ch);
curl_close($ch);
return $output;
} }
/**
* @param string $recordId
* @param array $queryParams
* @return mixed
*/
public function getOne(string $recordId, array $queryParams = []): array public function getOne(string $recordId, array $queryParams = []): array
{ {
// TODO params ?
$output = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'GET'); $output = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'GET');
return json_decode($output, JSON_FORCE_OBJECT); return json_decode($output, JSON_FORCE_OBJECT);
} }
/**
* @param string $email
* @param string $password
* @return void
*/
public function authAsAdmin(string $email, string $password): string 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/collections/_superusers/auth-with-password", 'POST', $bodyParams); $output = $this->doRequest($this->url . "/api/collections/_superusers/auth-with-password", 'POST', $bodyParams);
$token = json_decode($output, true)['token']; $token = json_decode($output, true)['token'];
if ($token) { if ($token) {
self::$token = $token; self::$token = $token;
@@ -209,4 +161,36 @@ class Collection
return $output; return $output;
} }
public static function getAuthToken(): string
{
return self::$token;
}
public static function setAuthToken(string $token): void
{
self::$token = $token;
}
public function getUrl(): string
{
return $this->url;
}
public function setUrl(string $url): Collection
{
$this->url = $url;
return $this;
}
public function getCollection(): string
{
return $this->collection;
}
public function setCollection(string $collection): Collection
{
$this->collection = $collection;
return $this;
}
} }

View File

@@ -41,6 +41,7 @@ class Settings
if (self::$token != '') { if (self::$token != '') {
$headers = array( $headers = array(
'Content-Type:application/json',
'Authorization: ' . self::$token 'Authorization: ' . self::$token
); );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

View File

@@ -0,0 +1,14 @@
<?php
namespace Pb\Exception;
class FirstListItemNotFoundException extends \Exception
{
/**
* @param string $string
*/
public function __construct(string $string)
{
}
}

38
tests/AuthTest.php Normal file
View File

@@ -0,0 +1,38 @@
<?php
use GuzzleHttp\Exception\ClientException;
use Pb\Collection;
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
{
$this->expectException(ClientException::class);
$this->collection->authAsUser('support@jonathan-martz.de', 'rockt');
}
public function testAuthSuperUser(): void
{
$this->collection = new Collection($this->url, '_superusers');
$this->expectException(ClientException::class);
$this->collection->authAsAdmin('admin@jonathan-martz.de', 'rockt');
}
public function testAuthSuperUser2(): void
{
$this->collection = new Collection($this->url, '_superusers');
$this->expectException(ClientException::class);
$this->collection->authAsAdmin('admin@jmartz.de', 'rockt123?!');
}
}

View File

@@ -0,0 +1,35 @@
<?php
use GuzzleHttp\Exception\ClientException;
use Pb\Collection;
use PHPUnit\Framework\TestCase;
class CollectionCreateTest 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 testCreateCollectionItem(){
$this->expectException(ClientException::class);
$this->expectExceptionCode(403);
$this->collection->create(['name' => 'Hallo Welt']);
}
public function testCreateCollectionItemAuthed(){
$this->collection->authAsUser('admin@jmartz.de', 'rockt123?!');#
self::assertNotEmpty($this->collection->getAuthToken());
}
public function testCreateCollectionItemAuthedAdmin(){
$this->collection->authAsAdmin('admin@jonathan-martz.de', 'rockt123?!');
$response = $this->collection->create(['email' => 'test@jonathan-martz.de','password' => 'rockt123?!','passwordConfirm' => 'rockt123?!']);
var_dump($response);
self::assertNotEmpty($this->collection->getAuthToken());
}
}

View File

@@ -1,6 +1,7 @@
<?php <?php
use Pb\Collection; use Pb\Collection;
use Pb\Exception\FirstListItemNotFoundException;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class CollectionGetFirstListItemTest extends TestCase final class CollectionGetFirstListItemTest extends TestCase
@@ -13,43 +14,22 @@ final class CollectionGetFirstListItemTest extends TestCase
$this->collection = new Collection($url, 'users'); $this->collection = new Collection($url, 'users');
} }
/**
* @throws FirstListItemNotFoundException
*/
public function test_getOne(): void public function test_getOne(): void
{ {
$id = '6588yk36406qqv1'; $id = '6588yk36406qqv1';
$actual = $this->collection->getFirstListItem('id="'.$id.'"'); $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->assertEquals($id, $actual['id']);
$this->assertCount(9, $actual); $this->assertCount(9, $actual);
} }
public function test_getOneWrongId(): void public function test_getOneWrongId(): void
{ {
$id = '6588yk36406qqva'; $id = '6588yk36406qqvb';
$actual = $this->collection->getFirstListItem('id="'.$id.'"'); $this->expectException(FirstListItemNotFoundException::class);
$expected = [ $this->collection->getFirstListItem('id="'.$id.'"');
'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,5 +1,6 @@
<?php <?php
use GuzzleHttp\Exception\ClientException;
use Pb\Collection; use Pb\Collection;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@@ -18,33 +19,15 @@ final class CollectionGetOneTest extends TestCase
{ {
$id = '6588yk36406qqv1'; $id = '6588yk36406qqv1';
$actual = $this->collection->getOne($id); $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->assertEquals($id, $actual['id']);
$this->assertCount(9, $actual); $this->assertCount(9, $actual);
} }
public function test_getOneWrongId(): void public function test_getOneWrongId(): void
{ {
$id = '6588yk36406qqva'; $id = '6588yk36406qqva';
$actual = $this->collection->getOne($id); $this->expectException(ClientException::class);
$expected = [ $this->collection->getOne($id);
'data' => [],
'message' => "The requested resource wasn't found.",
'status' => 404,
];
$this->assertEquals($expected, $actual);
$this->assertCount(3,$actual);
} }
} }