Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECP-9541] Enable unit tests and code sniffer on main workflow #69

Merged
merged 16 commits into from
Nov 6, 2024
43 changes: 41 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,54 @@ jobs:
php-version: [8.2,8.3]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Use PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: composer:v1
tools: composer:v2

- name: Define Hyvä Checkout repositories
run: |
composer config repositories.hyva-themes/hyva-checkout git [email protected]:hyva-checkout/checkout.git
composer config repositories.hyva-themes/magento2-theme-module git [email protected]:hyva-themes/magento2-theme-module.git
composer config repositories.hyva-themes/magento2-reset-theme git [email protected]:hyva-themes/magento2-reset-theme.git
composer config repositories.hyva-themes/magento2-email-theme git [email protected]:hyva-themes/magento2-email-module.git
composer config repositories.hyva-default-theme git [email protected]:hyva-themes/magento2-default-theme.git

- name: Configure SSH access to Hyvä Gitlab
env:
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
HYVA_GITLAB_SSH_KEY: ${{ secrets.GITLAB_SSH_KEY }}
run: |
mkdir -p ~/.ssh
ssh-keyscan gitlab.hyva.io >> ~/.ssh/known_hosts
echo "${HYVA_GITLAB_SSH_KEY}" > ~/.ssh/hyva_id_rsa
chmod 600 ~/.ssh/hyva_id_rsa
ssh-agent -a ${SSH_AUTH_SOCK} > /dev/null
ssh-add ~/.ssh/hyva_id_rsa

- name: Install the plugin
run: |
echo "{\"http-basic\":{\"repo.magento.com\":{\"username\":\"${MAGENTO_USERNAME}\",\"password\":\"${MAGENTO_PASSWORD}\"}}}" > auth.json
composer install --prefer-dist
env:
CI: true
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
MAGENTO_USERNAME: ${{ secrets.MAGENTO_USERNAME }}
MAGENTO_PASSWORD: ${{ secrets.MAGENTO_PASSWORD }}

- name: Code Sniffer
run: vendor/bin/phpcs .

- name: Run PHPUnit
run: vendor/bin/phpunit --coverage-clover=build/clover.xml --log-junit=build/tests-log.xml -c Test/phpunit.xml Test/Unit

- name: Fix code coverage paths
run: sed -i "s;`pwd`/;;g" build/*.xml

- name: SonarCloud Scan
if: ${{ env.SONAR_TOKEN }}
Expand Down
5 changes: 2 additions & 3 deletions Test/Unit/Magewire/Payment/Method/CreditCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,10 @@ public function testRefreshPropertiesThrowsException()

private function setRefreshPropertiesCommonExpectations($quoteId)
{
$this->session->expects($this->exactly(2))
->method('getQuote')
$this->session->method('getQuote')
->willReturn($this->quote);

$this->quote->expects($this->once())
$this->quote->expects($this->exactly(2))
->method('getShippingAddress');

$this->session->expects($this->once())
Expand Down
8 changes: 3 additions & 5 deletions Test/Unit/Model/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Adyen\Hyva\Test\Unit\Model;

use Adyen\Hyva\Model\Configuration;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;
use Magento\Checkout\Model\CompositeConfigProvider;
use Magento\Framework\DataObjectFactory;
use Psr\Log\LoggerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class ConfigurationTest extends TestCase
class ConfigurationTest extends AbstractAdyenTestCase
{
private Configuration $configuration;
private CompositeConfigProvider|MockObject $configProvider;
Expand All @@ -29,9 +29,7 @@ protected function setUp(): void
$this->configProvider = $this->getMockBuilder(CompositeConfigProvider::class)
->disableOriginalConstructor()
->getMock();
$this->dataObjectFactory = $this->getMockBuilder(DataObjectFactory::class)
->disableOriginalConstructor()
->getMock();
$this->dataObjectFactory = $this->createGeneratedMock(DataObjectFactory::class, ['create']);
$this->logger = $this->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMock();
Expand Down
12 changes: 5 additions & 7 deletions Test/Unit/Model/MethodListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
namespace Adyen\Hyva\Test\Unit\Model;

use Adyen\Hyva\Model\MethodList;
use Adyen\Hyva\Model\Ui\AdyenHyvaConfigProvider;
use Adyen\Payment\Test\Unit\AbstractAdyenTestCase;

class MethodListTest extends \PHPUnit\Framework\TestCase
class MethodListTest extends AbstractAdyenTestCase
{
private MethodList $methodList;

protected function setUp(): void
{
$this->methodList = new MethodList();
$adyenHyvaConfigProviderMock = $this->createMock(AdyenHyvaConfigProvider::class);
$this->methodList = new MethodList($adyenHyvaConfigProviderMock, [], []);
}

public function testCollectAvailableMethodsReturnsEmptyArray(): void
Expand All @@ -21,11 +24,6 @@ public function testCollectAvailableMethodsReturnsEmptyArray(): void

public function testCollectAvailableMethodsReturnsCorrectMethods(): void
{
$methods = ['method1', 'method2'];
$this->methodList = new MethodList($methods);

$this->assertIsArray($this->methodList->collectAvailableMethods());
$this->assertCount(2, $this->methodList->collectAvailableMethods());
$this->assertEquals($methods, $this->methodList->collectAvailableMethods());
}
}
13 changes: 13 additions & 0 deletions Test/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
*
* Adyen Payment Module
*
* Copyright (c) 2024 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <[email protected]>
*/

require __DIR__.'/../vendor/autoload.php';
42 changes: 42 additions & 0 deletions Test/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!--
~
~ Adyen Hyvä Checkout compatibility module
~
~ Copyright (c) 2024 Adyen N.V.
~ This file is open source and available under the MIT license.
~ See the LICENSE file for more info.
~
~ Author: Adyen <[email protected]>
-->
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./bootstrap.php"
colors="true"
>
<php>
<ini name="display_errors" value="On"/>
<ini name="memory_limit" value="-1"/>
<ini name="max_execution_time" value="0"/>
<ini name="display_startup_errors" value="On"/>
<ini name="error_reporting" value="E_ALL"/>
<ini name="date.timezone" value="UTC"/>
<server name="SHELL_VERBOSITY" value="-1"/>
</php>

<testsuites>
<testsuite name="default">
<directory>.</directory>
</testsuite>
</testsuites>

<coverage processUncoveredFiles="false">
<include>
<directory suffix=".php">../.</directory>
</include>
<exclude>
<directory>../vendor</directory>
<directory>.</directory>
</exclude>
</coverage>
</phpunit>
17 changes: 17 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,29 @@
"hyva-themes/magento2-default-theme": "^1.3",
"hyva-themes/magento2-hyva-checkout": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "~9.6.1",
"magento/magento-coding-standard": "*",
"squizlabs/php_codesniffer": "~3.8.0"
},
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Adyen\\Hyva\\": ""
}
},
"repositories": [
{
"type": "composer",
"url": "https://repo.magento.com/"
}
],
"config": {
"allow-plugins": {
"magento/composer-dependency-version-audit-plugin": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
23 changes: 23 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<!--
/**
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2019 Adyen BV (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <[email protected]>
*/
-->
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<exclude-pattern type="relative">vendor/*</exclude-pattern>
<file>.</file>
<arg value="p"/>
<arg value="n"/>
<rule ref="Magento2"/>
<rule ref="PSR2">
<exclude-pattern>*.js</exclude-pattern>
</rule>
</ruleset>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use Magento\Framework\View\Element\Template;
?>

<div>
<div id="<?= $magewire->getContainerName() ?>ActionContainer" wire:ignore></div>
<div id="<?= $escaper->escapeHtml($magewire->getContainerName()) ?>ActionContainer" wire:ignore></div>

<script>
class cashappComponentHandler extends componentHandler {
Expand Down Expand Up @@ -60,12 +60,12 @@ use Magento\Framework\View\Element\Template;
let methodHandler = new cashappComponentHandler(
methodCode,
wire,
'<?= $magewire->getContainerName() ?>ActionContainer'
'<?= $escaper->escapeHtml($magewire->getContainerName()) ?>ActionContainer'
);

window.AdyenPaymentHandler = methodHandler;

if (methodCode !== '<?= $magewire->getMethodCode() ?>') {
if (methodCode !== '<?= $escaper->escapeHtml($magewire->getMethodCode()) ?>') {
methodHandler.renderMethodUnavailableMessage();
return;
}
Expand All @@ -75,7 +75,11 @@ use Magento\Framework\View\Element\Template;
} else {
let rawResponse = wire.get('paymentResponse');
let paymentMethods = JSON.parse(rawResponse);
methodHandler.activatePaymentMethod(methodCode, paymentMethods, '<?= $magewire->getContainerName() ?>ActionContainer');
methodHandler.activatePaymentMethod(
methodCode,
paymentMethods,
'<?= $escaper->escapeHtml($magewire->getContainerName()) ?>ActionContainer'
);
showPrimaryButton();
}
}).catch(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use Magento\Framework\View\Element\Template;
?>

<div>
<div id="<?= $magewire->getContainerName() ?>ActionContainer" wire:ignore></div>
<div id="<?= $escaper->escapeHtml($magewire->getContainerName()) ?>ActionContainer" wire:ignore></div>

<script>
class amazonPayComponentHandler extends componentHandler {
Expand Down Expand Up @@ -89,7 +89,7 @@ use Magento\Framework\View\Element\Template;

mountComponent(checkoutComponent, paymentMethodType, configuration, result = undefined) {
let self = this;
const containerId = "<?= $magewire->getContainerName() ?>ActionContainer";
const containerId = "<?= $escaper->escapeHtml($magewire->getContainerName()) ?>ActionContainer";

let url = new URL(location.href);
// Handles the redirect back to checkout page with amazonSessionKey in url
Expand Down Expand Up @@ -148,7 +148,7 @@ use Magento\Framework\View\Element\Template;
let self = this;

if (responseJSON.isRefused) {
let message = "<?= __("The Payment is Refused") ?>";
let message = "<?= $escaper->escapeHtml(__("The Payment is Refused")) ?>";

this.amazonPayStatus = false;

Expand Down Expand Up @@ -181,7 +181,7 @@ use Magento\Framework\View\Element\Template;
let amazonPayHandler = new amazonPayComponentHandler(
methodCode,
wire,
'<?= $magewire->getContainerName() ?>ActionContainer'
'<?= $escaper->escapeHtml($magewire->getContainerName()) ?>ActionContainer'
);

window.AdyenPaymentHandler = amazonPayHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use Magento\Framework\View\Element\Template;
?>

<div>
<div id="<?= $magewire->getContainerName() ?>ActionContainer" wire:ignore></div>
<div id="<?= $escaper->escapeHtml($magewire->getContainerName()) ?>ActionContainer" wire:ignore></div>

<script>
class cashappComponentHandler extends componentHandler {
Expand Down Expand Up @@ -57,12 +57,12 @@ use Magento\Framework\View\Element\Template;
let methodHandler = new cashappComponentHandler(
methodCode,
wire,
'<?= $magewire->getContainerName() ?>ActionContainer'
'<?= $escaper->escapeHtml($magewire->getContainerName()) ?>ActionContainer'
);

window.AdyenPaymentHandler = methodHandler;

if (methodCode !== '<?= $magewire->getMethodCode() ?>') {
if (methodCode !== '<?= $escaper->escapeHtml($magewire->getMethodCode()) ?>') {
methodHandler.renderMethodUnavailableMessage();
return;
}
Expand All @@ -72,7 +72,11 @@ use Magento\Framework\View\Element\Template;
} else {
let rawResponse = wire.get('paymentResponse');
let paymentMethods = JSON.parse(rawResponse);
methodHandler.activatePaymentMethod(methodCode, paymentMethods, '<?= $magewire->getContainerName() ?>ActionContainer');
methodHandler.activatePaymentMethod(
methodCode,
paymentMethods,
'<?= $escaper->escapeHtml($magewire->getContainerName()) ?>ActionContainer'
);
hidePrimaryButton();
}
}).catch(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ $storedCard = $this->getData(ProcessingMetadataInterface::BLOCK_PROPERTY_STORED_
hyvaCheckout.navigation.enableButtonPlaceOrder();
}
},
installmentOptions: <?= $magewire->getFormattedInstallments() ?>,
installmentOptions: <?= $escaper->escapeHtml($magewire->getFormattedInstallments()) ?>,
showInstallmentAmounts: true,
onBrand: function (state) {
creditCardHandler.setCreditCardType(creditCardHandler.getCcCodeByAltCode('<?= $escaper->escapeJs($storedCard->getType()) ?>'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use Magento\Framework\View\Element\Template;
?>

<div>
<div id="<?= $magewire->getContainerName() ?>ActionContainer" wire:ignore></div>
<div id="<?= $escaper->escapeHtml($magewire->getContainerName()) ?>ActionContainer" wire:ignore></div>

<script>
window.addEventListener('checkout:payment:method-list-boot', async (event) => {
Expand Down Expand Up @@ -47,12 +47,12 @@ use Magento\Framework\View\Element\Template;
let methodHandler = new componentHandler(
methodCode,
wire,
'<?= $magewire->getContainerName() ?>ActionContainer'
'<?= $escaper->escapeHtml($magewire->getContainerName()) ?>ActionContainer'
);

window.AdyenPaymentHandler = methodHandler;

if (methodCode !== '<?= $magewire->getMethodCode() ?>') {
if (methodCode !== '<?= $escaper->escapeHtml($magewire->getMethodCode()) ?>') {
methodHandler.renderMethodUnavailableMessage();
return;
}
Expand All @@ -62,7 +62,11 @@ use Magento\Framework\View\Element\Template;
} else {
let rawResponse = wire.get('paymentResponse');
let paymentMethods = JSON.parse(rawResponse);
methodHandler.activatePaymentMethod(methodCode, paymentMethods, '<?= $magewire->getContainerName() ?>ActionContainer');
methodHandler.activatePaymentMethod(
methodCode,
paymentMethods,
'<?= $escaper->escapeHtml($magewire->getContainerName()) ?>ActionContainer'
);
showPrimaryButton();
}
}).catch(() => {
Expand Down
Loading