MediaTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Swatches\Test\Unit\Controller\Ajax;
  8. /**
  9. * Class Media
  10. */
  11. class MediaTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var array */
  14. private $mediaGallery;
  15. /** @var \Magento\Swatches\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
  16. private $swatchHelperMock;
  17. /** @var \Magento\Catalog\Model\ProductFactory|\PHPUnit_Framework_MockObject_MockObject */
  18. private $productModelFactoryMock;
  19. /** @var \Magento\PageCache\Model\Config|\PHPUnit_Framework_MockObject_MockObject */
  20. private $config;
  21. /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */
  22. private $productMock;
  23. /** @var \Magento\Framework\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */
  24. private $contextMock;
  25. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  26. private $requestMock;
  27. /** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
  28. private $responseMock;
  29. /** @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject */
  30. private $resultFactory;
  31. /** @var \Magento\Framework\Controller\Result\Json|\PHPUnit_Framework_MockObject_MockObject */
  32. private $jsonMock;
  33. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  34. private $objectManager;
  35. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager|\Magento\Swatches\Controller\Ajax\Media */
  36. private $controller;
  37. protected function setUp()
  38. {
  39. $this->mediaGallery = [
  40. 'image' => '/m/a/magento.png',
  41. 'small_image' => '/m/a/magento.png',
  42. 'thumbnail' => '/m/a/magento.png',
  43. 'swatch_image' => '/m/a/magento.png',
  44. ];
  45. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  46. $this->swatchHelperMock = $this->createMock(\Magento\Swatches\Helper\Data::class);
  47. $this->productModelFactoryMock = $this->createPartialMock(
  48. \Magento\Catalog\Model\ProductFactory::class,
  49. ['create']
  50. );
  51. $this->config = $this->createMock(\Magento\PageCache\Model\Config::class);
  52. $this->config->method('getTtl')->willReturn(1);
  53. $this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  54. $this->contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
  55. $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  56. $this->contextMock->method('getRequest')->willReturn($this->requestMock);
  57. $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
  58. ->disableOriginalConstructor()
  59. ->setMethods(['setPublicHeaders'])
  60. ->getMockForAbstractClass();
  61. $this->responseMock->method('setPublicHeaders')->willReturnSelf();
  62. $this->contextMock->method('getResponse')->willReturn($this->responseMock);
  63. $this->resultFactory = $this->createPartialMock(\Magento\Framework\Controller\ResultFactory::class, ['create']);
  64. $this->contextMock->method('getResultFactory')->willReturn($this->resultFactory);
  65. $this->jsonMock = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
  66. $this->resultFactory->expects($this->once())->method('create')->with('json')->willReturn($this->jsonMock);
  67. $this->controller = $this->objectManager->getObject(
  68. \Magento\Swatches\Controller\Ajax\Media::class,
  69. [
  70. 'context' => $this->contextMock,
  71. 'swatchHelper' => $this->swatchHelperMock,
  72. 'productModelFactory' => $this->productModelFactoryMock,
  73. 'config' => $this->config
  74. ]
  75. );
  76. }
  77. public function testExecute()
  78. {
  79. $this->requestMock->expects($this->any())->method('getParam')->with('product_id')->willReturn(59);
  80. $this->productMock
  81. ->expects($this->once())
  82. ->method('load')
  83. ->with(59)
  84. ->willReturn($this->productMock);
  85. $this->productMock
  86. ->expects($this->once())
  87. ->method('getIdentities')
  88. ->willReturn(['tags']);
  89. $this->productModelFactoryMock
  90. ->expects($this->once())
  91. ->method('create')
  92. ->willReturn($this->productMock);
  93. $this->swatchHelperMock
  94. ->expects($this->once())
  95. ->method('getProductMediaGallery')
  96. ->with($this->productMock)
  97. ->willReturn($this->mediaGallery);
  98. $this->jsonMock
  99. ->expects($this->once())
  100. ->method('setData')
  101. ->with($this->mediaGallery)
  102. ->will($this->returnSelf());
  103. $result = $this->controller->execute();
  104. $this->assertInstanceOf(\Magento\Framework\Controller\Result\Json::class, $result);
  105. }
  106. }