SampleRepositoryTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Test\Unit\Model;
  7. use Magento\Downloadable\Model\SampleRepository;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class SampleRepositoryTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $repositoryMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $productTypeMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $contentValidatorMock;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $contentUploaderMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $jsonEncoderMock;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $sampleFactoryMock;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $productMock;
  41. /**
  42. * @var SampleRepository
  43. */
  44. protected $service;
  45. /**
  46. * @var \PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $sampleDataObjectFactory;
  49. /**
  50. * @var \PHPUnit_Framework_MockObject_MockObject
  51. */
  52. protected $metadataPoolMock;
  53. /**
  54. * @var \PHPUnit_Framework_MockObject_MockObject
  55. */
  56. protected $sampleHandlerMock;
  57. /**
  58. * @var \PHPUnit_Framework_MockObject_MockObject
  59. */
  60. protected $entityMetadataMock;
  61. protected function setUp()
  62. {
  63. $this->productMock = $this->createPartialMock(
  64. \Magento\Catalog\Model\Product::class,
  65. ['__wakeup', 'getTypeId', 'setDownloadableData', 'save', 'getId', 'getStoreId', 'getData']
  66. );
  67. $this->repositoryMock = $this->createMock(\Magento\Catalog\Model\ProductRepository::class);
  68. $this->productTypeMock = $this->createMock(\Magento\Downloadable\Model\Product\Type::class);
  69. $this->contentValidatorMock = $this->createMock(\Magento\Downloadable\Model\Sample\ContentValidator::class);
  70. $this->contentUploaderMock = $this->createMock(
  71. \Magento\Downloadable\Api\Data\File\ContentUploaderInterface::class
  72. );
  73. $this->jsonEncoderMock = $this->createMock(
  74. \Magento\Framework\Json\EncoderInterface::class
  75. );
  76. $this->sampleFactoryMock = $this->createPartialMock(
  77. \Magento\Downloadable\Model\SampleFactory::class,
  78. ['create']
  79. );
  80. $this->productTypeMock = $this->getMockBuilder(\Magento\Downloadable\Model\Product\Type::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $this->sampleDataObjectFactory = $this->getMockBuilder(
  84. \Magento\Downloadable\Api\Data\SampleInterfaceFactory::class
  85. )->setMethods(['create'])
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $this->service = new \Magento\Downloadable\Model\SampleRepository(
  89. $this->repositoryMock,
  90. $this->productTypeMock,
  91. $this->sampleDataObjectFactory,
  92. $this->contentValidatorMock,
  93. $this->contentUploaderMock,
  94. $this->jsonEncoderMock,
  95. $this->sampleFactoryMock
  96. );
  97. $this->entityMetadataMock = $this->getMockBuilder(
  98. \Magento\Framework\EntityManager\EntityMetadataInterface::class
  99. )->getMockForAbstractClass();
  100. $linkRepository = new \ReflectionClass(get_class($this->service));
  101. $metadataPoolProperty = $linkRepository->getProperty('metadataPool');
  102. $this->metadataPoolMock = $this->getMockBuilder(
  103. \Magento\Framework\EntityManager\MetadataPool::class
  104. )->disableOriginalConstructor()->getMock();
  105. $metadataPoolProperty->setAccessible(true);
  106. $metadataPoolProperty->setValue(
  107. $this->service,
  108. $this->metadataPoolMock
  109. );
  110. $saveHandlerProperty = $linkRepository->getProperty('sampleTypeHandler');
  111. $this->sampleHandlerMock = $this->getMockBuilder(
  112. \Magento\Downloadable\Model\Product\TypeHandler\Sample::class
  113. )->disableOriginalConstructor()->getMock();
  114. $saveHandlerProperty->setAccessible(true);
  115. $saveHandlerProperty->setValue(
  116. $this->service,
  117. $this->sampleHandlerMock
  118. );
  119. $this->metadataPoolMock->expects($this->any())->method('getMetadata')->willReturn($this->entityMetadataMock);
  120. }
  121. /**
  122. * @param array $sampleData
  123. * @return \PHPUnit_Framework_MockObject_MockObject
  124. */
  125. protected function getSampleMock(array $sampleData)
  126. {
  127. $sampleMock = $this->createMock(\Magento\Downloadable\Api\Data\SampleInterface::class);
  128. if (isset($sampleData['id'])) {
  129. $sampleMock->expects($this->any())->method('getId')->willReturn($sampleData['id']);
  130. }
  131. $sampleMock->expects($this->any())->method('getTitle')->will($this->returnValue($sampleData['title']));
  132. $sampleMock->expects($this->any())->method('getSortOrder')->will($this->returnValue(
  133. $sampleData['sort_order']
  134. ));
  135. if (isset($sampleData['sample_type'])) {
  136. $sampleMock->expects($this->any())->method('getSampleType')->will($this->returnValue(
  137. $sampleData['sample_type']
  138. ));
  139. }
  140. if (isset($sampleData['sample_url'])) {
  141. $sampleMock->expects($this->any())->method('getSampleUrl')->will($this->returnValue(
  142. $sampleData['sample_url']
  143. ));
  144. }
  145. if (isset($sampleData['sample_file'])) {
  146. $sampleMock->expects($this->any())->method('getSampleFile')->will($this->returnValue(
  147. $sampleData['sample_file']
  148. ));
  149. }
  150. return $sampleMock;
  151. }
  152. public function testCreate()
  153. {
  154. $productSku = 'simple';
  155. $sampleData = [
  156. 'title' => 'Title',
  157. 'sort_order' => 1,
  158. 'sample_type' => 'url',
  159. 'sample_url' => 'http://example.com/',
  160. ];
  161. $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
  162. ->will($this->returnValue($this->productMock));
  163. $this->productMock->expects($this->any())->method('getTypeId')->will($this->returnValue('downloadable'));
  164. $sampleMock = $this->getSampleMock($sampleData);
  165. $this->contentValidatorMock->expects($this->any())->method('isValid')->with($sampleMock)
  166. ->will($this->returnValue(true));
  167. $this->sampleHandlerMock->expects($this->once())->method('save')->with(
  168. $this->productMock,
  169. [
  170. 'sample' => [
  171. [
  172. 'sample_id' => 0,
  173. 'is_delete' => 0,
  174. 'type' => $sampleData['sample_type'],
  175. 'sort_order' => $sampleData['sort_order'],
  176. 'title' => $sampleData['title'],
  177. 'sample_url' => $sampleData['sample_url'],
  178. ],
  179. ],
  180. ]
  181. );
  182. $this->service->save($productSku, $sampleMock);
  183. }
  184. /**
  185. * @expectedException \Magento\Framework\Exception\InputException
  186. * @expectedExceptionMessage The sample title is empty. Enter the title and try again.
  187. */
  188. public function testCreateThrowsExceptionIfTitleIsEmpty()
  189. {
  190. $productSku = 'simple';
  191. $sampleData = [
  192. 'title' => '',
  193. 'sort_order' => 1,
  194. 'sample_type' => 'url',
  195. 'sample_url' => 'http://example.com/',
  196. ];
  197. $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
  198. ->will($this->returnValue($this->productMock));
  199. $this->productMock->expects($this->any())->method('getTypeId')->will($this->returnValue('downloadable'));
  200. $sampleMock = $this->getSampleMock($sampleData);
  201. $this->contentValidatorMock->expects($this->any())->method('isValid')->with($sampleMock)
  202. ->will($this->returnValue(true));
  203. $this->sampleHandlerMock->expects($this->never())->method('save');
  204. $this->service->save($productSku, $sampleMock);
  205. }
  206. public function testUpdate()
  207. {
  208. $sampleId = 1;
  209. $productId = 1;
  210. $productSku = 'simple';
  211. $sampleData = [
  212. 'id' => $sampleId,
  213. 'title' => 'Updated Title',
  214. 'sort_order' => 1,
  215. 'sample_type' => 'url',
  216. 'sample_url' => 'http://example.com/',
  217. ];
  218. $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
  219. ->will($this->returnValue($this->productMock));
  220. $this->productMock->expects($this->any())->method('getData')->will($this->returnValue($productId));
  221. $existingSampleMock = $this->createPartialMock(
  222. \Magento\Downloadable\Model\Sample::class,
  223. ['__wakeup', 'getId', 'load', 'getProductId']
  224. );
  225. $this->sampleFactoryMock->expects($this->once())->method('create')
  226. ->will($this->returnValue($existingSampleMock));
  227. $sampleMock = $this->getSampleMock($sampleData);
  228. $this->contentValidatorMock->expects($this->any())->method('isValid')->with($sampleMock)
  229. ->will($this->returnValue(true));
  230. $existingSampleMock->expects($this->any())->method('getId')->will($this->returnValue($sampleId));
  231. $existingSampleMock->expects($this->any())->method('getProductId')->will($this->returnValue($productId));
  232. $existingSampleMock->expects($this->once())->method('load')->with($sampleId)->will($this->returnSelf());
  233. $this->sampleHandlerMock->expects($this->once())->method('save')->with(
  234. $this->productMock,
  235. [
  236. 'sample' => [
  237. [
  238. 'sample_id' => $sampleId,
  239. 'is_delete' => 0,
  240. 'type' => $sampleData['sample_type'],
  241. 'sort_order' => $sampleData['sort_order'],
  242. 'title' => $sampleData['title'],
  243. 'sample_url' => $sampleData['sample_url'],
  244. ],
  245. ],
  246. ]
  247. );
  248. $this->assertEquals($sampleId, $this->service->save($productSku, $sampleMock));
  249. }
  250. public function testUpdateWithExistingFile()
  251. {
  252. $sampleId = 1;
  253. $productId = 1;
  254. $productSku = 'simple';
  255. $sampleFile = '/s/a/sample.jpg';
  256. $encodedFile = 'something';
  257. $sampleData = [
  258. 'id' => $sampleId,
  259. 'title' => 'Updated Title',
  260. 'sort_order' => 1,
  261. 'sample_type' => 'file',
  262. 'sample_file' => $sampleFile,
  263. ];
  264. $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
  265. ->will($this->returnValue($this->productMock));
  266. $this->productMock->expects($this->any())->method('getData')->will($this->returnValue($productId));
  267. $existingSampleMock = $this->createPartialMock(
  268. \Magento\Downloadable\Model\Sample::class,
  269. ['__wakeup', 'getId', 'load', 'getProductId']
  270. );
  271. $this->sampleFactoryMock->expects($this->once())->method('create')
  272. ->will($this->returnValue($existingSampleMock));
  273. $sampleMock = $this->getSampleMock($sampleData);
  274. $this->contentValidatorMock->expects($this->any())->method('isValid')->with($sampleMock)
  275. ->will($this->returnValue(true));
  276. $existingSampleMock->expects($this->any())->method('getId')->will($this->returnValue($sampleId));
  277. $existingSampleMock->expects($this->any())->method('getProductId')->will($this->returnValue($productId));
  278. $existingSampleMock->expects($this->once())->method('load')->with($sampleId)->will($this->returnSelf());
  279. $this->jsonEncoderMock->expects($this->once())
  280. ->method('encode')
  281. ->with(
  282. [
  283. [
  284. 'file' => $sampleFile,
  285. 'status' => 'old',
  286. ]
  287. ]
  288. )->willReturn($encodedFile);
  289. $this->sampleHandlerMock->expects($this->once())->method('save')->with(
  290. $this->productMock,
  291. [
  292. 'sample' => [
  293. [
  294. 'sample_id' => $sampleId,
  295. 'is_delete' => 0,
  296. 'type' => $sampleData['sample_type'],
  297. 'sort_order' => $sampleData['sort_order'],
  298. 'title' => $sampleData['title'],
  299. 'file' => $encodedFile,
  300. ],
  301. ],
  302. ]
  303. );
  304. $this->assertEquals($sampleId, $this->service->save($productSku, $sampleMock));
  305. }
  306. /**
  307. * @expectedException \Magento\Framework\Exception\InputException
  308. * @expectedExceptionMessage The sample title is empty. Enter the title and try again.
  309. */
  310. public function testUpdateThrowsExceptionIfTitleIsEmptyAndScopeIsGlobal()
  311. {
  312. $sampleId = 1;
  313. $productSku = 'simple';
  314. $productId = 1;
  315. $sampleData = [
  316. 'id' => $sampleId,
  317. 'title' => '',
  318. 'sort_order' => 1,
  319. ];
  320. $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
  321. ->will($this->returnValue($this->productMock));
  322. $this->productMock->expects($this->any())->method('getData')->will($this->returnValue($productId));
  323. $existingSampleMock = $this->createPartialMock(
  324. \Magento\Downloadable\Model\Sample::class,
  325. ['__wakeup', 'getId', 'load', 'save', 'getProductId']
  326. );
  327. $existingSampleMock->expects($this->any())->method('getId')->will($this->returnValue($sampleId));
  328. $existingSampleMock->expects($this->once())->method('load')->with($sampleId)->will($this->returnSelf());
  329. $existingSampleMock->expects($this->any())->method('getProductId')->will($this->returnValue($productId));
  330. $this->sampleFactoryMock->expects($this->once())->method('create')
  331. ->will($this->returnValue($existingSampleMock));
  332. $sampleMock = $this->getSampleMock($sampleData);
  333. $this->contentValidatorMock->expects($this->any())->method('isValid')->with($sampleMock)
  334. ->will($this->returnValue(true));
  335. $this->sampleHandlerMock->expects($this->never())->method('save');
  336. $this->service->save($productSku, $sampleMock, true);
  337. }
  338. public function testDelete()
  339. {
  340. $sampleId = 1;
  341. $sampleMock = $this->createMock(\Magento\Downloadable\Model\Sample::class);
  342. $this->sampleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($sampleMock));
  343. $sampleMock->expects($this->once())->method('load')->with($sampleId)->will($this->returnSelf());
  344. $sampleMock->expects($this->any())->method('getId')->will($this->returnValue($sampleId));
  345. $sampleMock->expects($this->once())->method('delete');
  346. $this->assertTrue($this->service->delete($sampleId));
  347. }
  348. /**
  349. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  350. * @expectedExceptionMessage No downloadable sample with the provided ID was found. Verify the ID and try again.
  351. */
  352. public function testDeleteThrowsExceptionIfSampleIdIsNotValid()
  353. {
  354. $sampleId = 1;
  355. $sampleMock = $this->createMock(\Magento\Downloadable\Model\Sample::class);
  356. $this->sampleFactoryMock->expects($this->once())->method('create')->will($this->returnValue($sampleMock));
  357. $sampleMock->expects($this->once())->method('load')->with($sampleId)->will($this->returnSelf());
  358. $sampleMock->expects($this->once())->method('getId');
  359. $sampleMock->expects($this->never())->method('delete');
  360. $this->service->delete($sampleId);
  361. }
  362. public function testGetList()
  363. {
  364. $productSku = 'downloadable_sku';
  365. $sampleData = [
  366. 'id' => 324,
  367. 'store_title' => 'rock melody sample',
  368. 'title' => 'just melody sample',
  369. 'sort_order' => 21,
  370. 'sample_type' => 'file',
  371. 'sample_url' => null,
  372. 'sample_file' => '/r/o/rock.melody.ogg'
  373. ];
  374. $sampleMock = $this->createPartialMock(\Magento\Downloadable\Model\Sample::class, [
  375. 'getId',
  376. 'getStoreTitle',
  377. 'getTitle',
  378. 'getSampleType',
  379. 'getSampleFile',
  380. 'getSampleUrl',
  381. 'getSortOrder',
  382. 'getData',
  383. '__wakeup'
  384. ]);
  385. $sampleInterfaceMock = $this->createMock(\Magento\Downloadable\Api\Data\SampleInterface::class);
  386. $this->repositoryMock->expects($this->once())
  387. ->method('get')
  388. ->with($productSku)
  389. ->will($this->returnValue($this->productMock));
  390. $this->productTypeMock->expects($this->once())
  391. ->method('getSamples')
  392. ->with($this->productMock)
  393. ->will($this->returnValue([$sampleMock]));
  394. $this->setSampleAssertions($sampleMock, $sampleData);
  395. $this->sampleDataObjectFactory->expects($this->once())->method('create')->willReturn($sampleInterfaceMock);
  396. $this->assertEquals([$sampleInterfaceMock], $this->service->getList($productSku));
  397. }
  398. /**
  399. * @param $resource
  400. * @param $inputData
  401. */
  402. protected function setSampleAssertions($resource, $inputData)
  403. {
  404. $resource->expects($this->any())->method('getId')->will($this->returnValue($inputData['id']));
  405. $resource->expects($this->any())->method('getStoreTitle')
  406. ->will($this->returnValue($inputData['store_title']));
  407. $resource->expects($this->any())->method('getTitle')
  408. ->will($this->returnValue($inputData['title']));
  409. $resource->expects($this->any())->method('getSortOrder')
  410. ->will($this->returnValue($inputData['sort_order']));
  411. $resource->expects($this->any())->method('getSampleType')
  412. ->will($this->returnValue($inputData['sample_type']));
  413. $resource->expects($this->any())->method('getSampleFile')
  414. ->will($this->returnValue($inputData['sample_file']));
  415. $resource->expects($this->any())->method('getSampleUrl')
  416. ->will($this->returnValue($inputData['sample_url']));
  417. }
  418. }