|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogImportExport\Test\Unit\Model\Import\Product\Validator; |
| 9 | + |
| 10 | +use Magento\CatalogImportExport\Model\Import\Product; |
| 11 | +use Magento\CatalogImportExport\Model\Import\Product\Validator\Backorders; |
| 12 | +use Magento\CatalogInventory\Model\Source\Backorders as BackordersSource; |
| 13 | +use Magento\ImportExport\Model\Import; |
| 14 | +use PHPUnit\Framework\Attributes\TestWith; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +class BackordersTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var BackordersSource|MockObject |
| 22 | + */ |
| 23 | + private $backordersSourceMock; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var Backorders |
| 27 | + */ |
| 28 | + private $backorders; |
| 29 | + |
| 30 | + protected function setUp(): void |
| 31 | + { |
| 32 | + $this->backordersSourceMock = $this->createMock(BackordersSource::class); |
| 33 | + $this->backorders = new Backorders($this->backordersSourceMock); |
| 34 | + |
| 35 | + $this->backordersSourceMock->method('toOptionArray') |
| 36 | + ->willReturn([ |
| 37 | + ['value' => 0, 'label' => 'No Backorders'], |
| 38 | + ['value' => 1, 'label' => 'Allow Qty Below 0'], |
| 39 | + ['value' => 2, 'label' => 'Allow Qty Below 0 and Notify Customer'], |
| 40 | + ]); |
| 41 | + |
| 42 | + $contextStub = $this->createMock(Product::class); |
| 43 | + $contextStub->method('getEmptyAttributeValueConstant') |
| 44 | + ->willReturn(Import::DEFAULT_EMPTY_ATTRIBUTE_VALUE_CONSTANT); |
| 45 | + $contextStub->method('retrieveMessageTemplate') |
| 46 | + ->willReturnMap([ |
| 47 | + [Backorders::ERROR_INVALID_ATTRIBUTE_TYPE, 'Value for \'%s\' attribute contains incorrect value'], |
| 48 | + [Backorders::ERROR_INVALID_ATTRIBUTE_OPTION, 'Value for \'%s\' attribute contains unacceptable value'], |
| 49 | + ]); |
| 50 | + $this->backorders->init($contextStub); |
| 51 | + } |
| 52 | + |
| 53 | + #[ |
| 54 | + TestWith([['allow_backorders' => 0], true]), |
| 55 | + TestWith([['allow_backorders' => 1], true]), |
| 56 | + TestWith([['allow_backorders' => 2], true]), |
| 57 | + TestWith([['allow_backorders' => Import::DEFAULT_EMPTY_ATTRIBUTE_VALUE_CONSTANT], true]), |
| 58 | + TestWith([['some_field' => 'value'], true]), |
| 59 | + TestWith([['allow_backorders' => 3], false]), |
| 60 | + TestWith([['allow_backorders' => 'value'], false]), |
| 61 | + ] |
| 62 | + public function testIsValid(array $value, bool $expected): void |
| 63 | + { |
| 64 | + $result = $this->backorders->isValid($value); |
| 65 | + $this->assertEquals($expected, $result); |
| 66 | + } |
| 67 | +} |
0 commit comments