397.phpt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. --TEST--
  2. https://github.com/sebastianbergmann/phpunit-mock-objects/issues/397
  3. --SKIPIF--
  4. <?php
  5. if (!version_compare(PHP_VERSION, '7.1', '>=')) print 'skip: PHP >= 7.1 required';
  6. --FILE--
  7. <?php
  8. class C
  9. {
  10. public function m(?self $other): self
  11. {
  12. }
  13. }
  14. require __DIR__ . '/../../vendor/autoload.php';
  15. $generator = new \PHPUnit\Framework\MockObject\Generator;
  16. $mock = $generator->generate(
  17. C::class,
  18. [],
  19. 'MockC',
  20. true,
  21. true
  22. );
  23. print $mock['code'];
  24. --EXPECTF--
  25. class MockC extends C implements PHPUnit\Framework\MockObject\MockObject
  26. {
  27. private $__phpunit_invocationMocker;
  28. private $__phpunit_originalObject;
  29. private $__phpunit_configurable = ['m'];
  30. public function __clone()
  31. {
  32. $this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationMocker();
  33. }
  34. public function m(?C $other): C
  35. {
  36. $arguments = array($other);
  37. $count = func_num_args();
  38. if ($count > 1) {
  39. $_arguments = func_get_args();
  40. for ($i = 1; $i < $count; $i++) {
  41. $arguments[] = $_arguments[$i];
  42. }
  43. }
  44. $result = $this->__phpunit_getInvocationMocker()->invoke(
  45. new \PHPUnit\Framework\MockObject\Invocation\ObjectInvocation(
  46. 'C', 'm', $arguments, 'C', $this, true
  47. )
  48. );
  49. return $result;
  50. }
  51. public function expects(\PHPUnit\Framework\MockObject\Matcher\Invocation $matcher)
  52. {
  53. return $this->__phpunit_getInvocationMocker()->expects($matcher);
  54. }
  55. public function method()
  56. {
  57. $any = new \PHPUnit\Framework\MockObject\Matcher\AnyInvokedCount;
  58. $expects = $this->expects($any);
  59. return call_user_func_array(array($expects, 'method'), func_get_args());
  60. }
  61. public function __phpunit_setOriginalObject($originalObject)
  62. {
  63. $this->__phpunit_originalObject = $originalObject;
  64. }
  65. public function __phpunit_getInvocationMocker()
  66. {
  67. if ($this->__phpunit_invocationMocker === null) {
  68. $this->__phpunit_invocationMocker = new \PHPUnit\Framework\MockObject\InvocationMocker($this->__phpunit_configurable);
  69. }
  70. return $this->__phpunit_invocationMocker;
  71. }
  72. public function __phpunit_hasMatchers()
  73. {
  74. return $this->__phpunit_getInvocationMocker()->hasMatchers();
  75. }
  76. public function __phpunit_verify($unsetInvocationMocker = true)
  77. {
  78. $this->__phpunit_getInvocationMocker()->verify();
  79. if ($unsetInvocationMocker) {
  80. $this->__phpunit_invocationMocker = null;
  81. }
  82. }
  83. }