Skip to content

Commit

Permalink
BRAIN-43 - application tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lernhart committed Dec 2, 2024
1 parent 7195943 commit c3f1239
Show file tree
Hide file tree
Showing 17 changed files with 390 additions and 13 deletions.
8 changes: 8 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
KERNEL_CLASS='Swag\Braintree\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999

###> braintree/gateway ###
BRAINTREE_TEST_MERCHANT_ACCOUNT_ID='your_merchant_account_id'
BRAINTREE_TEST_ENVIRONMENT='sandbox'
BRAINTREE_TEST_MERCHANT_ID='your_merchant_id'
BRAINTREE_TEST_PUBLIC_KEY='your_public_key'
BRAINTREE_TEST_PRIVATE_KEY='your_private_key'
###< braintree/gateway ###
30 changes: 30 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,36 @@ jobs:
with:
files: ./coverage.xml

phpunit-application:
runs-on: ubuntu-latest
services:
mariadb:
image: mariadb:10.5
env:
MARIADB_ROOT_PASSWORD: swagbraintree
MYSQL_DATABASE: swagbraintree_test
ports: [ '3306:3306' ]
env:
DATABASE_URL: mysql://root:[email protected]:3306/swagbraintree
BRAINTREE_TEST_ENVIRONMENT: sandbox
BRAINTREE_TEST_MERCHANT_ID: ${{ secrets.BRAINTREE_TEST_MERCHANT_ID }}
BRAINTREE_TEST_MERCHANT_ACCOUNT_ID: ${{ secrets.BRAINTREE_TEST_MERCHANT_ACCOUNT_ID }}
BRAINTREE_TEST_PUBLIC_KEY: ${{ secrets.BRAINTREE_TEST_PUBLIC_KEY }}
BRAINTREE_TEST_PRIVATE_KEY: ${{ secrets.BRAINTREE_TEST_PRIVATE_KEY }}
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup-php
- name: Setup database
run: composer setup:test
- name: Run PHPUnit
continue-on-error: true
run: composer phpunit:application -- --coverage-clover=coverage.xml
- name: Codecov
if: steps.phpunit-application.outcome == 'success' && steps.phpunit-application.conclusion == 'success'
uses: codecov/codecov-action@v4
with:
files: ./coverage.xml

infection:
runs-on: ubuntu-latest
services:
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"autoload-dev": {
"psr-4": {
"Swag\\Braintree\\Tests\\Unit\\": "tests/unit/",
"Swag\\Braintree\\Tests\\Integration\\": "tests/integration/"
"Swag\\Braintree\\Tests\\Integration\\": "tests/integration/",
"Swag\\Braintree\\Tests\\Application\\": "tests/application/"
}
},
"replace": {
Expand Down Expand Up @@ -93,7 +94,8 @@
"bin/console doctrine:migrations:migrate --env=test -n"
],
"phpunit:unit": "@phpunit --testsuite=SwagBraintreeUnitTest",
"phpunit:integration": "@phpunit --testsuite=SwagBraintreeIntegrationTest"
"phpunit:integration": "@phpunit --testsuite=SwagBraintreeIntegrationTest",
"phpunit:application": "@phpunit --testsuite=SwagBraintreeApplicationTest"
},
"conflict": {
"symfony/symfony": "*"
Expand Down
4 changes: 4 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@
->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');

$container->import(__DIR__ . '/services/braintree.xml', 'xml');

if ($container->env() === 'test') {
$container->import(__DIR__ . '/services/braintree_test.xml', 'xml');
}
};
27 changes: 27 additions & 0 deletions config/services/braintree_test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd"
>
<parameters>
<parameter key="BRAINTREE_TEST_MERCHANT_ACCOUNT_ID">%env(BRAINTREE_TEST_MERCHANT_ACCOUNT_ID)%</parameter>
<parameter key="BRAINTREE_TEST_MERCHANT_ID">%env(BRAINTREE_TEST_MERCHANT_ID)%</parameter>
<parameter key="BRAINTREE_TEST_ENVIRONMENT">%env(BRAINTREE_TEST_ENVIRONMENT)%</parameter>
<parameter key="BRAINTREE_TEST_PUBLIC_KEY">%env(BRAINTREE_TEST_PUBLIC_KEY)%</parameter>
<parameter key="BRAINTREE_TEST_PRIVATE_KEY">%env(BRAINTREE_TEST_PRIVATE_KEY)%</parameter>
</parameters>

<services>
<service id="Swag\Braintree\Tests\Braintree\Gateway\BraintreeTestGatewayFactory">
<argument>%env(BRAINTREE_TEST_ENVIRONMENT)%</argument>
<argument>%env(BRAINTREE_TEST_MERCHANT_ID)%</argument>
<argument>%env(BRAINTREE_TEST_PUBLIC_KEY)%</argument>
<argument>%env(BRAINTREE_TEST_PRIVATE_KEY)%</argument>
</service>

<service id="Braintree\Gateway">
<factory service="Swag\Braintree\Tests\Braintree\Gateway\BraintreeTestGatewayFactory" method="createBraintreeGateway"/>
</service>
</services>
</container>
7 changes: 4 additions & 3 deletions infection.json5
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"tmpDir": "var/cache",
"testFramework":"phpunit",
"testFrameworkOptions": "--testsuite=SwagBraintreeUnitTest,SwagBraintreeIntegrationTest",
"mutators": {
"@default": true,
"LessThan": {
Expand All @@ -30,17 +31,17 @@
},
"PlusEqual": {
"ignore": [
"Swag\\Braintree\\Braintree\\Payment\\OrderInformationService::extractDiscountAmount",
"Swag\\Braintree\\Braintree\\Payment\\OrderInformationService::extractDiscountAmount"
]
},
"GreaterThan": {
"ignore": [
"Swag\\Braintree\\Braintree\\Payment\\OrderInformationService::extractDiscountAmount",
"Swag\\Braintree\\Braintree\\Payment\\OrderInformationService::extractDiscountAmount"
]
},
"ArrayItem": {
"ignore": [
"Swag\\Braintree\\Braintree\\Payment\\BraintreePaymentService::handleTransaction",
"Swag\\Braintree\\Braintree\\Payment\\BraintreePaymentService::handleTransaction"
]
}
}
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<testsuite name="SwagBraintreeIntegrationTest">
<directory>tests/integration</directory>
</testsuite>

<testsuite name="SwagBraintreeApplicationTest">
<directory>tests/application</directory>
</testsuite>
</testsuites>

<extensions>
Expand Down
56 changes: 56 additions & 0 deletions src/Tests/ApplicationBootstrapTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types=1);

namespace Swag\Braintree\Tests;

use Doctrine\ORM\EntityManagerInterface;
use Swag\Braintree\Entity\CurrencyMappingEntity;
use Swag\Braintree\Entity\ShopEntity;
use Swag\Braintree\Repository\ConfigRepository;
use Swag\Braintree\Repository\CurrencyMappingRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;

trait ApplicationBootstrapTrait
{
abstract protected static function getContainer(): ContainerInterface;

public function registerShop(): ShopEntity
{
$container = static::getContainer();
$em = $container->get(EntityManagerInterface::class);

$shop = new ShopEntity(Ids::get('shop-id'), 'https://platform.dev.localhost', 'this-is-shop-secret');
$shop->setBraintreeMerchantId($container->getParameter('BRAINTREE_TEST_MERCHANT_ID'));
$shop->setBraintreePublicKey($container->getParameter('BRAINTREE_TEST_PUBLIC_KEY'));
$shop->setBraintreePrivateKey($container->getParameter('BRAINTREE_TEST_PRIVATE_KEY'));
$shop->setShopActive(true);
$shop->setShopApiCredentials('this-is-the-client-id', 'this-is-the-client-secret');
$shop->setBraintreeSandbox(true);

$em->persist($shop);
$em->flush();

static::getContainer()
->get(ConfigRepository::class)
->upsert([[
'salesChannelId' => null,
'threeDSecureEnforced' => true,
'shipsFromPostalCode' => '48268',
]], $shop);

$currencyMapping = new CurrencyMappingEntity();
$currencyMapping->setCurrencyId('EUR');
$currencyMapping->setMerchantAccountId($container->getParameter('BRAINTREE_TEST_MERCHANT_ACCOUNT_ID'));
$currencyMapping->setShop($shop);

static::getContainer()
->get(CurrencyMappingRepository::class)
->upsert([[
'salesChannelId' => null,
'currencyId' => Ids::get('currency-id'),
'currencyIso' => 'EUR',
'merchantAccountId' => $container->getParameter('BRAINTREE_TEST_MERCHANT_ACCOUNT_ID'),
]], $shop);

return $shop;
}
}
31 changes: 31 additions & 0 deletions src/Tests/Braintree/Gateway/BraintreeTestGatewayFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace Swag\Braintree\Tests\Braintree\Gateway;

use Braintree\Configuration;
use Braintree\Gateway;

class BraintreeTestGatewayFactory
{
public function __construct(
private readonly string $braintreeEnv,
private readonly string $braintreeMerchantId,
private readonly string $braintreePublicKey,
private readonly string $braintreePrivateKey,
) {
}

public function createBraintreeGateway(): Gateway
{
$configuration = new Configuration(
[
'environment' => $this->braintreeEnv,
'merchantId' => $this->braintreeMerchantId,
'publicKey' => $this->braintreePublicKey,
'privateKey' => $this->braintreePrivateKey,
]
);

return new Gateway($configuration);
}
}
5 changes: 4 additions & 1 deletion src/Tests/Contract/IntegrationHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

use Doctrine\ORM\EntityManagerInterface;
use Swag\Braintree\Entity\ShopEntity;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* @infection-ignore-all
*/
trait IntegrationHelperTrait
{
use IntegrationHelperTrait;
use ShopHelperTrait;

abstract protected static function getContainer(): ContainerInterface;

protected static function getEntityManager(): EntityManagerInterface
{
Expand Down
137 changes: 137 additions & 0 deletions src/Tests/Contract/OrderHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,141 @@ protected static function createOrderWithDiscount(): Order
'id' => Ids::get('order-discount-id'),
]);
}

private function createOrderForApplication(): Order
{
return new Order([
'orderNumber' => '10068',
'salesChannelId' => Ids::get('order-sales-channel-id'),
'price' => [
'netPrice' => 100,
'totalPrice' => 119,
'calculatedTaxes' => [['tax' => 19, 'taxRate' => 19, 'price' => 100]],
'taxRules' => [['taxRate' => 19, 'percentage' => 100]],
'positionPrice' => 119,
'taxStatus' => 'gross',
'rawTotal' => 100,
],
'amountTotal' => 119,
'amountNet' => 100,
'positionPrice' => 100,
'taxStatus' => 'gross',
'shippingTotal' => 0,
'shippingCosts' => [
'unitPrice' => 0,
'quantity' => 0,
'totalPrice' => 0,
'calculatedTaxes' => [['tax' => 19, 'taxRate' => 19, 'price' => 100]],
'taxRules' => [['taxRate' => 19, 'percentage' => 100]],
],
'orderCustomer' => [
'email' => '[email protected]',
'orderId' => Ids::get('order-id'),
'firstName' => 'Application',
'lastName' => 'Tester',
'title' => null,
'company' => 'shopware AG',
'customerNumber' => '1337',
'customerId' => Ids::get('order-customer-id'),
'id' => Ids::get('order-order-customer-id'),
],
'currency' => [
'isoCode' => 'EUR',
'symbol' => '',
'shortName' => 'EUR',
'name' => 'Euro',
'itemRounding' => ['decimals' => 2, 'interval' => 0.01, 'roundForNet' => true],
'totalRounding' => ['decimals' => 2, 'interval' => 0.01, 'roundForNet' => true],
'id' => Ids::get('currency-id'),
],
'billingAddress' => [
'firstName' => 'Application',
'lastName' => 'Tester',
'street' => 'Bahnhofstraße 27',
'zipcode' => '10332',
'city' => 'Berlin',
'company' => null,
'title' => null,
'additionalAddressLine1' => null,
'additionalAddressLine2' => null,
'country' => [
'name' => 'Haiti',
'iso' => 'HT',
'iso3' => 'HTI',
'id' => Ids::get('order-country-id'),
],
'id' => Ids::get('order-billing-address-id'),
],
'deliveries' => [[
'shippingCosts' => [
'unitPrice' => 0,
'quantity' => 0,
'totalPrice' => 0,
'calculatedTaxes' => [[
'tax' => 0,
'taxRate' => 0,
'price' => 0,
]],
],
'shippingOrderAddress' => [
'firstName' => 'Application',
'lastName' => 'Tester',
'street' => 'Ebbinghoff 10',
'zipcode' => '1234567890',
'city' => 'Schöppingen',
'company' => 'company',
'title' => null,
'additionalAddressLine1' => 'additionalAddressLine1',
'additionalAddressLine2' => null,
'country' => [
'name' => 'Hungary',
'iso' => 'HU',
'iso3' => 'HUN',
'id' => Ids::get('order-country-id'),
],
'countryState' => [
'name' => 'countryState',
],
'id' => Ids::get('order-shipping-address-id'),
],
'id' => Ids::get('order-delivery-id'),
]],
'lineItems' => [[
'quantity' => 1,
'unitPrice' => 100,
'totalPrice' => 100,
'label' => 'Product 1 - Application test',
'description' => 'Product description 1 from application test',
'good' => true,
'type' => 'product',
'referencedId' => 'product-1',
'payload' => [
'customFields' => [
OrderInformationService::LINE_ITEM_COMMODITY_CODE_CUSTOM_FIELD => '1234567890',
],
],
'price' => [
'unitPrice' => 100,
'quantity' => 1,
'totalPrice' => 100,
'calculatedTaxes' => [['tax' => 19, 'taxRate' => 19, 'price' => 100]],
'taxRules' => [['taxRate' => 19, 'percentage' => 100]],
],
'id' => Ids::get('order-line-item-id'),
]],
'transactions' => [[
'amount' => [
'unitPrice' => 100,
'quantity' => 1,
'totalPrice' => 100,
'calculatedTaxes' => [['tax' => 19, 'taxRate' => 10, 'price' => 100]],
'taxRules' => [['taxRate' => 19, 'percentage' => 100]],
],
'id' => Ids::get('order-discount-transaction-id'),
]],
'itemRounding' => ['decimals' => 2, 'interval' => 0.01, 'roundForNet' => true],
'totalRounding' => ['decimals' => 2, 'interval' => 0.01, 'roundForNet' => true],
'id' => Ids::get('order-discount-id'),
]);
}
}
Loading

0 comments on commit c3f1239

Please sign in to comment.