init
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\FreeGift\Test\Unit\Model;
|
||||
|
||||
use Esun\FreeGift\Model\Config;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\Serialize\Serializer\Json;
|
||||
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ConfigTest extends TestCase
|
||||
{
|
||||
#[DataProvider('thresholdProvider')]
|
||||
public function testTargetQuantityUsesConfiguredTiers(float $subtotal, int $expectedQuantity): void
|
||||
{
|
||||
$config = $this->createConfig(true);
|
||||
|
||||
self::assertSame($expectedQuantity, $config->getTargetQuantity($subtotal, 16));
|
||||
}
|
||||
|
||||
public static function thresholdProvider(): array
|
||||
{
|
||||
return [
|
||||
'below first tier' => [198.99, 0],
|
||||
'first tier boundary' => [199.00, 1],
|
||||
'between first and second' => [298.99, 1],
|
||||
'second tier boundary' => [299.00, 2],
|
||||
'between second and third' => [398.99, 2],
|
||||
'third tier boundary' => [399.00, 3],
|
||||
'above third tier' => [1000.00, 3],
|
||||
];
|
||||
}
|
||||
|
||||
public function testDisabledCampaignReturnsNoGift(): void
|
||||
{
|
||||
self::assertSame(0, $this->createConfig(false)->getTargetQuantity(500.00, 16));
|
||||
}
|
||||
|
||||
private function createConfig(bool $enabled): Config
|
||||
{
|
||||
$values = [
|
||||
'esun_free_gift/general/gift_sku' => 'FREE-FILAMENT',
|
||||
'esun_free_gift/general/start_date' => '2026-08-01',
|
||||
'esun_free_gift/general/end_date' => '2026-08-31',
|
||||
'esun_free_gift/tiers/items' => '[{"amount":199,"qty":1},{"amount":299,"qty":2},{"amount":399,"qty":3}]',
|
||||
'esun_free_gift/tiers/tier_1_amount' => '199',
|
||||
'esun_free_gift/tiers/tier_1_qty' => '1',
|
||||
'esun_free_gift/tiers/tier_2_amount' => '299',
|
||||
'esun_free_gift/tiers/tier_2_qty' => '2',
|
||||
'esun_free_gift/tiers/tier_3_amount' => '399',
|
||||
'esun_free_gift/tiers/tier_3_qty' => '3',
|
||||
];
|
||||
|
||||
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
|
||||
$scopeConfig->method('isSetFlag')->willReturn($enabled);
|
||||
$scopeConfig->method('getValue')->willReturnCallback(
|
||||
static fn (string $path): ?string => $values[$path] ?? null
|
||||
);
|
||||
|
||||
$timezone = $this->createMock(TimezoneInterface::class);
|
||||
$timezone->method('scopeDate')->willReturn(new \DateTime('2026-08-18'));
|
||||
|
||||
return new Config($scopeConfig, $timezone, new Json());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?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)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\FreeGift\Test\Unit\Model;
|
||||
|
||||
use Esun\FreeGift\Model\InternalAddGuard;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class InternalAddGuardTest extends TestCase
|
||||
{
|
||||
public function testExecuteAllowsOnlyTheWrappedCallback(): void
|
||||
{
|
||||
$guard = new InternalAddGuard();
|
||||
|
||||
self::assertFalse($guard->isAllowed());
|
||||
self::assertTrue($guard->execute(static fn (): bool => $guard->isAllowed()));
|
||||
self::assertFalse($guard->isAllowed());
|
||||
}
|
||||
|
||||
public function testGuardIsResetWhenCallbackThrows(): void
|
||||
{
|
||||
$guard = new InternalAddGuard();
|
||||
|
||||
try {
|
||||
$guard->execute(static function (): void {
|
||||
throw new \RuntimeException('Expected test exception');
|
||||
});
|
||||
} catch (\RuntimeException) {
|
||||
self::assertFalse($guard->isAllowed());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user