mirror of
https://github.com/jonathan-martz/pocketbase-php-sdk.git
synced 2026-04-03 07:27:42 +00:00
start with create collection item test
This commit is contained in:
@@ -22,7 +22,7 @@ class Collection
|
|||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private static string $token = '';
|
public static string $token = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $url
|
* @param string $url
|
||||||
@@ -81,14 +81,10 @@ class Collection
|
|||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $email
|
|
||||||
* @param string $password
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function authAsUser(string $email, string $password): string
|
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]);
|
||||||
|
var_dump($result);
|
||||||
if (!empty($result['token'])) {
|
if (!empty($result['token'])) {
|
||||||
self::$token = $result['token'];
|
self::$token = $result['token'];
|
||||||
}
|
}
|
||||||
@@ -123,7 +119,7 @@ class Collection
|
|||||||
$response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET');
|
$response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET');
|
||||||
|
|
||||||
$data = json_decode($response, JSON_FORCE_OBJECT);
|
$data = json_decode($response, JSON_FORCE_OBJECT);
|
||||||
if(empty($data['items']) || count($data['items']) < 1){
|
if (empty($data['items']) || count($data['items']) < 1) {
|
||||||
throw new exception\FirstListItemNotFoundException('First doesnt exists');
|
throw new exception\FirstListItemNotFoundException('First doesnt exists');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,14 +159,22 @@ class Collection
|
|||||||
$this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'DELETE');
|
$this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'DELETE');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws GuzzleException
|
||||||
|
*/
|
||||||
public function doRequest(string $url, string $method, $bodyParams = []): string
|
public function doRequest(string $url, string $method, $bodyParams = []): string
|
||||||
{
|
{
|
||||||
|
$tmp = $bodyParams;
|
||||||
|
$bodyParams = [];
|
||||||
|
$bodyParams['json'] = $tmp;
|
||||||
|
$bodyParams['headers']['Content-Type'] = 'application/json';
|
||||||
|
|
||||||
if (self::$token != '') {
|
if (self::$token != '') {
|
||||||
// TODO token ?
|
$bodyParams['headers']['Authorization'] = 'Bearer ' . self::$token;
|
||||||
}
|
}
|
||||||
|
|
||||||
$client = new \GuzzleHttp\Client();
|
$client = new \GuzzleHttp\Client();
|
||||||
$response = $client->request($method, $url,$bodyParams);
|
$response = $client->request($method, $url, $bodyParams);
|
||||||
return $response->getBody()->getContents() ?? '';
|
return $response->getBody()->getContents() ?? '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,6 +189,7 @@ class Collection
|
|||||||
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function authAsAdmin(string $email, string $password): string
|
public function authAsAdmin(string $email, string $password): string
|
||||||
{
|
{
|
||||||
$bodyParams['identity'] = $email;
|
$bodyParams['identity'] = $email;
|
||||||
|
|||||||
29
tests/CollectionCreateTest.php
Normal file
29
tests/CollectionCreateTest.php
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?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->collection->create(['name' => 'Hallo Welt']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateCollectionItemAuthed(){
|
||||||
|
$this->collection->authAsUser('admin@jmartz.de', 'rockt123?!');
|
||||||
|
self::assertNotEmpty($this->collection::$token);
|
||||||
|
// $response = $this->collection->create(['name' => 'Hallo Welt']);
|
||||||
|
// var_dump($response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,9 +27,7 @@ final class CollectionGetOneTest extends TestCase
|
|||||||
public function test_getOneWrongId(): void
|
public function test_getOneWrongId(): void
|
||||||
{
|
{
|
||||||
$id = '6588yk36406qqva';
|
$id = '6588yk36406qqva';
|
||||||
|
|
||||||
$this->expectException(ClientException::class);
|
$this->expectException(ClientException::class);
|
||||||
|
|
||||||
$this->collection->getOne($id);
|
$this->collection->getOne($id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user