Skip to content

Commit

Permalink
Merge pull request #43 from PrestaShop/refactor-hooks-api
Browse files Browse the repository at this point in the history
Refactored Hooks API
  • Loading branch information
jolelievre authored Dec 19, 2024
2 parents be3cf5b + c7e8bfa commit bf29f91
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 175 deletions.
37 changes: 1 addition & 36 deletions src/ApiPlatform/Resources/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,51 +25,16 @@

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Hook\Command\UpdateHookStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Hook\Exception\HookNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Hook\Query\GetHook;
use PrestaShop\PrestaShop\Core\Domain\Hook\Query\GetHookStatus;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
use PrestaShopBundle\ApiPlatform\Metadata\PaginatedList;
use PrestaShopBundle\ApiPlatform\Provider\QueryListProvider;

#[ApiResource(
operations: [
new CQRSGet(
uriTemplate: '/hook-status/{id}',
requirements: ['id' => '\d+'],
openapiContext: [
'summary' => 'Get hook status A',
'description' => 'Get hook status B',
'parameters' => [
[
'name' => 'id',
'in' => 'path',
'required' => true,
'schema' => [
'type' => 'string',
],
'description' => 'Id of the hook you are requesting the status from',
],
[
'name' => 'Authorization',
'in' => 'scopes',
'description' => 'hook_read <br> hook_write ',
],
],
],
exceptionToStatus: [HookNotFoundException::class => 404],
CQRSQuery: GetHookStatus::class,
scopes: ['hook_read']
),
new CQRSUpdate(
uriTemplate: '/hook-status',
CQRSCommand: UpdateHookStatusCommand::class,
scopes: ['hook_write']
),
new CQRSGet(
uriTemplate: '/hooks/{id}',
uriTemplate: '/hook/{id}',
requirements: ['id' => '\d+'],
exceptionToStatus: [HookNotFoundException::class => 404],
CQRSQuery: GetHook::class,
Expand Down
132 changes: 0 additions & 132 deletions tests/Integration/ApiPlatform/GetHookStatusTest.php

This file was deleted.

12 changes: 5 additions & 7 deletions tests/Integration/ApiPlatform/GetHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
DatabaseDump::restoreTables(['hook']);
self::createApiClient(['hook_write', 'hook_read']);
self::createApiClient(['hook_read']);
}

public static function tearDownAfterClass(): void
Expand All @@ -44,7 +44,7 @@ public function getProtectedEndpoints(): iterable
{
yield 'get endpoint' => [
'GET',
'/hooks/1',
'/hook/1',
];
}

Expand All @@ -57,17 +57,16 @@ public function testGetHook(): void

$bearerToken = $this->getBearerToken([
'hook_read',
'hook_write',
]);

$response = static::createClient()->request('GET', '/hooks/' . (int) $hook->id, ['auth_bearer' => $bearerToken]);
$response = static::createClient()->request('GET', '/hook/' . (int) $hook->id, ['auth_bearer' => $bearerToken]);
self::assertEquals(json_decode($response->getContent())->active, $hook->active);
self::assertResponseStatusCodeSame(200);

static::createClient()->request('GET', '/hooks/' . 9999, ['auth_bearer' => $bearerToken]);
static::createClient()->request('GET', '/hook/' . 9999, ['auth_bearer' => $bearerToken]);
self::assertResponseStatusCodeSame(404);

static::createClient()->request('GET', '/hooks/' . $hook->id);
static::createClient()->request('GET', '/hook/' . $hook->id);
self::assertResponseStatusCodeSame(401);

$hook->delete();
Expand All @@ -78,7 +77,6 @@ public function testListHooks(): void
$hooks = $this->generateHooks();
$bearerToken = $this->getBearerToken([
'hook_read',
'hook_write',
]);

$response = static::createClient()->request('GET', '/hooks', ['auth_bearer' => $bearerToken]);
Expand Down

0 comments on commit bf29f91

Please sign in to comment.