10
10
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
11
use Magento \Catalog \Model \Product ;
12
12
use Magento \Catalog \Model \Product \Type ;
13
+ use Magento \Catalog \Model \ResourceModel \Product \Website \Link as ProductWebsiteLink ;
13
14
use Magento \ConfigurableProduct \Model \Plugin \ProductIdentitiesExtender ;
14
15
use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
16
+ use Magento \Store \Model \Store ;
15
17
use PHPUnit \Framework \MockObject \MockObject ;
16
18
use PHPUnit \Framework \TestCase ;
17
19
@@ -30,6 +32,11 @@ class ProductIdentitiesExtenderTest extends TestCase
30
32
*/
31
33
private $ productRepositoryMock ;
32
34
35
+ /**
36
+ * @var ProductWebsiteLink|MockObject
37
+ */
38
+ private $ productWebsiteLinkMock ;
39
+
33
40
/**
34
41
* @var ProductIdentitiesExtender
35
42
*/
@@ -40,13 +47,15 @@ class ProductIdentitiesExtenderTest extends TestCase
40
47
*/
41
48
protected function setUp (): void
42
49
{
43
- $ this ->configurableTypeMock = $ this ->getMockBuilder (Configurable::class)
44
- ->disableOriginalConstructor ()
45
- ->getMock ();
46
- $ this ->productRepositoryMock = $ this ->getMockBuilder (ProductRepositoryInterface::class)
47
- ->getMock ();
50
+ $ this ->configurableTypeMock = $ this ->createMock (Configurable::class);
51
+ $ this ->productRepositoryMock = $ this ->createMock (ProductRepositoryInterface::class);
52
+ $ this ->productWebsiteLinkMock = $ this ->createMock (ProductWebsiteLink::class);
48
53
49
- $ this ->plugin = new ProductIdentitiesExtender ($ this ->configurableTypeMock , $ this ->productRepositoryMock );
54
+ $ this ->plugin = new ProductIdentitiesExtender (
55
+ $ this ->configurableTypeMock ,
56
+ $ this ->productRepositoryMock ,
57
+ $ this ->productWebsiteLinkMock
58
+ );
50
59
}
51
60
52
61
/**
@@ -58,25 +67,30 @@ public function testAfterGetIdentities()
58
67
{
59
68
$ productId = 1 ;
60
69
$ productIdentity = 'cache_tag_1 ' ;
61
- $ productMock = $ this ->getMockBuilder (Product::class)
62
- ->disableOriginalConstructor ()
63
- ->getMock ();
70
+ $ productMock = $ this ->createMock (Product::class);
64
71
$ parentProductId = 2 ;
65
72
$ parentProductIdentity = 'cache_tag_2 ' ;
66
- $ parentProductMock = $ this ->getMockBuilder (Product::class)
67
- ->disableOriginalConstructor ()
68
- ->getMock ();
73
+ $ parentProductMock = $ this ->createMock (Product::class);
69
74
70
75
$ productMock ->expects ($ this ->exactly (2 ))
71
76
->method ('getId ' )
72
77
->willReturn ($ productId );
73
78
$ productMock ->expects ($ this ->exactly (2 ))
74
79
->method ('getTypeId ' )
75
80
->willReturn (Type::TYPE_SIMPLE );
81
+ $ storeMock = $ this ->createMock (Store::class);
82
+ $ productMock ->expects ($ this ->atLeastOnce ())
83
+ ->method ('getStore ' )
84
+ ->willReturn ($ storeMock );
85
+ $ storeMock ->expects ($ this ->atLeastOnce ())
86
+ ->method ('getId ' )
87
+ ->willReturn (Store::DEFAULT_STORE_ID );
76
88
$ this ->configurableTypeMock ->expects ($ this ->once ())
77
89
->method ('getParentIdsByChild ' )
78
90
->with ($ productId )
79
91
->willReturn ([$ parentProductId ]);
92
+ $ this ->productWebsiteLinkMock ->expects ($ this ->never ())
93
+ ->method ('getWebsiteIdsByProductId ' );
80
94
$ this ->productRepositoryMock ->expects ($ this ->exactly (2 ))
81
95
->method ('getById ' )
82
96
->with ($ parentProductId )
@@ -95,4 +109,88 @@ public function testAfterGetIdentities()
95
109
$ productIdentities = $ this ->plugin ->afterGetIdentities ($ productMock , [$ productIdentity ]);
96
110
$ this ->assertEquals ([$ productIdentity , $ parentProductIdentity ], $ productIdentities );
97
111
}
112
+
113
+ public function testAfterGetIdentitiesWhenWebsitesMatched ()
114
+ {
115
+ $ productId = 1 ;
116
+ $ websiteId = 1 ;
117
+ $ productIdentity = 'cache_tag_1 ' ;
118
+ $ productMock = $ this ->createMock (Product::class);
119
+ $ parentProductId = 2 ;
120
+ $ parentProductIdentity = 'cache_tag_2 ' ;
121
+ $ parentProductMock = $ this ->createMock (Product::class);
122
+
123
+ $ productMock ->expects ($ this ->atLeastOnce ())
124
+ ->method ('getId ' )
125
+ ->willReturn ($ productId );
126
+ $ productMock ->expects ($ this ->once ())
127
+ ->method ('getTypeId ' )
128
+ ->willReturn (Type::TYPE_SIMPLE );
129
+ $ storeMock = $ this ->createMock (Store::class);
130
+ $ productMock ->expects ($ this ->once ())
131
+ ->method ('getStore ' )
132
+ ->willReturn ($ storeMock );
133
+ $ storeMock ->expects ($ this ->atLeastOnce ())
134
+ ->method ('getId ' )
135
+ ->willReturn (1 );
136
+ $ storeMock ->expects ($ this ->atLeastOnce ())
137
+ ->method ('getWebsiteId ' )
138
+ ->willReturn ($ websiteId );
139
+ $ this ->configurableTypeMock ->expects ($ this ->once ())
140
+ ->method ('getParentIdsByChild ' )
141
+ ->with ($ productId )
142
+ ->willReturn ([$ parentProductId ]);
143
+ $ this ->productWebsiteLinkMock ->expects ($ this ->once ())
144
+ ->method ('getWebsiteIdsByProductId ' )
145
+ ->with ($ parentProductId )
146
+ ->willReturn ([$ websiteId ]);
147
+ $ this ->productRepositoryMock ->expects ($ this ->once ())
148
+ ->method ('getById ' )
149
+ ->with ($ parentProductId )
150
+ ->willReturn ($ parentProductMock );
151
+ $ parentProductMock ->expects ($ this ->once ())
152
+ ->method ('getIdentities ' )
153
+ ->willReturn ([$ parentProductIdentity ]);
154
+
155
+ $ productIdentities = $ this ->plugin ->afterGetIdentities ($ productMock , [$ productIdentity ]);
156
+ $ this ->assertEquals ([$ productIdentity , $ parentProductIdentity ], $ productIdentities );
157
+ }
158
+
159
+ public function testAfterGetIdentitiesWhenWebsitesNotMatched ()
160
+ {
161
+ $ productId = 1 ;
162
+ $ productIdentity = 'cache_tag_1 ' ;
163
+ $ productMock = $ this ->createMock (Product::class);
164
+ $ parentProductId = 2 ;
165
+
166
+ $ productMock ->expects ($ this ->atLeastOnce ())
167
+ ->method ('getId ' )
168
+ ->willReturn ($ productId );
169
+ $ productMock ->expects ($ this ->once ())
170
+ ->method ('getTypeId ' )
171
+ ->willReturn (Type::TYPE_SIMPLE );
172
+ $ storeMock = $ this ->createMock (Store::class);
173
+ $ productMock ->expects ($ this ->once ())
174
+ ->method ('getStore ' )
175
+ ->willReturn ($ storeMock );
176
+ $ storeMock ->expects ($ this ->atLeastOnce ())
177
+ ->method ('getId ' )
178
+ ->willReturn (1 );
179
+ $ storeMock ->expects ($ this ->atLeastOnce ())
180
+ ->method ('getWebsiteId ' )
181
+ ->willReturn (1 );
182
+ $ this ->configurableTypeMock ->expects ($ this ->once ())
183
+ ->method ('getParentIdsByChild ' )
184
+ ->with ($ productId )
185
+ ->willReturn ([$ parentProductId ]);
186
+ $ this ->productWebsiteLinkMock ->expects ($ this ->once ())
187
+ ->method ('getWebsiteIdsByProductId ' )
188
+ ->with ($ parentProductId )
189
+ ->willReturn ([2 ]);
190
+ $ this ->productRepositoryMock ->expects ($ this ->never ())
191
+ ->method ('getById ' );
192
+
193
+ $ productIdentities = $ this ->plugin ->afterGetIdentities ($ productMock , [$ productIdentity ]);
194
+ $ this ->assertEquals ([$ productIdentity ], $ productIdentities );
195
+ }
98
196
}
0 commit comments