TestCase.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace React\Tests\Socket;
  3. use PHPUnit\Framework\TestCase as BaseTestCase;
  4. use React\Promise\Promise;
  5. use React\Stream\ReadableStreamInterface;
  6. class TestCase extends BaseTestCase
  7. {
  8. public function expectCallableExactly($amount)
  9. {
  10. $mock = $this->createCallableMock();
  11. $mock
  12. ->expects($this->exactly($amount))
  13. ->method('__invoke');
  14. return $mock;
  15. }
  16. protected function expectCallableOnce()
  17. {
  18. $mock = $this->createCallableMock();
  19. $mock
  20. ->expects($this->once())
  21. ->method('__invoke');
  22. return $mock;
  23. }
  24. protected function expectCallableOnceWith($value)
  25. {
  26. $mock = $this->createCallableMock();
  27. $mock
  28. ->expects($this->once())
  29. ->method('__invoke')
  30. ->with($value);
  31. return $mock;
  32. }
  33. protected function expectCallableOnceWithException($type, $message = null, $code = null)
  34. {
  35. return $this->expectCallableOnceWith(
  36. $this->logicalAnd(
  37. $this->isInstanceOf($type),
  38. $this->callback(function (\Exception $e) use ($message) {
  39. return $message === null || $e->getMessage() === $message;
  40. }),
  41. $this->callback(function (\Exception $e) use ($code) {
  42. return $code === null || $e->getCode() === $code;
  43. })
  44. )
  45. );
  46. }
  47. protected function expectCallableNever()
  48. {
  49. $mock = $this->createCallableMock();
  50. $mock
  51. ->expects($this->never())
  52. ->method('__invoke');
  53. return $mock;
  54. }
  55. protected function createCallableMock()
  56. {
  57. return $this->getMockBuilder('React\Tests\Socket\Stub\CallableStub')->getMock();
  58. }
  59. protected function buffer(ReadableStreamInterface $stream, $timeout)
  60. {
  61. if (!$stream->isReadable()) {
  62. return '';
  63. }
  64. $buffer = \React\Async\await(\React\Promise\Timer\timeout(new Promise(
  65. function ($resolve, $reject) use ($stream) {
  66. $buffer = '';
  67. $stream->on('data', function ($chunk) use (&$buffer) {
  68. $buffer .= $chunk;
  69. });
  70. $stream->on('error', $reject);
  71. $stream->on('close', function () use (&$buffer, $resolve) {
  72. $resolve($buffer);
  73. });
  74. },
  75. function () use ($stream) {
  76. $stream->close();
  77. throw new \RuntimeException();
  78. }
  79. ), $timeout));
  80. // let loop tick for reactphp/async v4 to clean up any remaining stream resources
  81. // @link https://github.com/reactphp/async/pull/65 reported upstream // TODO remove me once merged
  82. if (function_exists('React\Async\async')) {
  83. \React\Async\await(\React\Promise\Timer\sleep(0));
  84. }
  85. return $buffer;
  86. }
  87. public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
  88. {
  89. if (method_exists($this, 'expectException')) {
  90. // PHPUnit 5+
  91. $this->expectException($exception);
  92. if ($exceptionMessage !== '') {
  93. $this->expectExceptionMessage($exceptionMessage);
  94. }
  95. if ($exceptionCode !== null) {
  96. $this->expectExceptionCode($exceptionCode);
  97. }
  98. } else {
  99. // legacy PHPUnit 4
  100. parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
  101. }
  102. }
  103. protected function supportsTls13()
  104. {
  105. // TLS 1.3 is supported as of OpenSSL 1.1.1 (https://www.openssl.org/blog/blog/2018/09/11/release111/)
  106. // The OpenSSL library version can only be obtained by parsing output from phpinfo().
  107. // OPENSSL_VERSION_TEXT refers to header version which does not necessarily match actual library version
  108. // see php -i | grep OpenSSL
  109. // OpenSSL Library Version => OpenSSL 1.1.1 11 Sep 2018
  110. ob_start();
  111. phpinfo(INFO_MODULES);
  112. $info = ob_get_clean();
  113. if (preg_match('/OpenSSL Library Version => OpenSSL ([\d\.]+)/', $info, $match)) {
  114. return version_compare($match[1], '1.1.1', '>=');
  115. }
  116. return false;
  117. }
  118. public function assertContainsString($needle, $haystack)
  119. {
  120. if (method_exists($this, 'assertStringContainsString')) {
  121. // PHPUnit 7.5+
  122. $this->assertStringContainsString($needle, $haystack);
  123. } else {
  124. // legacy PHPUnit 4- PHPUnit 7.5
  125. $this->assertContains($needle, $haystack);
  126. }
  127. }
  128. public function assertMatchesRegExp($pattern, $string)
  129. {
  130. if (method_exists($this, 'assertMatchesRegularExpression')) {
  131. // PHPUnit 10
  132. $this->assertMatchesRegularExpression($pattern, $string);
  133. } else {
  134. // legacy PHPUnit 4 - PHPUnit 9.2
  135. $this->assertRegExp($pattern, $string);
  136. }
  137. }
  138. public function assertDoesNotMatchRegExp($pattern, $string)
  139. {
  140. if (method_exists($this, 'assertDoesNotMatchRegularExpression')) {
  141. // PHPUnit 10
  142. $this->assertDoesNotMatchRegularExpression($pattern, $string);
  143. } else {
  144. // legacy PHPUnit 4 - PHPUnit 9.2
  145. $this->assertNotRegExp($pattern, $string);
  146. }
  147. }
  148. }