Skip to content

Commit

Permalink
Merge pull request #11 from BitBagCommerce/op263-dev
Browse files Browse the repository at this point in the history
OP-263  - Added phpSpec tests, enabled phpStan and ECS, fixed GitHub actions
  • Loading branch information
SzymonKostrubiec authored May 10, 2024
2 parents a20a35b + 0893920 commit 0a670b3
Show file tree
Hide file tree
Showing 30 changed files with 937 additions and 62 deletions.
26 changes: 21 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,26 @@ jobs:
name: Validate database schema
run: (cd tests/Application && bin/console doctrine:schema:validate)

-
name: Run PHPSpec
- name: Run PHPStan
run: vendor/bin/phpstan analyse -c phpstan.neon -l 8 src/

- name: Run ECS
run: vendor/bin/ecs check src

- name: Run PHPSpec
run: vendor/bin/phpspec run --ansi -f progress --no-interaction

-
name: Run PHPUnit
run: vendor/bin/phpunit --colors=always
- name: Load fixtures in test application
run: (cd tests/Application && bin/console sylius:fixtures:load -n)

- name: Failed build Slack notification
uses: rtCamp/action-slack-notify@v2
if: ${{ failure() && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') }}
env:
SLACK_CHANNEL: ${{ secrets.FAILED_BUILD_SLACK_CHANNEL }}
SLACK_COLOR: ${{ job.status }}
SLACK_ICON: https://github.com/rtCamp.png?size=48
SLACK_MESSAGE: ':x:'
SLACK_TITLE: Failed build on ${{ github.event.repository.name }} repository
SLACK_USERNAME: ${{ secrets.FAILED_BUILD_SLACK_USERNAME }}
SLACK_WEBHOOK: ${{ secrets.FAILED_BUILD_SLACK_WEBHOOK }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@
# Symfony CLI https://symfony.com/doc/current/setup/symfony_server.html#different-php-settings-per-project
/.php-version
/php.ini

###> friendsofphp/php-cs-fixer ###
/.php-cs-fixer.php
/.php-cs-fixer.cache
###< friendsofphp/php-cs-fixer ###
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
"license": "MIT",
"require": {
"php": "^8.1",
"doctrine/annotations": "^1.14",
"sylius/sylius": ">=1.12.13 || ~1.13.0",
"sylius/mailer-bundle": "^1.8 || ^2.0@beta",
"symfony/webpack-encore-bundle": "^1.15"
},
"require-dev": {
"behat/behat": "^3.6.1",
"behat/mink-selenium2-driver": "^1.4",
"bitbag/coding-standard": "^3.0",
"dmore/behat-chrome-extension": "^1.3",
"dmore/chrome-mink-driver": "^2.7",
"friends-of-behat/mink": "^1.8",
Expand All @@ -26,9 +28,9 @@
"friends-of-behat/suite-settings-extension": "^1.0",
"friends-of-behat/symfony-extension": "^2.1",
"friends-of-behat/variadic-extension": "^1.3",
"phpspec/phpspec": "^7.2",
"phpspec/phpspec": "^7.5",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^1.8.1",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-doctrine": "1.3.40",
"phpstan/phpstan-strict-rules": "^1.3.0",
"phpstan/phpstan-webmozart-assert": "^1.2.0",
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ parameters:
level: max
reportUnmatchedIgnoredErrors: false
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
paths:
- src
- tests/Behat
Expand All @@ -16,3 +17,4 @@ parameters:

ignoreErrors:
- '/Parameter #1 \$configuration of method Symfony\\Component\\DependencyInjection\\Extension\\Extension::processConfiguration\(\) expects Symfony\\Component\\Config\\Definition\\ConfigurationInterface, Symfony\\Component\\Config\\Definition\\ConfigurationInterface\|null given\./'
- '#Property .+::\$requestStack is never read, only written\.#'
117 changes: 117 additions & 0 deletions spec/Action/CaptureActionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusImojePlugin\Action;

use BitBag\SyliusImojePlugin\Api\ImojeApi;
use BitBag\SyliusImojePlugin\Api\ImojeApiInterface;
use BitBag\SyliusImojePlugin\Resolver\SignatureResolverInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Reply\HttpPostRedirect;
use Payum\Core\Request\Capture;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\PayumBundle\Model\PaymentSecurityTokenInterface;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Customer\Model\CustomerInterface;
use Symfony\Component\HttpFoundation\Request;

final class CaptureActionSpec extends ObjectBehavior
{
public function let(
SignatureResolverInterface $signatureResolver,
): void {
$this->beConstructedWith($signatureResolver);
}

public function it_should_return_true_when_request_and_model_is_valid(
Capture $request,
ArrayObject $arrayObject,
): void {
$request->getModel()->willReturn($arrayObject);

$this->supports($request)->shouldReturn(true);
}

public function it_should_return_false_when_model_is_invalid(
Capture $request,
): void {
$request->getModel()->willReturn(null);

$this->supports($request)->shouldReturn(false);
}

public function it_should_return_false_when_request_is_not_a_capture_instance(
Request $request,
): void {
$this->supports($request)->shouldReturn(false);
}

public function it_executes_correctly_with_valid_request(
Capture $request,
OrderInterface $order,
PaymentInterface $payment,
PaymentSecurityTokenInterface $token,
AddressInterface $address,
CustomerInterface $customer,
ImojeApi $apiClass,
SignatureResolverInterface $signatureResolver,
): void {
$data = ['statusImoje' => ImojeApiInterface::NEW_STATUS, 'paymentId' => 123, 'tokenHash' => '1234sdcsdfxz'];
$request->getModel()->willReturn(new ArrayObject($data));

$request->getToken()->willReturn('1234sdcsdfxz');
$request->getFirstModel()->willReturn($payment);
$request->getToken()->willReturn($token);

$payment->getOrder()->willReturn($order);
$payment->getId()->willReturn(123);

$token->getHash()->willReturn('1234sdcsdfxz');

$apiClass->getServiceKey()->willReturn('1234sdcsdfxz');
$order->getBillingAddress()->willReturn($address);
$order->getCustomer()->willReturn($customer);

$apiClass->getApiUrl()->willReturn('http://example.com/');

$apiClass->getServiceId()->willReturn('1');
$apiClass->getMerchantId()->willReturn('1');
$order->getTotal()->willReturn(1000);
$order->getCurrencyCode()->willReturn('EUR');
$order->getNumber()->willReturn('123');
$address->getFirstName()->willReturn('John Doe');
$address->getLastName()->willReturn('Smith');
$token->getAfterUrl()->willReturn('http://example.com/');
$customer->getEmail()->willReturn('[email protected]');

$orderData = [
'serviceId' => '1',
'merchantId' => '1',
'amount' => 1000,
'currency' => 'EUR',
'orderId' => '123',
'customerFirstName' => 'John Doe',
'customerLastName' => 'Smith',
'urlReturn' => 'http://example.com/',
'customerEmail' => '[email protected]',
];

$signatureResolver->createSignature($orderData, '1234sdcsdfxz')
->willReturn('signature');

$request->setModel(new ArrayObject($data))->shouldBeCalled();

$this->setApi($apiClass);
$this->shouldThrow(HttpPostRedirect::class)
->during('execute', [$request]);
}
}
108 changes: 108 additions & 0 deletions spec/Action/ConvertPaymentActionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusImojePlugin\Action;

use BitBag\SyliusImojePlugin\Action\ConvertPaymentAction;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Model\PaymentInterface;
use Payum\Core\Request\Convert;
use PhpSpec\ObjectBehavior;
use Symfony\Component\HttpFoundation\Request;

final class ConvertPaymentActionSpec extends ObjectBehavior
{
public function it_is_initializable(): void
{
$this->shouldHaveType(ConvertPaymentAction::class);
}

function it_implements_imoje_gateway_factory_interface(): void
{
$this->shouldHaveType(ActionInterface::class);
}

public function it_sets_result_from_payment_details_with_non_empty_details(
Convert $request,
PaymentInterface $payment,
): void {
$request->getSource()->willReturn($payment);
$payment->getDetails()->willReturn(['field' => '123']);
$request->setResult(['field' => '123'])->shouldBeCalled();

$this->execute($request);
}

public function it_sets_empty_result_when_payment_details_are_empty(
Convert $request,
PaymentInterface $payment,
): void {
$payment->getDetails()->willReturn([]);
$request->getSource()->willReturn($payment);
$request->setResult([])->shouldBeCalled();

$this->execute($request);
}

public function it_sets_result_when_payment_details_contain_null(
Convert $request,
PaymentInterface $payment,
): void {
$payment->getDetails()->willReturn(['key' => null]);
$request->getSource()->willReturn($payment);
$request->setResult(['key' => null])->shouldBeCalled();

$this->execute($request);
}

public function it_should_return_true_when_getTo_and_source_is_valid(
Convert $request,
PaymentInterface $payment,
): void {
$request->getSource()->willReturn($payment);
$request->getTo()->willReturn('array');

$this->supports($request)->shouldReturn(true);
}

public function it_should_return_false_when_source_is_invalid(
Convert $request,
): void {
$request->getSource()->willReturn(null);
$request->getTo()->willReturn('array');

$this->supports($request)->shouldReturn(false);
}

public function it_should_return_false_when_getTo_is_invalid(
Convert $request,
PaymentInterface $payment,
): void {
$request->getSource()->willReturn($payment);
$request->getTo()->willReturn('object');

$this->supports($request)->shouldReturn(false);
}

public function it_should_return_false_when_getTo_and_source_is_invalid(
Convert $request,
): void {
$request->getSource()->willReturn(null);
$request->getTo()->willReturn('object');

$this->supports($request)->shouldReturn(false);
}

public function it_should_return_false_when_request_invalid(
Request $request,
): void {
$this->supports($request)->shouldReturn(false);
}
}
Loading

0 comments on commit 0a670b3

Please sign in to comment.