Skip to content

Commit

Permalink
Fix "example.pdf" to "invoice.pdf" as invoice filename for attachment
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Sep 28, 2024
1 parent 2a325bf commit be5a2c8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 134 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [v3.0.1] - 2024-09-28

### Fixed
- Fix the issue with the invoice filename in the email - now its "invoice.pdf" instead of "example.pdf"

## [v3.0.0] - 2024-09-26

### Added
Expand Down Expand Up @@ -83,6 +88,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- PDF file generated from order data with possibility to adjust some of the fields and regenerate the invoice file
- Invoice generated in Shop's main language (if translation available)

[v3.0.1]: https://github.com/Fresh-Advance/Invoice/compare/v3.0.0...v3.0.1
[v3.0.0]: https://github.com/Fresh-Advance/Invoice/compare/v2.2.0...v3.0.0
[v2.2.0]: https://github.com/Fresh-Advance/Invoice/compare/v2.1.0...v2.2.0
[v2.1.0]: https://github.com/Fresh-Advance/Invoice/compare/v2.0.0...v2.1.0
[v2.0.0]: https://github.com/Fresh-Advance/Invoice/compare/v1.3.0...v2.0.0
Expand Down
2 changes: 1 addition & 1 deletion metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
'en' => 'Invoice module for OXID eShop.',
],
'thumbnail' => 'logo.png',
'version' => '3.0.0',
'version' => '3.0.1',
'author' => 'Anton Fedurtsya',
'email' => '[email protected]',
'url' => 'https://github.com/Fresh-Advance',
Expand Down
2 changes: 1 addition & 1 deletion src/Transition/Core/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function send()
if ($this->attachInvoice) {
$this->addAttachment(
path: $this->attachInvoice,
name: 'example.pdf',
name: 'invoice.pdf',
);
$this->attachInvoice = null;
}
Expand Down
191 changes: 59 additions & 132 deletions tests/Integration/Transition/Core/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,178 +10,105 @@
namespace FreshAdvance\Invoice\Tests\Integration\Transition\Core;

use FreshAdvance\Invoice\DataType\InvoiceDataInterface;
use FreshAdvance\Invoice\Document\InvoiceGeneratorInterface;
use FreshAdvance\Invoice\Service\Invoice;
use FreshAdvance\Invoice\Settings\ModuleSettingsInterface;
use FreshAdvance\Invoice\Transition\Core\Email;
use OxidEsales\Eshop\Application\Model\Order as OrderModel;
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase;

/** @covers \FreshAdvance\Invoice\Transition\Core\Email */
class EmailTest extends IntegrationTestCase
{
public function testInvoiceGeneratedDuringSendOrderEmailToUserMethodWithOptionOn(): void
public function testInvoiceGeneratedAndAttachedWithOptionOn(): void
{
$sut = $this->createPartialMock(
Email::class,
['faCallParentSendOrderEmailToUser', 'getServiceFromContainer']
['faCallParentSend', 'faCallParentSendOrderEmailToUser', 'getServiceFromContainer', 'addAttachment']
);
$sut->method('faCallParentSendOrderEmailToUser')->willReturn($parentOrderEmailSendResult = uniqid());
$sut->method('getServiceFromContainer')->willReturnMap([
[
\FreshAdvance\Invoice\Service\Invoice::class,
$invoiceDataService = $this->createMock(\FreshAdvance\Invoice\Service\Invoice::class)
],
[
\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class,
$generatorSpy = $this->createMock(\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class)
],
[
ModuleSettingsInterface::class,
$this->createConfiguredMock(ModuleSettingsInterface::class, [
'isSendInvoiceOnUserOrderEmailActive' => true
])
]
]);

$orderStub = $this->createConfiguredMock(\OxidEsales\Eshop\Application\Model\Order::class, [
'getId' => $orderId = uniqid()
]);
$sut->method('getServiceFromContainer')->willReturnMap(
$this->getDIConfiguration(
invoiceDataService: $invoiceDataService = $this->createMock(Invoice::class),
invoiceGenerator: $invoiceGeneratorSpy = $this->createMock(InvoiceGeneratorInterface::class),
moduleSettings: $this->createConfiguredMock(ModuleSettingsInterface::class, [
'isSendInvoiceOnUserOrderEmailActive' => true
]),
)
);

$order = $this->createConfiguredMock(OrderModel::class, ['getId' => $orderId = uniqid()]);
$invoiceDataService->method('getInvoiceDataByOrderId')
->with($orderId)
->willReturn(
$invoiceData = $this->createMock(InvoiceDataInterface::class)
$invoiceDataStub = $this->createConfiguredMock(InvoiceDataInterface::class, [
'getInvoicePath' => $invoicePath = uniqid()
])
);

$generatorSpy->expects($this->once())->method('generate')->with($invoiceData);

$orderEmailResult = $sut->sendOrderEmailToUser($orderStub, "some subject");

$this->assertSame($parentOrderEmailSendResult, $orderEmailResult);
}
$invoiceGeneratorSpy->expects($this->once())->method('generate')->with($invoiceDataStub);
$sut->method('faCallParentSendOrderEmailToUser')
->with($order, $emailSubject = uniqid())
->willReturn($parentOrderEmailSendResult = uniqid());
$this->assertSame($parentOrderEmailSendResult, $sut->sendOrderEmailToUser($order, $emailSubject));

public function testInvoiceNotGeneratedDuringSendOrderEmailToUserMethodWithOptionOff(): void
{
$sut = $this->createPartialMock(
Email::class,
['faCallParentSendOrderEmailToUser', 'getServiceFromContainer']
);
$sut->method('faCallParentSendOrderEmailToUser')->willReturn($parentOrderEmailSendResult = uniqid());
$sut->method('getServiceFromContainer')->willReturnMap([
[
\FreshAdvance\Invoice\Service\Invoice::class,
$invoiceDataServiceSpy = $this->createMock(\FreshAdvance\Invoice\Service\Invoice::class)
],
[
\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class,
$generatorSpy = $this->createMock(\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class)
],
[
ModuleSettingsInterface::class,
$this->createConfiguredMock(ModuleSettingsInterface::class, [
'isSendInvoiceOnUserOrderEmailActive' => false
])
],
]);

$invoiceDataServiceSpy->expects($this->never())->method('getInvoiceDataByOrderId');
$generatorSpy->expects($this->never())->method('generate');
$sut->expects($this->once())->method('addAttachment')
->with($invoicePath, 'invoice.pdf');
$sut->method('faCallParentSend')->willReturn($parentSendResult = uniqid());

$orderStub = $this->createStub(\OxidEsales\Eshop\Application\Model\Order::class);
$orderEmailResult = $sut->sendOrderEmailToUser($orderStub, "some subject");
$this->assertSame($parentSendResult, $sut->send());

$this->assertSame($parentOrderEmailSendResult, $orderEmailResult);
// check second send will not trigger the attachment again
$sut->send();
}

public function testInvoiceAttachedIfGeneratedDuringSendOrderEmailMethodWithOptionOn(): void
public function testInvoiceNotGeneratedAndNotAttachedWithOptionOff(): void
{
$sut = $this->createPartialMock(
Email::class,
['faCallParentSend', 'faCallParentSendOrderEmailToUser', 'getServiceFromContainer', 'addAttachment']
);
$sut->method('faCallParentSend')->willReturn($parentSendResult = uniqid());
$sut->method('getServiceFromContainer')->willReturnMap([
[
\FreshAdvance\Invoice\Service\Invoice::class,
$invoiceDataService = $this->createMock(\FreshAdvance\Invoice\Service\Invoice::class)
],
[
\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class,
$this->createMock(\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class)
],
[
ModuleSettingsInterface::class,
$this->createConfiguredMock(ModuleSettingsInterface::class, [
'isSendInvoiceOnUserOrderEmailActive' => true
])
]
]);

$order = $this->createConfiguredMock(\OxidEsales\Eshop\Application\Model\Order::class, [
'getId' => $orderId = uniqid()
]);

$invoiceDataService->method('getInvoiceDataByOrderId')
->with($orderId)
->willReturn(
$this->createConfiguredMock(InvoiceDataInterface::class, [
'getInvoicePath' => $invoicePath = 'example.pdf'
$sut->method('getServiceFromContainer')->willReturnMap(
$this->getDIConfiguration(
invoiceGenerator: $invoiceGeneratorSpy = $this->createMock(InvoiceGeneratorInterface::class),
moduleSettings: $this->createConfiguredMock(ModuleSettingsInterface::class, [
'isSendInvoiceOnUserOrderEmailActive' => false
])
);
)
);

$sut->sendOrderEmailToUser($order, "some subject");
$invoiceGeneratorSpy->expects($this->never())->method('generate');

$sut->expects($this->once())->method('addAttachment')->with(
$invoicePath,
'example.pdf'
);
$order = $this->createConfiguredMock(OrderModel::class, ['getId' => uniqid()]);

$sendResult = $sut->send();
$this->assertSame($parentSendResult, $sendResult);
$sut->method('faCallParentSendOrderEmailToUser')
->with($order, $emailSubject = uniqid())
->willReturn($parentOrderEmailSendResult = uniqid());
$this->assertSame($parentOrderEmailSendResult, $sut->sendOrderEmailToUser($order, $emailSubject));

// check second send will not trigger the attachment again
$sut->send();
$sut->method('faCallParentSend')->willReturn($parentSendResult = uniqid());
$sut->expects($this->never())->method('addAttachment');
$this->assertSame($parentSendResult, $sut->send());
}

public function testInvoiceNotAttachedIfGeneratedDuringSendOrderEmailMethodWithOptionOff(): void
{
$sut = $this->createPartialMock(
Email::class,
['faCallParentSend', 'faCallParentSendOrderEmailToUser', 'getServiceFromContainer', 'addAttachment']
);
$sut->method('faCallParentSend')->willReturn($parentSendResult = uniqid());
$sut->method('getServiceFromContainer')->willReturnMap([
protected function getDIConfiguration(
Invoice $invoiceDataService = null,
InvoiceGeneratorInterface $invoiceGenerator = null,
ModuleSettingsInterface $moduleSettings = null,
): array {
return [
[
\FreshAdvance\Invoice\Service\Invoice::class,
$invoiceDataService = $this->createMock(\FreshAdvance\Invoice\Service\Invoice::class)
Invoice::class,
$invoiceDataService ?? $this->createStub(Invoice::class)
],
[
\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class,
$this->createMock(\FreshAdvance\Invoice\Document\InvoiceGeneratorInterface::class)
InvoiceGeneratorInterface::class,
$invoiceGenerator ?? $this->createStub(InvoiceGeneratorInterface::class)
],
[
ModuleSettingsInterface::class,
$this->createConfiguredMock(ModuleSettingsInterface::class, [
'isSendInvoiceOnUserOrderEmailActive' => false
])
]
]);

$order = $this->createConfiguredMock(\OxidEsales\Eshop\Application\Model\Order::class, [
'getId' => $orderId = uniqid()
]);

$invoiceDataService->method('getInvoiceDataByOrderId')
->with($orderId)
->willReturn(
$this->createConfiguredMock(InvoiceDataInterface::class, [
'getInvoicePath' => $invoicePath = 'example.pdf'
])
);

$sut->sendOrderEmailToUser($order, "some subject");

$sut->expects($this->never())->method('addAttachment');

$sendResult = $sut->send();
$this->assertSame($parentSendResult, $sendResult);
$moduleSettings ?? $this->createStub(ModuleSettingsInterface::class)
],
];
}
}

0 comments on commit be5a2c8

Please sign in to comment.