|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Wishlist\Test\Unit\Controller\Index; |
| 7 | + |
| 8 | +class DownloadCustomOptionTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var \Magento\Wishlist\Controller\Index\DownloadCustomOption |
| 12 | + */ |
| 13 | + protected $model; |
| 14 | + |
| 15 | + /** |
| 16 | + * @var \Magento\Framework\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject |
| 17 | + */ |
| 18 | + protected $contextMock; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \Magento\Framework\App\Response\Http\FileFactory|\PHPUnit_Framework_MockObject_MockObject |
| 22 | + */ |
| 23 | + protected $fileResponseFactoryMock; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + protected $requestMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 32 | + */ |
| 33 | + protected $objectManagerMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + protected $resultFactoryMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject |
| 42 | + */ |
| 43 | + protected $jsonMock; |
| 44 | + |
| 45 | + protected function setUp() |
| 46 | + { |
| 47 | + $this->fileResponseFactoryMock = $this->getMockBuilder(\Magento\Framework\App\Response\Http\FileFactory::class) |
| 48 | + ->disableOriginalConstructor() |
| 49 | + ->getMock(); |
| 50 | + |
| 51 | + $this->jsonMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class) |
| 52 | + ->disableOriginalConstructor() |
| 53 | + ->getMock(); |
| 54 | + |
| 55 | + $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class) |
| 56 | + ->disableOriginalConstructor() |
| 57 | + ->setMethods(['create', 'get', 'configure']) |
| 58 | + ->getMock(); |
| 59 | + |
| 60 | + $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) |
| 61 | + ->disableOriginalConstructor() |
| 62 | + ->getMock(); |
| 63 | + |
| 64 | + $this->resultFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class) |
| 65 | + ->disableOriginalConstructor() |
| 66 | + ->getMock(); |
| 67 | + |
| 68 | + $this->contextMock = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class) |
| 69 | + ->disableOriginalConstructor() |
| 70 | + ->getMock(); |
| 71 | + $this->contextMock->expects($this->any()) |
| 72 | + ->method('getObjectManager') |
| 73 | + ->willReturn($this->objectManagerMock); |
| 74 | + $this->contextMock->expects($this->any()) |
| 75 | + ->method('getRequest') |
| 76 | + ->willReturn($this->requestMock); |
| 77 | + $this->contextMock->expects($this->any()) |
| 78 | + ->method('getResultFactory') |
| 79 | + ->willReturn($this->resultFactoryMock); |
| 80 | + |
| 81 | + $this->model = new \Magento\Wishlist\Controller\Index\DownloadCustomOption( |
| 82 | + $this->contextMock, |
| 83 | + $this->fileResponseFactoryMock, |
| 84 | + $this->jsonMock |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + public function testExecute() { |
| 89 | + $data = [ |
| 90 | + 'number' => 42, |
| 91 | + 'string' => 'string_value', |
| 92 | + 'boolean' => true, |
| 93 | + 'collection' => [1, 2, 3], |
| 94 | + 'secret_key' => 999 |
| 95 | + ]; |
| 96 | + $serialized_data = json_encode($data); |
| 97 | + |
| 98 | + $optionMock = $this->getMockBuilder(\Magento\Wishlist\Model\Item\Option::class) |
| 99 | + ->disableOriginalConstructor() |
| 100 | + ->getMock(); |
| 101 | + $optionMock->expects($this->any()) |
| 102 | + ->method('load') |
| 103 | + ->willReturnSelf(); |
| 104 | + $optionMock->expects($this->any()) |
| 105 | + ->method('getId') |
| 106 | + ->willReturn(true); |
| 107 | + $optionMock->expects($this->any()) |
| 108 | + ->method('getProductId') |
| 109 | + ->willReturn('some_value'); |
| 110 | + $optionMock->expects($this->any()) |
| 111 | + ->method('getValue') |
| 112 | + ->willReturn($serialized_data); |
| 113 | + |
| 114 | + $productOptionMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option::class) |
| 115 | + ->disableOriginalConstructor() |
| 116 | + ->getMock(); |
| 117 | + $productOptionMock->expects($this->any()) |
| 118 | + ->method('load') |
| 119 | + ->willReturnSelf(); |
| 120 | + $productOptionMock->expects($this->any()) |
| 121 | + ->method('getId') |
| 122 | + ->willReturn(true); |
| 123 | + $productOptionMock->expects($this->any()) |
| 124 | + ->method('getProductId') |
| 125 | + ->willReturn('some_value'); |
| 126 | + $productOptionMock->expects($this->any()) |
| 127 | + ->method('getType') |
| 128 | + ->willReturn('file'); |
| 129 | + |
| 130 | + $this->objectManagerMock->expects($this->any()) |
| 131 | + ->method('create') |
| 132 | + ->willReturnMap( |
| 133 | + [ |
| 134 | + [\Magento\Wishlist\Model\Item\Option::class, [], $optionMock], |
| 135 | + [\Magento\Catalog\Model\Product\Option::class, [], $productOptionMock] |
| 136 | + ] |
| 137 | + ); |
| 138 | + |
| 139 | + $this->requestMock->expects($this->any()) |
| 140 | + ->method('getParam') |
| 141 | + ->willReturn(1); |
| 142 | + |
| 143 | + $this->jsonMock->expects($this->once()) |
| 144 | + ->method('unserialize') |
| 145 | + ->willReturnCallback(function ($value) { |
| 146 | + return json_decode($value, true); |
| 147 | + }); |
| 148 | + |
| 149 | + $this->assertEquals(null, $this->model->execute()); |
| 150 | + } |
| 151 | +} |
0 commit comments