7
7
8
8
namespace Magento \CatalogRuleConfigurable \Test \Unit \Plugin \CatalogRule \Model \Rule ;
9
9
10
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
11
+ use Magento \Catalog \Model \Product ;
10
12
use Magento \CatalogRule \Model \Rule ;
11
13
use Magento \CatalogRuleConfigurable \Plugin \CatalogRule \Model \Rule \Validation ;
12
14
use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
13
- use Magento \Framework \DataObject ;
14
15
use Magento \Rule \Model \Condition \Combine ;
15
16
use PHPUnit \Framework \MockObject \MockObject ;
16
17
use PHPUnit \Framework \TestCase ;
@@ -30,13 +31,18 @@ class ValidationTest extends TestCase
30
31
*/
31
32
private $ configurableMock ;
32
33
34
+ /**
35
+ * @var ProductRepositoryInterface|MockObject
36
+ */
37
+ private $ productRepositoryMock ;
38
+
33
39
/** @var Rule|MockObject */
34
40
private $ ruleMock ;
35
41
36
42
/** @var Combine|MockObject */
37
43
private $ ruleConditionsMock ;
38
44
39
- /** @var DataObject |MockObject */
45
+ /** @var Product |MockObject */
40
46
private $ productMock ;
41
47
42
48
/**
@@ -48,16 +54,15 @@ protected function setUp(): void
48
54
Configurable::class,
49
55
['getParentIdsByChild ' ]
50
56
);
57
+ $ this ->productRepositoryMock = $ this ->createMock (ProductRepositoryInterface::class);
51
58
52
59
$ this ->ruleMock = $ this ->createMock (Rule::class);
53
60
$ this ->ruleConditionsMock = $ this ->createMock (Combine::class);
54
- $ this ->productMock = $ this ->getMockBuilder (DataObject::class)
55
- ->addMethods (['getId ' ])
56
- ->disableOriginalConstructor ()
57
- ->getMock ();
61
+ $ this ->productMock = $ this ->createMock (Product::class);
58
62
59
63
$ this ->validation = new Validation (
60
- $ this ->configurableMock
64
+ $ this ->configurableMock ,
65
+ $ this ->productRepositoryMock
61
66
);
62
67
}
63
68
@@ -75,13 +80,49 @@ public function testAfterValidateWithValidConfigurableProduct(
75
80
$ runValidateAmount ,
76
81
$ result
77
82
) {
78
- $ this ->productMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ('product_id ' );
79
- $ this ->configurableMock ->expects ($ this ->once ())->method ('getParentIdsByChild ' )->with ('product_id ' )
83
+ $ storeId = 1 ;
84
+ $ this ->productMock ->expects ($ this ->once ())
85
+ ->method ('getId ' )
86
+ ->willReturn (10 );
87
+ $ this ->configurableMock ->expects ($ this ->once ())
88
+ ->method ('getParentIdsByChild ' )
89
+ ->with (10 )
80
90
->willReturn ($ parentsIds );
81
- $ this ->ruleMock ->expects ($ this ->exactly ($ runValidateAmount ))->method ('getConditions ' )
91
+ $ this ->productMock ->expects ($ this ->exactly ($ runValidateAmount ))
92
+ ->method ('getStoreId ' )
93
+ ->willReturn ($ storeId );
94
+ $ parentsProducts = array_map (
95
+ function ($ parentsId ) {
96
+ $ parent = $ this ->createMock (Product::class);
97
+ $ parent ->method ('getId ' )->willReturn ($ parentsId );
98
+ return $ parent ;
99
+ },
100
+ $ parentsIds
101
+ );
102
+ $ this ->productRepositoryMock ->expects ($ this ->exactly ($ runValidateAmount ))
103
+ ->method ('getById ' )
104
+ ->withConsecutive (
105
+ ...array_map (
106
+ function ($ parentsId ) use ($ storeId ) {
107
+ return [$ parentsId , false , $ storeId ];
108
+ },
109
+ $ parentsIds
110
+ )
111
+ )->willReturnOnConsecutiveCalls (...$ parentsProducts );
112
+ $ this ->ruleMock ->expects ($ this ->exactly ($ runValidateAmount ))
113
+ ->method ('getConditions ' )
82
114
->willReturn ($ this ->ruleConditionsMock );
83
- $ this ->ruleConditionsMock ->expects ($ this ->exactly ($ runValidateAmount ))->method ('validateByEntityId ' )
84
- ->willReturnMap ($ validationResult );
115
+ $ this ->ruleConditionsMock ->expects ($ this ->exactly ($ runValidateAmount ))
116
+ ->method ('validate ' )
117
+ ->withConsecutive (
118
+ ...array_map (
119
+ function ($ parentsProduct ) {
120
+ return [$ parentsProduct ];
121
+ },
122
+ $ parentsProducts
123
+ )
124
+ )
125
+ ->willReturnOnConsecutiveCalls (...$ validationResult );
85
126
86
127
$ this ->assertEquals (
87
128
$ result ,
@@ -97,31 +138,19 @@ public function dataProviderForValidateWithValidConfigurableProduct()
97
138
return [
98
139
[
99
140
[1 , 2 , 3 ],
100
- [
101
- [1 , false ],
102
- [2 , true ],
103
- [3 , true ],
104
- ],
141
+ [false , true , true ],
105
142
2 ,
106
143
true ,
107
144
],
108
145
[
109
146
[1 , 2 , 3 ],
110
- [
111
- [1 , true ],
112
- [2 , false ],
113
- [3 , true ],
114
- ],
147
+ [true , false , true ],
115
148
1 ,
116
149
true ,
117
150
],
118
151
[
119
152
[1 , 2 , 3 ],
120
- [
121
- [1 , false ],
122
- [2 , false ],
123
- [3 , false ],
124
- ],
153
+ [false , false , false ],
125
154
3 ,
126
155
false ,
127
156
],
0 commit comments