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

Save credit memos on server during generation #373

Merged
merged 15 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
!/etc/build/.gitkeep

/tests/Application/yarn.lock
/tests/Application/private/credit_memos/*

phpspec.yml

Expand Down
23 changes: 23 additions & 0 deletions UPGRADE-1.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### UPGRADE FROM 1.2.X TO 1.3.0

1. Not passing the `$creditMemoFileNameGenerator` to `Sylius\RefundPlugin\Generator\CreditMemoPdfFileGenerator` constructor
is deprecated since 1.3 version and will be prohibited in 2.0.

1. Not passing the `$creditMemoPdfFileGenerator`, the `$creditMemoFileManager` and the `$hasEnabledPdfFileGenerator`
to `Sylius\RefundPlugin\CommandHandler\GenerateCreditMemoHandler` constructor is deprecated since 1.3 version and will be prohibited in 2.0.

1. Not passing the `$creditMemoFileProvider` to `Sylius\RefundPlugin\Sender\CreditMemoEmailSender` constructor
is deprecated since 1.3 version and will be prohibited in 2.0.

1. The first argument of `Sylius\RefundPlugin\Action\Admin\DownloadCreditMemoAction` and `Sylius\RefundPlugin\Action\Shop\DownloadCreditMemoAction`
controllers has been changed:

```php
public function __construct(
- private CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator,
+ private CreditMemoFileProviderInterface $creditMemoFileProvider,
//...
) {
//...
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@refunds
Feature: Saving credit memos on server during generation
In order to keep credit memos immutable for further usage
As a Store Owner
I want the credit memos to be saved on a server during generation

Background:
Given the store operates on a single channel in "United States"
And the store has a product "PHP T-Shirt" priced at "$10.00"
And the store allows shipping with "Galaxy Post"
And the store allows paying with "Space money"
And there is a customer "rick.sanchez@wubba-lubba-dub-dub.com" that placed an order "#00000022"
And the customer bought a single "PHP T-Shirt"
And the customer chose "Galaxy Post" shipping method to "United States" with "Space money" payment
And the order "#00000022" is already paid
And I am logged in as an administrator

@application
Scenario: Having credit memo saved on the server after the refund is made
When I refund all units of "#00000022" order with "Space money" payment method
Then the credit memo for "#00000022" order should be saved on the server
128 changes: 125 additions & 3 deletions spec/CommandHandler/GenerateCreditMemoHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use Sylius\RefundPlugin\Entity\CreditMemoInterface;
use Sylius\RefundPlugin\Event\CreditMemoGenerated;
use Sylius\RefundPlugin\Generator\CreditMemoGeneratorInterface;
use Sylius\RefundPlugin\Generator\CreditMemoPdfFileGeneratorInterface;
use Sylius\RefundPlugin\Manager\CreditMemoFileManagerInterface;
use Sylius\RefundPlugin\Model\CreditMemoPdf;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\ShipmentRefund;
use Symfony\Component\Messenger\Envelope;
Expand All @@ -32,16 +35,28 @@ function let(
CreditMemoGeneratorInterface $creditMemoGenerator,
ObjectManager $creditMemoManager,
MessageBusInterface $eventBus,
OrderRepositoryInterface $orderRepository
OrderRepositoryInterface $orderRepository,
CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator,
CreditMemoFileManagerInterface $creditMemoFileManager,
) {
$this->beConstructedWith($creditMemoGenerator, $creditMemoManager, $eventBus, $orderRepository);
$this->beConstructedWith(
$creditMemoGenerator,
$creditMemoManager,
$eventBus,
$orderRepository,
$creditMemoPdfFileGenerator,
$creditMemoFileManager,
true
);
}

function it_generates_credit_memo(
function it_generates_credit_memo_with_a_pdf_file(
CreditMemoGeneratorInterface $creditMemoGenerator,
ObjectManager $creditMemoManager,
MessageBusInterface $eventBus,
OrderRepositoryInterface $orderRepository,
CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator,
CreditMemoFileManagerInterface $creditMemoFileManager,
CreditMemoInterface $creditMemo,
OrderInterface $order
): void {
Expand All @@ -53,13 +68,120 @@ function it_generates_credit_memo(
$creditMemoGenerator->generate($order, 7000, $orderItemUnitRefunds, $shipmentRefunds, 'Comment')->willReturn($creditMemo);

$creditMemo->getNumber()->willReturn('2018/01/000001');
$creditMemo->getId()->willReturn('7903c83a-4c5e-4bcf-81d8-9dc304c6a353');

$creditMemoManager->persist($creditMemo)->shouldBeCalled();
$creditMemoManager->flush()->shouldBeCalled();

$creditMemoPdf = new CreditMemoPdf('credit_memo.pdf', 'CONTENT');
$creditMemoPdfFileGenerator->generate('7903c83a-4c5e-4bcf-81d8-9dc304c6a353')->willReturn($creditMemoPdf);
$creditMemoFileManager->save($creditMemoPdf)->shouldBeCalled();

$event = new CreditMemoGenerated('2018/01/000001', '000666');
$eventBus->dispatch($event)->willReturn(new Envelope($event))->shouldBeCalled();

$this(new GenerateCreditMemo('000666', 7000, $orderItemUnitRefunds, $shipmentRefunds, 'Comment'));
}

function it_generates_only_credit_memo_without_a_pdf_file(
CreditMemoGeneratorInterface $creditMemoGenerator,
ObjectManager $creditMemoManager,
MessageBusInterface $eventBus,
OrderRepositoryInterface $orderRepository,
CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator,
CreditMemoInterface $creditMemo,
OrderInterface $order
): void {
$this->beConstructedWith(
$creditMemoGenerator,
$creditMemoManager,
$eventBus,
$orderRepository,
);

$orderItemUnitRefunds = [new OrderItemUnitRefund(1, 1000), new OrderItemUnitRefund(3, 2000), new OrderItemUnitRefund(5, 3000)];
$shipmentRefunds = [new ShipmentRefund(3, 1000)];

$orderRepository->findOneByNumber('000666')->willReturn($order);

$creditMemoGenerator->generate($order, 7000, $orderItemUnitRefunds, $shipmentRefunds, 'Comment')->willReturn($creditMemo);

$creditMemo->getNumber()->willReturn('2018/01/000001');
$creditMemo->getId()->willReturn('7903c83a-4c5e-4bcf-81d8-9dc304c6a353');

$creditMemoManager->persist($creditMemo)->shouldBeCalled();
$creditMemoManager->flush()->shouldBeCalled();

$creditMemoPdfFileGenerator->generate('7903c83a-4c5e-4bcf-81d8-9dc304c6a353')->shouldNotBeCalled();

$event = new CreditMemoGenerated('2018/01/000001', '000666');
$eventBus->dispatch($event)->willReturn(new Envelope($event))->shouldBeCalled();

$this(new GenerateCreditMemo('000666', 7000, $orderItemUnitRefunds, $shipmentRefunds, 'Comment'));
}

function it_generates_only_credit_memo_without_a_pdf_file_if_pdf_generation_is_disabled(
CreditMemoGeneratorInterface $creditMemoGenerator,
ObjectManager $creditMemoManager,
MessageBusInterface $eventBus,
OrderRepositoryInterface $orderRepository,
CreditMemoPdfFileGeneratorInterface $creditMemoPdfFileGenerator,
CreditMemoFileManagerInterface $creditMemoFileManager,
CreditMemoInterface $creditMemo,
OrderInterface $order
): void {
$this->beConstructedWith(
$creditMemoGenerator,
$creditMemoManager,
$eventBus,
$orderRepository,
$creditMemoPdfFileGenerator,
$creditMemoFileManager,
false
);

$orderItemUnitRefunds = [new OrderItemUnitRefund(1, 1000), new OrderItemUnitRefund(3, 2000), new OrderItemUnitRefund(5, 3000)];
$shipmentRefunds = [new ShipmentRefund(3, 1000)];

$orderRepository->findOneByNumber('000666')->willReturn($order);

$creditMemoGenerator->generate($order, 7000, $orderItemUnitRefunds, $shipmentRefunds, 'Comment')->willReturn($creditMemo);

$creditMemo->getNumber()->willReturn('2018/01/000001');
$creditMemo->getId()->willReturn('7903c83a-4c5e-4bcf-81d8-9dc304c6a353');

$creditMemoManager->persist($creditMemo)->shouldBeCalled();
$creditMemoManager->flush()->shouldBeCalled();

$creditMemoPdfFileGenerator->generate('7903c83a-4c5e-4bcf-81d8-9dc304c6a353')->shouldNotBeCalled();

$event = new CreditMemoGenerated('2018/01/000001', '000666');
$eventBus->dispatch($event)->willReturn(new Envelope($event))->shouldBeCalled();

$this(new GenerateCreditMemo('000666', 7000, $orderItemUnitRefunds, $shipmentRefunds, 'Comment'));
}

function it_deprecates_not_passing_credit_memo_pdf_file_generator_and_manager(
CreditMemoGeneratorInterface $creditMemoGenerator,
ObjectManager $creditMemoManager,
MessageBusInterface $eventBus,
OrderRepositoryInterface $orderRepository,
): void {
$this->beConstructedWith(
$creditMemoGenerator,
$creditMemoManager,
$eventBus,
$orderRepository,
);

$this->shouldTrigger(
\E_USER_DEPRECATED,
'Not passing a $creditMemoPdfFileGenerator to Sylius\RefundPlugin\CommandHandler\GenerateCreditMemoHandler constructor is deprecated since sylius/refund-plugin 1.3 and will be prohibited in 2.0.'
)->duringInstantiation();

$this->shouldTrigger(
\E_USER_DEPRECATED,
'Not passing a $creditMemoFileManager to Sylius\RefundPlugin\CommandHandler\GenerateCreditMemoHandler constructor is deprecated since sylius/refund-plugin 1.3 and will be prohibited in 2.0.'
)->duringInstantiation();
}
}
33 changes: 33 additions & 0 deletions spec/Generator/CreditMemoFileNameGeneratorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\RefundPlugin\Generator;

use PhpSpec\ObjectBehavior;
use Sylius\RefundPlugin\Entity\CreditMemoInterface;
use Sylius\RefundPlugin\Generator\CreditMemoFileNameGeneratorInterface;

final class CreditMemoFileNameGeneratorSpec extends ObjectBehavior
{
function it_implements_credit_memo_file_name_generator_interface(): void
{
$this->shouldImplement(CreditMemoFileNameGeneratorInterface::class);
}

function it_generates_credit_memo_file_name_based_on_its_number(CreditMemoInterface $creditMemo): void
{
$creditMemo->getNumber()->willReturn('2018/05/000000006');

$this->generateForPdf($creditMemo)->shouldReturn('2018_05_000000006.pdf');
}
}
36 changes: 31 additions & 5 deletions spec/Generator/CreditMemoPdfFileGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\RefundPlugin\Entity\CreditMemoInterface;
use Sylius\RefundPlugin\Exception\CreditMemoNotFound;
use Sylius\RefundPlugin\Generator\CreditMemoFileNameGeneratorInterface;
use Sylius\RefundPlugin\Generator\CreditMemoPdfFileGeneratorInterface;
use Sylius\RefundPlugin\Generator\PdfOptionsGeneratorInterface;
use Sylius\RefundPlugin\Generator\TwigToPdfGeneratorInterface;
Expand All @@ -30,7 +31,8 @@ final class CreditMemoPdfFileGeneratorSpec extends ObjectBehavior
function let(
RepositoryInterface $creditMemoRepository,
FileLocatorInterface $fileLocator,
TwigToPdfGeneratorInterface $twigToPdfGenerator
TwigToPdfGeneratorInterface $twigToPdfGenerator,
CreditMemoFileNameGeneratorInterface $creditMemoFileNameGenerator,
): void {
$this->beConstructedWith(
$creditMemoRepository,
Expand All @@ -40,7 +42,8 @@ function let(
'creditMemoTemplate.html.twig',
'@SyliusRefundPlugin/Resources/assets/sylius-logo.png',
null,
$twigToPdfGenerator
$twigToPdfGenerator,
$creditMemoFileNameGenerator,
);
}

Expand All @@ -49,15 +52,18 @@ function it_implements_credit_memo_pdf_file_generator_interface(): void
$this->shouldImplement(CreditMemoPdfFileGeneratorInterface::class);
}

function it_creates_credit_memo_pdf_with_generated_content_and_filename_basing_on_credit_memo_number(
function it_creates_credit_memo_pdf_with_generated_content_and_file_name_basing_on_credit_memo_number(
RepositoryInterface $creditMemoRepository,
FileLocatorInterface $fileLocator,
TwigToPdfGeneratorInterface $twigToPdfGenerator,
CreditMemoFileNameGeneratorInterface $creditMemoFileNameGenerator,
CreditMemoInterface $creditMemo,
TwigToPdfGeneratorInterface $twigToPdfGenerator
): void {
$creditMemoRepository->find('7903c83a-4c5e-4bcf-81d8-9dc304c6a353')->willReturn($creditMemo);
$creditMemo->getNumber()->willReturn('2015/05/00004444');

$creditMemoFileNameGenerator->generateForPdf($creditMemo)->willReturn('2015_05_00004444.pdf');

$fileLocator
->locate('@SyliusRefundPlugin/Resources/assets/sylius-logo.png')
->willReturn('located-path/sylius-logo.png')
Expand Down Expand Up @@ -89,7 +95,6 @@ function it_deprecates_not_passing_twig_to_pdf_generator(
RepositoryInterface $creditMemoRepository,
Environment $twig,
GeneratorInterface $pdfGenerator,
CreditMemoInterface $creditMemo,
FileLocatorInterface $fileLocator
): void {
$this->beConstructedWith(
Expand All @@ -104,6 +109,27 @@ function it_deprecates_not_passing_twig_to_pdf_generator(
$this->shouldTrigger(\E_USER_DEPRECATED, 'Not passing a $twigToPdfGenerator to Sylius\RefundPlugin\Generator\CreditMemoPdfFileGenerator constructor is deprecated since sylius/refund-plugin 1.2 and will be prohibited in 2.0.')->duringInstantiation();
}

function it_deprecates_not_passing_credit_memo_file_name_generator(
RepositoryInterface $creditMemoRepository,
Environment $twig,
GeneratorInterface $pdfGenerator,
FileLocatorInterface $fileLocator,
TwigToPdfGeneratorInterface $twigToPdfGenerator,
): void {
$this->beConstructedWith(
$creditMemoRepository,
$twig,
$pdfGenerator,
$fileLocator,
'creditMemoTemplate.html.twig',
'@SyliusRefundPlugin/Resources/assets/sylius-logo.png',
null,
$twigToPdfGenerator,
);

$this->shouldTrigger(\E_USER_DEPRECATED, 'Not passing a $creditMemoFileNameGenerator to Sylius\RefundPlugin\Generator\CreditMemoPdfFileGenerator constructor is deprecated since sylius/refund-plugin 1.3 and will be prohibited in 2.0.')->duringInstantiation();
}

function it_deprecates_passing_pdf_options_generator(
RepositoryInterface $creditMemoRepository,
Environment $twig,
Expand Down
Loading