3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
-
7
6
namespace Magento \Framework \Validator \Test \Unit \Constraint \Option ;
8
7
9
- /**
10
- * Test case for \ Magento\Framework\Validator\Constraint\Option\ Callback
11
- */
8
+ use Magento \ Framework \ Validator \ Constraint \ Option \ Callback ;
9
+ use Magento \Framework \Validator \Test \ Unit \ Test \ Callback as TestCallback ;
10
+
12
11
class CallbackTest extends \PHPUnit \Framework \TestCase
13
12
{
14
13
/**
@@ -28,41 +27,46 @@ class CallbackTest extends \PHPUnit\Framework\TestCase
28
27
*/
29
28
public function testGetValue ($ callback , $ expectedResult , $ arguments = null , $ createInstance = false )
30
29
{
31
- $ option = new \ Magento \ Framework \ Validator \ Constraint \ Option \ Callback ($ callback , $ arguments , $ createInstance );
32
- $ this -> assertEquals ($ expectedResult , $ option ->getValue ());
30
+ $ option = new Callback ($ callback , $ arguments , $ createInstance );
31
+ self :: assertEquals ($ expectedResult , $ option ->getValue ());
33
32
}
34
33
35
34
/**
36
35
* Data provider for testGetValue
37
36
*/
38
37
public function getConfigDataProvider ()
39
38
{
40
- $ functionName = create_function ('' , 'return "Value from function"; ' );
41
39
$ closure = function () {
42
40
return 'Value from closure ' ;
43
41
};
44
42
45
- $ mock = $ this ->getMockBuilder ('Foo ' )->setMethods (['getValue ' ])->getMock ();
46
- $ mock ->expects (
47
- $ this ->once ()
48
- )->method (
49
- 'getValue '
50
- )->with (
51
- 'arg1 ' ,
52
- 'arg2 '
53
- )->will (
54
- $ this ->returnValue ('Value from mock ' )
55
- );
43
+ $ mock = $ this ->getMockBuilder ('Foo ' )
44
+ ->setMethods (['getValue ' ])
45
+ ->getMock ();
46
+ $ mock ->method ('getValue ' )
47
+ ->with ('arg1 ' , 'arg2 ' )
48
+ ->willReturn ('Value from mock ' );
56
49
57
50
return [
58
- [$ functionName , 'Value from function ' ],
59
- [$ closure , 'Value from closure ' ],
60
- [[$ this , 'getTestValue ' ], self ::TEST_VALUE ],
61
- [[__CLASS__ , 'getTestValueStatically ' ], self ::TEST_VALUE ],
62
- [[$ mock , 'getValue ' ], 'Value from mock ' , ['arg1 ' , 'arg2 ' ]],
63
51
[
64
- [\Magento \Framework \Validator \Test \Unit \Test \Callback::class, 'getId ' ],
65
- \Magento \Framework \Validator \Test \Unit \Test \Callback::ID ,
52
+ $ closure ,
53
+ 'Value from closure '
54
+ ],
55
+ [
56
+ [$ this , 'getTestValue ' ],
57
+ self ::TEST_VALUE
58
+ ],
59
+ [
60
+ [__CLASS__ , 'getTestValueStatically ' ],
61
+ self ::TEST_VALUE
62
+ ],
63
+ [
64
+ [$ mock , 'getValue ' ],
65
+ 'Value from mock ' , ['arg1 ' , 'arg2 ' ]
66
+ ],
67
+ [
68
+ [TestCallback::class, 'getId ' ],
69
+ TestCallback::ID ,
66
70
null ,
67
71
true
68
72
]
@@ -90,25 +94,29 @@ public function getTestValue()
90
94
*
91
95
* @dataProvider setArgumentsDataProvider
92
96
*
93
- * @param mixed $value
94
- * @param mixed $expectedValue
97
+ * @param string|array $value
98
+ * @param string|array $expectedValue
95
99
*/
96
100
public function testSetArguments ($ value , $ expectedValue )
97
101
{
98
- $ option = new \Magento \Framework \Validator \Constraint \Option \Callback (
99
- function () {
100
- }
101
- );
102
+ $ option = new Callback (function () {
103
+ });
102
104
$ option ->setArguments ($ value );
103
- $ this -> assertAttributeEquals ($ expectedValue , '_arguments ' , $ option );
105
+ self :: assertAttributeEquals ($ expectedValue , '_arguments ' , $ option );
104
106
}
105
107
106
108
/**
107
109
* Data provider for testGetValue
108
110
*/
109
111
public function setArgumentsDataProvider ()
110
112
{
111
- return [['baz ' , ['baz ' ]], [['foo ' , 'bar ' ], ['foo ' , 'bar ' ]]];
113
+ return [
114
+ ['baz ' , ['baz ' ]],
115
+ [
116
+ ['foo ' , 'bar ' ],
117
+ ['foo ' , 'bar ' ]
118
+ ]
119
+ ];
112
120
}
113
121
114
122
/**
@@ -119,11 +127,12 @@ public function setArgumentsDataProvider()
119
127
* @param mixed $callback
120
128
* @param string $expectedMessage
121
129
* @param bool $createInstance
130
+ * @expectedException \InvalidArgumentException
122
131
*/
123
132
public function testGetValueException ($ callback , $ expectedMessage , $ createInstance = false )
124
133
{
125
- $ option = new \ Magento \ Framework \ Validator \ Constraint \ Option \ Callback ($ callback , null , $ createInstance );
126
- $ this -> expectException ( ' InvalidArgumentException ' , $ expectedMessage );
134
+ $ option = new Callback ($ callback , null , $ createInstance );
135
+ self :: expectExceptionMessage ( $ expectedMessage );
127
136
$ option ->getValue ();
128
137
}
129
138
@@ -139,10 +148,22 @@ public function getValueExceptionDataProvider()
139
148
['Not_Existing_Callback_Class ' , 'someMethod ' ],
140
149
'Class "Not_Existing_Callback_Class" was not found ' ,
141
150
],
142
- [[$ this , 'notExistingMethod ' ], 'Callback does not callable ' ],
143
- [['object ' => $ this , 'method ' => 'getTestValue ' ], 'Callback does not callable ' ],
144
- ['unknown_function ' , 'Callback does not callable ' ],
145
- [new \stdClass (), 'Callback does not callable ' ],
151
+ [
152
+ [$ this , 'notExistingMethod ' ],
153
+ 'Callback does not callable '
154
+ ],
155
+ [
156
+ ['object ' => $ this , 'method ' => 'getTestValue ' ],
157
+ 'Callback does not callable '
158
+ ],
159
+ [
160
+ 'unknown_function ' ,
161
+ 'Callback does not callable '
162
+ ],
163
+ [
164
+ new \stdClass (),
165
+ 'Callback does not callable '
166
+ ],
146
167
[
147
168
[$ this , 'getTestValue ' ],
148
169
'Callable expected to be an array with class name as first element ' ,
0 commit comments