9
9
use Magento \Catalog \Model \Product \Type ;
10
10
use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
11
11
use Magento \Framework \Indexer \ActionInterface ;
12
+ use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
13
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
14
+ use Magento \Framework \Api \FilterBuilder ;
12
15
13
16
class ProductTest extends \PHPUnit \Framework \TestCase
14
17
{
@@ -31,18 +34,46 @@ class ProductTest extends \PHPUnit\Framework\TestCase
31
34
* @var \Magento\ConfigurableProduct\Plugin\Model\ResourceModel\Product
32
35
*/
33
36
private $ model ;
37
+ /**
38
+ * @var ProductAttributeRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
39
+ */
40
+ private $ prdAttributeRepository ;
41
+ /**
42
+ * @var SearchCriteriaBuilder|\PHPUnit\Framework\MockObject\MockObject
43
+ */
44
+ private $ searchCriteriaBuilder ;
45
+ /**
46
+ * @var FilterBuilder|\PHPUnit\Framework\MockObject\MockObject
47
+ */
48
+ private $ filterBuilder ;
34
49
35
50
public function setUp ()
36
51
{
37
52
$ this ->objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
38
53
$ this ->configurableMock = $ this ->createMock (Configurable::class);
39
54
$ this ->actionMock = $ this ->createMock (ActionInterface::class);
55
+ // $this->prdAttributeRepository = $this->createMock(ProductAttributeRepositoryInterface::class);
56
+ $ this ->prdAttributeRepository = $ this ->getMockBuilder (ProductAttributeRepositoryInterface::class)
57
+ ->disableOriginalConstructor ()
58
+ ->setMethods (['getList ' ])
59
+ ->getMockForAbstractClass ();
60
+ $ this ->searchCriteriaBuilder = $ this ->createPartialMock (
61
+ SearchCriteriaBuilder::class,
62
+ ['addFilters ' , 'create ' ]
63
+ );
64
+ $ this ->filterBuilder = $ this ->createPartialMock (
65
+ FilterBuilder::class,
66
+ ['setField ' , 'setConditionType ' , 'setValue ' , 'create ' ]
67
+ );
40
68
41
69
$ this ->model = $ this ->objectManager ->getObject (
42
70
\Magento \ConfigurableProduct \Plugin \Model \ResourceModel \Product::class,
43
71
[
44
72
'configurable ' => $ this ->configurableMock ,
45
73
'productIndexer ' => $ this ->actionMock ,
74
+ 'productAttributeRepository ' => $ this ->prdAttributeRepository ,
75
+ 'searchCriteriaBuilder ' => $ this ->searchCriteriaBuilder ,
76
+ 'filterBuilder ' => $ this ->filterBuilder
46
77
]
47
78
);
48
79
}
@@ -65,12 +96,45 @@ public function testBeforeSaveConfigurable()
65
96
\Magento \ConfigurableProduct \Model \Product \Type \Configurable::class,
66
97
['getSetAttributes ' ]
67
98
);
99
+
100
+ $ extensionAttributes = $ this ->createPartialMock (
101
+ \Magento \Framework \Api \ExtensionAttributesInterface::class,
102
+ ['getConfigurableProductOptions ' ]
103
+ );
104
+ $ option = $ this ->createPartialMock (
105
+ \Magento \ConfigurableProduct \Model \Product \Type \Configurable \Attribute::class,
106
+ ['getAttributeId ' ]
107
+ );
108
+ $ extensionAttributes ->expects ($ this ->exactly (2 ))->method ('getConfigurableProductOptions ' )
109
+ ->willReturn ([$ option ]);
110
+ $ object ->expects ($ this ->once ())->method ('getExtensionAttributes ' )
111
+ ->willReturn ($ extensionAttributes );
112
+
113
+ $ this ->filterBuilder ->expects ($ this ->atLeastOnce ())->method ('setField ' )->willReturnSelf ();
114
+ $ this ->filterBuilder ->expects ($ this ->atLeastOnce ())->method ('setValue ' )->willReturnSelf ();
115
+ $ this ->filterBuilder ->expects ($ this ->atLeastOnce ())->method ('setConditionType ' )->willReturnSelf ();
116
+ $ this ->filterBuilder ->expects ($ this ->atLeastOnce ())->method ('create ' )->willReturnSelf ();
117
+ $ searchCriteria = $ this ->createMock (\Magento \Framework \Api \SearchCriteria::class);
118
+ $ this ->searchCriteriaBuilder ->expects ($ this ->once ())->method ('create ' )->willReturn ($ searchCriteria );
119
+
120
+ $ searchResultMockClass = $ this ->createPartialMock (
121
+ \Magento \Catalog \Model \ProductAttributeSearchResults::class,
122
+ ['getItems ' ]
123
+ );
124
+ $ this ->prdAttributeRepository ->expects ($ this ->once ())
125
+ ->method ('getList ' )->with ($ searchCriteria )->willReturn ($ searchResultMockClass );
126
+ $ optionAttribute = $ this ->createPartialMock (
127
+ \Magento \Catalog \Model \ResourceModel \Eav \Attribute::class,
128
+ ['getAttributeCode ' ]
129
+ );
130
+ $ searchResultMockClass ->expects ($ this ->once ())->method ('getItems ' )->willReturn ([$ optionAttribute ]);
68
131
$ type ->expects ($ this ->once ())->method ('getSetAttributes ' )->with ($ object );
69
132
70
133
$ object ->expects ($ this ->once ())->method ('getTypeId ' )->will ($ this ->returnValue (Configurable::TYPE_CODE ));
71
134
$ object ->expects ($ this ->once ())->method ('getTypeInstance ' )->will ($ this ->returnValue ($ type ));
72
- $ object ->expects ($ this ->any ())->method ('getExtensionAttributes ' );
73
- $ object ->expects ($ this ->any ())->method ('setData ' );
135
+ $ object ->expects ($ this ->once ())->method ('setData ' );
136
+ $ option ->expects ($ this ->once ())->method ('getAttributeId ' );
137
+ $ optionAttribute ->expects ($ this ->once ())->method ('getAttributeCode ' );
74
138
75
139
$ this ->model ->beforeSave (
76
140
$ subject ,
0 commit comments