Skip to content

Commit

Permalink
Merge pull request #284 from PrestaShopCorp/refactor/phpunit-target
Browse files Browse the repository at this point in the history
refactor: phpunit setup
  • Loading branch information
hschoenenberger authored Nov 22, 2022
2 parents b883918 + 01d439e commit bbb29c9
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/accounts-qc-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ jobs:

# - name: PHPUnit tests 1.7
# run: |
# PHPUNIT_PS_VERSION="1.7" make phpunit
# DOCKER_INTERNAL="1.7" make phpunit

- name: PHPUnit tests nightly
run: |
PHPUNIT_PS_VERSION="nightly" make phpunit
DOCKER_INTERNAL="nightly" make phpunit
header-stamp:
name: Check license headers
Expand Down
52 changes: 28 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ PHPSTAN_VERSION ?= 0.12
PHPUNIT_VERSION ?= latest
PS_VERSION ?= latest #1.7.7.1
NEON_FILE ?= phpstan-PS-1.7.neon
PHPUNIT_PS_VERSION ?= nightly # 1.7|nightly
DOCKER_INTERNAL ?= nightly # 1.7|nightly

# target: default - Calling build by default
default: build

# target: help - Get help on this file
help:
@egrep "^#" Makefile
@egrep "^# target" Makefile

# target: build - Clean up the repository
clean:
Expand Down Expand Up @@ -94,11 +94,13 @@ test-back: lint-back phpstan phpunit
lint-back:
vendor/bin/php-cs-fixer fix --dry-run --diff --using-cache=no --diff-format udiff

# target: phpstan - Start phpstan
phpstan:
check-docker:
ifndef DOCKER
$(error "DOCKER is unavailable on your system")
endif

# target: phpstan - Start phpstan
phpstan: check-docker
docker pull phpstan/phpstan:${PHPSTAN_VERSION}
docker pull prestashop/prestashop:${PS_VERSION}
docker run --rm -d -v ps-volume:/var/www/html --entrypoint /bin/sleep --name test-phpstan prestashop/prestashop:${PS_VERSION} 2s
Expand All @@ -111,26 +113,28 @@ endif
docker volume rm ps-volume

# target: phpunit - Start phpunit
phpunit: phpunit-cleanup
ifndef DOCKER
$(error "DOCKER is unavailable on your system")
endif
docker run --rm -d -e PS_DOMAIN=localhost -e PS_ENABLE_SSL=0 -e PS_DEV_MODE=1 --name test-phpunit prestashop/docker-internal-images:${PHPUNIT_PS_VERSION}
-docker container exec -u www-data test-phpunit sh -c "sleep 1 && php -d memory_limit=-1 ./bin/console prestashop:module uninstall ps_accounts"
docker cp . test-phpunit:/var/www/html/modules/ps_accounts
docker cp ./config/config.yml.dist test-phpunit:/var/www/html/modules/ps_accounts/config/config.yml
docker container exec test-phpunit sh -c "chown -R www-data:www-data ./modules/ps_accounts"
docker container exec -u www-data test-phpunit sh -c "sleep 1 && php -d memory_limit=-1 ./bin/console prestashop:module install ps_accounts"
@docker container exec -u www-data test-phpunit sh -c "echo \"Testing module v\`cat /var/www/html/modules/ps_accounts/config.xml | grep '<version>' | sed 's/^.*\[CDATA\[\(.*\)\]\].*/\1/'\`\n\""
docker container exec test-phpunit sh -c "pecl install xdebug && docker-php-ext-enable xdebug"
docker container exec -u www-data --workdir /var/www/html/modules/ps_accounts test-phpunit sh -c "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-text"
@echo phpunit passed

phpunit-cleanup:
-docker container rm -f test-phpunit

phpunit-debug:
docker container exec -u www-data --workdir /var/www/html/modules/ps_accounts test-phpunit ./vendor/bin/phpunit
# FIXME: create two command to run test (feature with apache2 started et unit with just mysql
#PHPUNIT_CMD="./vendor/bin/phpunit --colors=always || bash"
#PHPUNIT_CMD="./vendor/bin/phpunit"
phpunit: check-docker
# -docker container rm -f phpunit
@docker run --rm \
--name phpunit \
-e PS_DOMAIN=localhost \
-e PS_ENABLE_SSL=0 \
-e PS_DEV_MODE=1 \
-v ${PWD}:/var/www/html/modules/ps_accounts \
-w /var/www/html/modules/ps_accounts \
prestashop/docker-internal-images:${DOCKER_INTERNAL} \
sh -c " \
service mysql start && \
service apache2 start && \
if [ ! -f ./config/config.yml ]; then cp ./config/config.yml.dist ./config/config.yml; fi && \
../../bin/console prestashop:module install ps_accounts && \
echo \"Testing module v\`cat config.xml | grep '<version>' | sed 's/^.*\[CDATA\[\(.*\)\]\].*/\1/'\`\n\" && \
chown -R www-data:www-data ../../var/logs && \
XDEBUG_MODE=coverage ./vendor/bin/phpunit \
"
@echo phpunit passed

vendor/phpunit/phpunit:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"set-license-header": [
"@php ./vendor/bin/header-stamp --license=\"assets/afl.txt\" --exclude=\".github,node_modules,vendor,vendor,tests,_dev\""
],
"test": "./vendor/bin/phpunit --configuration './phpunit.xml' --bootstrap './tests/bootstrap.php' --test-suffix 'Test.php,.phpt'"
"phpunit": "docker exec -u www-data -ti phpunit-dev ./vendor/bin/phpunit --configuration './phpunit.xml' --bootstrap './tests/bootstrap.php' --test-suffix 'Test.php,.phpt'"
},
"author": "PrestaShop",
"license": "AFL-3.0",
Expand Down
3 changes: 1 addition & 2 deletions config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
<confirmUninstall><![CDATA[This action will prevent immediately your PrestaShop services and Community services from working as they are using PrestaShop Accounts module for authentication.]]></confirmUninstall>
<is_configurable>1</is_configurable>
<need_instance>0</need_instance>
<limited_countries></limited_countries>
</module>
</module>
20 changes: 20 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: "3.7"
services:
phpunit:
container_name: phpunit-dev
image: prestashop/docker-internal-images:nightly
working_dir: /var/www/html/modules/ps_accounts
environment:
- PS_DOMAIN=localhost
- PS_ENABLE_SSL=0
- PS_DEV_MODE=1
volumes:
- ./:/var/www/html/modules/ps_accounts
entrypoint: [
"/bin/sh", "-c", " \
service mysql start && \
php -d memory_limit=-1 ../../bin/console prestashop:module install ps_accounts && \
chown -R www-data:www-data ../../var/logs && \
apache2-foreground
"
]
Empty file removed tests/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions tests/Feature/Api/v1/DecodePayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function itShouldEncodePayload()
public function itShouldDecodeMethod()
{
$response = $this->client->get('/module/ps_accounts/apiV1ShopUrl', [
# $response = $this->client->get('/?module=ps_accounts&fc=module&controller=apiV1ShopUrl', [
'headers' => [
AbstractRestController::TOKEN_HEADER => (string) $this->encodePayload([
'shop_id' => 1,
Expand Down
5 changes: 4 additions & 1 deletion tests/Feature/Api/v1/ShopLinkAccount/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ public function itShouldSucceed()

$response = $this->client->delete('/module/ps_accounts/apiV1ShopLinkAccount', [
'headers' => [
AbstractRestController::TOKEN_HEADER => (string) $this->encodePayload(['shop_id' => 1])
AbstractRestController::TOKEN_HEADER => (string) $this->encodePayload([
'method' => 'DELETE',
'shop_id' => 1,
])
],
]);

Expand Down
5 changes: 4 additions & 1 deletion tests/Feature/Api/v1/ShopOauth2Client/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public function itShouldSucceed()

$response = $this->client->delete('/module/ps_accounts/apiV1ShopOauth2Client', [
'headers' => [
AbstractRestController::TOKEN_HEADER => (string) $this->encodePayload(['shop_id' => 1])
AbstractRestController::TOKEN_HEADER => (string) $this->encodePayload([
'method' => 'DELETE',
'shop_id' => 1,
])
],
]);

Expand Down
10 changes: 9 additions & 1 deletion tests/Feature/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PrestaShop\Module\PsAccounts\Api\Client\Guzzle\AbstractGuzzleClient;
use PrestaShop\Module\PsAccounts\Api\Client\Guzzle\GuzzleClientFactory;
use PrestaShop\Module\PsAccounts\Provider\RsaKeysProvider;
use PrestaShop\Module\PsAccounts\Repository\UserTokenRepository;
use PrestaShop\Module\PsAccounts\Tests\TestCase;

class FeatureTestCase extends TestCase
Expand All @@ -35,6 +36,11 @@ class FeatureTestCase extends TestCase
*/
protected $rsaKeysProvider;

/**
* @var UserTokenRepository
*/
protected $userTokenRepository;

/**
* @throws \Exception
*/
Expand Down Expand Up @@ -63,9 +69,11 @@ public function setUp()
$this->client = $this->guzzleClient->getClient();

$this->rsaKeysProvider = $this->module->getService(RsaKeysProvider::class);

$this->rsaKeysProvider->regenerateKeys();

$this->userTokenRepository = $this->module->getService(UserTokenRepository::class);
$this->userTokenRepository->cleanupCredentials();

// FIXME: Link::getModuleLink
// FIXME: OR activate friendly urls
//$this->configuration->set('PS_REWRITING_SETTINGS', '1');
Expand Down
81 changes: 81 additions & 0 deletions tests/Unit/HookActionObjectShopUpdateAfterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace PrestaShop\Module\PsAccounts\Tests\Unit;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\OptimisticLockException;
use Exception;
use PrestaShop\Module\PsAccounts\Api\Client\AccountsClient;
use PrestaShop\Module\PsAccounts\DTO\UpdateShop;
use PrestaShop\Module\PsAccounts\Provider\ShopProvider;
use PrestaShop\Module\PsAccounts\Tests\TestCase;
use PrestaShop\PrestaShop\Adapter\Meta\ShopUrlDataConfiguration;
use PrestaShopBundle\Entity\Shop;

class HookActionObjectShopUpdateAfterTest extends TestCase
{
/**
* @not_a_test
*
* @throws Exception
* @throws OptimisticLockException
*/
public function itShouldUpdateBoBaseUrlInBoContext()
{
// TODO: spy on update shop api call boBaseUrl
///** @var AccountsClient $tokenRepos */
$apiClient = $this->getMockBuilder(AccountsClient::class)
->setConstructorArgs([
$this->module->getParameter('ps_accounts.accounts_api_url'),
$this->module->getService(ShopProvider::class)
])
->setMethods(['updateUserShop'])
->getMock();

// Replace with mocked version
//$this->module->getServiceContainer()->getContainer()->set(AccountsClient::class, $apiClient);

// $this->assertEquals('toto',
// $this->module->getService(AccountsClient::class)
// ->updateUserShop(new UpdateShop([
// 'shopId' => 1,
// 'name' => 'foo',
// 'domain' => 'my.shop',
// 'sslDomain' => 'my.secure.shop',
// 'virtualUri' => 'bar',
// 'physicalUri' => 'baz',
// 'boBaseUrl' => 'base.url',
// ]))
// );

$apiClient->expects($this->once())
->method('updateUserShop')
->willReturnCallback(function (UpdateShop $updateShopDto) {
$boBaseUrl = $updateShopDto->boBaseUrl;
error_log('############### ' . $boBaseUrl);
});

/** @var ShopUrlDataConfiguration $shopUrlData */
$shopUrlData = $this->module->getContainer()->get('prestashop.adapter.meta.shop_url.configuration');

$shopUrlData->updateConfiguration([
"domain" => 'foo.com',
"domain_ssl" => 'foo.secure.com',
"physical_uri" => '/',
]);

// /** @var EntityManagerInterface $entityManager */
// $entityManager = $this->module->getContainer()->get('doctrine.orm.entity_manager');
//
// $shopRepository = $entityManager->getRepository(Shop::class);
//
// /** @var Shop $shop */
// $shop = $shopRepository->findOneBy(['id' => 1]);
//
// // FIXME: update shop domain
// $shop->setName('Test PrestaShop');
//
// $entityManager->persist($shop);
// $entityManager->flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function itShouldReturnFalse()
'email_verified' => false,
]);

$refreshToken = $this->makeJwtToken(new \DateTimeImmutable('+1 year'));
$refreshToken = null; //$this->makeJwtToken(new \DateTimeImmutable('+1 year'));

/** @var UserTokenRepository $tokenRepos */
$tokenRepos = $this->module->getService(UserTokenRepository::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function itShouldReturnFalse()

$tokenRepos->updateCredentials(
$this->makeFirebaseToken(null, ['email_verified' => false]),
base64_encode($this->faker->name)
null //base64_encode($this->faker->name)
);

/** @var PsAccountsService $service */
Expand Down
23 changes: 16 additions & 7 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?php
@include (__DIR__ . '/local_config.php');

// depends on BE | FE context
if (!defined('_PS_ADMIN_DIR_')) {
define('_PS_ADMIN_DIR_', '');
define('_PS_ADMIN_DIR_', '/admin');
}

$rootDirectory = getenv('_PS_ROOT_DIR_') ?: __DIR__ . '/../../..';
$projectDir = __DIR__ . '/../';
if (!defined('_PS_MODE_DEV_')) {
define('_PS_MODE_DEV_', true);
}

$rootDirectory = getenv('_PS_ROOT_DIR_') ?: __DIR__ . '/../../..';
require_once $rootDirectory . '/config/config.inc.php';
require_once $projectDir . '/vendor/autoload.php';

//$projectDir = __DIR__ . '/../';
//require_once $projectDir . '/vendor/autoload.php';

// FIXME: load kernel when necessary
global $kernel;
if(!$kernel){
require_once _PS_ROOT_DIR_.'/app/AppKernel.php';
$kernel = new \AppKernel('dev', true);
$kernel->boot();
}

0 comments on commit bbb29c9

Please sign in to comment.