mirror of
https://github.com/jonathan-martz/pocketbase-php-sdk.git
synced 2026-04-03 07:27:42 +00:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
414bf4fe45 | ||
|
|
a21e474749 | ||
|
|
81e8486870 | ||
|
|
f9911ffc29 | ||
|
|
28df3f28e7 | ||
|
|
9b1ded7968 | ||
|
|
7c2253c896 | ||
|
|
d92bff0357 | ||
|
|
75354764d8 | ||
|
|
59a4edd7a5 | ||
|
|
f3f4e84688 | ||
|
|
e2f2ee4be0 | ||
|
|
d733fda627 | ||
|
|
cbd0438d22 | ||
|
|
9625de8b52 |
2
.github/workflows/tdd.yml
vendored
2
.github/workflows/tdd.yml
vendored
@@ -6,7 +6,7 @@ on:
|
||||
pull_request:
|
||||
branches: [master]
|
||||
schedule:
|
||||
- cron: '0 * * * *'
|
||||
- cron: '0 17 * * *'
|
||||
|
||||
jobs:
|
||||
run-mailer:
|
||||
|
||||
10
README.md
10
README.md
@@ -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());
|
||||
```
|
||||
|
||||
***
|
||||
|
||||
@@ -8,18 +8,17 @@
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"src/Client.php",
|
||||
"src/Settings.php",
|
||||
"src/Collection.php"
|
||||
]
|
||||
"psr-4": {
|
||||
"PocketBase\\": "src/",
|
||||
"PocketBase\\ViewModel\\": "src/viewModel"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"php": "^8.1|^8.2",
|
||||
"php": "^8.3|^8.4",
|
||||
"ext-curl": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^11.5",
|
||||
"tomasvotruba/phpunit-json-result-printer": "^0.2.1"
|
||||
"tomasvotruba/phpunit-json-result-printer": "^0.2.1",
|
||||
"phpunit/phpunit": "^11.5"
|
||||
}
|
||||
}
|
||||
|
||||
18
composer.lock
generated
18
composer.lock
generated
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "e3ef1b1407a46f944e79ec18eb6f6a9b",
|
||||
"content-hash": "a3032883f5e5af1996fb350351fc84ac",
|
||||
"packages": [],
|
||||
"packages-dev": [
|
||||
{
|
||||
@@ -568,16 +568,16 @@
|
||||
},
|
||||
{
|
||||
"name": "phpunit/phpunit",
|
||||
"version": "11.5.2",
|
||||
"version": "11.5.8",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/sebastianbergmann/phpunit.git",
|
||||
"reference": "153d0531b9f7e883c5053160cad6dd5ac28140b3"
|
||||
"reference": "c9bd61aab12f0fc5e82ecfe621ff518a1d1f1049"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/153d0531b9f7e883c5053160cad6dd5ac28140b3",
|
||||
"reference": "153d0531b9f7e883c5053160cad6dd5ac28140b3",
|
||||
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c9bd61aab12f0fc5e82ecfe621ff518a1d1f1049",
|
||||
"reference": "c9bd61aab12f0fc5e82ecfe621ff518a1d1f1049",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -598,7 +598,7 @@
|
||||
"phpunit/php-timer": "^7.0.1",
|
||||
"sebastian/cli-parser": "^3.0.2",
|
||||
"sebastian/code-unit": "^3.0.2",
|
||||
"sebastian/comparator": "^6.2.1",
|
||||
"sebastian/comparator": "^6.3.0",
|
||||
"sebastian/diff": "^6.0.2",
|
||||
"sebastian/environment": "^7.2.0",
|
||||
"sebastian/exporter": "^6.3.0",
|
||||
@@ -649,7 +649,7 @@
|
||||
"support": {
|
||||
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
|
||||
"security": "https://github.com/sebastianbergmann/phpunit/security/policy",
|
||||
"source": "https://github.com/sebastianbergmann/phpunit/tree/11.5.2"
|
||||
"source": "https://github.com/sebastianbergmann/phpunit/tree/11.5.8"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
@@ -665,7 +665,7 @@
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2024-12-21T05:51:08+00:00"
|
||||
"time": "2025-02-18T06:26:59+00:00"
|
||||
},
|
||||
{
|
||||
"name": "sebastian/cli-parser",
|
||||
@@ -1742,7 +1742,7 @@
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": "^8.1|^8.2",
|
||||
"php": "^8.3|^8.4",
|
||||
"ext-curl": "*"
|
||||
},
|
||||
"platform-dev": [],
|
||||
|
||||
12
docker-compose.yml
Normal file
12
docker-compose.yml
Normal file
@@ -0,0 +1,12 @@
|
||||
services:
|
||||
pocketbase_php_sdk:
|
||||
image: ghcr.io/muchobien/pocketbase:latest
|
||||
container_name: pocketbase_php_sdk
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "7090:8090"
|
||||
healthcheck: # optional, recommended since v0.10.0
|
||||
test: wget --no-verbose --tries=1 --spider http://localhost:8090/api/health || exit 1
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
96
mailer.go
96
mailer.go
@@ -1,96 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/smtp"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
now := time.Now()
|
||||
|
||||
// Define flags
|
||||
smtpPass := flag.String("password", "", "SMTP password")
|
||||
sender := flag.String("sender", "", "SMTP sender")
|
||||
smtpUser := flag.String("user", "", "SMTP username")
|
||||
|
||||
// Parse the flags
|
||||
flag.Parse()
|
||||
|
||||
// Check if required flags are provided
|
||||
if *smtpPass == "" {
|
||||
log.Fatal("Error: Password is required. Use -password flag.")
|
||||
}
|
||||
if *smtpUser == "" {
|
||||
log.Fatal("Error: SMTP username is required. Use -user flag.")
|
||||
}
|
||||
if *sender == "" {
|
||||
log.Fatal("Error: Sender email is required. Use -sender flag.")
|
||||
}
|
||||
|
||||
// Format the date as YYYY-MM-DD
|
||||
currentDate := now.Format("01-02-2006")
|
||||
|
||||
recipient := "support@jonathan-martz.de"
|
||||
subject := "PHPUnit - PocketBase PHP SDK - " + currentDate
|
||||
smtpHost := "smtps.udag.de" // Replace with your SMTP host
|
||||
smtpPort := "587" // Replace with your SMTP port
|
||||
|
||||
// Run PHPUnit with combined stdout and stderr
|
||||
cmd := exec.Command("php", "vendor/bin/phpunit")
|
||||
var output bytes.Buffer
|
||||
cmd.Stdout = &output
|
||||
cmd.Stderr = &output
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
log.Printf("PHPUnit command failed: %v", err)
|
||||
}
|
||||
|
||||
// Check if output is empty
|
||||
if output.Len() == 0 {
|
||||
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 {
|
||||
log.Fatalf("Failed to parse JSON output: %v", err)
|
||||
}
|
||||
|
||||
// Extract the 'counts' field
|
||||
counts, ok := result["counts"].(map[string]interface{})
|
||||
if !ok {
|
||||
log.Fatal("Counts field not found in PHPUnit output.")
|
||||
}
|
||||
|
||||
// Serialize the counts field to JSON for the email body
|
||||
countsJSON, err := json.MarshalIndent(counts, "", " ")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to serialize counts to JSON: %v", err)
|
||||
}
|
||||
|
||||
// Prepare the email body
|
||||
emailBody := fmt.Sprintf("Subject: %s\r\n\r\n%s", subject, string(countsJSON))
|
||||
|
||||
// Connect to the SMTP server
|
||||
auth := smtp.PlainAuth("", *smtpUser, *smtpPass, smtpHost)
|
||||
err = smtp.SendMail(
|
||||
smtpHost+":"+smtpPort,
|
||||
auth,
|
||||
*sender,
|
||||
[]string{recipient},
|
||||
[]byte(emailBody),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to send email: %v", err)
|
||||
}
|
||||
|
||||
log.Println("Test results sent to", recipient)
|
||||
}
|
||||
@@ -5,7 +5,6 @@
|
||||
cacheDirectory=".phpunit.cache"
|
||||
executionOrder="depends,defects"
|
||||
shortenArraysForExportThreshold="10"
|
||||
requireCoverageMetadata="true"
|
||||
beStrictAboutCoverageMetadata="true"
|
||||
beStrictAboutOutputDuringTests="true"
|
||||
displayDetailsOnPhpunitDeprecations="true"
|
||||
@@ -17,9 +16,10 @@
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<extensions>
|
||||
|
||||
<!-- <extensions>
|
||||
<bootstrap class="TomasVotruba\PHPUnitJsonResultPrinter\PHPUnitJsonResultPrinterExtension" />
|
||||
</extensions>
|
||||
</extensions> -->
|
||||
|
||||
<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
|
||||
<include>
|
||||
|
||||
@@ -1,23 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Pb;
|
||||
namespace PocketBase;
|
||||
|
||||
class Client
|
||||
{
|
||||
private string $url;
|
||||
|
||||
public function __construct(string $url)
|
||||
public ?string $baseurl = null;
|
||||
public ?string $token = null;
|
||||
public HttpClient $http;
|
||||
|
||||
public function __construct($baseurl = null)
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->baseurl = $baseurl ?? 'http://localhost:7090';
|
||||
$this->http = new HttpClient();
|
||||
$this->token = '';
|
||||
}
|
||||
|
||||
public function collection(string $collection): Collection
|
||||
public function collection(string $name): Collection
|
||||
{
|
||||
return new Collection($this->url ,$collection);
|
||||
return new Collection($this->baseurl, $name);
|
||||
}
|
||||
|
||||
public function settings(): Settings
|
||||
public function setBaseUrl($baseurl){
|
||||
$this->baseurl = $baseurl;
|
||||
}
|
||||
|
||||
public function getBaseUrl(){
|
||||
return $this->baseurl;
|
||||
}
|
||||
|
||||
public function getToken(): ?string
|
||||
{
|
||||
return new Settings($this->url);
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
public function setToken(?string $token): void
|
||||
{
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
public function getHttp(): HttpClient
|
||||
{
|
||||
return $this->http;
|
||||
}
|
||||
|
||||
public function setHttp(HttpClient $http): Client
|
||||
{
|
||||
$this->http = $http;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -1,201 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace Pb;
|
||||
namespace PocketBase;
|
||||
|
||||
use PocketBase\ViewModel\CustomRecordViewModel;
|
||||
use PocketBase\ViewModel\RecordListViewModel;
|
||||
use PocketBase\ViewModel\RecordViewModel;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Collection
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $collection;
|
||||
public HttpClient $http;
|
||||
private Client $client;
|
||||
private string $name;
|
||||
public string $path;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $url;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private static string $token = '';
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param string $collection
|
||||
*/
|
||||
public function __construct(string $url, string $collection)
|
||||
public function getPath(): string
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->collection = $collection;
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 setPath(string $path): Collection
|
||||
{
|
||||
$queryParams['perPage'] = $end;
|
||||
$this->path = $path;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __construct(string $url = null, string $name = 'users')
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->http = new HttpClient();
|
||||
$this->client = new Client();
|
||||
}
|
||||
|
||||
public function getFullList(int $batch, $bodyParams = [], array $queryParams = []): RecordListViewModel
|
||||
{
|
||||
$queryParams['limit'] = $batch;
|
||||
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
|
||||
$response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET');
|
||||
|
||||
return json_decode($response, JSON_FORCE_OBJECT);
|
||||
return new RecordListViewModel(
|
||||
json_decode(
|
||||
$this->http->doRequest(
|
||||
$this->client->getBaseUrl() . $this->path . '?' . $getParams,
|
||||
'GET',
|
||||
$bodyParams,
|
||||
$this->client->token
|
||||
),
|
||||
true)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $recordId
|
||||
* @param string $field
|
||||
* @param string $filepath
|
||||
* @return void
|
||||
*/
|
||||
public function upload(string $recordId, string $field, string $filepath): void
|
||||
public function getList(int $page, int $limit, $bodyParams = [], array $queryParams = []): RecordListViewModel
|
||||
{
|
||||
$ch = curl_init($this->url . "/api/collections/".$this->collection."/records/" . $recordId);
|
||||
curl_setopt_array($ch, array(
|
||||
CURLOPT_CUSTOMREQUEST => 'PATCH',
|
||||
CURLOPT_POSTFIELDS => array(
|
||||
$field => new \CURLFile($filepath)
|
||||
)
|
||||
));
|
||||
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
|
||||
$headers = array('Content-Type: multipart/form-data');
|
||||
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @param string $password
|
||||
* @return void
|
||||
*/
|
||||
public function authAsUser(string $email, string $password)
|
||||
{
|
||||
$result = $this->doRequest($this->url . "/api/collections/users/auth-with-password", 'POST', ['identity' => $email, 'password' => $password]);
|
||||
if (!empty($result['token'])) {
|
||||
self::$token = $result['token'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $batch
|
||||
* @param array $queryParams
|
||||
* @return array
|
||||
*/
|
||||
public function getFullList(array $queryParams, int $batch = 200): array
|
||||
{
|
||||
$queryParams = [... $queryParams, 'perPage'=> $batch];
|
||||
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
|
||||
$response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET');
|
||||
|
||||
return json_decode($response, JSON_FORCE_OBJECT);
|
||||
return new RecordListViewModel(json_decode(
|
||||
$this->http->doRequest(
|
||||
$this->client->getBaseUrl() . $this->path . '?' . $getParams,
|
||||
'GET',
|
||||
$bodyParams,
|
||||
$this->client->token
|
||||
), true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filter
|
||||
* @param array $queryParams
|
||||
* @return array
|
||||
*/
|
||||
public function getFirstListItem(string $filter, array $queryParams = []): array
|
||||
public function authAsUser(string $email, string $password): ?string
|
||||
{
|
||||
$queryParams['perPage'] = 1;
|
||||
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
|
||||
$response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET');
|
||||
return json_decode($response, JSON_FORCE_OBJECT)['items'][0];
|
||||
$data = json_decode(
|
||||
$this->http->doRequest(
|
||||
$this->client->getBaseUrl() . '/api/collections/users/auth-with-password' . '?' . $getParams,
|
||||
'POST',
|
||||
[
|
||||
'identity' => $email,
|
||||
'password' => $password,
|
||||
]
|
||||
), true);
|
||||
return $data['token'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $bodyParams
|
||||
* @param array $queryParams
|
||||
* @return void
|
||||
*/
|
||||
public function create(array $bodyParams = [], array $queryParams = []): string
|
||||
public function authAsAdmin(string $email, string $password): ?string
|
||||
{
|
||||
return $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records", 'POST', json_encode($bodyParams));
|
||||
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
|
||||
$data = json_decode($this->http->doRequest(
|
||||
$this->client->baseurl . "/api/collections/_superusers/auth-with-password" . '?' . $getParams,
|
||||
'POST',
|
||||
[
|
||||
'identity' => $email,
|
||||
'password' => $password,
|
||||
]), true);
|
||||
return $data['token'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $recordId
|
||||
* @param array $bodyParams
|
||||
* @param array $queryParams
|
||||
* @return void
|
||||
*/
|
||||
public function update(string $recordId, array $bodyParams = [], array $queryParams = []): void
|
||||
public function create(array $bodyParams, array $queryParams = []): CustomRecordViewModel
|
||||
{
|
||||
// Todo bodyParams equals json, currently workaround
|
||||
$this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'PATCH', json_encode($bodyParams));
|
||||
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
|
||||
|
||||
return new CustomRecordViewModel(json_decode(
|
||||
$this->http->doRequest(
|
||||
$this->client->getBaseUrl() . $this->getPath() . '?' . $getParams,
|
||||
'POST',
|
||||
$bodyParams,
|
||||
$this->client->token
|
||||
), true) ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $recordId
|
||||
* @param array $queryParams
|
||||
* @return void
|
||||
*/
|
||||
public function delete(string $recordId, array $queryParams = []): void
|
||||
public function update($id, $bodyParams, $token = '', array $queryParams = []): RecordViewModel
|
||||
{
|
||||
$this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'DELETE');
|
||||
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
|
||||
return new RecordViewModel(json_decode($this->http->doRequest(
|
||||
$this->client->getBaseUrl() . '/api/collections/test/records/' . $id . '?' . $getParams
|
||||
, 'PATCH', json_encode($bodyParams), $token), true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $recordId
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
* @return bool|string
|
||||
*/
|
||||
public function doRequest(string $url, string $method, $bodyParams = []): string
|
||||
public function getOne($id, $token = '', array $queryParams = []): CustomRecordViewModel
|
||||
{
|
||||
$ch = curl_init();
|
||||
|
||||
if (self::$token != '') {
|
||||
$headers = array(
|
||||
'Content-Type:application/json',
|
||||
'Authorization: ' . self::$token
|
||||
);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
}
|
||||
|
||||
if ($bodyParams) {
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyParams);
|
||||
}
|
||||
|
||||
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;
|
||||
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
|
||||
return new CustomRecordViewModel(json_decode($this->http->doRequest(
|
||||
$this->client->getBaseUrl() . '/api/collections/test/records/' . $id . '?' . $getParams#
|
||||
, 'GET', [], $token), true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $recordId
|
||||
* @param array $queryParams
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOne(string $recordId, array $queryParams = []): array
|
||||
public function delete($id, $token = '', array $queryParams = []): CustomRecordViewModel
|
||||
{
|
||||
$output = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'GET');
|
||||
return json_decode($output, JSON_FORCE_OBJECT);
|
||||
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
|
||||
return new CustomRecordViewModel(json_decode($this->http->doRequest(
|
||||
$this->client->getBaseUrl() . '/api/collections/test/records/' . $id . '?' . $getParams
|
||||
, 'DELETE', [], $token), true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @param string $password
|
||||
* @return void
|
||||
*/
|
||||
public function authAsAdmin(string $email, string $password): void
|
||||
public function setClient(Client $client): void
|
||||
{
|
||||
$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'];
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getAuthToken(): ?string
|
||||
{
|
||||
return $this->client->token;
|
||||
}
|
||||
|
||||
public function setHttp(HttpClient $httpClient): void
|
||||
{
|
||||
$this->http = $httpClient;
|
||||
}
|
||||
}
|
||||
31
src/HttpClient.php
Normal file
31
src/HttpClient.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace PocketBase;
|
||||
|
||||
class HttpClient
|
||||
{
|
||||
public function doRequest(string $url, string $method, $bodyParams = [], ?string $token = null): string
|
||||
{
|
||||
$ch = curl_init();
|
||||
|
||||
if ($token != '') {
|
||||
$headers = array(
|
||||
'Content-Type:application/json',
|
||||
'Authorization: ' . $token
|
||||
);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
}
|
||||
|
||||
if ($bodyParams) {
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyParams);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Pb;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Settings
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $url;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private static string $token = '';
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param string $collection
|
||||
*/
|
||||
public function __construct(string $url)
|
||||
{
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
if ($bodyParams) {
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyParams);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function getAll():array
|
||||
{
|
||||
return json_decode($this->doRequest($this->url . '/api/settings', 'GET', []), true);
|
||||
}
|
||||
|
||||
public function update($bodyParam):array{
|
||||
return json_decode($this->doRequest($this->url . '/api/settings', 'PATCH', json_encode($bodyParam)), true);
|
||||
}
|
||||
}
|
||||
15
src/viewModel/CustomRecordViewModel.php
Normal file
15
src/viewModel/CustomRecordViewModel.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace PocketBase\ViewModel;
|
||||
|
||||
class CustomRecordViewModel extends RecordViewModel
|
||||
{
|
||||
public function __construct(array $data)
|
||||
{
|
||||
foreach ($data as $key => $item){
|
||||
$this->$key = $item;
|
||||
}
|
||||
|
||||
parent::__construct($data);
|
||||
}
|
||||
}
|
||||
88
src/viewModel/RecordListViewModel.php
Normal file
88
src/viewModel/RecordListViewModel.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace PocketBase\ViewModel;
|
||||
|
||||
class RecordListViewModel extends RecordViewModel
|
||||
{
|
||||
private array $items;
|
||||
private int $page;
|
||||
private int $perPage;
|
||||
private int $totalItems;
|
||||
private int $totalPages;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
if (key_exists('items', $data)) {
|
||||
$this->setItems($data['items']);
|
||||
}
|
||||
if (key_exists('page', $data)) {
|
||||
$this->setPage($data['page']);
|
||||
}
|
||||
if (key_exists('perPage', $data)) {
|
||||
$this->setPerPage($data['perPage']);
|
||||
}
|
||||
if (key_exists('totalItems', $data)) {
|
||||
$this->setTotalItems($data['totalItems']);
|
||||
}
|
||||
if (key_exists('totalPages', $data)) {
|
||||
$this->setTotalPages($data['totalPages']);
|
||||
}
|
||||
|
||||
parent::__construct($data);
|
||||
}
|
||||
|
||||
public function setTotalItems(int $totalItems): RecordListViewModel
|
||||
{
|
||||
$this->totalItems = $totalItems;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function getItems(): array
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
public function setItems(array $items): RecordListViewModel
|
||||
{
|
||||
$this->items = $items;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPage(): int
|
||||
{
|
||||
return $this->page;
|
||||
}
|
||||
|
||||
public function setPage(int $page): RecordListViewModel
|
||||
{
|
||||
$this->page = $page;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPerPage(): int
|
||||
{
|
||||
return $this->perPage;
|
||||
}
|
||||
|
||||
public function setPerPage(int $perPage): RecordListViewModel
|
||||
{
|
||||
$this->perPage = $perPage;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTotalItems(): int
|
||||
{
|
||||
return $this->totalItems;
|
||||
}
|
||||
|
||||
public function getTotalPages(): int
|
||||
{
|
||||
return $this->totalPages;
|
||||
}
|
||||
|
||||
public function setTotalPages(int $totalPages): void
|
||||
{
|
||||
$this->totalPages = $totalPages;
|
||||
}
|
||||
}
|
||||
86
src/viewModel/RecordViewModel.php
Normal file
86
src/viewModel/RecordViewModel.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace PocketBase\ViewModel;
|
||||
|
||||
class RecordViewModel
|
||||
{
|
||||
private string $collectionName;
|
||||
private string $collectionId;
|
||||
private string $created;
|
||||
private string $updated;
|
||||
private string $id;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
if(key_exists('id', $data)){
|
||||
$this->setId($data['id']);
|
||||
}
|
||||
if(key_exists('updated', $data)){
|
||||
$this->setUpdated($data['updated']);
|
||||
}
|
||||
if(key_exists('created', $data)){
|
||||
$this->setCreated($data['created']);
|
||||
}
|
||||
if(key_exists('collectionName', $data)){
|
||||
$this->setCollectionName($data['collectionName']);
|
||||
}
|
||||
if(key_exists('collectionId', $data)){
|
||||
$this->setCollectionId($data['collectionId']);
|
||||
}
|
||||
}
|
||||
|
||||
public function getCollectionName(): string
|
||||
{
|
||||
return $this->collectionName;
|
||||
}
|
||||
|
||||
public function setCollectionName(string $collectionName): RecordViewModel
|
||||
{
|
||||
$this->collectionName = $collectionName;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCollectionId(): string
|
||||
{
|
||||
return $this->collectionId;
|
||||
}
|
||||
|
||||
public function setCollectionId(string $collectionId): RecordViewModel
|
||||
{
|
||||
$this->collectionId = $collectionId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreated(): string
|
||||
{
|
||||
return $this->created;
|
||||
}
|
||||
|
||||
public function setCreated(string $created): RecordViewModel
|
||||
{
|
||||
$this->created = $created;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUpdated(): string
|
||||
{
|
||||
return $this->updated;
|
||||
}
|
||||
|
||||
public function setUpdated(string $updated): RecordViewModel
|
||||
{
|
||||
$this->updated = $updated;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setId(string $id): RecordViewModel
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
46
tests/ClientTest.php
Normal file
46
tests/ClientTest.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PocketBase\Client;
|
||||
use PocketBase\Collection;
|
||||
use PocketBase\HttpClient;
|
||||
|
||||
final class ClientTest extends TestCase
|
||||
{
|
||||
|
||||
private Collection $collection;
|
||||
private ?Client $client = null;
|
||||
public ?HttpClient $http = null;
|
||||
|
||||
public function test_collection(): void
|
||||
{
|
||||
$this->client = new Client();
|
||||
$this->collection = $this->client->collection('_superusers');
|
||||
$this->assertEquals(Collection::class, get_class($this->collection));
|
||||
}
|
||||
|
||||
public function test_authAsUser(): void
|
||||
{
|
||||
$this->client = new Client();
|
||||
$this->collection = $this->client->collection('users');
|
||||
$this->collection->setPath('/api/collections/users/auth-with-password');
|
||||
|
||||
$token = $this->collection->authAsUser('admin@jonathan-martz.de', 'Password123');
|
||||
$this->assertNotEmpty($token);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \PHPUnit\Framework\MockObject\Exception
|
||||
*/
|
||||
// @sk
|
||||
public function test_authAsAdmin(): void
|
||||
{
|
||||
$this->client = new Client();
|
||||
$this->collection = $this->client->collection('_superusers');
|
||||
$this->collection->setPath('/api/collections/' . $this->collection->getName() . '/auth-with-password');
|
||||
|
||||
$token = $this->collection->authAsUser('admin@jonathan-martz.de', 'Password123');
|
||||
$this->assertNotEmpty($token);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,174 @@
|
||||
<?php declare(strict_types=1);
|
||||
<?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
|
||||
{
|
||||
public function test(): void
|
||||
|
||||
private Collection $collection;
|
||||
private ?Client $client = null;
|
||||
public ?HttpClient $http = null;
|
||||
|
||||
public string $name = '';
|
||||
|
||||
public function test_getFullList_empty()
|
||||
{
|
||||
$expected = [];
|
||||
$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, []);
|
||||
|
||||
$url = 'https://admin.pocketbase.dev';
|
||||
$collection = new \Pb\Collection($url, 'users');
|
||||
$actual = $collection->getList(1,10);
|
||||
$this->assertEquals(RecordListViewModel::class, get_class($data));
|
||||
$this->assertCount(0, $data->getItems());
|
||||
}
|
||||
|
||||
$this->assertSame($expected, $actual);
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user