|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Weee\Test\Unit\Plugin\Catalog\Controller\Adminhtml\Product\Initialization\Helper; |
| 9 | + |
| 10 | +use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper; |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; |
| 13 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 14 | +use Magento\Weee\Plugin\Catalog\Controller\Adminhtml\Product\Initialization\Helper\ProcessTaxAttribute; |
| 15 | +use PHPUnit\Framework\MockObject\Matcher\InvokedCount as InvokedCountMatcher; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +class ProcessTaxAttributeTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Weee frontend input |
| 23 | + */ |
| 24 | + private const WEEE_FRONTEND_INPUT = 'weee'; |
| 25 | + |
| 26 | + /** |
| 27 | + * Text frontend input |
| 28 | + */ |
| 29 | + private const TEXT_FRONTEND_INPUT = 'text'; |
| 30 | + |
| 31 | + /** |
| 32 | + * Stub weee attribute code |
| 33 | + */ |
| 34 | + private const STUB_WEEE_ATTRIBUTE_CODE = 'weee_1'; |
| 35 | + |
| 36 | + /** |
| 37 | + * Stub weee attribute value |
| 38 | + */ |
| 39 | + private const STUB_WEEE_ATTRIBUTE_VALUE = 1122; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var ProcessTaxAttribute |
| 43 | + */ |
| 44 | + private $plugin; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var Helper|MockObject |
| 48 | + */ |
| 49 | + private $subjectMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var Product|MockObject |
| 53 | + */ |
| 54 | + private $productMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var Product|MockObject |
| 58 | + */ |
| 59 | + private $resultMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * Prepare environment for test |
| 63 | + */ |
| 64 | + protected function setUp() |
| 65 | + { |
| 66 | + $this->subjectMock = $this->createMock(Helper::class); |
| 67 | + $this->resultMock = $this->createMock(Product::class); |
| 68 | + $this->productMock = $this->createMock(Product::class); |
| 69 | + |
| 70 | + $objectManager = new ObjectManager($this); |
| 71 | + $this->plugin = $objectManager->getObject(ProcessTaxAttribute::class); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Test afterInitializeFromData when attributes are empty |
| 76 | + */ |
| 77 | + public function testAfterInitializeFromDataWhenAttributesAreEmpty() |
| 78 | + { |
| 79 | + $this->resultMock->expects($this->any())->method('getAttributes') |
| 80 | + ->willReturn([]); |
| 81 | + |
| 82 | + $this->resultMock->expects($this->never())->method('setData'); |
| 83 | + |
| 84 | + $this->plugin->afterInitializeFromData($this->subjectMock, $this->resultMock, $this->productMock, []); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Test afterInitializeFromData when attributes do not include weee frontend input |
| 89 | + */ |
| 90 | + public function testAfterInitializeFromDataWhenAttributesDoNotIncludeWeee() |
| 91 | + { |
| 92 | + /** @var AbstractAttribute|MockObject $attributeMock */ |
| 93 | + $attributeMock = $this->createMock(AbstractAttribute::class); |
| 94 | + |
| 95 | + $attributeMock->expects($this->any())->method('getFrontendInput') |
| 96 | + ->willReturn(self::TEXT_FRONTEND_INPUT); |
| 97 | + |
| 98 | + $this->resultMock->expects($this->any())->method('getAttributes') |
| 99 | + ->willReturn([$attributeMock]); |
| 100 | + |
| 101 | + $this->resultMock->expects($this->never())->method('setData'); |
| 102 | + |
| 103 | + $this->plugin->afterInitializeFromData($this->subjectMock, $this->resultMock, $this->productMock, []); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Test afterInitializeFromData when attributes include weee |
| 108 | + * |
| 109 | + * @param array $productData |
| 110 | + * @param InvokedCountMatcher $expected |
| 111 | + * @dataProvider afterInitializeFromDataWhenAttributesIncludeWeeeDataProvider |
| 112 | + */ |
| 113 | + public function testAfterInitializeFromDataWhenAttributesIncludeWeee($productData, $expected) |
| 114 | + { |
| 115 | + /** @var AbstractAttribute|MockObject $attributeMock */ |
| 116 | + $attributeMock = $this->createMock(AbstractAttribute::class); |
| 117 | + |
| 118 | + $attributeMock->expects($this->any())->method('getFrontendInput') |
| 119 | + ->willReturn(self::WEEE_FRONTEND_INPUT); |
| 120 | + $attributeMock->expects($this->any())->method('getAttributeCode') |
| 121 | + ->willReturn(self::STUB_WEEE_ATTRIBUTE_CODE); |
| 122 | + $this->resultMock->expects($this->any())->method('getAttributes') |
| 123 | + ->willReturn([$attributeMock]); |
| 124 | + |
| 125 | + $this->resultMock->expects($expected)->method('setData') |
| 126 | + ->with(self::STUB_WEEE_ATTRIBUTE_CODE, []) |
| 127 | + ->willReturnSelf(); |
| 128 | + |
| 129 | + $this->plugin->afterInitializeFromData( |
| 130 | + $this->subjectMock, |
| 131 | + $this->resultMock, |
| 132 | + $this->productMock, |
| 133 | + $productData |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * ProductData data provider for testAfterInitializeFromDataWhenAttributesIncludeWeee |
| 139 | + * |
| 140 | + * @return array |
| 141 | + */ |
| 142 | + public function afterInitializeFromDataWhenAttributesIncludeWeeeDataProvider() |
| 143 | + { |
| 144 | + return [ |
| 145 | + 'Product data includes wee' => [ |
| 146 | + [ |
| 147 | + self::STUB_WEEE_ATTRIBUTE_CODE => self::STUB_WEEE_ATTRIBUTE_VALUE |
| 148 | + ], |
| 149 | + $this->never() |
| 150 | + ], |
| 151 | + 'Product data does not include wee' => [ |
| 152 | + [], |
| 153 | + $this->once() |
| 154 | + ] |
| 155 | + ]; |
| 156 | + } |
| 157 | +} |
0 commit comments