3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
declare (strict_types=1 );
7
8
8
9
namespace Magento \Downloadable \Test \Unit \Model \Link ;
16
17
use PHPUnit \Framework \MockObject \MockObject ;
17
18
use PHPUnit \Framework \TestCase ;
18
19
20
+ /**
21
+ * Test for \Magento\Downloadable\Model\Link\UpdateHandler.
22
+ */
19
23
class UpdateHandlerTest extends TestCase
20
24
{
21
- /** @var UpdateHandler */
22
- protected $ model ;
23
-
24
- /** @var LinkRepositoryInterface|MockObject */
25
- protected $ linkRepositoryMock ;
26
-
25
+ /**
26
+ * @var UpdateHandler
27
+ */
28
+ private $ model ;
29
+
30
+ /**
31
+ * @var LinkRepositoryInterface|MockObject
32
+ */
33
+ private $ linkRepositoryMock ;
34
+
35
+ /**
36
+ * @var LinkInterface|MockObject
37
+ */
38
+ private $ linkMock ;
39
+
40
+ /**
41
+ * @var ProductExtensionInterface|MockObject
42
+ */
43
+ private $ productExtensionMock ;
44
+
45
+ /**
46
+ * @var ProductInterface|MockObject
47
+ */
48
+ private $ entityMock ;
49
+
50
+ /**
51
+ * @inheritdoc
52
+ */
27
53
protected function setUp (): void
28
54
{
29
55
$ this ->linkRepositoryMock = $ this ->getMockBuilder (LinkRepositoryInterface::class)
30
56
->getMockForAbstractClass ();
57
+ $ this ->linkMock = $ this ->getMockBuilder (LinkInterface::class)
58
+ ->getMock ();
59
+ $ this ->productExtensionMock = $ this ->createMock (ProductExtensionInterface::class);
60
+ $ this ->productExtensionMock ->expects ($ this ->once ())
61
+ ->method ('getDownloadableProductLinks ' )
62
+ ->willReturn ([$ this ->linkMock ]);
63
+ $ this ->entityMock = $ this ->getMockBuilder (ProductInterface::class)
64
+ ->addMethods (['getStoreId ' ])
65
+ ->getMockForAbstractClass ();
31
66
32
67
$ this ->model = new UpdateHandler (
33
68
$ this ->linkRepositoryMock
34
69
);
35
70
}
36
71
37
- public function testExecute ()
72
+ /**
73
+ * Update links for downloadable product
74
+ *
75
+ * @return void
76
+ */
77
+ public function testExecute (): void
38
78
{
39
79
$ entitySku = 'sku ' ;
40
80
$ entityStoreId = 0 ;
41
- $ linkId = 11 ;
42
81
$ linkToDeleteId = 22 ;
43
82
44
- /** @var LinkInterface|MockObject $linkMock */
45
- $ linkMock = $ this ->getMockBuilder (LinkInterface::class)
46
- ->getMock ();
47
- $ linkMock ->expects ($ this ->exactly (3 ))
83
+ $ this ->linkMock ->expects ($ this ->exactly (3 ))
48
84
->method ('getId ' )
49
- ->willReturn ($ linkId );
85
+ ->willReturn (1 );
50
86
51
87
/** @var LinkInterface|MockObject $linkToDeleteMock */
52
88
$ linkToDeleteMock = $ this ->getMockBuilder (LinkInterface::class)
@@ -55,59 +91,49 @@ public function testExecute()
55
91
->method ('getId ' )
56
92
->willReturn ($ linkToDeleteId );
57
93
58
- /** @var ProductExtensionInterface|MockObject $productExtensionMock */
59
- $ productExtensionMock = $ this ->getMockBuilder (ProductExtensionInterface::class)
60
- ->setMethods (['getDownloadableProductLinks ' ])
61
- ->getMockForAbstractClass ();
62
- $ productExtensionMock ->expects ($ this ->once ())
63
- ->method ('getDownloadableProductLinks ' )
64
- ->willReturn ([$ linkMock ]);
65
-
66
- /** @var ProductInterface|MockObject $entityMock */
67
- $ entityMock = $ this ->getMockBuilder (ProductInterface::class)
68
- ->setMethods (['getTypeId ' , 'getExtensionAttributes ' , 'getSku ' , 'getStoreId ' ])
69
- ->getMockForAbstractClass ();
70
- $ entityMock ->expects ($ this ->once ())
94
+ $ this ->entityMock ->expects ($ this ->once ())
71
95
->method ('getTypeId ' )
72
96
->willReturn (Type::TYPE_DOWNLOADABLE );
73
- $ entityMock ->expects ($ this ->once ())
97
+ $ this -> entityMock ->expects ($ this ->once ())
74
98
->method ('getExtensionAttributes ' )
75
- ->willReturn ($ productExtensionMock );
76
- $ entityMock ->expects ($ this ->exactly (2 ))
99
+ ->willReturn ($ this -> productExtensionMock );
100
+ $ this -> entityMock ->expects ($ this ->exactly (2 ))
77
101
->method ('getSku ' )
78
102
->willReturn ($ entitySku );
79
- $ entityMock ->expects ($ this ->once ())
103
+ $ this -> entityMock ->expects ($ this ->once ())
80
104
->method ('getStoreId ' )
81
105
->willReturn ($ entityStoreId );
82
106
83
107
$ this ->linkRepositoryMock ->expects ($ this ->once ())
84
108
->method ('getList ' )
85
109
->with ($ entitySku )
86
- ->willReturn ([$ linkMock , $ linkToDeleteMock ]);
110
+ ->willReturn ([$ this -> linkMock , $ linkToDeleteMock ]);
87
111
$ this ->linkRepositoryMock ->expects ($ this ->once ())
88
112
->method ('save ' )
89
- ->with ($ entitySku , $ linkMock , !$ entityStoreId );
113
+ ->with ($ entitySku , $ this -> linkMock , !$ entityStoreId );
90
114
$ this ->linkRepositoryMock ->expects ($ this ->once ())
91
115
->method ('delete ' )
92
116
->with ($ linkToDeleteId );
93
117
94
- $ this ->assertEquals ($ entityMock , $ this ->model ->execute ($ entityMock ));
118
+ $ this ->assertEquals ($ this -> entityMock , $ this ->model ->execute ($ this -> entityMock ));
95
119
}
96
120
97
- public function testExecuteNonDownloadable ()
121
+ /**
122
+ * Update links for non downloadable product
123
+ *
124
+ * @return void
125
+ */
126
+ public function testExecuteNonDownloadable (): void
98
127
{
99
- /** @var ProductInterface|MockObject $entityMock */
100
- $ entityMock = $ this ->getMockBuilder (ProductInterface::class)
101
- ->setMethods (['getTypeId ' , 'getExtensionAttributes ' , 'getSku ' , 'getStoreId ' ])
102
- ->getMockForAbstractClass ();
103
- $ entityMock ->expects ($ this ->once ())
128
+ $ this ->entityMock ->expects ($ this ->once ())
104
129
->method ('getTypeId ' )
105
130
->willReturn (Type::TYPE_DOWNLOADABLE . 'some ' );
106
- $ entityMock ->expects ($ this ->never ())
107
- ->method ('getExtensionAttributes ' );
108
- $ entityMock ->expects ($ this ->never ())
131
+ $ this ->entityMock ->expects ($ this ->once ())
132
+ ->method ('getExtensionAttributes ' )
133
+ ->willReturn ($ this ->productExtensionMock );
134
+ $ this ->entityMock ->expects ($ this ->never ())
109
135
->method ('getSku ' );
110
- $ entityMock ->expects ($ this ->never ())
136
+ $ this -> entityMock ->expects ($ this ->never ())
111
137
->method ('getStoreId ' );
112
138
113
139
$ this ->linkRepositoryMock ->expects ($ this ->never ())
@@ -117,6 +143,6 @@ public function testExecuteNonDownloadable()
117
143
$ this ->linkRepositoryMock ->expects ($ this ->never ())
118
144
->method ('delete ' );
119
145
120
- $ this ->assertEquals ($ entityMock , $ this ->model ->execute ($ entityMock ));
146
+ $ this ->assertEquals ($ this -> entityMock , $ this ->model ->execute ($ this -> entityMock ));
121
147
}
122
148
}
0 commit comments