77
88use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
99
10+ /**
11+ * Unit tests for \Magento\Downloadable\Controller\Download\LinkSample.
12+ *
13+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14+ */
1015class LinkSampleTest extends \PHPUnit_Framework_TestCase
1116{
1217 /** @var \Magento\Downloadable\Controller\Download\LinkSample */
@@ -60,16 +65,21 @@ class LinkSampleTest extends \PHPUnit_Framework_TestCase
6065 */
6166 protected $ urlInterface ;
6267
68+ /**
69+ * @var \Magento\Catalog\Model\Product\SalabilityChecker|\PHPUnit_Framework_MockObject_MockObject
70+ */
71+ private $ salabilityCheckerMock ;
72+
6373 /**
6474 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
6575 */
6676 protected function setUp ()
6777 {
6878 $ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
6979
70- $ this ->request = $ this ->getMock (' Magento\Framework\App\RequestInterface ' );
80+ $ this ->request = $ this ->getMock (\ Magento \Framework \App \RequestInterface::class );
7181 $ this ->response = $ this ->getMock (
72- ' \Magento\Framework\App\ResponseInterface ' ,
82+ \Magento \Framework \App \ResponseInterface::class ,
7383 [
7484 'setHttpResponseCode ' ,
7585 'clearBody ' ,
@@ -81,7 +91,7 @@ protected function setUp()
8191 );
8292
8393 $ this ->helperData = $ this ->getMock (
84- ' Magento\Downloadable\Helper\Data ' ,
94+ \ Magento \Downloadable \Helper \Data::class ,
8595 [
8696 'getIsShareable '
8797 ],
@@ -90,7 +100,7 @@ protected function setUp()
90100 false
91101 );
92102 $ this ->downloadHelper = $ this ->getMock (
93- ' Magento\Downloadable\Helper\Download ' ,
103+ \ Magento \Downloadable \Helper \Download::class ,
94104 [
95105 'setResource ' ,
96106 'getFilename ' ,
@@ -104,7 +114,7 @@ protected function setUp()
104114 false
105115 );
106116 $ this ->product = $ this ->getMock (
107- ' Magento\Catalog\Model\Product ' ,
117+ \ Magento \Catalog \Model \Product::class ,
108118 [
109119 '_wakeup ' ,
110120 'load ' ,
@@ -117,28 +127,28 @@ protected function setUp()
117127 false
118128 );
119129 $ this ->messageManager = $ this ->getMock (
120- ' Magento\Framework\Message\ManagerInterface ' ,
130+ \ Magento \Framework \Message \ManagerInterface::class ,
121131 [],
122132 [],
123133 '' ,
124134 false
125135 );
126136 $ this ->redirect = $ this ->getMock (
127- ' Magento\Framework\App\Response\RedirectInterface ' ,
137+ \ Magento \Framework \App \Response \RedirectInterface::class ,
128138 [],
129139 [],
130140 '' ,
131141 false
132142 );
133143 $ this ->urlInterface = $ this ->getMock (
134- ' Magento\Framework\UrlInterface ' ,
144+ \ Magento \Framework \UrlInterface::class ,
135145 [],
136146 [],
137147 '' ,
138148 false
139149 );
140150 $ this ->objectManager = $ this ->getMock (
141- ' \Magento\Framework\ObjectManager\ObjectManager ' ,
151+ \Magento \Framework \ObjectManager \ObjectManager::class ,
142152 [
143153 'create ' ,
144154 'get '
@@ -147,39 +157,53 @@ protected function setUp()
147157 '' ,
148158 false
149159 );
160+ $ this ->salabilityCheckerMock = $ this ->getMock (
161+ \Magento \Catalog \Model \Product \SalabilityChecker::class,
162+ ['isSalable ' ],
163+ [],
164+ '' ,
165+ false
166+ );
150167 $ this ->linkSample = $ this ->objectManagerHelper ->getObject (
151- ' Magento\Downloadable\Controller\Download\LinkSample ' ,
168+ \ Magento \Downloadable \Controller \Download \LinkSample::class ,
152169 [
153170 'objectManager ' => $ this ->objectManager ,
154171 'request ' => $ this ->request ,
155172 'response ' => $ this ->response ,
156173 'messageManager ' => $ this ->messageManager ,
157- 'redirect ' => $ this ->redirect
174+ 'redirect ' => $ this ->redirect ,
175+ 'salabilityChecker ' => $ this ->salabilityCheckerMock ,
158176 ]
159177 );
160178 }
161179
180+ /**
181+ * Execute Download link's sample action with Url link.
182+ *
183+ * @return void
184+ */
162185 public function testExecuteLinkTypeUrl ()
163186 {
164- $ linkMock = $ this ->getMockBuilder (' Magento\Downloadable\Model\Link ' )
187+ $ linkMock = $ this ->getMockBuilder (\ Magento \Downloadable \Model \Link::class )
165188 ->disableOriginalConstructor ()
166189 ->setMethods (['getId ' , 'load ' , 'getSampleType ' , 'getSampleUrl ' ])
167190 ->getMock ();
168191
169192 $ this ->request ->expects ($ this ->once ())->method ('getParam ' )->with ('link_id ' , 0 )->willReturn ('some_link_id ' );
170193 $ this ->objectManager ->expects ($ this ->once ())
171194 ->method ('create ' )
172- ->with (' Magento\Downloadable\Model\Link ' )
195+ ->with (\ Magento \Downloadable \Model \Link::class )
173196 ->willReturn ($ linkMock );
174197 $ linkMock ->expects ($ this ->once ())->method ('load ' )->with ('some_link_id ' )->willReturnSelf ();
175198 $ linkMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ('some_link_id ' );
199+ $ this ->salabilityCheckerMock ->expects ($ this ->once ())->method ('isSalable ' )->willReturn (true );
176200 $ linkMock ->expects ($ this ->once ())->method ('getSampleType ' )->willReturn (
177201 \Magento \Downloadable \Helper \Download::LINK_TYPE_URL
178202 );
179203 $ linkMock ->expects ($ this ->once ())->method ('getSampleUrl ' )->willReturn ('sample_url ' );
180204 $ this ->objectManager ->expects ($ this ->at (1 ))
181205 ->method ('get ' )
182- ->with (' Magento\Downloadable\Helper\Download ' )
206+ ->with (\ Magento \Downloadable \Helper \Download::class )
183207 ->willReturn ($ this ->downloadHelper );
184208 $ this ->response ->expects ($ this ->once ())->method ('setHttpResponseCode ' )->with (200 )->willReturnSelf ();
185209 $ this ->response ->expects ($ this ->any ())->method ('setHeader ' )->willReturnSelf ();
@@ -194,39 +218,45 @@ public function testExecuteLinkTypeUrl()
194218 $ this ->assertEquals ($ this ->response , $ this ->linkSample ->execute ());
195219 }
196220
221+ /**
222+ * Execute Download link's sample action with File link.
223+ *
224+ * @return void
225+ */
197226 public function testExecuteLinkTypeFile ()
198227 {
199- $ linkMock = $ this ->getMockBuilder (' Magento\Downloadable\Model\Link ' )
228+ $ linkMock = $ this ->getMockBuilder (\ Magento \Downloadable \Model \Link::class )
200229 ->disableOriginalConstructor ()
201230 ->setMethods (['getId ' , 'load ' , 'getSampleType ' , 'getSampleUrl ' , 'getBaseSamplePath ' ])
202231 ->getMock ();
203- $ fileMock = $ this ->getMockBuilder (' Magento\Downloadable\Helper\File ' )
232+ $ fileMock = $ this ->getMockBuilder (\ Magento \Downloadable \Helper \File::class )
204233 ->disableOriginalConstructor ()
205234 ->setMethods (['getFilePath ' , 'load ' , 'getSampleType ' , 'getSampleUrl ' ])
206235 ->getMock ();
207236
208237 $ this ->request ->expects ($ this ->once ())->method ('getParam ' )->with ('link_id ' , 0 )->willReturn ('some_link_id ' );
209238 $ this ->objectManager ->expects ($ this ->at (0 ))
210239 ->method ('create ' )
211- ->with (' Magento\Downloadable\Model\Link ' )
240+ ->with (\ Magento \Downloadable \Model \Link::class )
212241 ->willReturn ($ linkMock );
213242 $ linkMock ->expects ($ this ->once ())->method ('load ' )->with ('some_link_id ' )->willReturnSelf ();
214243 $ linkMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ('some_link_id ' );
244+ $ this ->salabilityCheckerMock ->expects ($ this ->once ())->method ('isSalable ' )->willReturn (true );
215245 $ linkMock ->expects ($ this ->any ())->method ('getSampleType ' )->willReturn (
216246 \Magento \Downloadable \Helper \Download::LINK_TYPE_FILE
217247 );
218248 $ this ->objectManager ->expects ($ this ->at (1 ))
219249 ->method ('get ' )
220- ->with (' Magento\Downloadable\Helper\File ' )
250+ ->with (\ Magento \Downloadable \Helper \File::class )
221251 ->willReturn ($ fileMock );
222252 $ this ->objectManager ->expects ($ this ->at (2 ))
223253 ->method ('get ' )
224- ->with (' Magento\Downloadable\Model\Link ' )
254+ ->with (\ Magento \Downloadable \Model \Link::class )
225255 ->willReturn ($ linkMock );
226256 $ linkMock ->expects ($ this ->once ())->method ('getBaseSamplePath ' )->willReturn ('downloadable/files/link_samples ' );
227257 $ this ->objectManager ->expects ($ this ->at (3 ))
228258 ->method ('get ' )
229- ->with (' Magento\Downloadable\Helper\Download ' )
259+ ->with (\ Magento \Downloadable \Helper \Download::class )
230260 ->willReturn ($ this ->downloadHelper );
231261 $ this ->response ->expects ($ this ->once ())->method ('setHttpResponseCode ' )->with (200 )->willReturnSelf ();
232262 $ this ->response ->expects ($ this ->any ())->method ('setHeader ' )->willReturnSelf ();
0 commit comments