init
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Test\Unit\Model\MixStepPricing;
|
||||
|
||||
use Esun\Catalog\Model\MixStepPricing\BatchValidator;
|
||||
use Esun\Catalog\Model\MixStepPricing\RuleNormalizer;
|
||||
use Esun\Catalog\Model\MixStepPricing\WebsiteResolver;
|
||||
use Magento\Catalog\Api\Data\ProductInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class BatchValidatorTest extends TestCase
|
||||
{
|
||||
private ProductRepositoryInterface&MockObject $productRepository;
|
||||
private WebsiteResolver&MockObject $websiteResolver;
|
||||
private BatchValidator $validator;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->productRepository = $this->createMock(ProductRepositoryInterface::class);
|
||||
$this->websiteResolver = $this->createMock(WebsiteResolver::class);
|
||||
$this->websiteResolver->method('resolve')->willReturn([
|
||||
'alias' => 'us',
|
||||
'code' => 'us_site',
|
||||
'website_id' => 3,
|
||||
'store_ids' => [2, 3],
|
||||
]);
|
||||
$this->validator = new BatchValidator(
|
||||
$this->productRepository,
|
||||
$this->websiteResolver,
|
||||
new RuleNormalizer()
|
||||
);
|
||||
}
|
||||
|
||||
public function testImportRowsAreGroupedAndSortedByQuantity(): void
|
||||
{
|
||||
$this->productRepository->method('get')->willReturn($this->configurableProduct(42));
|
||||
|
||||
$result = $this->validator->validateImport([
|
||||
$this->importRow(2, 20, 15),
|
||||
$this->importRow(3, 5, 5),
|
||||
]);
|
||||
|
||||
self::assertTrue($result['valid']);
|
||||
self::assertSame([5, 20], array_column($result['groups'][0]['rules'], 'qty'));
|
||||
self::assertSame([20, 5], array_column($result['valid_rows'], 'combined_qty_from'));
|
||||
self::assertSame('Discount Percentage', $result['valid_rows'][0]['pricing_method']);
|
||||
self::assertSame(2, $result['stats']['valid_rows']);
|
||||
self::assertSame(2, $result['stats']['store_view_writes']);
|
||||
}
|
||||
|
||||
public function testDuplicateQuantityRejectsWholeImport(): void
|
||||
{
|
||||
$this->productRepository->method('get')->willReturn($this->configurableProduct(42));
|
||||
|
||||
$result = $this->validator->validateImport([
|
||||
$this->importRow(2, 5, 5),
|
||||
$this->importRow(3, 5, 10),
|
||||
]);
|
||||
|
||||
self::assertFalse($result['valid']);
|
||||
self::assertStringContainsString('duplicated', $result['errors'][0]);
|
||||
self::assertCount(1, $result['valid_rows']);
|
||||
self::assertCount(1, $result['invalid_rows']);
|
||||
self::assertSame(3, $result['invalid_rows'][0]['row']);
|
||||
self::assertStringContainsString('duplicated', $result['invalid_rows'][0]['reason']);
|
||||
}
|
||||
|
||||
public function testMissingDeleteSkuIsWarningButEmptyPlanCannotRun(): void
|
||||
{
|
||||
$this->productRepository->method('get')->willThrowException(new NoSuchEntityException());
|
||||
|
||||
$result = $this->validator->validateDelete([
|
||||
['row' => 2, 'sku' => 'MISSING', 'website' => 'us'],
|
||||
]);
|
||||
|
||||
self::assertFalse($result['valid']);
|
||||
self::assertNotEmpty($result['warnings']);
|
||||
self::assertSame([], $result['groups']);
|
||||
self::assertSame('MISSING', $result['invalid_rows'][0]['sku']);
|
||||
self::assertStringContainsString('does not exist', $result['invalid_rows'][0]['reason']);
|
||||
}
|
||||
|
||||
private function configurableProduct(int $id): ProductInterface
|
||||
{
|
||||
$product = $this->createMock(ProductInterface::class);
|
||||
$product->method('getId')->willReturn($id);
|
||||
$product->method('getTypeId')->willReturn(Configurable::TYPE_CODE);
|
||||
|
||||
return $product;
|
||||
}
|
||||
|
||||
private function importRow(int $row, int $quantity, float $value): array
|
||||
{
|
||||
return [
|
||||
'row' => $row,
|
||||
'sku' => 'PLA-BASIC',
|
||||
'website' => 'us',
|
||||
'combined_qty_from' => $quantity,
|
||||
'pricing_method' => 'Discount Percentage',
|
||||
'value' => $value,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Test\Unit\Model\MixStepPricing;
|
||||
|
||||
use Esun\Catalog\Model\MixStepPricing\RuleNormalizer;
|
||||
use Magento\Customer\Model\Group;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RuleNormalizerTest extends TestCase
|
||||
{
|
||||
private RuleNormalizer $normalizer;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->normalizer = new RuleNormalizer();
|
||||
}
|
||||
|
||||
public function testSpreadsheetDiscountIsMappedToPersistedRule(): void
|
||||
{
|
||||
self::assertSame([
|
||||
'enabled' => true,
|
||||
'customer_group_id' => Group::CUST_GROUP_ALL,
|
||||
'qty' => 10,
|
||||
'price_type' => 'percent',
|
||||
'value' => 12.3457,
|
||||
], $this->normalizer->fromSpreadsheetRow([
|
||||
'combined_qty_from' => 10,
|
||||
'pricing_method' => 'Discount Percentage',
|
||||
'value' => 12.34567,
|
||||
]));
|
||||
}
|
||||
|
||||
public function testSpreadsheetUnitPriceIsMappedToFixedRule(): void
|
||||
{
|
||||
$rule = $this->normalizer->fromSpreadsheetRow([
|
||||
'combined_qty_from' => '20',
|
||||
'pricing_method' => 'Unit Price',
|
||||
'value' => '19.99',
|
||||
]);
|
||||
|
||||
self::assertSame('fixed', $rule['price_type']);
|
||||
self::assertSame(19.99, $rule['value']);
|
||||
}
|
||||
|
||||
public function testSpreadsheetQuantityOneIsAccepted(): void
|
||||
{
|
||||
$rule = $this->normalizer->fromSpreadsheetRow([
|
||||
'combined_qty_from' => 1,
|
||||
'pricing_method' => 'Discount Percentage',
|
||||
'value' => 5,
|
||||
]);
|
||||
|
||||
self::assertSame(1, $rule['qty']);
|
||||
}
|
||||
|
||||
#[DataProvider('invalidSpreadsheetRows')]
|
||||
public function testInvalidSpreadsheetRowsAreRejected(array $row): void
|
||||
{
|
||||
$this->expectException(LocalizedException::class);
|
||||
$this->normalizer->fromSpreadsheetRow($row);
|
||||
}
|
||||
|
||||
public static function invalidSpreadsheetRows(): array
|
||||
{
|
||||
return [
|
||||
'quantity below minimum' => [[
|
||||
'combined_qty_from' => 0,
|
||||
'pricing_method' => 'Unit Price',
|
||||
'value' => 10,
|
||||
]],
|
||||
'fractional quantity' => [[
|
||||
'combined_qty_from' => 2.5,
|
||||
'pricing_method' => 'Unit Price',
|
||||
'value' => 10,
|
||||
]],
|
||||
'unknown method' => [[
|
||||
'combined_qty_from' => 2,
|
||||
'pricing_method' => 'Fixed Price',
|
||||
'value' => 10,
|
||||
]],
|
||||
'100 percent discount' => [[
|
||||
'combined_qty_from' => 2,
|
||||
'pricing_method' => 'Discount Percentage',
|
||||
'value' => 100,
|
||||
]],
|
||||
];
|
||||
}
|
||||
|
||||
public function testProductFormNormalizationKeepsExistingBehavior(): void
|
||||
{
|
||||
$rows = $this->normalizer->normalize([
|
||||
['qty' => 10, 'price_type' => 'fixed', 'value' => 20],
|
||||
['qty' => 2, 'price_type' => 'percent', 'value' => 5],
|
||||
['qty' => 1, 'price_type' => 'percent', 'value' => 5],
|
||||
['qty' => 4, 'price_type' => 'percent', 'value' => 10, 'delete' => 1],
|
||||
]);
|
||||
|
||||
self::assertSame([1, 2, 10], array_column($rows, 'qty'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Test\Unit\Model\MixStepPricing;
|
||||
|
||||
use Esun\Catalog\Model\MixStepPricing\SpreadsheetReader;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Filesystem\Driver\File;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SpreadsheetReaderTest extends TestCase
|
||||
{
|
||||
private array $files = [];
|
||||
private File $fileDriver;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->fileDriver = new File();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
foreach ($this->files as $file) {
|
||||
if ($this->fileDriver->isFile($file)) {
|
||||
$this->fileDriver->deleteFile($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function testReadsImportRowsAndSkipsBlankRows(): void
|
||||
{
|
||||
$path = $this->createWorkbook(SpreadsheetReader::IMPORT_SHEET, [
|
||||
['sku', 'website', 'Combined Qty From', 'Pricing Method', 'Discount % / Unit Price'],
|
||||
['PLA-BASIC', 'us', 5, 'Discount Percentage', 10],
|
||||
[null, null, null, null, null],
|
||||
['PETG-HS', 'au', 20, 'Unit Price', 24.99],
|
||||
]);
|
||||
|
||||
$rows = (new SpreadsheetReader())->readImport($path);
|
||||
|
||||
self::assertCount(2, $rows);
|
||||
self::assertSame(2, $rows[0]['row']);
|
||||
self::assertSame('PLA-BASIC', $rows[0]['sku']);
|
||||
self::assertSame(20, $rows[1]['combined_qty_from']);
|
||||
}
|
||||
|
||||
public function testRejectsWrongWorksheet(): void
|
||||
{
|
||||
$path = $this->createWorkbook('Sheet1', [['sku', 'website'], ['PLA-BASIC', 'us']]);
|
||||
|
||||
$this->expectException(LocalizedException::class);
|
||||
(new SpreadsheetReader())->readDelete($path);
|
||||
}
|
||||
|
||||
public function testRejectsChangedHeader(): void
|
||||
{
|
||||
$path = $this->createWorkbook(SpreadsheetReader::DELETE_SHEET, [
|
||||
['SKU', 'website'],
|
||||
['PLA-BASIC', 'us'],
|
||||
]);
|
||||
|
||||
$this->expectException(LocalizedException::class);
|
||||
(new SpreadsheetReader())->readDelete($path);
|
||||
}
|
||||
|
||||
private function createWorkbook(string $sheetName, array $rows): string
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$spreadsheet->getActiveSheet()->setTitle($sheetName)->fromArray($rows, null, 'A1');
|
||||
$path = tempnam(sys_get_temp_dir(), 'mix-step-pricing-') . '.xlsx';
|
||||
(new Xlsx($spreadsheet))->save($path);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
$this->files[] = $path;
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Test\Unit\Model\MixStepPricing;
|
||||
|
||||
use Esun\Catalog\Model\MixStepPricing\ValidationState;
|
||||
use Magento\Backend\Model\Session;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ValidationStateTest extends TestCase
|
||||
{
|
||||
public function testPreviewContainsValidRowsInsteadOfGroupSummaries(): void
|
||||
{
|
||||
$saved = [];
|
||||
$session = $this->getMockBuilder(Session::class)
|
||||
->disableOriginalConstructor()
|
||||
->addMethods(['setData'])
|
||||
->getMock();
|
||||
$session->expects(self::once())
|
||||
->method('setData')
|
||||
->willReturnCallback(function (string $key, array $value) use (&$saved, $session): Session {
|
||||
$saved = ['key' => $key, 'value' => $value];
|
||||
return $session;
|
||||
});
|
||||
|
||||
(new ValidationState($session))->save('import', '/tmp/prices.xlsx', [
|
||||
'valid' => false,
|
||||
'stats' => ['rows' => 2, 'valid_rows' => 1, 'groups' => 1, 'store_view_writes' => 11],
|
||||
'errors' => ['Row 3: invalid value'],
|
||||
'warnings' => [],
|
||||
'groups' => [['sku' => 'IGNORED-GROUP-SUMMARY']],
|
||||
'valid_rows' => [[
|
||||
'row' => 2,
|
||||
'sku' => 'PLA-BASIC',
|
||||
'website' => 'us',
|
||||
'combined_qty_from' => 5,
|
||||
'pricing_method' => 'Discount Percentage',
|
||||
'value' => 10.0,
|
||||
]],
|
||||
'invalid_rows' => [[
|
||||
'row' => 3,
|
||||
'sku' => 'PETG-HS',
|
||||
'website' => 'usa',
|
||||
'combined_qty_from' => 10,
|
||||
'pricing_method' => 'Discount Percentage',
|
||||
'value' => 5,
|
||||
'reason' => 'Website must be one of: us, au, eu, uk.',
|
||||
]],
|
||||
]);
|
||||
|
||||
self::assertSame('esun_mix_step_pricing_import', $saved['key']);
|
||||
self::assertSame(2, $saved['value']['version']);
|
||||
self::assertSame('PLA-BASIC', $saved['value']['preview'][0]['sku']);
|
||||
self::assertArrayNotHasKey('tier_count', $saved['value']['preview'][0]);
|
||||
self::assertSame('PETG-HS', $saved['value']['invalid_preview'][0]['sku']);
|
||||
self::assertStringContainsString('Website must be', $saved['value']['invalid_preview'][0]['reason']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Test\Unit\Model\ProductRecommendation;
|
||||
|
||||
use Esun\Catalog\Model\ProductRecommendation\SpreadsheetReader;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
||||
|
||||
class SpreadsheetReaderTest extends TestCase
|
||||
{
|
||||
private array $temporaryFiles = [];
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
foreach ($this->temporaryFiles as $path) {
|
||||
@unlink($path);
|
||||
}
|
||||
}
|
||||
|
||||
public function testRecommendationsInSeparateCellsAreExpandedInDisplayOrder(): void
|
||||
{
|
||||
$path = $this->createWorkbook([
|
||||
$this->headers(),
|
||||
array_pad(['PLA-001', 'us', 'ACC-001', 'ACC-002', 'ACC-003'], 22, null),
|
||||
]);
|
||||
|
||||
self::assertSame([
|
||||
['row' => 2, 'source_sku' => 'PLA-001', 'website' => 'us', 'recommended_sku' => 'ACC-001', 'sort_order' => 0],
|
||||
['row' => 2, 'source_sku' => 'PLA-001', 'website' => 'us', 'recommended_sku' => 'ACC-002', 'sort_order' => 1],
|
||||
['row' => 2, 'source_sku' => 'PLA-001', 'website' => 'us', 'recommended_sku' => 'ACC-003', 'sort_order' => 2],
|
||||
], (new SpreadsheetReader())->read($path));
|
||||
}
|
||||
|
||||
public function testBlankRecommendationCellsCreateAnExplicitEmptySet(): void
|
||||
{
|
||||
$path = $this->createWorkbook([
|
||||
$this->headers(),
|
||||
array_pad(['PLA-001', 'uk'], 22, null),
|
||||
]);
|
||||
|
||||
self::assertSame([[
|
||||
'row' => 2,
|
||||
'source_sku' => 'PLA-001',
|
||||
'website' => 'uk',
|
||||
'recommended_sku' => '',
|
||||
'sort_order' => '',
|
||||
]], (new SpreadsheetReader())->read($path));
|
||||
}
|
||||
|
||||
public function testLegacyRowBasedHeaderIsRejected(): void
|
||||
{
|
||||
$path = $this->createWorkbook([
|
||||
['source_sku', 'website', 'recommended_sku', 'sort_order'],
|
||||
['PLA-001', 'us', 'ACC-001', 0],
|
||||
]);
|
||||
|
||||
$this->expectException(LocalizedException::class);
|
||||
(new SpreadsheetReader())->read($path);
|
||||
}
|
||||
|
||||
private function headers(): array
|
||||
{
|
||||
$headers = ['source_sku', 'website'];
|
||||
for ($position = 1; $position <= SpreadsheetReader::MAX_RECOMMENDATIONS; $position++) {
|
||||
$headers[] = 'recommended_sku_' . $position;
|
||||
}
|
||||
|
||||
return $headers;
|
||||
}
|
||||
|
||||
private function createWorkbook(array $rows): string
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$sheet->setTitle(SpreadsheetReader::SHEET);
|
||||
$sheet->fromArray($rows, null, 'A1');
|
||||
|
||||
$path = tempnam(sys_get_temp_dir(), 'product-recommendation-test-') . '.xlsx';
|
||||
(new Xlsx($spreadsheet))->save($path);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
$this->temporaryFiles[] = $path;
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user