Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProxyTestingTest.php
Go to the documentation of this file.
1 <?php
7 
8 class ProxyTestingTest extends \PHPUnit\Framework\TestCase
9 {
22  public function testInvokeWithExpectations(
23  $method,
24  $params,
25  $proxiedResult,
26  $proxiedMethod,
27  $proxiedParams,
28  $callProxiedMethod,
29  $passProxiedParams,
30  $expectedResult
31  ) {
32  // Create proxied object with $callProxiedMethod
33  $proxiedObject = $this->createPartialMock('stdClass', [$callProxiedMethod]);
34 
35  // Create object, which reacts on called $method by calling $callProxiedMethod from proxied object
36  $callProxy = function () use ($proxiedObject, $callProxiedMethod, $passProxiedParams) {
37  return call_user_func_array([$proxiedObject, $callProxiedMethod], $passProxiedParams);
38  };
39 
40  $object = $this->createPartialMock('stdClass', [$method]);
41  $builder = $object->expects($this->once())->method($method);
42  call_user_func_array([$builder, 'with'], $params);
43  $builder->will($this->returnCallback($callProxy));
44 
45  // Test it
46  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ProxyTesting();
47  $result = $helper->invokeWithExpectations(
48  $object,
49  $proxiedObject,
50  $method,
51  $params,
52  $proxiedResult,
53  $proxiedMethod,
54  $proxiedParams
55  );
56  $this->assertSame($expectedResult, $result);
57  }
58 
62  public static function invokeWithExpectationsDataProvider()
63  {
64  return [
65  'all parameters passed' => [
66  'method' => 'returnAplusB',
67  'params' => [3, 4],
68  'proxiedResult' => 7,
69  'proxiedMethod' => 'returnAplusB',
70  'proxiedParams' => [3, 4],
71  'callProxiedMethod' => 'returnAplusB',
72  'passProxiedParams' => [3, 4],
73  'expectedResult' => 7,
74  ],
75  'proxied method and params are to be set from proxy method and params' => [
76  'method' => 'returnAplusB',
77  'params' => [3, 4],
78  'proxiedResult' => 7,
79  'proxiedMethod' => null,
80  'proxiedParams' => null,
81  'callProxiedMethod' => 'returnAplusB',
82  'passProxiedParams' => [3, 4],
83  'expectedResult' => 7,
84  ],
85  'proxy and proxied method and params differ' => [
86  'method' => 'returnAminusBminusC',
87  'params' => [10, 1, 2],
88  'proxiedResult' => 7,
89  'proxiedMethod' => 'returnAminusB',
90  'proxiedParams' => [10, 3],
91  'callProxiedMethod' => 'returnAminusB',
92  'passProxiedParams' => [10, 3],
93  'expectedResult' => 7,
94  ]
95  ];
96  }
97 }
testInvokeWithExpectations( $method, $params, $proxiedResult, $proxiedMethod, $proxiedParams, $callProxiedMethod, $passProxiedParams, $expectedResult)
$helper
Definition: iframe.phtml:13
$method
Definition: info.phtml:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18