ltsecurityinc安防产品在线商城代码
This commit is contained in:
+204
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\SalesRule\Model\Quote\Address\Total;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Quote\Api\CartRepositoryInterface;
|
||||
use Magento\Quote\Api\Data\AddressInterface;
|
||||
use Magento\Quote\Api\Data\AddressInterfaceFactory;
|
||||
use Magento\Quote\Api\Data\CartInterface;
|
||||
use Magento\Quote\Api\Data\ShippingMethodInterface;
|
||||
use Magento\Quote\Api\GuestCartItemRepositoryInterface;
|
||||
use Magento\Quote\Api\GuestCartManagementInterface;
|
||||
use Magento\Quote\Api\GuestShipmentEstimationInterface;
|
||||
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
|
||||
use Magento\SalesRule\Model\Rule\Condition\Combine;
|
||||
use Magento\SalesRule\Model\Rule\Condition\Product;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class ShippingTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var GuestCartManagementInterface
|
||||
*/
|
||||
private $cartManagement;
|
||||
|
||||
/**
|
||||
* @var GuestCartItemRepositoryInterface
|
||||
*/
|
||||
private $itemRepository;
|
||||
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->cartManagement = $this->objectManager->get(GuestCartManagementInterface::class);
|
||||
$this->itemRepository = $this->objectManager->get(GuestCartItemRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/SalesRule/_files/rule_free_shipping_by_product_weight.php
|
||||
* @magentoDataFixture Magento/Quote/_files/is_salable_product.php
|
||||
*/
|
||||
public function testRuleByProductWeightWithFreeShipping()
|
||||
{
|
||||
$cartId = $this->cartManagement->createEmptyCart();
|
||||
$this->addToCart($cartId, 'simple-99', 1);
|
||||
$methods = $this->estimateShipping($cartId);
|
||||
|
||||
$this->assertTrue(count($methods) > 0);
|
||||
$this->assertEquals('flatrate', $methods[0]->getMethodCode());
|
||||
$this->assertEquals(0, $methods[0]->getAmount());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/SalesRule/_files/rule_free_shipping_by_product_weight.php
|
||||
* @magentoDataFixture Magento/Quote/_files/is_salable_product.php
|
||||
*/
|
||||
public function testRuleByProductWeightWithoutFreeShipping()
|
||||
{
|
||||
$cartId = $this->cartManagement->createEmptyCart();
|
||||
$this->addToCart($cartId, 'simple-99', 5);
|
||||
$methods = $this->estimateShipping($cartId);
|
||||
|
||||
$this->assertTrue(count($methods) > 0);
|
||||
$this->assertEquals('flatrate', $methods[0]->getMethodCode());
|
||||
$this->assertEquals(25, $methods[0]->getAmount());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/SalesRule/_files/cart_rule_free_shipping.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/products_list.php
|
||||
*/
|
||||
public function testFreeMethodWeight()
|
||||
{
|
||||
$this->setFreeShippingForProduct('simple-249');
|
||||
$cartId = $this->cartManagement->createEmptyCart();
|
||||
$this->addToCart($cartId, 'simple-249', 3);
|
||||
$this->addToCart($cartId, 'simple-156', 1);
|
||||
$this->estimateShipping($cartId);
|
||||
$quote = $this->getQuote($cartId);
|
||||
$this->assertEquals(10, $quote->getShippingAddress()->getFreeMethodWeight());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/SalesRule/_files/cart_rule_free_shipping.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/products_list.php
|
||||
*/
|
||||
public function testFreeMethodWeightWithMaximumQtyDiscount()
|
||||
{
|
||||
$this->setFreeShippingForProduct('simple-249', 2);
|
||||
$cartId = $this->cartManagement->createEmptyCart();
|
||||
$this->addToCart($cartId, 'simple-249', 5);
|
||||
$this->addToCart($cartId, 'simple-156', 1);
|
||||
$this->estimateShipping($cartId);
|
||||
$quote = $this->getQuote($cartId);
|
||||
$this->assertEquals(40, $quote->getShippingAddress()->getFreeMethodWeight());
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimate shipment for guest cart
|
||||
*
|
||||
* @param int $cartId
|
||||
* @return ShippingMethodInterface[]
|
||||
*/
|
||||
private function estimateShipping($cartId)
|
||||
{
|
||||
$addressFactory = $this->objectManager->get(AddressInterfaceFactory::class);
|
||||
/** @var AddressInterface $address */
|
||||
$address = $addressFactory->create();
|
||||
$address->setCountryId('US');
|
||||
$address->setRegionId(2);
|
||||
|
||||
/** @var GuestShipmentEstimationInterface $estimation */
|
||||
$estimation = $this->objectManager->get(GuestShipmentEstimationInterface::class);
|
||||
return $estimation->estimateByExtendedAddress($cartId, $address);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $cartMaskId
|
||||
* @return CartInterface
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
private function getQuote(string $cartMaskId): CartInterface
|
||||
{
|
||||
/** @var CartRepositoryInterface $cartRepository */
|
||||
$cartRepository = $this->objectManager->get(CartRepositoryInterface::class);
|
||||
/** @var MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId */
|
||||
$maskedQuoteIdToQuoteId = $this->objectManager->get(MaskedQuoteIdToQuoteIdInterface::class);
|
||||
$cartId = $maskedQuoteIdToQuoteId->execute($cartMaskId);
|
||||
return $cartRepository->get($cartId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $cartMaskId
|
||||
* @param string $sku
|
||||
* @param int $qty
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
private function addToCart(string $cartMaskId, string $sku, int $qty): void
|
||||
{
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
/** @var CartRepositoryInterface $cartRepository */
|
||||
$cartRepository = $this->objectManager->get(CartRepositoryInterface::class);
|
||||
$product = $productRepository->get($sku);
|
||||
$quote = $this->getQuote($cartMaskId);
|
||||
$quote->addProduct($product, $qty);
|
||||
$cartRepository->save($quote);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sku
|
||||
* @param int $qty
|
||||
*/
|
||||
public function setFreeShippingForProduct(string $sku, int $qty = 0): void
|
||||
{
|
||||
/** @var Registry $registry */
|
||||
$registry = $this->objectManager->get(Registry::class);
|
||||
$salesRule = $registry->registry('cart_rule_free_shipping');
|
||||
$salesRule->setDiscountQty($qty);
|
||||
$data = [
|
||||
'actions' => [
|
||||
1 => [
|
||||
'type' => Combine::class,
|
||||
'attribute' => null,
|
||||
'operator' => null,
|
||||
'value' => '1',
|
||||
'is_value_processed' => null,
|
||||
'aggregator' => 'all',
|
||||
'actions' => [
|
||||
1 => [
|
||||
'type' => Product::class,
|
||||
'attribute' => 'sku',
|
||||
'operator' => '==',
|
||||
'value' => $sku,
|
||||
'is_value_processed' => false,
|
||||
]
|
||||
]
|
||||
]
|
||||
],
|
||||
];
|
||||
$salesRule->loadPost($data);
|
||||
$salesRule->save();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\SalesRule\Model\Quote;
|
||||
|
||||
use Magento\Framework\Api\SearchCriteriaBuilder;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Quote\Api\CartRepositoryInterface;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\Quote\Model\Quote\Item;
|
||||
use Magento\SalesRule\Test\Fixture\ProductCondition as ProductConditionFixture;
|
||||
use Magento\SalesRule\Test\Fixture\Rule as RuleFixture;
|
||||
use Magento\TestFramework\Fixture\AppIsolation;
|
||||
use Magento\TestFramework\Fixture\DataFixture;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test discount totals calculation model
|
||||
*/
|
||||
class DiscountTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
/**
|
||||
* @var SearchCriteriaBuilder
|
||||
*/
|
||||
private $criteriaBuilder;
|
||||
/**
|
||||
* @var CartRepositoryInterface
|
||||
*/
|
||||
private $quoteRepository;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->criteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
|
||||
$this->quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Checkout/_files/quote_with_bundle_product_with_dynamic_price.php
|
||||
* @dataProvider bundleProductWithDynamicPriceAndCartPriceRuleDataProvider
|
||||
* @param string $coupon
|
||||
* @param array $discounts
|
||||
* @param float $totalDiscount
|
||||
* @return void
|
||||
*/
|
||||
#[
|
||||
AppIsolation(true),
|
||||
DataFixture(
|
||||
ProductConditionFixture::class,
|
||||
['attribute' => 'sku', 'value' => 'bundle_product_with_dynamic_price'],
|
||||
'cond1'
|
||||
),
|
||||
DataFixture(
|
||||
ProductConditionFixture::class,
|
||||
['attribute' => 'sku', 'value' => 'simple1'],
|
||||
'cond2'
|
||||
),
|
||||
DataFixture(
|
||||
ProductConditionFixture::class,
|
||||
['attribute' => 'sku', 'value' => 'simple2'],
|
||||
'cond3'
|
||||
),
|
||||
DataFixture(
|
||||
RuleFixture::class,
|
||||
['coupon_code' => 'bundle_cc', 'discount_amount' => 50, 'actions' => ['$cond1$']],
|
||||
'rule1'
|
||||
),
|
||||
DataFixture(
|
||||
RuleFixture::class,
|
||||
['coupon_code' => 'simple1_cc', 'discount_amount' => 50, 'actions' => ['$cond2$']],
|
||||
'rule2'
|
||||
),
|
||||
DataFixture(
|
||||
RuleFixture::class,
|
||||
['coupon_code' => 'simple2_cc', 'discount_amount' => 50, 'actions' => ['$cond3$']],
|
||||
'rule3'
|
||||
),
|
||||
]
|
||||
public function testBundleProductWithDynamicPriceAndCartPriceRule(
|
||||
string $coupon,
|
||||
array $discounts,
|
||||
float $totalDiscount
|
||||
): void {
|
||||
$quote = $this->getQuote('quote_with_bundle_product_with_dynamic_price');
|
||||
$quote->setCouponCode($coupon);
|
||||
$quote->collectTotals();
|
||||
$this->quoteRepository->save($quote);
|
||||
$this->assertEquals(21.98, $quote->getBaseSubtotal());
|
||||
$this->assertEquals($totalDiscount, $quote->getShippingAddress()->getDiscountAmount());
|
||||
$items = $quote->getAllItems();
|
||||
$this->assertCount(3, $items);
|
||||
/** @var Item $item*/
|
||||
$item = array_shift($items);
|
||||
$this->assertEquals('bundle_product_with_dynamic_price-simple1-simple2', $item->getSku());
|
||||
$this->assertEquals($discounts[$item->getSku()], $item->getDiscountAmount());
|
||||
$item = array_shift($items);
|
||||
$this->assertEquals('simple1', $item->getSku());
|
||||
$this->assertEquals(5.99, $item->getPrice());
|
||||
$this->assertEquals($discounts[$item->getSku()], $item->getDiscountAmount());
|
||||
$item = array_shift($items);
|
||||
$this->assertEquals('simple2', $item->getSku());
|
||||
$this->assertEquals(15.99, $item->getPrice());
|
||||
$this->assertEquals($discounts[$item->getSku()], $item->getDiscountAmount());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function bundleProductWithDynamicPriceAndCartPriceRuleDataProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'bundle_cc',
|
||||
[
|
||||
'bundle_product_with_dynamic_price-simple1-simple2' => 0,
|
||||
'simple1' => 3,
|
||||
'simple2' => 7.99,
|
||||
],
|
||||
-10.99
|
||||
],
|
||||
[
|
||||
'simple1_cc',
|
||||
[
|
||||
'bundle_product_with_dynamic_price-simple1-simple2' => 0,
|
||||
'simple1' => 3,
|
||||
'simple2' => 0,
|
||||
],
|
||||
-3
|
||||
],
|
||||
[
|
||||
'simple2_cc',
|
||||
[
|
||||
'bundle_product_with_dynamic_price-simple1-simple2' => 0,
|
||||
'simple1' => 0,
|
||||
'simple2' => 8,
|
||||
],
|
||||
-8
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $reservedOrderId
|
||||
* @return Quote
|
||||
*/
|
||||
private function getQuote(string $reservedOrderId): Quote
|
||||
{
|
||||
$searchCriteria = $this->criteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
|
||||
->create();
|
||||
$carts = $this->quoteRepository->getList($searchCriteria)
|
||||
->getItems();
|
||||
return array_shift($carts);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user