11
11
use Magento \Catalog \Model \Product ;
12
12
use Magento \CatalogUrlRewrite \Model \ProductUrlRewriteGenerator ;
13
13
use Magento \CatalogUrlRewrite \Observer \ProductProcessUrlRewriteRemovingObserver ;
14
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
15
14
use Magento \Framework \Event ;
16
15
use Magento \Framework \Event \Observer ;
17
- use Magento \UrlRewrite \ Service \ V1 \ Data \ UrlRewrite ;
16
+ use Magento \Framework \ TestFramework \ Unit \ Helper \ ObjectManager ;
18
17
use Magento \UrlRewrite \Model \UrlPersistInterface ;
19
- use PHPUnit \ Framework \ TestCase ;
18
+ use Magento \ UrlRewrite \ Service \ V1 \ Data \ UrlRewrite ;
20
19
use PHPUnit \Framework \MockObject \MockObject ;
20
+ use PHPUnit \Framework \TestCase ;
21
21
22
22
/**
23
23
* Unit test for Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteRemovingObserver
24
24
*/
25
25
class ProductProcessUrlRewriteRemovingObserverTest extends TestCase
26
26
{
27
+ /*
28
+ * Stub product ID
29
+ */
30
+ private const STUB_PRODUCT_ID = 333 ;
31
+
27
32
/**
28
33
* Testable Object
29
34
*
@@ -54,7 +59,7 @@ class ProductProcessUrlRewriteRemovingObserverTest extends TestCase
54
59
/**
55
60
* @var UrlPersistInterface|MockObject
56
61
*/
57
- private $ urlPersist ;
62
+ private $ urlPersistMock ;
58
63
59
64
/**
60
65
* @inheritdoc
@@ -74,25 +79,23 @@ protected function setUp(): void
74
79
->setMethods (['getId ' ])
75
80
->getMock ();
76
81
77
- $ this ->urlPersist = $ this ->getMockBuilder (UrlPersistInterface::class)
82
+ $ this ->urlPersistMock = $ this ->getMockBuilder (UrlPersistInterface::class)
78
83
->disableOriginalConstructor ()
79
84
->getMock ();
80
85
81
86
$ this ->observer = $ this ->objectManager ->getObject (
82
87
ProductProcessUrlRewriteRemovingObserver::class,
83
88
[
84
- 'urlPersist ' => $ this ->urlPersist
89
+ 'urlPersist ' => $ this ->urlPersistMock
85
90
]
86
91
);
87
92
}
88
93
89
94
/**
90
- * Test for execute()
95
+ * Test for execute(), covers test case for removing product URLs from storage
91
96
*/
92
- public function testRemoveProductUrlsFromStorage ()
97
+ public function testRemoveProductUrlsFromStorage (): void
93
98
{
94
- $ productId = 333 ;
95
-
96
99
$ this ->observerMock
97
100
->expects ($ this ->once ())
98
101
->method ('getEvent ' )
@@ -106,15 +109,40 @@ public function testRemoveProductUrlsFromStorage()
106
109
$ this ->productMock
107
110
->expects ($ this ->exactly (2 ))
108
111
->method ('getId ' )
109
- ->willReturn ($ productId );
112
+ ->willReturn (self :: STUB_PRODUCT_ID );
110
113
111
- $ this ->urlPersist ->expects ($ this ->once ())
114
+ $ this ->urlPersistMock ->expects ($ this ->once ())
112
115
->method ('deleteByData ' )
113
116
->with ([
114
- UrlRewrite::ENTITY_ID => $ productId ,
117
+ UrlRewrite::ENTITY_ID => self :: STUB_PRODUCT_ID ,
115
118
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE ,
116
119
]);
117
120
118
121
$ this ->observer ->execute ($ this ->observerMock );
119
122
}
123
+
124
+ /**
125
+ * Test for execute(), covers test case for removing product URLs when the product doesn't have an ID
126
+ */
127
+ public function testRemoveProductUrlsWithEmptyProductId ()
128
+ {
129
+ $ this ->observerMock
130
+ ->expects ($ this ->once ())
131
+ ->method ('getEvent ' )
132
+ ->willReturn ($ this ->eventMock );
133
+
134
+ $ this ->eventMock
135
+ ->expects ($ this ->once ())
136
+ ->method ('getProduct ' )
137
+ ->willReturn ($ this ->productMock );
138
+
139
+ $ this ->productMock
140
+ ->expects ($ this ->once ())
141
+ ->method ('getId ' )
142
+ ->willReturn (null );
143
+
144
+ $ this ->urlPersistMock ->expects ($ this ->never ())->method ('deleteByData ' );
145
+
146
+ $ this ->observer ->execute ($ this ->observerMock );
147
+ }
120
148
}
0 commit comments