Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CallbackTest.php
Go to the documentation of this file.
1 <?php
8 
11 
15 class CallbackTest extends \PHPUnit\Framework\TestCase
16 {
20  const TEST_VALUE = 'test';
21 
32  public function testGetValue($callback, $expectedResult, $arguments = null, $createInstance = false)
33  {
34  $option = new Callback($callback, $arguments, $createInstance);
35  $this->assertEquals($expectedResult, $option->getValue());
36  }
37 
41  public function getConfigDataProvider()
42  {
43  $closure = function () {
44  return 'Value from closure';
45  };
46 
47  $mock = $this->getMockBuilder('Foo')
48  ->setMethods(['getValue'])
49  ->getMock();
50  $mock->method('getValue')
51  ->with('arg1', 'arg2')
52  ->willReturn('Value from mock');
53 
54  return [
55  [
56  $closure,
57  'Value from closure'
58  ],
59  [
60  [$this, 'getTestValue'],
62  ],
63  [
64  [__CLASS__, 'getTestValueStatically'],
66  ],
67  [
68  [$mock, 'getValue'],
69  'Value from mock', ['arg1', 'arg2']
70  ],
71  [
72  [TestCallback::class, 'getId'],
73  TestCallback::ID,
74  null,
75  true
76  ]
77  ];
78  }
79 
83  public static function getTestValueStatically()
84  {
85  return self::TEST_VALUE;
86  }
87 
91  public function getTestValue()
92  {
93  return self::TEST_VALUE;
94  }
95 
104  public function testSetArguments($value, $expectedValue)
105  {
106  $option = new Callback(function () {
107  });
108  $option->setArguments($value);
109  $this->assertAttributeEquals($expectedValue, '_arguments', $option);
110  }
111 
115  public function setArgumentsDataProvider()
116  {
117  return [
118  ['baz', ['baz']],
119  [
120  ['foo', 'bar'],
121  ['foo', 'bar']
122  ]
123  ];
124  }
125 
136  public function testGetValueException($callback, $expectedMessage, $createInstance = false)
137  {
138  $option = new Callback($callback, null, $createInstance);
139  $this->expectException('InvalidArgumentException');
140  $this->expectExceptionMessage($expectedMessage);
141  $option->getValue();
142  }
143 
150  {
151  return [
152  [
153  ['Not_Existing_Callback_Class', 'someMethod'],
154  'Class "Not_Existing_Callback_Class" was not found',
155  ],
156  [
157  [$this, 'notExistingMethod'],
158  'Callback does not callable'
159  ],
160  [
161  ['object' => $this, 'method' => 'getTestValue'],
162  'Callback does not callable'
163  ],
164  [
165  'unknown_function',
166  'Callback does not callable'
167  ],
168  [
169  new \stdClass(),
170  'Callback does not callable'
171  ],
172  [
173  [$this, 'getTestValue'],
174  'Callable expected to be an array with class name as first element',
175  true
176  ]
177  ];
178  }
179 }
testGetValueException($callback, $expectedMessage, $createInstance=false)
$value
Definition: gender.phtml:16
$arguments
testGetValue($callback, $expectedResult, $arguments=null, $createInstance=false)