7
7
8
8
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
9
9
10
+ /**
11
+ * Unit tests for \Magento\Downloadable\Controller\Download\LinkSample.
12
+ *
13
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14
+ */
10
15
class LinkSampleTest extends \PHPUnit_Framework_TestCase
11
16
{
12
17
/** @var \Magento\Downloadable\Controller\Download\LinkSample */
@@ -60,16 +65,21 @@ class LinkSampleTest extends \PHPUnit_Framework_TestCase
60
65
*/
61
66
protected $ urlInterface ;
62
67
68
+ /**
69
+ * @var \Magento\Catalog\Model\Product\SalabilityChecker|\PHPUnit_Framework_MockObject_MockObject
70
+ */
71
+ private $ salabilityCheckerMock ;
72
+
63
73
/**
64
74
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
65
75
*/
66
76
protected function setUp ()
67
77
{
68
78
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
69
79
70
- $ this ->request = $ this ->getMock (' Magento\Framework\App\RequestInterface ' );
80
+ $ this ->request = $ this ->getMock (\ Magento \Framework \App \RequestInterface::class );
71
81
$ this ->response = $ this ->getMock (
72
- ' \Magento\Framework\App\ResponseInterface ' ,
82
+ \Magento \Framework \App \ResponseInterface::class ,
73
83
[
74
84
'setHttpResponseCode ' ,
75
85
'clearBody ' ,
@@ -81,7 +91,7 @@ protected function setUp()
81
91
);
82
92
83
93
$ this ->helperData = $ this ->getMock (
84
- ' Magento\Downloadable\Helper\Data ' ,
94
+ \ Magento \Downloadable \Helper \Data::class ,
85
95
[
86
96
'getIsShareable '
87
97
],
@@ -90,7 +100,7 @@ protected function setUp()
90
100
false
91
101
);
92
102
$ this ->downloadHelper = $ this ->getMock (
93
- ' Magento\Downloadable\Helper\Download ' ,
103
+ \ Magento \Downloadable \Helper \Download::class ,
94
104
[
95
105
'setResource ' ,
96
106
'getFilename ' ,
@@ -104,7 +114,7 @@ protected function setUp()
104
114
false
105
115
);
106
116
$ this ->product = $ this ->getMock (
107
- ' Magento\Catalog\Model\Product ' ,
117
+ \ Magento \Catalog \Model \Product::class ,
108
118
[
109
119
'_wakeup ' ,
110
120
'load ' ,
@@ -117,28 +127,28 @@ protected function setUp()
117
127
false
118
128
);
119
129
$ this ->messageManager = $ this ->getMock (
120
- ' Magento\Framework\Message\ManagerInterface ' ,
130
+ \ Magento \Framework \Message \ManagerInterface::class ,
121
131
[],
122
132
[],
123
133
'' ,
124
134
false
125
135
);
126
136
$ this ->redirect = $ this ->getMock (
127
- ' Magento\Framework\App\Response\RedirectInterface ' ,
137
+ \ Magento \Framework \App \Response \RedirectInterface::class ,
128
138
[],
129
139
[],
130
140
'' ,
131
141
false
132
142
);
133
143
$ this ->urlInterface = $ this ->getMock (
134
- ' Magento\Framework\UrlInterface ' ,
144
+ \ Magento \Framework \UrlInterface::class ,
135
145
[],
136
146
[],
137
147
'' ,
138
148
false
139
149
);
140
150
$ this ->objectManager = $ this ->getMock (
141
- ' \Magento\Framework\ObjectManager\ObjectManager ' ,
151
+ \Magento \Framework \ObjectManager \ObjectManager::class ,
142
152
[
143
153
'create ' ,
144
154
'get '
@@ -147,39 +157,53 @@ protected function setUp()
147
157
'' ,
148
158
false
149
159
);
160
+ $ this ->salabilityCheckerMock = $ this ->getMock (
161
+ \Magento \Catalog \Model \Product \SalabilityChecker::class,
162
+ ['isSalable ' ],
163
+ [],
164
+ '' ,
165
+ false
166
+ );
150
167
$ this ->linkSample = $ this ->objectManagerHelper ->getObject (
151
- ' Magento\Downloadable\Controller\Download\LinkSample ' ,
168
+ \ Magento \Downloadable \Controller \Download \LinkSample::class ,
152
169
[
153
170
'objectManager ' => $ this ->objectManager ,
154
171
'request ' => $ this ->request ,
155
172
'response ' => $ this ->response ,
156
173
'messageManager ' => $ this ->messageManager ,
157
- 'redirect ' => $ this ->redirect
174
+ 'redirect ' => $ this ->redirect ,
175
+ 'salabilityChecker ' => $ this ->salabilityCheckerMock ,
158
176
]
159
177
);
160
178
}
161
179
180
+ /**
181
+ * Execute Download link's sample action with Url link.
182
+ *
183
+ * @return void
184
+ */
162
185
public function testExecuteLinkTypeUrl ()
163
186
{
164
- $ linkMock = $ this ->getMockBuilder (' Magento\Downloadable\Model\Link ' )
187
+ $ linkMock = $ this ->getMockBuilder (\ Magento \Downloadable \Model \Link::class )
165
188
->disableOriginalConstructor ()
166
189
->setMethods (['getId ' , 'load ' , 'getSampleType ' , 'getSampleUrl ' ])
167
190
->getMock ();
168
191
169
192
$ this ->request ->expects ($ this ->once ())->method ('getParam ' )->with ('link_id ' , 0 )->willReturn ('some_link_id ' );
170
193
$ this ->objectManager ->expects ($ this ->once ())
171
194
->method ('create ' )
172
- ->with (' Magento\Downloadable\Model\Link ' )
195
+ ->with (\ Magento \Downloadable \Model \Link::class )
173
196
->willReturn ($ linkMock );
174
197
$ linkMock ->expects ($ this ->once ())->method ('load ' )->with ('some_link_id ' )->willReturnSelf ();
175
198
$ linkMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ('some_link_id ' );
199
+ $ this ->salabilityCheckerMock ->expects ($ this ->once ())->method ('isSalable ' )->willReturn (true );
176
200
$ linkMock ->expects ($ this ->once ())->method ('getSampleType ' )->willReturn (
177
201
\Magento \Downloadable \Helper \Download::LINK_TYPE_URL
178
202
);
179
203
$ linkMock ->expects ($ this ->once ())->method ('getSampleUrl ' )->willReturn ('sample_url ' );
180
204
$ this ->objectManager ->expects ($ this ->at (1 ))
181
205
->method ('get ' )
182
- ->with (' Magento\Downloadable\Helper\Download ' )
206
+ ->with (\ Magento \Downloadable \Helper \Download::class )
183
207
->willReturn ($ this ->downloadHelper );
184
208
$ this ->response ->expects ($ this ->once ())->method ('setHttpResponseCode ' )->with (200 )->willReturnSelf ();
185
209
$ this ->response ->expects ($ this ->any ())->method ('setHeader ' )->willReturnSelf ();
@@ -194,39 +218,45 @@ public function testExecuteLinkTypeUrl()
194
218
$ this ->assertEquals ($ this ->response , $ this ->linkSample ->execute ());
195
219
}
196
220
221
+ /**
222
+ * Execute Download link's sample action with File link.
223
+ *
224
+ * @return void
225
+ */
197
226
public function testExecuteLinkTypeFile ()
198
227
{
199
- $ linkMock = $ this ->getMockBuilder (' Magento\Downloadable\Model\Link ' )
228
+ $ linkMock = $ this ->getMockBuilder (\ Magento \Downloadable \Model \Link::class )
200
229
->disableOriginalConstructor ()
201
230
->setMethods (['getId ' , 'load ' , 'getSampleType ' , 'getSampleUrl ' , 'getBaseSamplePath ' ])
202
231
->getMock ();
203
- $ fileMock = $ this ->getMockBuilder (' Magento\Downloadable\Helper\File ' )
232
+ $ fileMock = $ this ->getMockBuilder (\ Magento \Downloadable \Helper \File::class )
204
233
->disableOriginalConstructor ()
205
234
->setMethods (['getFilePath ' , 'load ' , 'getSampleType ' , 'getSampleUrl ' ])
206
235
->getMock ();
207
236
208
237
$ this ->request ->expects ($ this ->once ())->method ('getParam ' )->with ('link_id ' , 0 )->willReturn ('some_link_id ' );
209
238
$ this ->objectManager ->expects ($ this ->at (0 ))
210
239
->method ('create ' )
211
- ->with (' Magento\Downloadable\Model\Link ' )
240
+ ->with (\ Magento \Downloadable \Model \Link::class )
212
241
->willReturn ($ linkMock );
213
242
$ linkMock ->expects ($ this ->once ())->method ('load ' )->with ('some_link_id ' )->willReturnSelf ();
214
243
$ linkMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ('some_link_id ' );
244
+ $ this ->salabilityCheckerMock ->expects ($ this ->once ())->method ('isSalable ' )->willReturn (true );
215
245
$ linkMock ->expects ($ this ->any ())->method ('getSampleType ' )->willReturn (
216
246
\Magento \Downloadable \Helper \Download::LINK_TYPE_FILE
217
247
);
218
248
$ this ->objectManager ->expects ($ this ->at (1 ))
219
249
->method ('get ' )
220
- ->with (' Magento\Downloadable\Helper\File ' )
250
+ ->with (\ Magento \Downloadable \Helper \File::class )
221
251
->willReturn ($ fileMock );
222
252
$ this ->objectManager ->expects ($ this ->at (2 ))
223
253
->method ('get ' )
224
- ->with (' Magento\Downloadable\Model\Link ' )
254
+ ->with (\ Magento \Downloadable \Model \Link::class )
225
255
->willReturn ($ linkMock );
226
256
$ linkMock ->expects ($ this ->once ())->method ('getBaseSamplePath ' )->willReturn ('downloadable/files/link_samples ' );
227
257
$ this ->objectManager ->expects ($ this ->at (3 ))
228
258
->method ('get ' )
229
- ->with (' Magento\Downloadable\Helper\Download ' )
259
+ ->with (\ Magento \Downloadable \Helper \Download::class )
230
260
->willReturn ($ this ->downloadHelper );
231
261
$ this ->response ->expects ($ this ->once ())->method ('setHttpResponseCode ' )->with (200 )->willReturnSelf ();
232
262
$ this ->response ->expects ($ this ->any ())->method ('setHeader ' )->willReturnSelf ();
0 commit comments