Skip to content

Commit

Permalink
Merge pull request #3 from Setono/custom-action-order-update
Browse files Browse the repository at this point in the history
Updating order with a custom controller action
  • Loading branch information
Zales0123 authored Jun 17, 2024
2 parents 9353eae + 2e795bd commit 609f8ea
Show file tree
Hide file tree
Showing 37 changed files with 887 additions and 247 deletions.
58 changes: 48 additions & 10 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
dependencies:
- "lowest"
- "highest"

symfony:
- "~5.4.0"
- "~6.4.0"
Expand All @@ -97,7 +97,7 @@ jobs:

- name: "Remove require-dev section in composer.json"
run: "composer config --unset require-dev"

- name: "Install composer dependencies"
uses: "ramsey/composer-install@v3"
env:
Expand All @@ -110,7 +110,7 @@ jobs:

- name: "Run composer-unused/composer-unused"
run: "composer-unused"

static-code-analysis:
name: "Static Code Analysis (PHP${{ matrix.php-version }} | Deps: ${{ matrix.dependencies }} | SF${{ matrix.symfony }})"

Expand Down Expand Up @@ -144,7 +144,7 @@ jobs:

- name: "Remove sylius/sylius from composer.json"
run: "composer remove --dev --no-update --no-plugins --no-scripts sylius/sylius"

- name: "Install composer dependencies"
uses: "ramsey/composer-install@v3"
env:
Expand All @@ -165,7 +165,7 @@ jobs:
php-version:
- "8.1"
- "8.2"

dependencies:
- "lowest"
- "highest"
Expand Down Expand Up @@ -194,7 +194,7 @@ jobs:
dependency-versions: "${{ matrix.dependencies }}"

- name: "Run phpunit"
run: "composer phpunit"
run: "composer unit-tests"

integration-tests:
name: "Integration tests (PHP${{ matrix.php-version }} | Deps: ${{ matrix.dependencies }} | SF${{ matrix.symfony }})"
Expand All @@ -208,9 +208,8 @@ jobs:
- "8.2"

dependencies:
- "lowest"
- "highest"

symfony:
- "~5.4.0"
- "~6.4.0"
Expand Down Expand Up @@ -248,7 +247,16 @@ jobs:

- name: "Validate Doctrine mapping"
run: "(cd tests/Application && bin/console doctrine:schema:validate -vvv)" # The verbose flag will show 'missing' SQL statements, if any


- name: "Load fixtures for functional tests"
run: "(cd tests/Application && bin/console sylius:fixtures:load --no-interaction)"

- name: "Run yarn"
run: "(cd tests/Application && yarn install && yarn build)"

- name: "Run phpunit"
run: "composer functional-tests"

mutation-tests:
name: "Mutation tests"

Expand All @@ -263,6 +271,9 @@ jobs:
- "highest"

steps:
- name: "Start MySQL"
run: "sudo /etc/init.d/mysql start"

- name: "Checkout"
uses: "actions/checkout@v4"

Expand All @@ -278,11 +289,23 @@ jobs:
with:
dependency-versions: "${{ matrix.dependencies }}"

- name: "Create database"
run: "(cd tests/Application && bin/console doctrine:database:create)"

- name: "Create database schema"
run: "(cd tests/Application && bin/console doctrine:schema:create)"

- name: "Load fixtures for functional tests"
run: "(cd tests/Application && bin/console sylius:fixtures:load --no-interaction)"

- name: "Run yarn"
run: "(cd tests/Application && yarn install && yarn build)"

- name: "Run infection"
run: "vendor/bin/infection"
env:
STRYKER_DASHBOARD_API_KEY: "${{ secrets.STRYKER_DASHBOARD_API_KEY }}"

code-coverage:
name: "Code Coverage (PHP${{ matrix.php-version }} | Deps: ${{ matrix.dependencies }})"

Expand All @@ -297,6 +320,9 @@ jobs:
- "highest"

steps:
- name: "Start MySQL"
run: "sudo /etc/init.d/mysql start"

- name: "Checkout"
uses: "actions/checkout@v4"

Expand All @@ -315,6 +341,18 @@ jobs:
with:
dependency-versions: "${{ matrix.dependencies }}"

- name: "Create database"
run: "(cd tests/Application && bin/console doctrine:database:create)"

- name: "Create database schema"
run: "(cd tests/Application && bin/console doctrine:schema:create)"

- name: "Load fixtures for functional tests"
run: "(cd tests/Application && bin/console sylius:fixtures:load --no-interaction)"

- name: "Run yarn"
run: "(cd tests/Application && yarn install && yarn build)"

- name: "Collect code coverage with pcov and phpunit/phpunit"
run: "vendor/bin/phpunit --coverage-clover=.build/logs/clover.xml"

Expand Down
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
],
"require": {
"php": ">=8.1",
"doctrine/orm": "^2.0 || ^3.0",
"doctrine/persistence": "^2.0 || ^3.0",
"sylius/core": "^1.0",
"sylius/core-bundle": "^1.0",
Expand All @@ -18,11 +19,13 @@
"sylius/resource-bundle": "^1.6",
"symfony/config": "^5.4 || ^6.4 || ^7.0",
"symfony/dependency-injection": "^5.4 || ^6.4 || ^7.0",
"symfony/doctrine-bridge": "^5.4 || ^6.3 || ^7.0",
"symfony/event-dispatcher": "^5.4 || ^6.4 || ^7.0",
"symfony/form": "^5.4 || ^6.4 || ^7.0",
"symfony/http-foundation": "^5.4 || ^6.4 || ^7.0",
"symfony/http-kernel": "^5.4 || ^6.4 || ^7.0",
"symfony/options-resolver": "^5.4 || ^6.4 || ^7.0",
"symfony/routing": "^5.4 || ^6.4 || ^7.0",
"webmozart/assert": "^1.11"
},
"require-dev": {
Expand All @@ -34,10 +37,12 @@
"jms/serializer-bundle": "^4.2",
"lexik/jwt-authentication-bundle": "^2.17",
"matthiasnoback/symfony-dependency-injection-test": "^4.3 || ^5.1",
"phpspec/prophecy-phpunit": "^2.2",
"phpunit/phpunit": "^9.6.17",
"psalm/plugin-phpunit": "^0.18.4",
"setono/code-quality-pack": "^2.7",
"sylius/sylius": "~1.12.13",
"symfony/browser-kit": "^5.4 || ^6.4 || ^7.0",
"symfony/debug-bundle": "^5.4 || ^6.4 || ^7.0",
"symfony/dotenv": "^5.4 || ^6.4 || ^7.0",
"symfony/intl": "^5.4 || ^6.4 || ^7.0",
Expand All @@ -48,6 +53,10 @@
"symfony/workflow": "^6.4",
"willdurand/negotiation": "^3.1"
},
"conflict": {
"doctrine/annotations": "<1.13.2",
"doctrine/data-fixtures": "<1.5.1"
},
"prefer-stable": true,
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -80,6 +89,7 @@
"analyse": "psalm",
"check-style": "ecs check",
"fix-style": "ecs check --fix",
"phpunit": "phpunit"
"functional-tests": "phpunit tests/Functional/",
"unit-tests": "phpunit tests/Unit/"
}
}
6 changes: 3 additions & 3 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"source": {
"directories": [
"src"
]
],
},
"logs": {
"text": "php://stderr",
Expand All @@ -11,6 +11,6 @@
"badge": "master"
}
},
"minMsi": 100.00,
"minCoveredMsi": 100.00
"minMsi": 70.00,
"minCoveredMsi": 70.00,
}
7 changes: 5 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
</include>
</coverage>
<testsuites>
<testsuite name="SetonoSyliusOrderEditPlugin Test Suite">
<directory>tests</directory>
<testsuite name="unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="functional">
<directory>tests/Functional</directory>
</testsuite>
</testsuites>
<php>
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<directory name="tests"/>
<ignoreFiles>
<directory name="tests/Application"/>
<directory name="tests/Functional"/>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
Expand Down
18 changes: 18 additions & 0 deletions src/Checker/PostUpdateChangesChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Checker;

use Setono\SyliusOrderEditPlugin\Exception\NewOrderWrongTotalException;
use Sylius\Component\Core\Model\OrderInterface;

final class PostUpdateChangesChecker implements PostUpdateChangesCheckerInterface
{
public function check(OrderInterface $previousOrder, OrderInterface $newOrder): void
{
if ($newOrder->getTotal() > $previousOrder->getTotal()) {
throw new NewOrderWrongTotalException();
}
}
}
12 changes: 12 additions & 0 deletions src/Checker/PostUpdateChangesCheckerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Checker;

use Sylius\Component\Core\Model\OrderInterface;

interface PostUpdateChangesCheckerInterface
{
public function check(OrderInterface $previousOrder, OrderInterface $newOrder): void;
}
82 changes: 82 additions & 0 deletions src/Controller/EditOrderAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusOrderEditPlugin\Controller;

use Doctrine\ORM\EntityManagerInterface;
use Setono\SyliusOrderEditPlugin\Checker\PostUpdateChangesCheckerInterface;
use Setono\SyliusOrderEditPlugin\Exception\NewOrderWrongTotalException;
use Setono\SyliusOrderEditPlugin\Preparer\OrderPreparerInterface;
use Setono\SyliusOrderEditPlugin\Processor\UpdatedOrderProcessorInterface;
use Setono\SyliusOrderEditPlugin\Provider\UpdatedOrderProviderInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class EditOrderAction
{
public function __construct(
private readonly OrderPreparerInterface $oldOrderProvider,
private readonly UpdatedOrderProviderInterface $updatedOrderProvider,
private readonly UpdatedOrderProcessorInterface $updatedOrderProcessor,
private readonly PostUpdateChangesCheckerInterface $postUpdateChangesChecker,
private readonly UrlGeneratorInterface $router,
private readonly RequestStack $requestStack,
private readonly EntityManagerInterface $entityManager,
) {
}

public function __invoke(Request $request, int $id): Response
{
$order = $this->oldOrderProvider->prepareToUpdate($id);

$oldOrder = clone $order;
$updatedOrder = $this->updatedOrderProvider->provideFromOldOrderAndRequest($order, $request);

try {
$this->updatedOrderProcessor->process($updatedOrder);
$this->postUpdateChangesChecker->check($oldOrder, $updatedOrder);
$this->entityManager->flush();
} catch (NewOrderWrongTotalException) {
return $this->addFlashAndRedirect(
'error',
'setono_sylius_order_edit.order_update.total_error',
'sylius_admin_order_update',
$id,
);
} catch (\Throwable) {
return $this->addFlashAndRedirect(
'error',
'setono_sylius_order_edit.order_update.general_error',
'sylius_admin_order_update',
$id,
);
}

return $this->addFlashAndRedirect(
'success',
'setono_sylius_order_edit.order_update.success',
'sylius_admin_order_show',
$id,
);
}

private function addFlashAndRedirect(
string $type,
string $message,
string $route,
int $orderId,
): RedirectResponse {
$session = $this->requestStack->getSession();

if ($session instanceof Session) {
$session->getFlashBag()->add($type, $message);
}

return new RedirectResponse($this->router->generate($route, ['id' => $orderId]));
}
}
Loading

0 comments on commit 609f8ea

Please sign in to comment.