Skip to content

Commit

Permalink
Merge pull request #11 from PrestaShop/dev
Browse files Browse the repository at this point in the history
Merge dev into main
  • Loading branch information
M0rgan01 authored Feb 12, 2024
2 parents 76aaee8 + 94092f0 commit 1b42dc1
Show file tree
Hide file tree
Showing 18 changed files with 1,868 additions and 4 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Integration tests
on:
push:
pull_request:

permissions:
contents: read # to clone the repos and get release assets (shivammathur/setup-php)

jobs:
integration:
permissions:
contents: read # to clone the repos and get release assets (shivammathur/setup-php)
name: Integration tests
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '8.1', '8.2' ]
fail-fast: false
steps:
- name: Checkout module code
uses: actions/checkout@v3
with:
path: ps_apiresources
- uses: actions/checkout@v3
name: Checkout PrestaShop repository
with:
fetch-depth: 0
repository: PrestaShop/PrestaShop
path: prestashop
- name: Build Docker
run: |
cd prestashop
USER_ID=$(id -u) GROUP_ID=$(id -g) PS_INSTALL_AUTO=0 docker-compose build --no-cache && docker-compose up -d --force-recreate
if [ $? -ne 0 ]; then
echo "docker install failed"
exit
fi
bash -l -c 'while [[ "$(curl -L -s -o /dev/null -w %{http_code} 'http://localhost:8001/install-dev/')" != "200" ]]; do echo "waiting for shop install"; sleep 5; done'
USER_ID=$(id -u) GROUP_ID=$(id -g) docker exec prestashop_prestashop-git_1 php bin/console prestashop:module uninstall ps_apiresources
- name: Install Module
run: |
rm -rf prestashop/modules/ps_apiresources
mkdir -p prestashop/modules/ps_apiresources
cp -r ps_apiresources/* prestashop/modules/ps_apiresources
ls -l prestashop/modules/ps_apiresources
USER_ID=$(id -u) GROUP_ID=$(id -g) docker exec prestashop_prestashop-git_1 composer install --no-interaction --working-dir=/var/www/html/modules/ps_apiresources
USER_ID=$(id -u) GROUP_ID=$(id -g) docker exec prestashop_prestashop-git_1 php bin/console prestashop:module install ps_apiresources
USER_ID=$(id -u) GROUP_ID=$(id -g) docker exec prestashop_prestashop-git_1 composer create-test-db
- name: Run integration tests
run : |
USER_ID=$(id -u) GROUP_ID=$(id -g) docker exec prestashop_prestashop-git_1 vendor/bin/phpunit -c modules/ps_apiresources/tests/Integration/phpunit.xml
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
},
"classmap": ["ps_apiresources.php"]
},
"autoload-dev": {
"psr-4": {
"PsApiResourcesTest\\": "tests/"
}
},
"config": {
"preferred-install": "dist",
"classmap-authoritative": true,
Expand Down
74 changes: 70 additions & 4 deletions src/ApiPlatform/Resources/ApiAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@

namespace PrestaShop\Module\APIResources\ApiPlatform\Resources;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Command\AddApiAccessCommand;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Command\DeleteApiAccessCommand;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Command\EditApiAccessCommand;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Exception\ApiAccessNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Query\GetApiAccessForEditing;
use PrestaShopBundle\ApiPlatform\Processor\CommandProcessor;
use PrestaShopBundle\ApiPlatform\Provider\QueryProvider;

#[ApiResource(
Expand Down Expand Up @@ -60,25 +67,84 @@
],
],
],
exceptionToStatus: [ApiAccessNotFoundException::class => 404],
provider: QueryProvider::class,
extraProperties: [
'query' => GetApiAccessForEditing::class,
'CQRSQuery' => GetApiAccessForEditing::class,
'scopes' => ['api_access_read'],
]
),
new Delete(
uriTemplate: '/api-access/{apiAccessId}',
requirements: ['apiAccessId' => '\d+'],
openapiContext: [
'summary' => 'Delete API Access details',
'description' => 'Delete API Access public details only, sensitive information like secrets is not returned',
'parameters' => [
[
'name' => 'apiAccessId',
'in' => 'path',
'required' => true,
'schema' => [
'type' => 'string',
],
'description' => 'Id of the API Access you are deleting',
],
[
'name' => 'Authorization',
'in' => 'scopes',
'description' => 'api_access_write',
],
],
],
output: false,
provider: QueryProvider::class,
extraProperties: [
'query' => DeleteApiAccessCommand::class,
'CQRSQuery' => DeleteApiAccessCommand::class,
'scopes' => ['api_access_write'],
]
),
new Post(
uriTemplate: '/api-access',
processor: CommandProcessor::class,
extraProperties: [
'command' => AddApiAccessCommand::class,
'CQRSCommand' => AddApiAccessCommand::class,
'scopes' => ['api_access_write'],
]
),
new Put(
uriTemplate: '/api-access/{apiAccessId}',
read: false,
processor: CommandProcessor::class,
extraProperties: [
'command' => EditApiAccessCommand::class,
'query' => GetApiAccessForEditing::class,
'CQRSCommand' => EditApiAccessCommand::class,
'CQRSQuery' => GetApiAccessForEditing::class,
'scopes' => ['api_access_write'],
]
),
],
exceptionToStatus: [ApiAccessNotFoundException::class => 404],
)]
class ApiAccess
{
#[ApiProperty(identifier: true)]
public int $apiAccessId;

public string $clientName;
public string $secret;

public string $clientId;
public string $apiClientId;

public string $clientName;

public string $description;

public bool $enabled;

public int $lifetime;

public array $scopes;
}
76 changes: 76 additions & 0 deletions src/ApiPlatform/Resources/CartRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\Module\APIResources\ApiPlatform\Resources;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Put;
use PrestaShop\PrestaShop\Core\Domain\CartRule\Command\EditCartRuleCommand;
use PrestaShopBundle\ApiPlatform\Processor\CommandProcessor;

#[ApiResource(
operations: [
new Put(
uriTemplate: '/cart-rule',
processor: CommandProcessor::class,
extraProperties: ['CQRSCommand' => EditCartRuleCommand::class]
),
],
)]
class CartRule
{
public int $cartRuleId;

public string $description;

public string $code;

public array $minimumAmount;

public bool $minimumAmountShippingIncluded;

public int $customerId;

public array $localizedNames;

public bool $highlightInCart;

public bool $allowPartialUse;

public int $priority;

public bool $active;

public array $validityDateRange;

public int $totalQuantity;

public int $quantityPerUser;

public array $cartRuleAction;
}
119 changes: 119 additions & 0 deletions src/ApiPlatform/Resources/CustomerGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\Module\APIResources\ApiPlatform\Resources;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\AddCustomerGroupCommand;
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\DeleteCustomerGroupCommand;
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\EditCustomerGroupCommand;
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Exception\GroupNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Query\GetCustomerGroupForEditing;
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\QueryResult\EditableCustomerGroup;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSCreate;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;

#[ApiResource(
operations: [
new CQRSGet(
uriTemplate: '/customers/group/{customerGroupId}',
CQRSQuery: GetCustomerGroupForEditing::class,
scopes: [
'customer_group_read',
],
// QueryResult format doesn't match with ApiResource, so we can specify a mapping so that it is normalized with extra fields adapted for the ApiResource DTO
CQRSQueryMapping: [
// EditableCustomerGroup::$id is normalized as [customerGroupId]
'[id]' => '[customerGroupId]',
// EditableCustomerGroup::$reduction is normalized as [reductionPercent]
'[reduction]' => '[reductionPercent]',
],
),
new CQRSCreate(
uriTemplate: '/customers/group',
CQRSCommand: AddCustomerGroupCommand::class,
CQRSQuery: GetCustomerGroupForEditing::class,
scopes: [
'customer_group_write',
],
// Here, we use query mapping to adapt normalized query result for the ApiPlatform DTO
CQRSQueryMapping: [
'[id]' => '[customerGroupId]',
'[reduction]' => '[reductionPercent]',
],
// Here, we use command mapping to adapt the normalized command result for the CQRS query
CQRSCommandMapping: [
'[groupId]' => '[customerGroupId]',
],
),
new CQRSUpdate(
uriTemplate: '/customers/group/{customerGroupId}',
CQRSCommand: EditCustomerGroupCommand::class,
CQRSQuery: GetCustomerGroupForEditing::class,
scopes: [
'customer_group_write',
],
// Here we use the ApiResource DTO mapping to transform the normalized query result
ApiResourceMapping: [
'[id]' => '[customerGroupId]',
'[reduction]' => '[reductionPercent]',
],
),
new CQRSDelete(
uriTemplate: '/customers/group/{customerGroupId}',
CQRSQuery: DeleteCustomerGroupCommand::class,
scopes: [
'customer_group_write',
],
// Here, we use query mapping to adapt URI parameters to the expected constructor parameter name
CQRSQueryMapping: [
'[customerGroupId]' => '[groupId]',
],
),
],
exceptionToStatus: [GroupNotFoundException::class => 404],
)]
class CustomerGroup
{
#[ApiProperty(identifier: true)]
public int $customerGroupId;

public array $localizedNames;

public float $reductionPercent;

public bool $displayPriceTaxExcluded;

public bool $showPrice;

public array $shopIds;
}
Loading

0 comments on commit 1b42dc1

Please sign in to comment.