Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MediaTest.php
Go to the documentation of this file.
1 <?php
8 
12 class MediaTest extends \PHPUnit\Framework\TestCase
13 {
15  private $mediaGallery;
16 
18  private $swatchHelperMock;
19 
21  private $productModelFactoryMock;
22 
24  private $productMock;
25 
27  private $contextMock;
28 
30  private $requestMock;
31 
33  private $resultFactory;
34 
36  private $jsonMock;
37 
39  private $objectManager;
40 
42  private $controller;
43 
44  protected function setUp()
45  {
46  $this->mediaGallery = [
47  'image' => '/m/a/magento.png',
48  'small_image' => '/m/a/magento.png',
49  'thumbnail' => '/m/a/magento.png',
50  'swatch_image' => '/m/a/magento.png',
51  ];
52 
53  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
54 
55  $this->swatchHelperMock = $this->createMock(\Magento\Swatches\Helper\Data::class);
56  $this->productModelFactoryMock = $this->createPartialMock(
57  \Magento\Catalog\Model\ProductFactory::class,
58  ['create']
59  );
60  $this->productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
61  $this->contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
62 
63  $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
64  $this->contextMock->method('getRequest')->willReturn($this->requestMock);
65  $this->resultFactory = $this->createPartialMock(\Magento\Framework\Controller\ResultFactory::class, ['create']);
66  $this->contextMock->method('getResultFactory')->willReturn($this->resultFactory);
67 
68  $this->jsonMock = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
69  $this->resultFactory->expects($this->once())->method('create')->with('json')->willReturn($this->jsonMock);
70 
71  $this->controller = $this->objectManager->getObject(
72  \Magento\Swatches\Controller\Ajax\Media::class,
73  [
74  'context' => $this->contextMock,
75  'swatchHelper' => $this->swatchHelperMock,
76  'productModelFactory' => $this->productModelFactoryMock
77  ]
78  );
79  }
80 
81  public function testExecute()
82  {
83  $this->requestMock->expects($this->any())->method('getParam')->with('product_id')->willReturn(59);
84  $this->productMock
85  ->expects($this->once())
86  ->method('load')
87  ->with(59)
88  ->willReturn($this->productMock);
89 
90  $this->productModelFactoryMock
91  ->expects($this->once())
92  ->method('create')
93  ->willReturn($this->productMock);
94 
95  $this->swatchHelperMock
96  ->expects($this->once())
97  ->method('getProductMediaGallery')
98  ->with($this->productMock)
99  ->willReturn($this->mediaGallery);
100 
101  $this->jsonMock
102  ->expects($this->once())
103  ->method('setData')
104  ->with($this->mediaGallery)
105  ->will($this->returnSelf());
106 
107  $result = $this->controller->execute();
108 
109  $this->assertInstanceOf(\Magento\Framework\Controller\Result\Json::class, $result);
110  }
111 }