ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Sales\Model\Service;
|
||||
|
||||
use Magento\Sales\Api\Data\OrderInterface;
|
||||
use Magento\Sales\Model\Order;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
/**
|
||||
* Tests \Magento\Sales\Model\Service\InvoiceService
|
||||
*/
|
||||
class InvoiceServiceTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var InvoiceService
|
||||
*/
|
||||
private $invoiceService;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->invoiceService = Bootstrap::getObjectManager()->create(InvoiceService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $invoiceQty
|
||||
* @magentoDataFixture Magento/Sales/_files/order_configurable_product.php
|
||||
* @return void
|
||||
* @dataProvider prepareInvoiceConfigurableProductDataProvider
|
||||
*/
|
||||
public function testPrepareInvoiceConfigurableProduct(int $invoiceQty): void
|
||||
{
|
||||
/** @var OrderInterface $order */
|
||||
$order = Bootstrap::getObjectManager()->create(Order::class)->load('100000001', 'increment_id');
|
||||
$orderItems = $order->getItems();
|
||||
foreach ($orderItems as $orderItem) {
|
||||
if ($orderItem->getParentItemId()) {
|
||||
$parentItemId = $orderItem->getParentItemId();
|
||||
}
|
||||
}
|
||||
$invoice = $this->invoiceService->prepareInvoice($order, [$parentItemId => $invoiceQty]);
|
||||
$invoiceItems = $invoice->getItems();
|
||||
foreach ($invoiceItems as $invoiceItem) {
|
||||
$this->assertEquals($invoiceQty, $invoiceItem->getQty());
|
||||
}
|
||||
}
|
||||
|
||||
public function prepareInvoiceConfigurableProductDataProvider()
|
||||
{
|
||||
return [
|
||||
'full invoice' => [2],
|
||||
'partial invoice' => [1]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $invoiceQty
|
||||
* @magentoDataFixture Magento/Sales/_files/order.php
|
||||
* @return void
|
||||
* @dataProvider prepareInvoiceSimpleProductDataProvider
|
||||
*/
|
||||
public function testPrepareInvoiceSimpleProduct(int $invoiceQty): void
|
||||
{
|
||||
/** @var OrderInterface $order */
|
||||
$order = Bootstrap::getObjectManager()->create(Order::class)->load('100000001', 'increment_id');
|
||||
$orderItems = $order->getItems();
|
||||
$invoiceQtys = [];
|
||||
foreach ($orderItems as $orderItem) {
|
||||
$invoiceQtys[$orderItem->getItemId()] = $invoiceQty;
|
||||
}
|
||||
$invoice = $this->invoiceService->prepareInvoice($order, $invoiceQtys);
|
||||
$invoiceItems = $invoice->getItems();
|
||||
foreach ($invoiceItems as $invoiceItem) {
|
||||
$this->assertEquals($invoiceQty, $invoiceItem->getQty());
|
||||
}
|
||||
}
|
||||
|
||||
public function prepareInvoiceSimpleProductDataProvider()
|
||||
{
|
||||
return [
|
||||
'full invoice' => [2],
|
||||
'partial invoice' => [1]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if ordered and invoiced qty of bundle product does match.
|
||||
*
|
||||
* @param array $qtyToInvoice
|
||||
* @param array $qtyInvoiced
|
||||
* @param string $errorMsg
|
||||
* @return void
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
* @magentoDataFixture Magento/Sales/_files/order_with_bundle.php
|
||||
* @dataProvider bundleProductQtyOrderedDataProvider
|
||||
*/
|
||||
public function testPrepareInvoiceBundleProduct(
|
||||
array $qtyToInvoice,
|
||||
array $qtyInvoiced,
|
||||
string $errorMsg
|
||||
): void {
|
||||
/** @var Order $order */
|
||||
$order = Bootstrap::getObjectManager()->create(Order::class)
|
||||
->load('100000001', 'increment_id');
|
||||
|
||||
$predefinedQtyToInvoice = $this->getPredefinedQtyToInvoice($order, $qtyToInvoice);
|
||||
$invoice = $this->invoiceService->prepareInvoice($order, $predefinedQtyToInvoice);
|
||||
|
||||
foreach ($invoice->getItems() as $invoiceItem) {
|
||||
if (isset($qtyInvoiced[$invoiceItem->getSku()])) {
|
||||
$this->assertEquals(
|
||||
$qtyInvoiced[$invoiceItem->getSku()],
|
||||
$invoiceItem->getQty(),
|
||||
sprintf($errorMsg, $invoiceItem->getSku())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for invoice creation with and w/o predefined qty to invoice.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function bundleProductQtyOrderedDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'Create invoice w/o predefined qty' => [
|
||||
'Qty to invoice' => [],
|
||||
'Qty ordered' => [
|
||||
'bundle_1' => 2,
|
||||
'bundle_simple_1' => 10,
|
||||
],
|
||||
'Error msg' => 'Invoiced qty for product %s does not match.',
|
||||
],
|
||||
'Create invoice with predefined qty' => [
|
||||
'Qty to invoice' => [
|
||||
'bundle_1' => 2,
|
||||
'bundle_simple_1' => 10,
|
||||
],
|
||||
'Qty ordered' => [
|
||||
'bundle_1' => 2,
|
||||
'bundle_simple_1' => 10,
|
||||
],
|
||||
'Error msg' => 'Invoiced qty for product %s does not match.',
|
||||
],
|
||||
'Create invoice with partial predefined qty for bundle' => [
|
||||
'Qty to invoice' => [
|
||||
'bundle_1' => 1,
|
||||
],
|
||||
'Qty ordered' => [
|
||||
'bundle_1' => 1,
|
||||
'bundle_simple_1' => 5,
|
||||
],
|
||||
'Error msg' => 'Invoiced qty for product %s does not match.',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate product qty to invoice to order item id.
|
||||
*
|
||||
* @param Order $order
|
||||
* @param array $qtyToInvoice
|
||||
* @return array
|
||||
*/
|
||||
private function getPredefinedQtyToInvoice(Order $order, array $qtyToInvoice): array
|
||||
{
|
||||
$predefinedQtyToInvoice = [];
|
||||
|
||||
foreach ($order->getAllItems() as $orderItem) {
|
||||
if (array_key_exists($orderItem->getSku(), $qtyToInvoice)) {
|
||||
$predefinedQtyToInvoice[$orderItem->getId()] = $qtyToInvoice[$orderItem->getSku()];
|
||||
}
|
||||
}
|
||||
|
||||
return $predefinedQtyToInvoice;
|
||||
}
|
||||
}
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Sales\Model\Service;
|
||||
|
||||
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
|
||||
use Magento\Quote\Api\CartRepositoryInterface;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\Sales\Api\PaymentFailuresInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
|
||||
/**
|
||||
* Tests \Magento\Sales\Api\PaymentFailuresInterface.
|
||||
*/
|
||||
class PaymentFailuresServiceTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var PaymentFailuresInterface
|
||||
*/
|
||||
private $paymentFailures;
|
||||
|
||||
/**
|
||||
* @var Quote
|
||||
*/
|
||||
private $quote;
|
||||
|
||||
/**
|
||||
* @var CartRepositoryInterface|MockObject
|
||||
*/
|
||||
private $cartRepositoryMock;
|
||||
|
||||
/**
|
||||
* @var TimezoneInterface|MockObject
|
||||
*/
|
||||
private $localeDateMock;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->quote = Bootstrap::getObjectManager()->create(Quote::class);
|
||||
$this->cartRepositoryMock = $this->getMockBuilder(CartRepositoryInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(['get'])
|
||||
->getMockForAbstractClass();
|
||||
$this->localeDateMock = $this->getMockBuilder(TimezoneInterface::class)
|
||||
->setMethods(['formatDateTime'])
|
||||
->getMockForAbstractClass();
|
||||
|
||||
$this->paymentFailures = Bootstrap::getObjectManager()->create(
|
||||
PaymentFailuresInterface::class,
|
||||
[
|
||||
'cartRepository' => $this->cartRepositoryMock,
|
||||
'localeDate' => $this->localeDateMock,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Sales/_files/quote_with_two_products_and_customer.php
|
||||
* @magentoConfigFixture cataloginventory/options/enable_inventory_check 1
|
||||
* @magentoConfigFixture current_store payment/payflowpro/title Some Title Of The Method
|
||||
* @magentoConfigFixture current_store carriers/freeshipping/title Some Shipping Method
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @return void
|
||||
*/
|
||||
public function testHandlerWithCustomer(): void
|
||||
{
|
||||
$errorMessage = __('Transaction declined.');
|
||||
$checkoutType = 'custom_checkout';
|
||||
|
||||
$this->quote->load('test01', 'reserved_order_id');
|
||||
$this->cartRepositoryMock->method('get')
|
||||
->with($this->quote->getId())
|
||||
->willReturn($this->quote);
|
||||
|
||||
$dateAndTime = 'Nov 22, 2019, 1:00:00 AM';
|
||||
$this->localeDateMock->expects($this->atLeastOnce())->method('formatDateTime')->willReturn($dateAndTime);
|
||||
$this->paymentFailures->handle((int)$this->quote->getId(), $errorMessage->render());
|
||||
|
||||
$paymentReflection = new \ReflectionClass($this->paymentFailures);
|
||||
$templateVarsMethod = $paymentReflection->getMethod('getTemplateVars');
|
||||
$templateVarsMethod->setAccessible(true);
|
||||
|
||||
$templateVars = $templateVarsMethod->invoke($this->paymentFailures, $this->quote, $errorMessage, $checkoutType);
|
||||
$expectedVars = [
|
||||
'reason' => $errorMessage->render(),
|
||||
'checkoutType' => $checkoutType,
|
||||
'dateAndTime' => $dateAndTime,
|
||||
'customer' => 'John Smith',
|
||||
'customerEmail' => 'aaa@aaa.com',
|
||||
'paymentMethod' => 'Some Title Of The Method',
|
||||
'shippingMethod' => 'Some Shipping Method',
|
||||
'items' => 'Simple Product x 2 USD 10<br />Custom Design Simple Product x 1 USD 10',
|
||||
'total' => 'USD 30.0000',
|
||||
'billingAddress' => $this->quote->getBillingAddress(),
|
||||
'shippingAddress' => $this->quote->getShippingAddress(),
|
||||
'billingAddressHtml' => $this->quote->getBillingAddress()->format('html'),
|
||||
'shippingAddressHtml' => $this->quote->getShippingAddress()->format('html'),
|
||||
];
|
||||
|
||||
$this->assertEquals($expectedVars, $templateVars);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Sales/_files/quote_with_two_products_and_customer.php
|
||||
* @magentoConfigFixture cataloginventory/options/enable_inventory_check 0
|
||||
* @magentoConfigFixture current_store payment/payflowpro/title Some Title Of The Method
|
||||
* @magentoConfigFixture current_store carriers/freeshipping/title Some Shipping Method
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @return void
|
||||
*/
|
||||
public function testHandlerWithCustomerWithInventoryCheckDisabled(): void
|
||||
{
|
||||
$errorMessage = __('Transaction declined.');
|
||||
$checkoutType = 'custom_checkout';
|
||||
|
||||
$this->quote->load('test01', 'reserved_order_id');
|
||||
$this->cartRepositoryMock->method('get')
|
||||
->with($this->quote->getId())
|
||||
->willReturn($this->quote);
|
||||
|
||||
$dateAndTime = 'Nov 22, 2019, 1:00:00 AM';
|
||||
$this->localeDateMock->expects($this->atLeastOnce())->method('formatDateTime')->willReturn($dateAndTime);
|
||||
$this->paymentFailures->handle((int)$this->quote->getId(), $errorMessage->render());
|
||||
|
||||
$paymentReflection = new \ReflectionClass($this->paymentFailures);
|
||||
$templateVarsMethod = $paymentReflection->getMethod('getTemplateVars');
|
||||
$templateVarsMethod->setAccessible(true);
|
||||
|
||||
$templateVars = $templateVarsMethod->invoke($this->paymentFailures, $this->quote, $errorMessage, $checkoutType);
|
||||
$expectedVars = [
|
||||
'reason' => $errorMessage->render(),
|
||||
'checkoutType' => $checkoutType,
|
||||
'dateAndTime' => $dateAndTime,
|
||||
'customer' => 'John Smith',
|
||||
'customerEmail' => 'aaa@aaa.com',
|
||||
'paymentMethod' => 'Some Title Of The Method',
|
||||
'shippingMethod' => 'Some Shipping Method',
|
||||
'items' => 'Simple Product x 2.0000 USD 10<br />Custom Design Simple Product x 1.0000 USD 10',
|
||||
'total' => 'USD 30.0000',
|
||||
'billingAddress' => $this->quote->getBillingAddress(),
|
||||
'shippingAddress' => $this->quote->getShippingAddress(),
|
||||
'billingAddressHtml' => $this->quote->getBillingAddress()->format('html'),
|
||||
'shippingAddressHtml' => $this->quote->getShippingAddress()->format('html'),
|
||||
];
|
||||
|
||||
$this->assertEquals($expectedVars, $templateVars);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user