Pest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Test Case
  5. |--------------------------------------------------------------------------
  6. |
  7. | The closure you provide to your test functions is always bound to a specific PHPUnit test
  8. | case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
  9. | need to change it using the "uses()" function to bind a different classes or traits.
  10. |
  11. */
  12. // uses(Tests\TestCase::class)->in('Feature');
  13. /*
  14. |--------------------------------------------------------------------------
  15. | Expectations
  16. |--------------------------------------------------------------------------
  17. |
  18. | When you're writing tests, you often need to check that values meet certain conditions. The
  19. | "expect()" function gives you access to a set of "expectations" methods that you can use
  20. | to assert different things. Of course, you may extend the Expectation API at any time.
  21. |
  22. */
  23. use Workerman\Connection\TcpConnection;
  24. /*
  25. |--------------------------------------------------------------------------
  26. | Functions
  27. |--------------------------------------------------------------------------
  28. |
  29. | While Pest is very powerful out-of-the-box, you may have some testing code specific to your
  30. | project that you don't want to repeat in every file. Here you can also expose helpers as
  31. | global functions to help you to reduce the number of lines of code in your test files.
  32. |
  33. */
  34. function something()
  35. {
  36. // ..
  37. }
  38. function testWithConnectionClose(Closure $closure, ?string $dataContains = null, $connectionClass = TcpConnection::class): void
  39. {
  40. $tcpConnection = Mockery::spy($connectionClass);
  41. $closure($tcpConnection);
  42. if ($dataContains) {
  43. $tcpConnection->shouldHaveReceived('close', function ($actual) use ($dataContains) {
  44. return str_contains($actual, $dataContains);
  45. });
  46. } else {
  47. $tcpConnection->shouldHaveReceived('close');
  48. }
  49. }
  50. function getNonFrameOutput(string $output): string
  51. {
  52. $end = "Start success.\n";
  53. $pos = strpos($output, $end);
  54. if ($pos !== false) {
  55. return substr($output, $pos + strlen($end));
  56. }
  57. return $output;
  58. }