getMockBuilder(\Magento\Framework\Controller\Result\ForwardFactory::class) ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); $this->resultForwardMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Forward::class) ->disableOriginalConstructor() ->setMethods(['forward']) ->getMock(); $resultForwardFactoryMock->expects($this->any())->method('create')->willReturn($this->resultForwardMock); $this->downloadMock = $this->getMockBuilder(\Magento\Sales\Model\Download::class) ->disableOriginalConstructor() ->setMethods(['downloadFile']) ->getMock(); $this->serializerMock = $this->getMockBuilder(Json::class) ->disableOriginalConstructor() ->setMethods(['serialize', 'unserialize']) ->getMock(); $requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class) ->disableOriginalConstructor() ->setMethods(['getParam']) ->getMock(); $requestMock->expects($this->any())->method('getParam') ->will( $this->returnValueMap( [ ['id', null, self::OPTION_ID], ['key', null, self::SECRET_KEY], ] ) ); $this->itemOptionMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Option::class) ->disableOriginalConstructor() ->setMethods(['load', 'getId', 'getCode', 'getProductId', 'getValue']) ->getMock(); $this->productOptionMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option::class) ->disableOriginalConstructor() ->setMethods(['load', 'getId', 'getProductId', 'getType']) ->getMock(); $objectManagerMock = $this->getMockBuilder(\Magento\Sales\Model\Download::class) ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); $objectManagerMock->expects($this->any())->method('create') ->will( $this->returnValueMap( [ [\Magento\Quote\Model\Quote\Item\Option::class, $this->itemOptionMock], [\Magento\Catalog\Model\Product\Option::class, $this->productOptionMock], ] ) ); $contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class) ->disableOriginalConstructor() ->setMethods( [ 'getRequest', 'getObjectManager', ] ) ->getMock(); $contextMock->expects($this->once())->method('getObjectManager')->willReturn($objectManagerMock); $contextMock->expects($this->once())->method('getRequest')->willReturn($requestMock); $this->objectMock = $this->getMockBuilder(\Magento\Sales\Controller\Download\DownloadCustomOption::class) ->setMethods(['endExecute']) ->setConstructorArgs( [ 'context' => $contextMock, 'resultForwardFactory' => $resultForwardFactoryMock, 'download' => $this->downloadMock, 'unserialize' => $this->createMock(Unserialize::class), 'serializer' => $this->serializerMock ] ) ->getMock(); } /** * @param array $itemOptionValues * @param array $productOptionValues * @param bool $noRouteOccurs * @dataProvider executeDataProvider */ public function testExecute($itemOptionValues, $productOptionValues, $noRouteOccurs) { if (!empty($itemOptionValues)) { $this->itemOptionMock->expects($this->once())->method('load')->willReturnSelf(); $this->itemOptionMock->expects($this->once()) ->method('getId') ->willReturn($itemOptionValues[self::OPTION_ID]); $this->itemOptionMock->expects($this->any()) ->method('getCode') ->willReturn($itemOptionValues[self::OPTION_CODE]); $this->itemOptionMock->expects($this->any()) ->method('getProductId') ->willReturn($itemOptionValues[self::OPTION_PRODUCT_ID]); $this->itemOptionMock->expects($this->any()) ->method('getValue') ->willReturn($itemOptionValues[self::OPTION_VALUE]); } if (!empty($productOptionValues)) { $this->productOptionMock->expects($this->once())->method('load')->willReturnSelf(); $this->productOptionMock->expects($this->any()) ->method('getId') ->willReturn($productOptionValues[self::OPTION_ID]); $this->productOptionMock->expects($this->any()) ->method('getProductId') ->willReturn($productOptionValues[self::OPTION_PRODUCT_ID]); $this->productOptionMock->expects($this->any()) ->method('getType') ->willReturn($productOptionValues[self::OPTION_TYPE]); } if ($noRouteOccurs) { $this->resultForwardMock->expects($this->once())->method('forward')->with('noroute')->willReturn(true); } else { $unserializeResult = [self::SECRET_KEY => self::SECRET_KEY]; $this->serializerMock->expects($this->once()) ->method('unserialize') ->with($itemOptionValues[self::OPTION_VALUE]) ->willReturn($unserializeResult); $this->downloadMock->expects($this->once()) ->method('downloadFile') ->with($unserializeResult) ->willReturn(true); $this->objectMock->expects($this->once())->method('endExecute')->willReturn(true); } $this->objectMock->execute(); } /** * @return array */ public function executeDataProvider() { return [ [ //Good [ self::OPTION_ID => self::OPTION_ID, self::OPTION_CODE => self::OPTION_CODE, self::OPTION_PRODUCT_ID => self::OPTION_PRODUCT_ID, self::OPTION_VALUE => self::OPTION_VALUE ], [ self::OPTION_ID => self::OPTION_ID, self::OPTION_PRODUCT_ID => self::OPTION_PRODUCT_ID, self::OPTION_TYPE => self::OPTION_TYPE, ], false ], [ //No Option ID [ self::OPTION_ID => false, self::OPTION_CODE => self::OPTION_CODE, self::OPTION_PRODUCT_ID => self::OPTION_PRODUCT_ID, self::OPTION_VALUE => self::OPTION_VALUE ], [], true ], [ //No Product Option [ self::OPTION_ID => self::OPTION_ID, self::OPTION_CODE => self::OPTION_CODE, self::OPTION_PRODUCT_ID => self::OPTION_PRODUCT_ID, self::OPTION_VALUE => self::OPTION_VALUE ], [], true ], [ //No Product Option ID [ self::OPTION_ID => self::OPTION_ID, self::OPTION_CODE => self::OPTION_CODE, self::OPTION_PRODUCT_ID => self::OPTION_PRODUCT_ID, self::OPTION_VALUE => self::OPTION_VALUE ], [ self::OPTION_ID => null, self::OPTION_PRODUCT_ID => self::OPTION_PRODUCT_ID, self::OPTION_TYPE => self::OPTION_TYPE, ], true ], [ //Not Matching Product IDs in Inventory Option [ self::OPTION_ID => self::OPTION_ID, self::OPTION_CODE => self::OPTION_CODE, self::OPTION_PRODUCT_ID => 'bad_test_product_ID', self::OPTION_VALUE => self::OPTION_VALUE ], [ self::OPTION_ID => self::OPTION_ID, self::OPTION_PRODUCT_ID => self::OPTION_PRODUCT_ID, self::OPTION_TYPE => self::OPTION_TYPE, ], true ], [ //Not Matching Product IDs in Product Option [ self::OPTION_ID => self::OPTION_ID, self::OPTION_CODE => self::OPTION_CODE, self::OPTION_PRODUCT_ID => self::OPTION_PRODUCT_ID, self::OPTION_VALUE => self::OPTION_VALUE ], [ self::OPTION_ID => self::OPTION_ID, self::OPTION_PRODUCT_ID => 'bad_test_product_ID', self::OPTION_TYPE => self::OPTION_TYPE, ], true ], [ //Incorrect Option Type [ self::OPTION_ID => self::OPTION_ID, self::OPTION_CODE => self::OPTION_CODE, self::OPTION_PRODUCT_ID => self::OPTION_PRODUCT_ID, self::OPTION_VALUE => self::OPTION_VALUE ], [ self::OPTION_ID => self::OPTION_ID, self::OPTION_PRODUCT_ID => self::OPTION_PRODUCT_ID, self::OPTION_TYPE => 'bad_test_option_type', ], true ], ]; } public function testExecuteBadSecretKey() { $this->itemOptionMock->expects($this->once())->method('load')->willReturnSelf(); $this->itemOptionMock->expects($this->once())->method('getId')->willReturn(self::OPTION_ID); $this->itemOptionMock->expects($this->any())->method('getCode')->willReturn(self::OPTION_CODE); $this->itemOptionMock->expects($this->any())->method('getProductId')->willReturn(self::OPTION_PRODUCT_ID); $this->itemOptionMock->expects($this->any())->method('getValue')->willReturn(self::OPTION_VALUE); $this->productOptionMock->expects($this->once())->method('load')->willReturnSelf(); $this->productOptionMock->expects($this->any())->method('getId')->willReturn(self::OPTION_ID); $this->productOptionMock->expects($this->any())->method('getProductId')->willReturn(self::OPTION_PRODUCT_ID); $this->productOptionMock->expects($this->any())->method('getType')->willReturn(self::OPTION_TYPE); $this->serializerMock->expects($this->once()) ->method('unserialize') ->with(self::OPTION_VALUE) ->willReturn([self::SECRET_KEY => 'bad_test_secret_key']); $this->resultForwardMock->expects($this->once())->method('forward')->with('noroute')->willReturn(true); $this->objectMock->execute(); } }