Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PropertyTest.php
Go to the documentation of this file.
1 <?php
8 
12 class PropertyTest extends \PHPUnit\Framework\TestCase
13 {
14  const PROPERTY_NAME = 'test';
15 
19  protected $_constraint;
20 
24  protected $_validatorMock;
25 
29  protected function setUp()
30  {
31  $this->_validatorMock = $this->createMock(\Magento\Framework\Validator\ValidatorInterface::class);
32  $this->_constraint = new \Magento\Framework\Validator\Constraint\Property(
33  $this->_validatorMock,
34  self::PROPERTY_NAME
35  );
36  }
37 
41  public function testGetAlias()
42  {
43  $this->assertEmpty($this->_constraint->getAlias());
44  $alias = 'foo';
45  $constraint = new \Magento\Framework\Validator\Constraint\Property(
46  $this->_validatorMock,
47  self::PROPERTY_NAME,
48  $alias
49  );
50  $this->assertEquals($alias, $constraint->getAlias());
51  }
52 
64  public function testIsValid(
65  $value,
66  $validateValue,
67  $expectedResult,
68  $validatorMessages = [],
69  $expectedMessages = []
70  ) {
71  $this->_validatorMock->expects(
72  $this->once()
73  )->method(
74  'isValid'
75  )->with(
76  $validateValue
77  )->will(
78  $this->returnValue($expectedResult)
79  );
80 
81  if ($expectedResult) {
82  $this->_validatorMock->expects($this->never())->method('getMessages');
83  } else {
84  $this->_validatorMock->expects(
85  $this->once()
86  )->method(
87  'getMessages'
88  )->will(
89  $this->returnValue($validatorMessages)
90  );
91  }
92 
93  $this->assertEquals($expectedResult, $this->_constraint->isValid($value));
94  $this->assertEquals($expectedMessages, $this->_constraint->getMessages());
95  }
96 
102  public function isValidDataProvider()
103  {
104  return [
105  [[self::PROPERTY_NAME => 'Property value', 'foo' => 'Foo value'], 'Property value', true],
106  [
107  new \Magento\Framework\DataObject([self::PROPERTY_NAME => 'Property value']),
108  'Property value',
109  true
110  ],
111  [new \ArrayObject([self::PROPERTY_NAME => 'Property value']), 'Property value', true],
112  [
113  [self::PROPERTY_NAME => 'Property value', 'foo' => 'Foo value'],
114  'Property value',
115  false,
116  ['Error message 1', 'Error message 2'],
117  [self::PROPERTY_NAME => ['Error message 1', 'Error message 2']]
118  ],
119  [
120  ['foo' => 'Foo value'],
121  null,
122  false,
123  ['Error message 1'],
124  [self::PROPERTY_NAME => ['Error message 1']]
125  ],
126  [
127  'scalar',
128  null,
129  false,
130  ['Error message 1'],
131  [self::PROPERTY_NAME => ['Error message 1']]
132  ]
133  ];
134  }
135 }
$value
Definition: gender.phtml:16
testIsValid( $value, $validateValue, $expectedResult, $validatorMessages=[], $expectedMessages=[])
if(!trim($html)) $alias
Definition: details.phtml:20