ProxyTesting.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Helper class for testing the proxy objects
  8. */
  9. namespace Magento\Framework\TestFramework\Unit\Helper;
  10. class ProxyTesting
  11. {
  12. /**
  13. * Invoke the proxy's method, imposing expectations on proxied object, that it must be invoked as well with
  14. * appropriate parameters.
  15. *
  16. * @param mixed $object Proxy
  17. * @param \PHPUnit_Framework_MockObject_MockObject $proxiedObject
  18. * @param string $method Proxy's method to invoke
  19. * @param array $params Parameters to be passed to proxy
  20. * @param null $proxiedResult Result, that must be returned by the proxied object
  21. * @param null $expectedMethod Expected method, to be invoked in the proxied method
  22. * @param null $expectedParams Expected parameters, to be passed to the proxied method
  23. * @return mixed
  24. */
  25. public function invokeWithExpectations(
  26. $object,
  27. \PHPUnit_Framework_MockObject_MockObject $proxiedObject,
  28. $method,
  29. $params = [],
  30. $proxiedResult = null,
  31. $expectedMethod = null,
  32. $expectedParams = null
  33. ) {
  34. if ($expectedMethod === null) {
  35. $expectedMethod = $method;
  36. }
  37. if ($expectedParams === null) {
  38. $expectedParams = $params;
  39. }
  40. $builder = $proxiedObject->expects(
  41. new \PHPUnit\Framework\MockObject\Matcher\InvokedCount(1)
  42. )->method(
  43. $expectedMethod
  44. );
  45. $builder = call_user_func_array([$builder, 'with'], $expectedParams);
  46. $builder->will(new \PHPUnit\Framework\MockObject\Stub\ReturnStub($proxiedResult));
  47. return call_user_func_array([$object, $method], $params);
  48. }
  49. }