Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ValidatorInfoTest.php
Go to the documentation of this file.
1 <?php
8 
13 class ValidatorInfoTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $model;
19 
23  protected $objectManager;
24 
26  protected $maxFileSizeInMb;
27 
32 
36  protected function setUp()
37  {
40  $fileSize = $this->objectManager->create(\Magento\Framework\File\Size::class);
41  $this->maxFileSizeInMb = $fileSize->getMaxFileSizeInMb();
42 
43  $this->validateFactoryMock = $this->createPartialMock(
44  \Magento\Catalog\Model\Product\Option\Type\File\ValidateFactory::class,
45  ['create']
46  );
47  $this->model = $this->objectManager->create(
48  \Magento\Catalog\Model\Product\Option\Type\File\ValidatorInfo::class,
49  [
50  'validateFactory' => $this->validateFactoryMock,
51  ]
52  );
53  }
54 
58  public function testExceptionWithErrors()
59  {
60  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
61  $this->expectExceptionMessage(
62  "The file 'test.jpg' for 'MediaOption' has an invalid extension.\n"
63  . "The file 'test.jpg' for 'MediaOption' has an invalid extension.\n"
64  . "The maximum allowed image size for 'MediaOption' is 2000x2000 px.\n"
65  . sprintf(
66  "The file 'test.jpg' you uploaded is larger than the %s megabytes allowed by our server.",
67  $this->maxFileSizeInMb
68  )
69  );
70 
71  $validateMock = $this->createPartialMock(\Zend_Validate::class, ['isValid', 'getErrors']);
72  $validateMock->expects($this->once())->method('isValid')->will($this->returnValue(false));
73  $validateMock->expects($this->exactly(2))->method('getErrors')->will($this->returnValue([
78  ]));
79  $this->validateFactoryMock->expects($this->once())
80  ->method('create')
81  ->will($this->returnValue($validateMock));
82 
83  $this->model->validate(
84  $this->getOptionValue(),
85  $this->getProductOption()
86  );
87  }
88 
92  public function testExceptionWithoutErrors()
93  {
94  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
95  $this->expectExceptionMessage(
96  "The product's required option(s) weren't entered. Make sure the options are entered and try again."
97  );
98 
99  $validateMock = $this->createPartialMock(\Zend_Validate::class, ['isValid', 'getErrors']);
100  $validateMock->expects($this->once())->method('isValid')->will($this->returnValue(false));
101  $validateMock->expects($this->exactly(1))->method('getErrors')->will($this->returnValue(false));
102  $this->validateFactoryMock->expects($this->once())
103  ->method('create')
104  ->will($this->returnValue($validateMock));
105 
106  $this->model->validate(
107  $this->getOptionValue(),
108  $this->getProductOption()
109  );
110  }
111 
115  public function testValidate()
116  {
117  //use actual zend class to test changed functionality
118  $validate = $this->objectManager->create(\Zend_Validate::class);
119  $this->validateFactoryMock->expects($this->once())
120  ->method('create')
121  ->will($this->returnValue($validate));
122  $this->assertTrue(
123  $this->model->validate(
124  $this->getOptionValue(),
125  $this->getProductOption()
126  )
127  );
128  }
129 
134  protected function getProductOption(array $options = [])
135  {
136  $data = [
137  'option_id' => '1',
138  'product_id' => '4',
139  'type' => 'file',
140  'is_require' => '1',
141  'sku' => null,
142  'max_characters' => null,
143  'file_extension' => null,
144  'image_size_x' => '2000',
145  'image_size_y' => '2000',
146  'sort_order' => '0',
147  'default_title' => 'MediaOption',
148  'store_title' => null,
149  'title' => 'MediaOption',
150  'default_price' => '5.0000',
151  'default_price_type' => 'fixed',
152  'store_price' => null,
153  'store_price_type' => null,
154  'price' => '5.0000',
155  'price_type' => 'fixed',
156  ];
157  $option = $this->objectManager->create(
158  \Magento\Catalog\Model\Product\Option::class,
159  [
160  'data' => array_merge($data, $options)
161  ]
162  );
163 
164  return $option;
165  }
166 
170  protected function getOptionValue()
171  {
173  $config = $this->objectManager->get(\Magento\Catalog\Model\Product\Media\Config::class);
174  $file = $config->getBaseTmpMediaPath() . '/magento_small_image.jpg';
175 
177  $filesystem = $this->objectManager->get(\Magento\Framework\Filesystem::class);
178  $tmpDirectory = $filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
179  $filePath = $tmpDirectory->getAbsolutePath($file);
180 
181  return [
182  'title' => 'test.jpg',
183  'quote_path' => $file,
184  'order_path' => $file,
185  'secret_key' => substr(md5(file_get_contents($filePath)), 0, 20),
186  ];
187  }
188 }
$config
Definition: fraud_order.php:17
$tmpDirectory
$filesystem