forked from PrestaShop/ps_apiresources
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
524 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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 code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Run ci.sh script | ||
run: | | ||
echo "Starting ci.sh script" | ||
chmod +x ci.sh | ||
./ci.sh | ||
echo "Script completed" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
BASEDIR=$(dirname "$(readlink -f "$0")") | ||
# create a tmp directory and clone the project & check if folder exist before running this cmd | ||
if [ ! -d "/tmp" ]; then | ||
mkdir /tmp | ||
fi | ||
cd /tmp | ||
if [ ! -d "/tmp/prestashop" ]; then | ||
git clone https://github.com/PrestaShop/PrestaShop.git prestashop | ||
# Remove this next line once the file has been transfered from the core to the module. | ||
rm prestashop/src/PrestaShopBundle/ApiPlatform/Resources/Hook.php | ||
fi | ||
cd /tmp/prestashop | ||
git checkout develop | ||
# install the project & mysql | ||
rm -rf /tmp/prestashop/modules/ps_apiresources/* | ||
cp -r $BASEDIR/* /tmp/prestashop/modules/ps_apiresources | ||
PS_INSTALL_AUTO=0 docker compose build --no-cache && docker compose up -d --force-recreate | ||
if [ $? -ne 0 ]; then | ||
exit | ||
fi | ||
bash -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' | ||
# install the module | ||
docker-compose exec prestashop-git composer i --working-dir=/var/www/html/modules/ps_apiresources | ||
docker-compose exec prestashop-git composer create-test-db | ||
# run tests | ||
docker-compose exec prestashop-git vendor/bin/phpunit modules/ps_apiresources/tests/* -c modules/ps_apiresources/tests/Integration/phpunit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?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\Core\Annotation\ApiProperty; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\Put; | ||
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\Processor\CommandProcessor; | ||
use PrestaShopBundle\ApiPlatform\Provider\QueryProvider; | ||
|
||
#[ApiResource( | ||
operations: [ | ||
new Get( | ||
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], | ||
provider: QueryProvider::class, | ||
extraProperties: [ | ||
'CQRSQuery' => GetHookStatus::class, | ||
'scopes' => ['hook_read'], | ||
] | ||
), | ||
new Put( | ||
uriTemplate: '/hook-status', | ||
processor: CommandProcessor::class, | ||
extraProperties: [ | ||
'CQRSCommand' => UpdateHookStatusCommand::class, | ||
'scopes' => ['hook_write'], | ||
] | ||
), | ||
new Get( | ||
uriTemplate: '/hooks/{id}', | ||
requirements: ['id' => '\d+'], | ||
exceptionToStatus: [HookNotFoundException::class => 404], | ||
provider: QueryProvider::class, | ||
extraProperties: [ | ||
'CQRSQuery' => GetHook::class, | ||
'scopes' => ['hook_read'], | ||
] | ||
), | ||
], | ||
)] | ||
class Hook | ||
{ | ||
#[ApiProperty(identifier: true)] | ||
public int $id; | ||
|
||
public bool $active; | ||
|
||
public string $name; | ||
|
||
public string $title; | ||
|
||
public string $description; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?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 PsApiResourcesTest\Integration\ApiPlatform; | ||
|
||
use ApiPlatform\Symfony\Bundle\Test\Client; | ||
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Command\AddApiAccessCommand; | ||
use Tests\Resources\DatabaseDump; | ||
|
||
abstract class ApiTestCase extends \ApiPlatform\Symfony\Bundle\Test\ApiTestCase | ||
{ | ||
protected const CLIENT_ID = 'test_client_id'; | ||
protected const CLIENT_NAME = 'test_client_name'; | ||
|
||
protected static ?string $clientSecret = null; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
parent::setUpBeforeClass(); | ||
DatabaseDump::restoreTables(['api_access']); | ||
} | ||
|
||
public static function tearDownAfterClass(): void | ||
{ | ||
parent::tearDownAfterClass(); | ||
DatabaseDump::restoreTables(['api_access']); | ||
self::$clientSecret = null; | ||
} | ||
|
||
protected static function createClient(array $kernelOptions = [], array $defaultOptions = []): Client | ||
{ | ||
if (!isset($defaultOptions['headers']['accept'])) { | ||
$defaultOptions['headers']['accept'] = ['application/json']; | ||
} | ||
|
||
return parent::createClient($kernelOptions, $defaultOptions); | ||
} | ||
|
||
protected function getBearerToken(array $scopes = []): string | ||
{ | ||
if (null === self::$clientSecret) { | ||
self::createApiAccess($scopes); | ||
} | ||
$client = static::createClient(); | ||
$parameters = ['parameters' => [ | ||
'client_id' => static::CLIENT_ID, | ||
'client_secret' => static::$clientSecret, | ||
'grant_type' => 'client_credentials', | ||
'scope' => $scopes, | ||
]]; | ||
$options = ['extra' => $parameters]; | ||
$response = $client->request('POST', '/api/oauth2/token', $options); | ||
|
||
return json_decode($response->getContent())->access_token; | ||
} | ||
|
||
protected static function createApiAccess(array $scopes = [], int $lifetime = 10000): void | ||
{ | ||
$client = static::createClient(); | ||
$command = new AddApiAccessCommand( | ||
static::CLIENT_NAME, | ||
static::CLIENT_ID, | ||
true, | ||
'', | ||
$lifetime, | ||
$scopes | ||
); | ||
|
||
$container = $client->getContainer(); | ||
$commandBus = $container->get('prestashop.core.command_bus'); | ||
$createdApiAccess = $commandBus->handle($command); | ||
|
||
self::$clientSecret = $createdApiAccess->getSecret(); | ||
} | ||
} |
Oops, something went wrong.