Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
HelperMethodTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\View\Layout\Argument\Interpreter\HelperMethod;
9 
10 class HelperMethodTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $_objectManager;
16 
20  protected $_interpreter;
21 
25  protected $_model;
26 
27  protected function setUp()
28  {
29  $this->_objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
30  $this->_interpreter = $this->createMock(\Magento\Framework\View\Layout\Argument\Interpreter\NamedParams::class);
31  $this->_model = new HelperMethod($this->_objectManager, $this->_interpreter);
32  }
33 
34  public function testEvaluate()
35  {
36  $input = ['value' => 'some text', 'helper' => __CLASS__ . '::help'];
37 
38  $evaluatedValue = ['value' => 'some text (evaluated)'];
39  $this->_interpreter->expects(
40  $this->once()
41  )->method(
42  'evaluate'
43  )->with(
44  $input
45  )->will(
46  $this->returnValue($evaluatedValue)
47  );
48 
49  $this->_objectManager->expects($this->once())->method('get')->with(__CLASS__)->will($this->returnValue($this));
50 
51  $expected = 'some text (evaluated) (updated)';
52  $actual = $this->_model->evaluate($input);
53  $this->assertSame($expected, $actual);
54  }
55 
60  public function help($input)
61  {
62  $this->assertSame('some text (evaluated)', $input);
63  return $input . ' (updated)';
64  }
65 
72  public function testEvaluateException($helperMethod, $expectedExceptionMessage)
73  {
74  $this->expectException('\InvalidArgumentException');
75  $this->expectExceptionMessage($expectedExceptionMessage);
76  $input = ['value' => 'some text', 'helper' => $helperMethod];
77  $this->_model->evaluate($input);
78  }
79 
84  {
85  $nonExistingHelper = __CLASS__ . '::non_existing';
86  return [
87  'wrong method format' => [
88  'help',
89  'Helper method name in format "\Class\Name::methodName" is expected',
90  ],
91  'non-existing method' => [$nonExistingHelper, "Helper method '{$nonExistingHelper}' does not exist"]
92  ];
93  }
94 }