Skip to content

Commit 5f1809f

Browse files
karyna-txmav
authored andcommitted
fix unit tests
1 parent 432ec4a commit 5f1809f

File tree

9 files changed

+56
-40
lines changed

9 files changed

+56
-40
lines changed

app/code/Magento/Catalog/Helper/Product/Configuration.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Catalog\Helper\Product;
77

8+
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
89
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\Escaper;
1011
use Magento\Framework\Serialize\Serializer\Json;
@@ -26,8 +27,6 @@ class Configuration extends AbstractHelper implements ConfigurationInterface
2627
protected $filter;
2728

2829
/**
29-
* Product option factory
30-
*
3130
* @var \Magento\Catalog\Model\Product\OptionFactory
3231
*/
3332
protected $_productOptionFactory;
@@ -76,15 +75,17 @@ public function __construct(
7675
/**
7776
* Retrieves product configuration options
7877
*
79-
* @param \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
78+
* @param ItemInterface $item
79+
*
8080
* @return array
81+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
8182
*/
82-
public function getCustomOptions(\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item)
83+
public function getCustomOptions(ItemInterface $item) //phpcs:ignore Generic.Metrics.NestingLevel
8384
{
8485
$product = $item->getProduct();
8586
$options = [];
8687
$optionIds = $item->getOptionByCode('option_ids');
87-
if ($optionIds) {
88+
if ($optionIds && $optionIds->getValue()) {
8889
$options = [];
8990
foreach (explode(',', $optionIds->getValue()) as $optionId) {
9091
$option = $product->getOptionById($optionId);
@@ -133,10 +134,10 @@ public function getCustomOptions(\Magento\Catalog\Model\Product\Configuration\It
133134
/**
134135
* Retrieves product options list
135136
*
136-
* @param \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
137+
* @param ItemInterface $item
137138
* @return array
138139
*/
139-
public function getOptions(\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item)
140+
public function getOptions(ItemInterface $item)
140141
{
141142
return $this->getCustomOptions($item);
142143
}

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Magento\Catalog\Model\ProductRepository\MediaGalleryProcessor;
2626
use Magento\Catalog\Model\ResourceModel\Product\Collection;
2727
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
28+
use Magento\Eav\Model\Entity\Attribute\Exception as AttributeException;
2829
use Magento\Framework\Api\Data\ImageContentInterface;
2930
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
3031
use Magento\Framework\Api\ExtensibleDataObjectConverter;
@@ -263,6 +264,7 @@ protected function setUp(): void
263264
['create']
264265
);
265266
$this->resourceModel = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product::class);
267+
$this->resourceModel->method('getLinkField')->willReturn('some_value');
266268
$this->objectManager = new ObjectManager($this);
267269
$this->extensibleDataObjectConverter = $this->getMockBuilder(ExtensibleDataObjectConverter::class)
268270
->onlyMethods(['toNestedArray'])
@@ -326,6 +328,7 @@ function ($value) {
326328
return json_decode($value, true);
327329
}
328330
);
331+
$this->serializerMock->method('serialize')->willReturn(''); // PHP 8.1. Compatibility
329332

330333
$mediaProcessor = $this->objectManager->getObject(
331334
MediaGalleryProcessor::class,
@@ -793,7 +796,7 @@ public function testSaveUnableToSaveException(): void
793796
public function testSaveException(): void
794797
{
795798
$this->expectException('Magento\Framework\Exception\InputException');
796-
$this->expectExceptionMessage('Invalid value of "" provided for the field.');
799+
$this->expectExceptionMessage('Invalid value of "" provided for the attribute_code field.');
797800
$this->storeManager->expects($this->any())->method('getWebsites')->willReturn([1 => 'default']);
798801
$this->resourceModel->expects($this->exactly(1))->method('getIdBySku')->willReturn(null);
799802
$this->productFactory->expects($this->exactly(2))
@@ -802,8 +805,11 @@ public function testSaveException(): void
802805
$this->initializationHelper->expects($this->never())->method('initialize');
803806
$this->resourceModel->expects($this->once())->method('validate')->with($this->product)
804807
->willReturn(true);
808+
809+
$attributeException = new AttributeException(__('123'));
810+
$attributeException->setAttributeCode('attribute_code');
805811
$this->resourceModel->expects($this->once())->method('save')->with($this->product)
806-
->willThrowException(new \Magento\Eav\Model\Entity\Attribute\Exception(__('123')));
812+
->willThrowException($attributeException);
807813
$this->product->expects($this->exactly(2))->method('getId')->willReturn(null);
808814
$this->extensibleDataObjectConverter
809815
->expects($this->once())

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,8 +3119,11 @@ private function parseMultipleValues($labelRow)
31193119
*/
31203120
private function isSkuExist($sku)
31213121
{
3122-
$sku = strtolower($sku);
3123-
return isset($this->_oldSku[$sku]);
3122+
if ($sku !== null) {
3123+
$sku = strtolower($sku);
3124+
return isset($this->_oldSku[$sku]);
3125+
}
3126+
return false;
31243127
}
31253128

31263129
/**

app/code/Magento/CatalogImportExport/Model/Import/Product/Validator/Quantity.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,28 @@
77

88
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
99

10-
/**
11-
* Class Quantity
12-
*/
1310
class Quantity extends AbstractImportValidator implements RowValidatorInterface
1411
{
1512
/**
16-
* {@inheritdoc}
13+
* @inheritdoc
1714
*/
1815
public function isValid($value)
1916
{
2017
$this->_clearMessages();
2118
if (!empty($value['qty']) && (!is_numeric($value['qty'])
2219
&& $value['qty'] !== $this->context->getEmptyAttributeValueConstant())
2320
) {
24-
$this->_addMessages(
25-
[
26-
sprintf(
27-
$this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE),
28-
'qty',
29-
'decimal'
30-
),
31-
]
32-
);
21+
if ($this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE) !== null) {
22+
$this->_addMessages(
23+
[
24+
sprintf(
25+
$this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE),
26+
'qty',
27+
'decimal'
28+
),
29+
]
30+
);
31+
}
3332
return false;
3433
}
3534
return true;

app/code/Magento/CatalogImportExport/Model/Import/Product/Validator/Weight.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,25 @@
1010
class Weight extends AbstractImportValidator implements RowValidatorInterface
1111
{
1212
/**
13-
* {@inheritdoc}
13+
* @inheritdoc
1414
*/
1515
public function isValid($value)
1616
{
1717
$this->_clearMessages();
1818
if (!empty($value['weight']) && (!is_numeric($value['weight']) || $value['weight'] < 0)
1919
&& $value['weight'] !== $this->context->getEmptyAttributeValueConstant()
2020
) {
21-
$this->_addMessages(
22-
[
23-
sprintf(
24-
$this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE),
25-
'weight',
26-
'decimal'
27-
)
28-
]
29-
);
21+
if ($this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE) !== null) {
22+
$this->_addMessages(
23+
[
24+
sprintf(
25+
$this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE),
26+
'weight',
27+
'decimal'
28+
)
29+
]
30+
);
31+
}
3032
return false;
3133
}
3234
return true;

app/code/Magento/Checkout/Test/Unit/Model/DefaultConfigProviderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use Magento\Quote\Model\Quote\Address;
4040
use Magento\Quote\Model\QuoteIdMaskFactory;
4141
use Magento\Shipping\Model\Config;
42+
use Magento\Store\Model\ScopeInterface;
4243
use Magento\Store\Model\Store;
4344
use Magento\Store\Model\StoreManagerInterface;
4445
use PHPUnit\Framework\MockObject\MockObject;
@@ -122,6 +123,13 @@ protected function setUp(): void
122123
$this->addressMetadata = $this->createMock(AddressMetadataInterface::class);
123124
$attributeOptionManager = $this->createMock(AttributeOptionManagementInterface::class);
124125
$customerAddressData = $this->createMock(CustomerAddressDataProvider::class);
126+
127+
// PHP 8.1 compatibility; The real code returns '', not null.
128+
$scopeConfig->method('getValue')
129+
->withConsecutive(
130+
['shipping/shipping_policy/shipping_policy_content', ScopeInterface::SCOPE_STORE]
131+
)->willReturn('');
132+
125133
$this->model = new DefaultConfigProvider(
126134
$checkoutHelper,
127135
$this->checkoutSession,

app/code/Magento/Cms/Test/Unit/Model/Wysiwyg/ConfigTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,7 @@ public function testGetConfig($data, $isAuthorizationAllowed, $expectedResults)
183183
{
184184
$this->backendUrlMock->expects($this->atLeastOnce())
185185
->method('getUrl')
186-
->withConsecutive(
187-
['cms/wysiwyg/directive'],
188-
['cms/wysiwyg_images/index']
189-
);
186+
->willReturn('some_link');
190187
$this->backendUrlMock->expects($this->once())
191188
->method('getBaseUrl')
192189
->willReturn('localhost/index.php/');

app/code/Magento/Config/Model/Config/Backend/Email/Sender.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Sender extends \Magento\Framework\App\Config\Value
2424
public function beforeSave()
2525
{
2626
$value = $this->getValue();
27-
if (!preg_match("/^[\S ]+$/", $value)) {
27+
if ($value === null || !preg_match("/^[\S ]+$/", $value)) {
2828
throw new \Magento\Framework\Exception\LocalizedException(
2929
__('The sender name "%1" is not valid. Please use only visible characters and spaces.', $value)
3030
);

app/code/Magento/Contact/Test/Unit/Controller/Index/PostTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function testExecuteValidPost(): void
181181
'name' => 'Name',
182182
'comment' => 'Comment',
183183
'email' => 'valid@mail.com',
184-
'hideit' => null
184+
'hideit' => ''
185185
];
186186

187187
$this->dataPersistorMock->expects($this->once())

0 commit comments

Comments
 (0)