13 Commits

Author SHA1 Message Date
jonathan-martz
76038f8bfb #8 - fix header json 2025-09-02 19:49:49 +02:00
Jonathan Martz
f9911ffc29 refactoring collection a little bit, adjust token from collection manual,write some test for the simple logic, fixing superusers authAdmin 2025-01-21 23:52:11 +01:00
Jonathan Martz
28df3f28e7 Merge pull request #2 from antharuu/master
allow to manually define the auth token for client and settings
2025-01-21 22:18:40 +01:00
Anthony Bellancourt
9b1ded7968 allow to manually define the auth token for client and settings 2025-01-21 17:21:53 +01:00
Jonathan Martz
7c2253c896 fix mailer go 2025-01-12 19:22:11 +01:00
Jonathan Martz
d92bff0357 add message id 2025-01-12 19:18:52 +01:00
Jonathan Martz
75354764d8 add failimng test 2025-01-12 19:05:51 +01:00
Jonathan Martz
59a4edd7a5 send mail when failed is bigger then 0 2025-01-12 19:03:44 +01:00
Jonathan Martz
f3f4e84688 workflow one time per day 2025-01-12 18:55:44 +01:00
Jonathan Martz
e2f2ee4be0 update README.md 2025-01-12 13:09:15 +01:00
Jonathan Martz
d733fda627 add tests for simple user and users with filter by name 2025-01-12 13:06:21 +01:00
Jonathan Martz
cbd0438d22 remove output debug 2025-01-12 12:33:13 +01:00
Jonathan Martz
9625de8b52 change date format 2025-01-12 12:32:43 +01:00
17 changed files with 432 additions and 66 deletions

View File

@@ -6,7 +6,7 @@ on:
pull_request:
branches: [master]
schedule:
- cron: '0 * * * *'
- cron: '0 17 * * *'
jobs:
run-mailer:

View File

@@ -4,6 +4,12 @@
##### This Repository are in active Development
***
***
### Gitlab is the one for Packagist, but they should both be up to Date.
##### Github: https://github.com/jonathan-martz/pocketbase-php-sdk/
##### Gitlab: https://gitlab.jonathan-martz.de/softwareentwicklung/pocketbase-php-sdk
***
``` bash
composer require jonathan-martz/pocketbase-php-sdk
```
@@ -11,8 +17,8 @@ composer require jonathan-martz/pocketbase-php-sdk
``` php
// Example init and use
use \Pb\Client as pb;
$pb = new pb('https://backend-shop.mkay.dev');
var_dump($pb->collection('countries')->getList());
$pb = new pb('https://admin.pocketbase.dev');
var_dump($pb->collection('users')->getList());
```
***

View File

@@ -34,7 +34,7 @@ func main() {
}
// Format the date as YYYY-MM-DD
currentDate := now.Format("01-02-2006")
currentDate := now.Format("01.02.2006")
recipient := "support@jonathan-martz.de"
subject := "PHPUnit - PocketBase PHP SDK - " + currentDate
@@ -56,8 +56,6 @@ func main() {
log.Fatal("PHPUnit output is empty. Please check your configuration.")
}
fmt.Println(output.String())
// Parse the JSON output
var result map[string]interface{}
if err := json.Unmarshal(output.Bytes(), &result); err != nil {
@@ -70,6 +68,13 @@ fmt.Println(output.String())
log.Fatal("Counts field not found in PHPUnit output.")
}
// Check if 'fails' is greater than 0
failed, ok := counts["failed"].(float64)
if !ok {
log.Fatal("'fails' field not found in counts.")
}
if failed > 0 {
// Serialize the counts field to JSON for the email body
countsJSON, err := json.MarshalIndent(counts, "", " ")
if err != nil {
@@ -93,4 +98,7 @@ fmt.Println(output.String())
}
log.Println("Test results sent to", recipient)
} else {
log.Println("No failures detected. No email sent.")
}
}

46
src/AuthTest.php Normal file
View 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']);
}
}

View File

@@ -5,6 +5,7 @@ namespace Pb;
class Client
{
private string $url;
public string $token = '';
public function __construct(string $url)
{
@@ -13,11 +14,21 @@ class Client
public function collection(string $collection): Collection
{
return new Collection($this->url ,$collection);
return new Collection($this->url ,$collection, $this->token);
}
public function settings(): Settings
{
return new Settings($this->url);
return new Settings($this->url, $this->token);
}
public function setAuthToken(string $token): void
{
$this->token = $token;
}
public function getAuthToken(): string
{
return $this->token;
}
}

View File

@@ -25,11 +25,15 @@ class Collection
/**
* @param string $url
* @param string $collection
* @param string $token
*/
public function __construct(string $url, string $collection)
public function __construct(string $url, string $collection, string $token = '')
{
$this->url = $url;
$this->collection = $collection;
if (!empty($token)) {
self::$token = $token;
}
}
/**
@@ -77,12 +81,13 @@ class Collection
* @param string $password
* @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]);
if (!empty($result['token'])) {
self::$token = $result['token'];
}
return $result;
}
/**
@@ -119,7 +124,7 @@ class Collection
*/
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);
}
/**
@@ -131,7 +136,7 @@ class Collection
public function update(string $recordId, array $bodyParams = [], array $queryParams = []): void
{
// 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);
}
/**
@@ -152,11 +157,12 @@ class Collection
*/
public function doRequest(string $url, string $method, $bodyParams = []): string
{
// TODO move doRequestIntoService ?
// TODO replace curl with HttpClient
$ch = curl_init();
if (self::$token != '') {
$headers = array(
'Content-Type:application/json',
'Authorization: ' . self::$token
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
@@ -191,11 +197,16 @@ class Collection
* @param string $password
* @return void
*/
public function authAsAdmin(string $email, string $password): void
public function authAsAdmin(string $email, string $password): string
{
$bodyParams['identity'] = $email;
$bodyParams['password'] = $password;
$output = $this->doRequest($this->url . "/api/admins/auth-with-password", 'POST', $bodyParams);
self::$token = json_decode($output, true)['token'];
$output = $this->doRequest($this->url . "/api/collections/_superusers/auth-with-password", 'POST', $bodyParams);
$token = json_decode($output, true)['token'];
if ($token) {
self::$token = $token;
}
return $output;
}
}

View File

@@ -19,34 +19,28 @@ class Settings
/**
* @param string $url
* @param string $collection
* @param string $token
*/
public function __construct(string $url)
public function __construct(string $url, string $token = '')
{
$this->url = $url;
self::$token = $token;
}
public function authAsAdmin(string $email, string $password): void
{
$bodyParams['identity'] = $email;
$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'];
}
/**
* @param string $recordId
* @param string $url
* @param string $method
* @return bool|string
*/
public function doRequest(string $url, string $method, $bodyParams = []): string
{
$ch = curl_init();
if (self::$token != '') {
$headers = array(
'Content-Type:application/json',
'Authorization: ' . self::$token
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
@@ -65,9 +59,6 @@ class Settings
return $output;
}
/**
* @return void
*/
public function getAll():array
{
return json_decode($this->doRequest($this->url . '/api/settings', 'GET', []), true);

8
src/SuperUser.php Normal file
View File

@@ -0,0 +1,8 @@
<?php
namespace Pb;
class SuperUser
{
}

18
src/User.php Normal file
View 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;
}

View 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;
}

View 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
View 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());
}
}

View 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);
}
}

View 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.');
}
}

View 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.');
}
}

View 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);
}
}

View File

@@ -1,16 +0,0 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class CollectionTest extends TestCase
{
public function test(): void
{
$expected = [];
$url = 'https://admin.pocketbase.dev';
$collection = new \Pb\Collection($url, 'users');
$actual = $collection->getList(1,10);
$this->assertSame($expected, $actual);
}
}