Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConstraintTest.php
Go to the documentation of this file.
1 <?php
11 
12 class ConstraintTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $_constraint;
18 
22  protected $_validatorMock;
23 
27  protected function setUp()
28  {
29  $this->_validatorMock = $this->getMockBuilder(
30  \Magento\Framework\Validator\AbstractValidator::class
31  )->setMethods(
32  ['isValid', 'getMessages']
33  )->getMock();
34  $this->_constraint = new \Magento\Framework\Validator\Constraint($this->_validatorMock);
35  }
36 
40  public function testGetAlias()
41  {
42  $this->assertEmpty($this->_constraint->getAlias());
43  $alias = 'foo';
44  $constraint = new \Magento\Framework\Validator\Constraint($this->_validatorMock, $alias);
45  $this->assertEquals($alias, $constraint->getAlias());
46  }
47 
57  public function testIsValid($value, $expectedResult, $expectedMessages = [])
58  {
59  $this->_validatorMock->expects(
60  $this->once()
61  )->method(
62  'isValid'
63  )->with(
64  $value
65  )->will(
66  $this->returnValue($expectedResult)
67  );
68 
69  if ($expectedResult) {
70  $this->_validatorMock->expects($this->never())->method('getMessages');
71  } else {
72  $this->_validatorMock->expects(
73  $this->once()
74  )->method(
75  'getMessages'
76  )->will(
77  $this->returnValue($expectedMessages)
78  );
79  }
80 
81  $this->assertEquals($expectedResult, $this->_constraint->isValid($value));
82  $this->assertEquals($expectedMessages, $this->_constraint->getMessages());
83  }
84 
90  public function isValidDataProvider()
91  {
92  return [['test', true], ['test', false, ['foo']]];
93  }
94 
98  public function testSetTranslator()
99  {
101  $translator = $this->getMockBuilder(
102  \Magento\Framework\Translate\AdapterInterface::class
103  )->getMockForAbstractClass();
104  $this->_constraint->setTranslator($translator);
105  $this->assertEquals($translator, $this->_validatorMock->getTranslator());
106  $this->assertEquals($translator, $this->_constraint->getTranslator());
107  }
108 }
testIsValid($value, $expectedResult, $expectedMessages=[])
$value
Definition: gender.phtml:16
if(!trim($html)) $alias
Definition: details.phtml:20