PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/Magento/Catalog/Test/Unit/Model/Product/Option/Validator/DefaultValidatorTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 163 lines | 107 code | 11 blank | 45 comment | 0 complexity | f6b461c95a8a8c880b4743b93568d964 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Test\Unit\Model\Product\Option\Validator;
  7. class DefaultValidatorTest extends \PHPUnit_Framework_TestCase
  8. {
  9. /**
  10. * @var \Magento\Catalog\Model\Product\Option\Validator\DefaultValidator
  11. */
  12. protected $validator;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $valueMock;
  17. protected function setUp()
  18. {
  19. $configMock = $this->getMock(\Magento\Catalog\Model\ProductOptions\ConfigInterface::class);
  20. $priceConfigMock = new \Magento\Catalog\Model\Config\Source\Product\Options\Price();
  21. $config = [
  22. [
  23. 'label' => 'group label 1',
  24. 'types' => [
  25. [
  26. 'label' => 'label 1.1',
  27. 'name' => 'name 1.1',
  28. 'disabled' => false,
  29. ],
  30. ],
  31. ],
  32. [
  33. 'label' => 'group label 2',
  34. 'types' => [
  35. [
  36. 'label' => 'label 2.2',
  37. 'name' => 'name 2.2',
  38. 'disabled' => true,
  39. ],
  40. ]
  41. ],
  42. ];
  43. $configMock->expects($this->once())->method('getAll')->will($this->returnValue($config));
  44. $this->validator = new \Magento\Catalog\Model\Product\Option\Validator\DefaultValidator(
  45. $configMock,
  46. $priceConfigMock
  47. );
  48. }
  49. /**
  50. * Data provider for testIsValidSuccess
  51. * @return array
  52. */
  53. public function isValidTitleDataProvider()
  54. {
  55. $mess = ['option required fields' => 'Missed values for option required fields'];
  56. return [
  57. ['option_title', 'name 1.1', 'fixed', 10, new \Magento\Framework\DataObject(['store_id' => 1]), [], true],
  58. ['option_title', 'name 1.1', 'fixed', 10, new \Magento\Framework\DataObject(['store_id' => 0]), [], true],
  59. [null, 'name 1.1', 'fixed', 10, new \Magento\Framework\DataObject(['store_id' => 1]), [], true],
  60. [null, 'name 1.1', 'fixed', 10, new \Magento\Framework\DataObject(['store_id' => 0]), $mess, false],
  61. ];
  62. }
  63. /**
  64. * @param $title
  65. * @param $type
  66. * @param $priceType
  67. * @param $price
  68. * @param $product
  69. * @param $messages
  70. * @param $result
  71. * @dataProvider isValidTitleDataProvider
  72. */
  73. public function testIsValidTitle($title, $type, $priceType, $price, $product, $messages, $result)
  74. {
  75. $methods = ['getTitle', 'getType', 'getPriceType', 'getPrice', '__wakeup', 'getProduct'];
  76. $valueMock = $this->getMock(\Magento\Catalog\Model\Product\Option::class, $methods, [], '', false);
  77. $valueMock->expects($this->once())->method('getTitle')->will($this->returnValue($title));
  78. $valueMock->expects($this->any())->method('getType')->will($this->returnValue($type));
  79. $valueMock->expects($this->once())->method('getPriceType')->will($this->returnValue($priceType));
  80. $valueMock->expects($this->once())->method('getPrice')->will($this->returnValue($price));
  81. $valueMock->expects($this->once())->method('getProduct')->will($this->returnValue($product));
  82. $this->assertEquals($result, $this->validator->isValid($valueMock));
  83. $this->assertEquals($messages, $this->validator->getMessages());
  84. }
  85. /**
  86. * Data provider for testIsValidFail
  87. *
  88. * @return array
  89. */
  90. public function isValidFailDataProvider()
  91. {
  92. return [
  93. [new \Magento\Framework\DataObject(['store_id' => 1])],
  94. [new \Magento\Framework\DataObject(['store_id' => 0])],
  95. ];
  96. }
  97. /**
  98. * @param $product
  99. * @dataProvider isValidFailDataProvider
  100. */
  101. public function testIsValidFail($product)
  102. {
  103. $methods = ['getTitle', 'getType', 'getPriceType', 'getPrice', '__wakeup', 'getProduct'];
  104. $valueMock = $this->getMock(\Magento\Catalog\Model\Product\Option::class, $methods, [], '', false);
  105. $valueMock->expects($this->once())->method('getProduct')->will($this->returnValue($product));
  106. $valueMock->expects($this->once())->method('getTitle');
  107. $valueMock->expects($this->any())->method('getType');
  108. $valueMock->expects($this->once())->method('getPriceType')->will($this->returnValue('some_new_value'));
  109. $valueMock->expects($this->never())->method('getPrice');
  110. $messages = [
  111. 'option required fields' => 'Missed values for option required fields',
  112. 'option type' => 'Invalid option type',
  113. 'option values' => 'Invalid option value',
  114. ];
  115. $this->assertFalse($this->validator->isValid($valueMock));
  116. $this->assertEquals($messages, $this->validator->getMessages());
  117. }
  118. /**
  119. * Data provider for testValidationNegativePrice
  120. * @return array
  121. */
  122. public function validationNegativePriceDataProvider()
  123. {
  124. return [
  125. ['option_title', 'name 1.1', 'fixed', -12, new \Magento\Framework\DataObject(['store_id' => 1])],
  126. ['option_title', 'name 1.1', 'fixed', -12, new \Magento\Framework\DataObject(['store_id' => 0])],
  127. ];
  128. }
  129. /**
  130. * @param $title
  131. * @param $type
  132. * @param $priceType
  133. * @param $price
  134. * @param $product
  135. * @dataProvider validationNegativePriceDataProvider
  136. */
  137. public function testValidationNegativePrice($title, $type, $priceType, $price, $product)
  138. {
  139. $methods = ['getTitle', 'getType', 'getPriceType', 'getPrice', '__wakeup', 'getProduct'];
  140. $valueMock = $this->getMock(\Magento\Catalog\Model\Product\Option::class, $methods, [], '', false);
  141. $valueMock->expects($this->once())->method('getTitle')->will($this->returnValue($title));
  142. $valueMock->expects($this->exactly(2))->method('getType')->will($this->returnValue($type));
  143. $valueMock->expects($this->once())->method('getPriceType')->will($this->returnValue($priceType));
  144. $valueMock->expects($this->once())->method('getPrice')->will($this->returnValue($price));
  145. $valueMock->expects($this->once())->method('getProduct')->will($this->returnValue($product));
  146. $messages = [
  147. 'option values' => 'Invalid option value',
  148. ];
  149. $this->assertFalse($this->validator->isValid($valueMock));
  150. $this->assertEquals($messages, $this->validator->getMessages());
  151. }
  152. }