diff --git a/src/Collection.php b/src/Collection.php index a20d3e1..7a63a20 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -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']; } @@ -123,7 +119,7 @@ class Collection $response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET'); $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'); } @@ -163,14 +159,22 @@ 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(); - $response = $client->request($method, $url,$bodyParams); + $response = $client->request($method, $url, $bodyParams); return $response->getBody()->getContents() ?? ''; } @@ -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; diff --git a/tests/CollectionCreateTest.php b/tests/CollectionCreateTest.php new file mode 100644 index 0000000..bc79c8c --- /dev/null +++ b/tests/CollectionCreateTest.php @@ -0,0 +1,29 @@ +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); + } +} \ No newline at end of file diff --git a/tests/CollectionGetOneTest.php b/tests/CollectionGetOneTest.php index 8399e9a..78c5182 100644 --- a/tests/CollectionGetOneTest.php +++ b/tests/CollectionGetOneTest.php @@ -27,9 +27,7 @@ final class CollectionGetOneTest extends TestCase public function test_getOneWrongId(): void { $id = '6588yk36406qqva'; - $this->expectException(ClientException::class); - $this->collection->getOne($id); } } \ No newline at end of file