Files
esun3d/app/code/Esun/FreeGift/Test/Unit/Model/GiftSynchronizerTest.php
T
2026-09-15 10:35:56 +08:00

98 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\FreeGift\Test\Unit\Model;
use Esun\FreeGift\Model\Config;
use Esun\FreeGift\Model\GiftItem;
use Esun\FreeGift\Model\GiftSynchronizer;
use Esun\FreeGift\Model\InternalAddGuard;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\DataObjectFactory;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Item;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
class GiftSynchronizerTest extends TestCase
{
public function testExistingGiftQuantityAndPriceAreNormalized(): void
{
$config = $this->createMock(Config::class);
$config->method('getTargetQuantity')->with(299.00, 16)->willReturn(2);
$config->method('isGiftSku')->willReturn(true);
$product = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->addMethods(['setIsSuperMode'])
->getMock();
$product->expects(self::once())->method('setIsSuperMode')->with(true);
$item = $this->getMockBuilder(Item::class)
->disableOriginalConstructor()
->onlyMethods([
'getData',
'getQty',
'getProduct',
'setQty',
'setCustomPrice',
'setOriginalPrice'
])
->addMethods([
'getCustomPrice',
'setOriginalCustomPrice',
'setBaseOriginalPrice',
'setNoDiscount',
'setDiscountAmount',
'setBaseDiscountAmount',
'setAppliedRuleIds'
])
->getMock();
$item->method('getData')->willReturnCallback(
static fn (string $key): mixed => $key === GiftItem::FLAG ? 1 : 'FREE-FILAMENT'
);
$item->method('getQty')->willReturn(1.0);
$item->method('getCustomPrice')->willReturn(0.0);
$item->method('getProduct')->willReturn($product);
$item->expects(self::once())->method('setQty')->with(2);
$item->expects(self::once())->method('setCustomPrice')->with(0.0);
$item->expects(self::once())->method('setNoDiscount')->with(1);
$quote = $this->createMock(Quote::class);
$quote->method('getStoreId')->willReturn(16);
$quote->method('getAllVisibleItems')->willReturn([$item]);
self::assertTrue($this->createSynchronizer($config)->synchronize($quote, 299.00));
}
public function testGiftIsRemovedWhenCartDropsBelowThreshold(): void
{
$config = $this->createMock(Config::class);
$config->method('getTargetQuantity')->with(198.99, 16)->willReturn(0);
$item = $this->createMock(Item::class);
$item->method('getData')->with(GiftItem::FLAG)->willReturn(1);
$quote = $this->createMock(Quote::class);
$quote->method('getStoreId')->willReturn(16);
$quote->method('getAllVisibleItems')->willReturn([$item]);
$quote->expects(self::once())->method('deleteItem')->with($item);
self::assertTrue($this->createSynchronizer($config)->synchronize($quote, 198.99));
}
private function createSynchronizer(Config $config): GiftSynchronizer
{
return new GiftSynchronizer(
$config,
$this->createMock(ProductRepositoryInterface::class),
$this->createMock(DataObjectFactory::class),
new Json(),
new InternalAddGuard(),
$this->createMock(LoggerInterface::class)
);
}
}