protected function setUp() { $this->_uploader = $this->getMock('Magento\\MediaStorage\\Model\\File\\Uploader', [], [], '', false); $this->_uploaderFactory = $this->getMock('Magento\\MediaStorage\\Model\\File\\UploaderFactory', ['create'], [], '', false); $this->_uploaderFactory->expects($this->any())->method('create')->will($this->returnValue($this->_uploader)); $this->_directoryMock = $this->getMock('Magento\\Framework\\Filesystem\\Directory\\Read', [], [], '', false); $this->_filesystemMock = $this->getMock('Magento\\Framework\\Filesystem', [], [], '', false); $this->_filesystemMock->expects($this->any())->method('getDirectoryRead')->will($this->returnValue($this->_directoryMock)); /** @var $service \Magento\Theme\Model\Uploader\Service */ $this->_fileSizeMock = $this->getMockBuilder('Magento\\Framework\\File\\Size')->setMethods(['getMaxFileSize'])->disableOriginalConstructor()->getMock(); $this->_fileSizeMock->expects($this->any())->method('getMaxFileSize')->will($this->returnValue(600 * self::MB_MULTIPLIER)); }
/** * @dataProvider dataForExecute */ public function testExecute($fileResult, $expectedResult) { $this->uploaderFactoryMock->expects($this->once())->method('create')->willReturn($this->uploaderMock); $this->adapterFactoryMock->expects($this->once())->method('create')->willReturn($this->adapterMock); $this->filesystemMock->expects($this->once())->method('getDirectoryRead')->with('media')->willReturn($this->mediaDirectoryMock); $this->uploaderMock->expects($this->once())->method('save')->willReturn($fileResult); $this->configMock->expects($this->once())->method('getTmpMediaUrl')->with($fileResult['file'])->willReturn('http://domain.com/tpm_dir/m/a/magento.png'); $this->swatchHelperMock->expects($this->once())->method('moveImageFromTmp')->with('/m/a/magento.png.tmp')->willReturn('/m/a/magento.png'); $this->swatchHelperMock->expects($this->once())->method('generateSwatchVariations'); $this->swatchHelperMock->expects($this->once())->method('getSwatchMediaUrl')->willReturn('http://domain.com/media/path/'); $this->responseMock->expects($this->once())->method('setBody')->willReturn(json_encode($expectedResult)); $this->controller->execute(); }
protected function setUp() { $this->contextMock = $this->getMockBuilder('Magento\\Framework\\Model\\Context')->disableOriginalConstructor()->getMock(); $this->registryMock = $this->getMockBuilder('Magento\\Framework\\Registry')->getMockForAbstractClass(); $this->scopeConfigMock = $this->getMockBuilder('Magento\\Framework\\App\\Config\\ScopeConfigInterface')->getMockForAbstractClass(); $this->typeListMock = $this->getMockBuilder('Magento\\Framework\\App\\Cache\\TypeListInterface')->getMockForAbstractClass(); $this->uploaderFactoryMock = $this->getMockBuilder('Magento\\MediaStorage\\Model\\File\\UploaderFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->requestDataMock = $this->getMockBuilder('Magento\\Config\\Model\\Config\\Backend\\File\\RequestData\\RequestDataInterface')->getMockForAbstractClass(); $this->filesystemMock = $this->getMockBuilder('Magento\\Framework\\Filesystem')->disableOriginalConstructor()->getMock(); $this->writeMock = $this->getMockBuilder('Magento\\Framework\\Filesystem\\Directory\\WriteInterface')->getMock(); $this->uploaderMock = $this->getMockBuilder('Magento\\MediaStorage\\Model\\File\\Uploader')->disableOriginalConstructor()->getMock(); $this->uploaderFactoryMock->expects($this->any())->method('create')->willReturn($this->uploaderMock); $this->filesystemMock->expects($this->once())->method('getDirectoryWrite')->with(DirectoryList::MEDIA)->willReturn($this->writeMock); $this->model = new Logo($this->contextMock, $this->registryMock, $this->scopeConfigMock, $this->typeListMock, $this->uploaderFactoryMock, $this->requestDataMock, $this->filesystemMock); }
/** * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage Exception! */ public function testBeforeSaveWithException() { $value = 'value'; $groupId = 1; $field = 'field'; $tmpFileName = 'tmp_file_name'; $name = 'name'; $path = 'path'; $scope = 'scope'; $scopeId = 2; $uploadDir = 'upload_dir'; $fieldConfig = ['upload_dir' => $uploadDir]; $fileData = ['tmp_name' => $tmpFileName, 'name' => $name]; $exception = 'Exception!'; $this->model->setValue($value); $this->model->setPath($path); $this->model->setScope($scope); $this->model->setScopeId($scopeId); $this->model->setFieldConfig($fieldConfig); $_FILES['groups']['tmp_name'][$groupId]['fields'][$field]['value'] = $tmpFileName; $this->requestDataMock->expects($this->once())->method('getTmpName')->with($path)->willReturn($tmpFileName); $this->requestDataMock->expects($this->once())->method('getName')->with($path)->willReturn($name); $this->uploaderFactoryMock->expects($this->any())->method('create')->with(['fileId' => $fileData])->willReturn($this->uploaderMock); $this->uploaderMock->expects($this->once())->method('save')->with($uploadDir, null)->willThrowException(new \Exception($exception)); $this->model->beforeSave(); }
public function testSaveToTmp() { $path = 'design/header/logo_src'; $fieldCode = 'header_logo_src'; $metadata = [$fieldCode => ['path' => $path, 'backend_model' => 'Magento\\Theme\\Model\\Design\\Backend\\Image']]; $this->metadataProvider->expects($this->once())->method('get')->willReturn($metadata); $this->backendModelFactory->expects($this->once())->method('createByPath')->with($path)->willReturn($this->backendModel); $this->uploaderFactory->expects($this->once())->method('create')->with(['fileId' => $fieldCode])->willReturn($this->uploader); $this->uploader->expects($this->once())->method('setAllowRenameFiles')->with(true); $this->uploader->expects($this->once())->method('setFilesDispersion')->with(false); $this->backendModel->expects($this->once())->method('getAllowedExtensions')->willReturn(['png', 'jpg']); $this->uploader->expects($this->once())->method('setAllowedExtensions')->with(['png', 'jpg']); $this->uploader->expects($this->once())->method('addValidateCallback')->with('size', $this->backendModel, 'validateMaxSize'); $this->imageConfig->expects($this->once())->method('getAbsoluteTmpMediaPath')->willReturn('absolute/path/to/tmp/media'); $this->uploader->expects($this->once())->method('save')->with('absolute/path/to/tmp/media')->willReturn(['file' => 'file.jpg', 'size' => '234234']); $this->imageConfig->expects($this->once())->method('getTmpMediaUrl')->with('file.jpg')->willReturn('http://magento2.com/pub/media/tmp/file.jpg'); $this->assertEquals(['file' => 'file.jpg', 'size' => '234234', 'url' => 'http://magento2.com/pub/media/tmp/file.jpg'], $this->imageProcessor->saveToTmp($fieldCode)); }
public function testExecute() { $data = ['tmp_name' => 'tmp_name', 'path' => 'path', 'file' => 'file']; $uploader = $this->getMockBuilder('Magento\\MediaStorage\\Model\\File\\Uploader')->disableOriginalConstructor()->getMock(); $resultJson = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Json')->disableOriginalConstructor()->setMethods(['setData'])->getMock(); $this->request->expects($this->once())->method('getParam')->with('type')->willReturn('samples'); $this->sample->expects($this->once())->method('getBaseTmpPath')->willReturn('base_tmp_path'); $this->uploaderFactory->expects($this->once())->method('create')->willReturn($uploader); $this->fileHelper->expects($this->once())->method('uploadFromTmp')->willReturn($data); $this->storageDatabase->expects($this->once())->method('saveFile'); $this->session->expects($this->once())->method('getName')->willReturn('Name'); $this->session->expects($this->once())->method('getSessionId')->willReturn('SessionId'); $this->session->expects($this->once())->method('getCookieLifetime')->willReturn('CookieLifetime'); $this->session->expects($this->once())->method('getCookiePath')->willReturn('CookiePath'); $this->session->expects($this->once())->method('getCookieDomain')->willReturn('CookieDomain'); $this->resultFactory->expects($this->once())->method('create')->willReturn($resultJson); $resultJson->expects($this->once())->method('setData')->willReturnSelf(); $this->assertEquals($resultJson, $this->upload->execute()); }