Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DefaultValidatorTest.php
Go to the documentation of this file.
1 <?php
8 
9 class DefaultValidatorTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $validator;
15 
19  protected $valueMock;
20 
24  protected function setUp()
25  {
26  $configMock = $this->createMock(\Magento\Catalog\Model\ProductOptions\ConfigInterface::class);
27  $storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
28  $priceConfigMock = new \Magento\Catalog\Model\Config\Source\Product\Options\Price($storeManagerMock);
29  $config = [
30  [
31  'label' => 'group label 1',
32  'types' => [
33  [
34  'label' => 'label 1.1',
35  'name' => 'name 1.1',
36  'disabled' => false,
37  ],
38  ],
39  ],
40  [
41  'label' => 'group label 2',
42  'types' => [
43  [
44  'label' => 'label 2.2',
45  'name' => 'name 2.2',
46  'disabled' => true,
47  ],
48  ]
49  ],
50  ];
51  $configMock->expects($this->once())->method('getAll')->will($this->returnValue($config));
52  $this->validator = new \Magento\Catalog\Model\Product\Option\Validator\DefaultValidator(
53  $configMock,
54  $priceConfigMock
55  );
56  }
57 
62  public function isValidTitleDataProvider()
63  {
64  $mess = ['option required fields' => 'Missed values for option required fields'];
65  return [
66  ['option_title', 'name 1.1', 'fixed', new \Magento\Framework\DataObject(['store_id' => 1]), [], true],
67  ['option_title', 'name 1.1', 'fixed', new \Magento\Framework\DataObject(['store_id' => 0]), [], true],
68  [null, 'name 1.1', 'fixed', new \Magento\Framework\DataObject(['store_id' => 1]), [], true],
69  [null, 'name 1.1', 'fixed', new \Magento\Framework\DataObject(['store_id' => 0]), $mess, false],
70  ];
71  }
72 
82  public function testIsValidTitle($title, $type, $priceType, $product, $messages, $result)
83  {
84  $methods = ['getTitle', 'getType', 'getPriceType', '__wakeup', 'getProduct'];
85  $valueMock = $this->createPartialMock(\Magento\Catalog\Model\Product\Option::class, $methods);
86  $valueMock->expects($this->once())->method('getTitle')->will($this->returnValue($title));
87  $valueMock->expects($this->any())->method('getType')->will($this->returnValue($type));
88  $valueMock->expects($this->once())->method('getPriceType')->will($this->returnValue($priceType));
89  // $valueMock->expects($this->once())->method('getPrice')->will($this->returnValue($price));
90  $valueMock->expects($this->once())->method('getProduct')->will($this->returnValue($product));
91  $this->assertEquals($result, $this->validator->isValid($valueMock));
92  $this->assertEquals($messages, $this->validator->getMessages());
93  }
94 
100  public function isValidFailDataProvider()
101  {
102  return [
103  [new \Magento\Framework\DataObject(['store_id' => 1])],
104  [new \Magento\Framework\DataObject(['store_id' => 0])],
105  ];
106  }
107 
112  public function testIsValidFail($product)
113  {
114  $methods = ['getTitle', 'getType', 'getPriceType', 'getPrice', '__wakeup', 'getProduct'];
115  $valueMock = $this->createPartialMock(\Magento\Catalog\Model\Product\Option::class, $methods);
116  $valueMock->expects($this->once())->method('getProduct')->will($this->returnValue($product));
117  $valueMock->expects($this->once())->method('getTitle');
118  $valueMock->expects($this->any())->method('getType');
119  $valueMock->expects($this->once())->method('getPriceType')->will($this->returnValue('some_new_value'));
120  $valueMock->expects($this->never())->method('getPrice');
121  $messages = [
122  'option required fields' => 'Missed values for option required fields',
123  'option type' => 'Invalid option type',
124  'option values' => 'Invalid option value',
125  ];
126  $this->assertFalse($this->validator->isValid($valueMock));
127  $this->assertEquals($messages, $this->validator->getMessages());
128  }
129 }
$title
Definition: default.phtml:14
$config
Definition: fraud_order.php:17
testIsValidTitle($title, $type, $priceType, $product, $messages, $result)
$type
Definition: item.phtml:13
$methods
Definition: billing.phtml:71
$priceType
Definition: msrp.phtml:18