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
|
||||
*/
|
||||
private static string $token = '';
|
||||
public static string $token = '';
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
@@ -81,14 +81,10 @@ class Collection
|
||||
$response = curl_exec($ch);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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]);
|
||||
var_dump($result);
|
||||
if (!empty($result['token'])) {
|
||||
self::$token = $result['token'];
|
||||
}
|
||||
@@ -163,10 +159,18 @@ class Collection
|
||||
$this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'DELETE');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function doRequest(string $url, string $method, $bodyParams = []): string
|
||||
{
|
||||
$tmp = $bodyParams;
|
||||
$bodyParams = [];
|
||||
$bodyParams['json'] = $tmp;
|
||||
$bodyParams['headers']['Content-Type'] = 'application/json';
|
||||
|
||||
if (self::$token != '') {
|
||||
// TODO token ?
|
||||
$bodyParams['headers']['Authorization'] = 'Bearer ' . self::$token;
|
||||
}
|
||||
|
||||
$client = new \GuzzleHttp\Client();
|
||||
@@ -185,6 +189,7 @@ class Collection
|
||||
$output = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'GET');
|
||||
return json_decode($output, JSON_FORCE_OBJECT);
|
||||
}
|
||||
|
||||
public function authAsAdmin(string $email, string $password): string
|
||||
{
|
||||
$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
|
||||
{
|
||||
$id = '6588yk36406qqva';
|
||||
|
||||
$this->expectException(ClientException::class);
|
||||
|
||||
$this->collection->getOne($id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user