EavValidationRulesTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Test\Unit\DataProvider;
  7. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\Ui\DataProvider\EavValidationRules;
  10. /**
  11. * Class EavValidationRulesTest
  12. */
  13. class EavValidationRulesTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var ObjectManager
  17. */
  18. protected $objectManager;
  19. /**
  20. * @var \Magento\Ui\DataProvider\EavValidationRules
  21. */
  22. protected $subject;
  23. /**
  24. * @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $attributeMock;
  27. /**
  28. * {@inheritDoc}
  29. */
  30. protected function setUp()
  31. {
  32. $this->objectManager = new ObjectManager($this);
  33. $this->attributeMock =
  34. $this->getMockBuilder(AbstractAttribute::class)
  35. ->setMethods(['getFrontendInput', 'getValidateRules'])
  36. ->disableOriginalConstructor()
  37. ->getMockForAbstractClass();
  38. $this->subject = new EavValidationRules();
  39. }
  40. /**
  41. * @param string $attributeInputType
  42. * @param mixed $validateRules
  43. * @param array $data
  44. * @param array $expected
  45. * @dataProvider buildDataProvider
  46. */
  47. public function testBuild($attributeInputType, $validateRules, $data, $expected): void
  48. {
  49. $this->attributeMock->expects($this->once())->method('getFrontendInput')->willReturn($attributeInputType);
  50. $this->attributeMock->expects($this->any())->method('getValidateRules')->willReturn($validateRules);
  51. $validationRules = $this->subject->build($this->attributeMock, $data);
  52. $this->assertEquals($expected, $validationRules);
  53. }
  54. /**
  55. * @return array
  56. */
  57. public function buildDataProvider()
  58. {
  59. return [
  60. ['', '', [], []],
  61. ['', null, [], []],
  62. ['', false, [], []],
  63. ['', [], [], []],
  64. ['', '', ['required' => 1], ['required-entry' => true]],
  65. ['price', '', [], ['validate-zero-or-greater' => true]],
  66. ['price', '', ['required' => 1], ['validate-zero-or-greater' => true, 'required-entry' => true]],
  67. ['', ['input_validation' => 'email'], [], ['validate-email' => true]],
  68. ['', ['input_validation' => 'date'], [], ['validate-date' => true]],
  69. ['', ['input_validation' => 'other'], [], []],
  70. ['', ['max_text_length' => '254'], ['required' => 1], ['required-entry' => true]],
  71. [
  72. '',
  73. ['input_validation' => 'other', 'max_text_length' => '254'],
  74. ['required' => 1],
  75. ['max_text_length' => 254, 'required-entry' => true]
  76. ],
  77. [
  78. '',
  79. ['input_validation' => 'other', 'max_text_length' => '254', 'min_text_length' => 1],
  80. [],
  81. ['max_text_length' => 254, 'min_text_length' => 1]
  82. ],
  83. [
  84. '',
  85. ['max_text_length' => '254', 'input_validation' => 'date'],
  86. [],
  87. ['max_text_length' => 254, 'validate-date' => true]
  88. ],
  89. ];
  90. }
  91. }