TestCase.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace React\Tests\Dns;
  3. use PHPUnit\Framework\TestCase as BaseTestCase;
  4. abstract class TestCase extends BaseTestCase
  5. {
  6. protected function expectCallableOnce()
  7. {
  8. $mock = $this->createCallableMock();
  9. $mock
  10. ->expects($this->once())
  11. ->method('__invoke');
  12. return $mock;
  13. }
  14. protected function expectCallableOnceWith($value)
  15. {
  16. $mock = $this->createCallableMock();
  17. $mock
  18. ->expects($this->once())
  19. ->method('__invoke')
  20. ->with($value);
  21. return $mock;
  22. }
  23. protected function expectCallableNever()
  24. {
  25. $mock = $this->createCallableMock();
  26. $mock
  27. ->expects($this->never())
  28. ->method('__invoke');
  29. return $mock;
  30. }
  31. protected function createCallableMock()
  32. {
  33. if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'addMethods')) {
  34. // PHPUnit 9+
  35. return $this->getMockBuilder('stdClass')->addMethods(array('__invoke'))->getMock();
  36. } else {
  37. // legacy PHPUnit 4 - PHPUnit 9
  38. return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
  39. }
  40. }
  41. public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
  42. {
  43. if (method_exists($this, 'expectException')) {
  44. // PHPUnit 5
  45. $this->expectException($exception);
  46. if ($exceptionMessage !== '') {
  47. $this->expectExceptionMessage($exceptionMessage);
  48. }
  49. if ($exceptionCode !== null) {
  50. $this->expectExceptionCode($exceptionCode);
  51. }
  52. } else {
  53. // legacy PHPUnit 4
  54. parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
  55. }
  56. }
  57. }