ObjectTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Validator\Test\Unit;
  7. class ObjectTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\Validator\DataObject
  11. */
  12. protected $_model;
  13. protected function setUp()
  14. {
  15. $this->_model = new \Magento\Framework\Validator\DataObject();
  16. $fieldOneExactValue = new \Zend_Validate_Identical('field_one_value');
  17. $fieldOneExactValue->setMessage("'field_one' does not match expected value");
  18. $fieldOneLength = new \Zend_Validate_StringLength(['min' => 10]);
  19. $fieldTwoExactValue = new \Zend_Validate_Identical('field_two_value');
  20. $fieldTwoExactValue->setMessage("'field_two' does not match expected value");
  21. $fieldTwoLength = new \Zend_Validate_StringLength(['min' => 5]);
  22. $entityValidity = new \Zend_Validate_Callback([$this, 'isEntityValid']);
  23. $entityValidity->setMessage('Entity is not valid.');
  24. $this->_model->addRule(
  25. $fieldOneLength,
  26. 'field_one'
  27. )->addRule(
  28. $fieldOneExactValue,
  29. 'field_one'
  30. )->addRule(
  31. $fieldTwoLength,
  32. 'field_two'
  33. )->addRule(
  34. $fieldTwoExactValue,
  35. 'field_two'
  36. )->addRule(
  37. $entityValidity
  38. );
  39. }
  40. protected function tearDown()
  41. {
  42. $this->_model = null;
  43. }
  44. /**
  45. * Entity validation routine to be used as a callback
  46. *
  47. * @param \Magento\Framework\DataObject $entity
  48. * @return bool
  49. */
  50. public function isEntityValid(\Magento\Framework\DataObject $entity)
  51. {
  52. return (bool)$entity->getData('is_valid');
  53. }
  54. public function testAddRule()
  55. {
  56. $actualResult = $this->_model->addRule(new \Zend_Validate_Identical('field_one_value'), 'field_one');
  57. $this->assertSame($this->_model, $actualResult, 'Methods chaining is broken.');
  58. }
  59. public function testGetMessages()
  60. {
  61. $messages = $this->_model->getMessages();
  62. $this->assertInternalType('array', $messages);
  63. }
  64. /**
  65. * @param array $inputEntityData
  66. * @param array $expectedErrors
  67. * @dataProvider validateDataProvider
  68. */
  69. public function testIsValid(array $inputEntityData, array $expectedErrors)
  70. {
  71. $entity = new \Magento\Framework\DataObject($inputEntityData);
  72. $isValid = $this->_model->isValid($entity);
  73. $this->assertFalse($isValid, 'Validation is expected to fail.');
  74. $actualMessages = $this->_model->getMessages();
  75. $this->assertCount(count($expectedErrors), $actualMessages, 'Number of messages does not meet expectations.');
  76. foreach ($expectedErrors as $errorIndex => $expectedErrorMessage) {
  77. /** @var $actualMessage \Magento\Framework\Message\AbstractMessage */
  78. $actualMessage = $actualMessages[$errorIndex];
  79. $this->assertEquals($expectedErrorMessage, $actualMessage);
  80. }
  81. }
  82. /**
  83. * @return array
  84. */
  85. public function validateDataProvider()
  86. {
  87. return [
  88. 'only "field_one" is invalid' => [
  89. ['field_one' => 'one_value', 'field_two' => 'field_two_value', 'is_valid' => true],
  90. ["'one_value' is less than 10 characters long", "'field_one' does not match expected value"],
  91. ],
  92. 'only "field_two" is invalid' => [
  93. ['field_one' => 'field_one_value', 'field_two' => 'two_value', 'is_valid' => true],
  94. ["'field_two' does not match expected value"],
  95. ],
  96. 'entity as a whole is invalid' => [
  97. ['field_one' => 'field_one_value', 'field_two' => 'field_two_value'],
  98. ['Entity is not valid.'],
  99. ],
  100. 'errors aggregation' => [
  101. ['field_one' => 'one_value', 'field_two' => 'two'],
  102. [
  103. "'one_value' is less than 10 characters long",
  104. "'field_one' does not match expected value",
  105. "'two' is less than 5 characters long",
  106. "'field_two' does not match expected value",
  107. 'Entity is not valid.'
  108. ],
  109. ]
  110. ];
  111. }
  112. }