1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Test case for \Magento\Framework\Validator\Constraint
*/
namespace Magento\Framework\Validator\Test\Unit;
class ConstraintTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\Validator\Constraint
*/
protected $_constraint;
/**
* @var \Magento\Framework\Validator\ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $_validatorMock;
/**
* Set up
*/
protected function setUp()
{
$this->_validatorMock = $this->getMockBuilder(
\Magento\Framework\Validator\AbstractValidator::class
)->setMethods(
['isValid', 'getMessages']
)->getMock();
$this->_constraint = new \Magento\Framework\Validator\Constraint($this->_validatorMock);
}
/**
* Test getAlias method
*/
public function testGetAlias()
{
$this->assertEmpty($this->_constraint->getAlias());
$alias = 'foo';
$constraint = new \Magento\Framework\Validator\Constraint($this->_validatorMock, $alias);
$this->assertEquals($alias, $constraint->getAlias());
}
/**
* Test isValid method
*
* @dataProvider isValidDataProvider
*
* @param mixed $value
* @param bool $expectedResult
* @param array $expectedMessages
*/
public function testIsValid($value, $expectedResult, $expectedMessages = [])
{
$this->_validatorMock->expects(
$this->once()
)->method(
'isValid'
)->with(
$value
)->will(
$this->returnValue($expectedResult)
);
if ($expectedResult) {
$this->_validatorMock->expects($this->never())->method('getMessages');
} else {
$this->_validatorMock->expects(
$this->once()
)->method(
'getMessages'
)->will(
$this->returnValue($expectedMessages)
);
}
$this->assertEquals($expectedResult, $this->_constraint->isValid($value));
$this->assertEquals($expectedMessages, $this->_constraint->getMessages());
}
/**
* Data provider for testIsValid
*
* @return array
*/
public function isValidDataProvider()
{
return [['test', true], ['test', false, ['foo']]];
}
/**
* Check translator was set into wrapped validator
*/
public function testSetTranslator()
{
/** @var \Magento\Framework\Translate\AbstractAdapter $translator */
$translator = $this->getMockBuilder(
\Magento\Framework\Translate\AdapterInterface::class
)->getMockForAbstractClass();
$this->_constraint->setTranslator($translator);
$this->assertEquals($translator, $this->_validatorMock->getTranslator());
$this->assertEquals($translator, $this->_constraint->getTranslator());
}
}