From 666395275b96b2f967d84fb94eb9f76bbffa301f Mon Sep 17 00:00:00 2001 From: Simon Micheneau Date: Sat, 3 Sep 2022 14:26:14 +0200 Subject: [PATCH 1/2] feat: Add getter for collection and single types --- README.md | 13 +++- docs/README.md | 6 +- docs/collection.md | 30 +++++++++ docs/single.md | 24 ++++++++ src/CollectionType.php | 115 +++++++++++++++++++++++++++++++++++ src/SingleType.php | 86 ++++++++++++++++++++++++++ tests/CollectionTypeTest.php | 97 +++++++++++++++++++++++++++++ tests/SingleTypeTest.php | 62 +++++++++++++++++++ 8 files changed, 428 insertions(+), 5 deletions(-) create mode 100644 docs/collection.md create mode 100644 docs/single.md create mode 100644 src/CollectionType.php create mode 100644 src/SingleType.php create mode 100644 tests/CollectionTypeTest.php create mode 100644 tests/SingleTypeTest.php diff --git a/README.md b/README.md index b56fa7f..23dccb8 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,17 @@ PHP library allowing you to use the [Strapi](https://strapi.io/) API via various ## TODO - [x] User system (without external provider) -- [ ] Collection types -- [ ] Single types +- Collection types + - [x] Get + - [x] Get Id + - [ ] Post + - [ ] Put + - [ ] Delete +- Single types + - [x] Get + - [ ] Put + - [ ] Delete + - [ ] Post - [ ] Upload Check this [docs](https://github.com/SimonDevelop/strapi-api/blob/master/docs) for more. diff --git a/docs/README.md b/docs/README.md index 73e511e..729762a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,6 +2,6 @@ - [SimonDevelop\Strapi\Setup](https://github.com/SimonDevelop/strapi-api/blob/master/docs/setup.md) - [SimonDevelop\Strapi\Auth](https://github.com/SimonDevelop/strapi-api/blob/master/docs/auth.md) -- SimonDevelop\Strapi\CollectionTypes -- SimonDevelop\Strapi\SingleTypes -- SimonDevelop\Strapi\Upload \ No newline at end of file +- [SimonDevelop\Strapi\CollectionType](https://github.com/SimonDevelop/strapi-api/blob/master/docs/collection.md) +- [SimonDevelop\Strapi\SingleType](https://github.com/SimonDevelop/strapi-api/blob/master/docs/single.md) +- SimonDevelop\Strapi\Upload diff --git a/docs/collection.md b/docs/collection.md new file mode 100644 index 0000000..564fde5 --- /dev/null +++ b/docs/collection.md @@ -0,0 +1,30 @@ +# SimonDevelop\Strapi\CollectionType references + +## __constructor(SimonDevelop\Strapi\Setup $setup) +```php +$collection = new CollectionType($setup); +``` + +## getSetup(): SimonDevelop\Strapi\Setup +### Get Setup object +```php +$setup = $collection->getSetup(); +``` + +## setSetup(SimonDevelop\Strapi\Setup $setup): self +### Define Setup object +```php +$collection->setSetup($setup); +``` + +## get(string $collection): array|string +### CollectionType data (response strapi) if the request is passed, else information in string +```php +$response = $collection->get('users'); +``` + +## getById(string $collection, int $id): array|string +### CollectionType data item by id (response strapi) if the request is passed, else information in string +```php +$response = $collection->get('users', 1); +``` diff --git a/docs/single.md b/docs/single.md new file mode 100644 index 0000000..88ab034 --- /dev/null +++ b/docs/single.md @@ -0,0 +1,24 @@ +# SimonDevelop\Strapi\SingleType references + +## __constructor(SimonDevelop\Strapi\Setup $setup) +```php +$single = new SingleType($setup); +``` + +## getSetup(): SimonDevelop\Strapi\Setup +### Get Setup object +```php +$setup = $single->getSetup(); +``` + +## setSetup(SimonDevelop\Strapi\Setup $setup): self +### Define Setup object +```php +$single->setSetup($setup); +``` + +## get(string $single): array|string +### SingleType data (response strapi) if the request is passed, else information in string +```php +$response = $single->get('homepage'); +``` diff --git a/src/CollectionType.php b/src/CollectionType.php new file mode 100644 index 0000000..0e900c6 --- /dev/null +++ b/src/CollectionType.php @@ -0,0 +1,115 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace SimonDevelop\Strapi; + +use SimonDevelop\Strapi\Setup; +use GuzzleHttp\Client; +use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Exception\ConnectException; + +class CollectionType +{ + /** + * @var Setup + */ + private Setup $setup; + + /** + * @var Client + */ + private Client $client; + + /** + * @param Setup $setup + */ + public function __construct(Setup $setup) + { + $this->setup = $setup; + $this->client = new Client(); + } + + /** + * @param string $collection + * @return array|string + */ + public function get(string $collection) + { + // Function tested with mock + try { + // @codeCoverageIgnoreStart + $response = $this->client->request('GET', $this->setup->getUrl() . "/$collection", [ + 'headers' => [ + 'authorization' => 'Bearer '.$this->setup->getToken() + ] + ]); + + return json_decode($response->getBody()->getContents(), true); + // @codeCoverageIgnoreEnd + } catch (ClientException $e) { + // @codeCoverageIgnoreStart + $json = json_decode($e->getResponse()->getBody()->getContents(), true); + + return $json['error']['message']; + // @codeCoverageIgnoreEnd + } catch (ConnectException $e) { + return $e->getMessage(); + } + } + + /** + * @param string $collection + * @param int $id + * @return array|string + */ + public function getById(string $collection, int $id) + { + // Function tested with mock + try { + // @codeCoverageIgnoreStart + $response = $this->client->request('GET', $this->setup->getUrl() . "/$collection/$id", [ + 'headers' => [ + 'authorization' => 'Bearer '.$this->setup->getToken() + ] + ]); + + return json_decode($response->getBody()->getContents(), true); + // @codeCoverageIgnoreEnd + } catch (ClientException $e) { + // @codeCoverageIgnoreStart + $json = json_decode($e->getResponse()->getBody()->getContents(), true); + + return $json['error']['message']; + // @codeCoverageIgnoreEnd + } catch (ConnectException $e) { + return $e->getMessage(); + } + } + + /** + * @return Setup Current setup + */ + public function getSetup(): Setup + { + return $this->setup; + } + + /** + * @param Setup $setup + * @return CollectionType + */ + public function setSetup(Setup $setup): self + { + $this->setup = $setup; + + return $this; + } +} diff --git a/src/SingleType.php b/src/SingleType.php new file mode 100644 index 0000000..22ddf55 --- /dev/null +++ b/src/SingleType.php @@ -0,0 +1,86 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace SimonDevelop\Strapi; + +use SimonDevelop\Strapi\Setup; +use GuzzleHttp\Client; +use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Exception\ConnectException; + +class SingleType +{ + /** + * @var Setup + */ + private Setup $setup; + + /** + * @var Client + */ + private Client $client; + + /** + * @param Setup $setup + */ + public function __construct(Setup $setup) + { + $this->setup = $setup; + $this->client = new Client(); + } + + /** + * @param string $single + * @return array|string + */ + public function get(string $single) + { + // Function tested with mock + try { + // @codeCoverageIgnoreStart + $response = $this->client->request('GET', $this->setup->getUrl() . "/$single", [ + 'headers' => [ + 'authorization' => 'Bearer '.$this->setup->getToken() + ] + ]); + + return json_decode($response->getBody()->getContents(), true); + // @codeCoverageIgnoreEnd + } catch (ClientException $e) { + // @codeCoverageIgnoreStart + $json = json_decode($e->getResponse()->getBody()->getContents(), true); + + return $json['error']['message']; + // @codeCoverageIgnoreEnd + } catch (ConnectException $e) { + return $e->getMessage(); + } + } + + /** + * @return Setup Current setup + */ + public function getSetup(): Setup + { + return $this->setup; + } + + /** + * @param Setup $setup + * @return SingleType + */ + public function setSetup(Setup $setup): self + { + $this->setup = $setup; + + return $this; + } +} diff --git a/tests/CollectionTypeTest.php b/tests/CollectionTypeTest.php new file mode 100644 index 0000000..69d6115 --- /dev/null +++ b/tests/CollectionTypeTest.php @@ -0,0 +1,97 @@ +assertEquals($setup->getUrl(), $collection->getSetup()->getUrl()); + $this->assertIsObject($collection->setSetup($setup)); + + return $setup; + } + + /** + * getter test + * @depends testInitConstructor + * @covers ::get + */ + public function testGet($setup): void + { + $stub = $this->getMockBuilder(CollectionType::class)->setConstructorArgs([$setup])->getMock(); + $stub->method('get') + ->with('users') + ->willReturn([ + [ + 'id' => 1, + 'username' => "simondevelop", + 'email' => "contact@simon-micheneau.fr", + 'provider' => "local", + 'confirmed' => true, + 'blocked' => false, + 'createdAt' => "2022-04-20T15:52:08.659Z", + 'updatedAt' => "2022-04-22T12:39:51.479Z", + ], [ + 'id' => 2, + 'username' => "test", + 'email' => "test@gmail.com", + 'provider' => "local", + 'confirmed' => true, + 'blocked' => false, + 'createdAt' => "2022-04-20T15:52:08.659Z", + 'updatedAt' => "2022-04-22T12:39:51.479Z", + ], + ]); + + $this->assertEquals( + 1, + $stub->get('users')[0]['id'] + ); + } + + /** + * getter by id test + * @depends testInitConstructor + * @covers ::getById + */ + public function testGetById($setup): void + { + $stub = $this->getMockBuilder(CollectionType::class)->setConstructorArgs([$setup])->getMock(); + $stub->method('getById') + ->with('users', 1) + ->willReturn([ + 'id' => 1, + 'username' => "simondevelop", + 'email' => "contact@simon-micheneau.fr", + 'provider' => "local", + 'confirmed' => true, + 'blocked' => false, + 'createdAt' => "2022-04-20T15:52:08.659Z", + 'updatedAt' => "2022-04-22T12:39:51.479Z", + ]); + + $this->assertEquals( + 1, + $stub->getById('users', 1)['id'] + ); + } +} diff --git a/tests/SingleTypeTest.php b/tests/SingleTypeTest.php new file mode 100644 index 0000000..83fdb68 --- /dev/null +++ b/tests/SingleTypeTest.php @@ -0,0 +1,62 @@ +assertEquals($setup->getUrl(), $single->getSetup()->getUrl()); + $this->assertIsObject($single->setSetup($setup)); + + return $setup; + } + + /** + * getter test + * @depends testInitConstructor + * @covers ::get + */ + public function testGet($setup): void + { + $stub = $this->getMockBuilder(SingleType::class)->setConstructorArgs([$setup])->getMock(); + $stub->method('get') + ->with('homepage') + ->willReturn([ + 'data' => [ + 'id' => 1, + 'attributes' => [ + 'content' => '

title of page

', + 'createdAt' => '2022-04-27T15:17:07.028Z', + 'updatedAt' => '2022-04-27T15:17:07.028Z', + 'publishedAt' => '2022-04-27T15:17:07.028Z', + 'locale' => 'en', + ] + ], + 'meta' => [] + ]); + + $this->assertEquals( + '

title of page

', + $stub->get('homepage')['data']['attributes']['content'] + ); + } +} From e25a31b9f262744398c1a2e715e33ba443409055 Mon Sep 17 00:00:00 2001 From: Simon Micheneau Date: Sat, 3 Sep 2022 14:40:43 +0200 Subject: [PATCH 2/2] chore: clear class --- src/Auth.php | 14 +++++--------- src/Setup.php | 4 ---- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/Auth.php b/src/Auth.php index a2344ce..36f6330 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -16,10 +16,6 @@ use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ConnectException; -/** - * Class Auth - * Authentication, register and more function. - */ class Auth { /** @@ -50,19 +46,19 @@ class Auth /** * @var Setup */ - private $setup; + private Setup $setup; /** * @var Client */ - private $client; + private Client $client; /** * @param Setup $setup */ public function __construct(Setup $setup) { - $this->setup = $setup; + $this->setup = $setup; $this->client = new Client(); } @@ -175,8 +171,8 @@ public function resetPassword(string $code, string $newPassword, string $newPass // @codeCoverageIgnoreStart $response = $this->client->request('POST', $this->setup->getUrl() . self::RESET_PASSWORD_URL, [ 'json' => [ - 'code' => $code, - 'password' => $newPassword, + 'code' => $code, + 'password' => $newPassword, 'passwordConfirmation' => $newPasswordConfirm ] ]); diff --git a/src/Setup.php b/src/Setup.php index 14e3bad..56ce836 100644 --- a/src/Setup.php +++ b/src/Setup.php @@ -11,10 +11,6 @@ namespace SimonDevelop\Strapi; -/** - * Class Setup - * Configure your setup strapi api. - */ class Setup { /**