5
5
*/
6
6
namespace Magento \CatalogUrlRewrite \Test \Unit \Observer ;
7
7
8
+ use Magento \Catalog \Model \ResourceModel \Product \Collection ;
8
9
use Magento \CatalogUrlRewrite \Observer \UrlRewriteHandler ;
9
10
use Magento \CatalogUrlRewrite \Model \Category \ChildrenCategoriesProvider ;
11
+ use Magento \CatalogUrlRewrite \Model \CategoryBasedProductRewriteGenerator ;
10
12
use Magento \CatalogUrlRewrite \Model \CategoryUrlRewriteGenerator ;
11
13
use Magento \CatalogUrlRewrite \Model \ProductUrlRewriteGenerator ;
12
14
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
16
18
use Magento \UrlRewrite \Model \MergeDataProvider ;
17
19
use Magento \CatalogUrlRewrite \Model \CategoryProductUrlPathGenerator ;
18
20
21
+ /**
22
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23
+ */
19
24
class UrlRewriteHandlerTest extends \PHPUnit \Framework \TestCase
20
25
{
21
26
/**
@@ -58,6 +63,11 @@ class UrlRewriteHandlerTest extends \PHPUnit\Framework\TestCase
58
63
*/
59
64
private $ mergeDataProviderFactoryMock ;
60
65
66
+ /**
67
+ * @var MergeDataProvider|\PHPUnit_Framework_MockObject_MockObject
68
+ */
69
+ private $ mergeDataProviderMock ;
70
+
61
71
/**
62
72
* @var Json|\PHPUnit_Framework_MockObject_MockObject
63
73
*/
@@ -85,16 +95,15 @@ protected function setUp()
85
95
->setMethods (['create ' ])
86
96
->disableOriginalConstructor ()
87
97
->getMock ();
88
- $ mergeDataProviderMock = $ this ->getMockBuilder (MergeDataProvider::class)
98
+ $ this -> mergeDataProviderMock = $ this ->getMockBuilder (MergeDataProvider::class)
89
99
->disableOriginalConstructor ()
90
100
->getMock ();
91
101
$ this ->categoryBasedProductRewriteGeneratorMock = $ this ->getMockBuilder (CategoryProductUrlPathGenerator::class)
92
102
->disableOriginalConstructor ()
93
103
->getMock ();
94
104
$ this ->mergeDataProviderFactoryMock ->expects ($ this ->any ())
95
105
->method ('create ' )
96
- ->willReturn ($ mergeDataProviderMock );
97
-
106
+ ->willReturn ($ this ->mergeDataProviderMock );
98
107
$ this ->serializerMock = $ this ->getMockBuilder (Json::class)
99
108
->disableOriginalConstructor ()
100
109
->getMock ();
@@ -111,6 +120,72 @@ protected function setUp()
111
120
);
112
121
}
113
122
123
+ /**
124
+ * @test
125
+ */
126
+ public function testGenerateProductUrlRewrites ()
127
+ {
128
+ /* @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject $category */
129
+ $ category = $ this ->getMockBuilder (\Magento \Catalog \Model \Category::class)
130
+ ->setMethods (['getEntityId ' , 'getStoreId ' , 'getData ' , 'getAffectedProductIds ' ])
131
+ ->disableOriginalConstructor ()
132
+ ->getMock ();
133
+ $ category ->expects ($ this ->any ())
134
+ ->method ('getEntityId ' )
135
+ ->willReturn (2 );
136
+ $ category ->expects ($ this ->any ())
137
+ ->method ('getStoreId ' )
138
+ ->willReturn (1 );
139
+ $ category ->expects ($ this ->any ())
140
+ ->method ('getData ' )
141
+ ->with ('save_rewrites_history ' )
142
+ ->willReturn (true );
143
+
144
+ /* @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject $childCategory1 */
145
+ $ childCategory1 = $ this ->getMockBuilder (\Magento \Catalog \Model \Category::class)
146
+ ->setMethods (['getEntityId ' ])
147
+ ->disableOriginalConstructor ()
148
+ ->getMock ();
149
+ $ childCategory1 ->expects ($ this ->any ())
150
+ ->method ('getEntityId ' )
151
+ ->willReturn (100 );
152
+
153
+ /* @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject $childCategory1 */
154
+ $ childCategory2 = $ this ->getMockBuilder (\Magento \Catalog \Model \Category::class)
155
+ ->setMethods (['getEntityId ' ])
156
+ ->disableOriginalConstructor ()
157
+ ->getMock ();
158
+ $ childCategory1 ->expects ($ this ->any ())
159
+ ->method ('getEntityId ' )
160
+ ->willReturn (200 );
161
+
162
+ $ this ->childrenCategoriesProviderMock ->expects ($ this ->once ())
163
+ ->method ('getChildren ' )
164
+ ->with ($ category , true )
165
+ ->willReturn ([$ childCategory1 , $ childCategory2 ]);
166
+
167
+ /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $productCollection */
168
+ $ productCollection = $ this ->getMockBuilder (\Magento \Catalog \Model \ResourceModel \Product \Collection::class)
169
+ ->disableOriginalConstructor ()
170
+ ->getMock ();
171
+ $ productCollection ->expects ($ this ->any ())
172
+ ->method ('addCategoriesFilter ' )
173
+ ->willReturnSelf ();
174
+ $ productCollection ->expects ($ this ->any ())
175
+ ->method ('addIdFilter ' )
176
+ ->willReturnSelf ();
177
+ $ productCollection ->expects ($ this ->any ())->method ('setStoreId ' )->willReturnSelf ();
178
+ $ productCollection ->expects ($ this ->any ())->method ('addAttributeToSelect ' )->willReturnSelf ();
179
+ $ iterator = new \ArrayIterator ([]);
180
+ $ productCollection ->expects ($ this ->any ())->method ('getIterator ' )->will ($ this ->returnValue ($ iterator ));
181
+
182
+ $ this ->collectionFactoryMock ->expects ($ this ->any ())->method ('create ' )->willReturn ($ productCollection );
183
+
184
+ $ this ->mergeDataProviderMock ->expects ($ this ->any ())->method ('getData ' )->willReturn ([1 , 2 ]);
185
+
186
+ $ this ->urlRewriteHandler ->generateProductUrlRewrites ($ category );
187
+ }
188
+
114
189
public function testDeleteCategoryRewritesForChildren ()
115
190
{
116
191
$ category = $ this ->getMockBuilder (\Magento \Catalog \Model \Category::class)
0 commit comments