start with create collection item test

This commit is contained in:
Jonathan Martz
2025-01-26 22:46:57 +01:00
parent 40db76d3ed
commit 0d7255d77e
3 changed files with 43 additions and 11 deletions

View File

@@ -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;

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

View File

@@ -27,9 +27,7 @@ final class CollectionGetOneTest extends TestCase
public function test_getOneWrongId(): void
{
$id = '6588yk36406qqva';
$this->expectException(ClientException::class);
$this->collection->getOne($id);
}
}