MiddlewareTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace GuzzleHttp\Tests\Ring\Client;
  3. use GuzzleHttp\Ring\Client\Middleware;
  4. use GuzzleHttp\Ring\Future\CompletedFutureArray;
  5. class MiddlewareTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testFutureCallsDefaultHandler()
  8. {
  9. $future = new CompletedFutureArray(['status' => 200]);
  10. $calledA = false;
  11. $a = function (array $req) use (&$calledA, $future) {
  12. $calledA = true;
  13. return $future;
  14. };
  15. $calledB = false;
  16. $b = function (array $req) use (&$calledB) { $calledB = true; };
  17. $s = Middleware::wrapFuture($a, $b);
  18. $s([]);
  19. $this->assertTrue($calledA);
  20. $this->assertFalse($calledB);
  21. }
  22. public function testFutureCallsStreamingHandler()
  23. {
  24. $future = new CompletedFutureArray(['status' => 200]);
  25. $calledA = false;
  26. $a = function (array $req) use (&$calledA) { $calledA = true; };
  27. $calledB = false;
  28. $b = function (array $req) use (&$calledB, $future) {
  29. $calledB = true;
  30. return $future;
  31. };
  32. $s = Middleware::wrapFuture($a, $b);
  33. $result = $s(['client' => ['future' => true]]);
  34. $this->assertFalse($calledA);
  35. $this->assertTrue($calledB);
  36. $this->assertSame($future, $result);
  37. }
  38. public function testStreamingCallsDefaultHandler()
  39. {
  40. $calledA = false;
  41. $a = function (array $req) use (&$calledA) { $calledA = true; };
  42. $calledB = false;
  43. $b = function (array $req) use (&$calledB) { $calledB = true; };
  44. $s = Middleware::wrapStreaming($a, $b);
  45. $s([]);
  46. $this->assertTrue($calledA);
  47. $this->assertFalse($calledB);
  48. }
  49. public function testStreamingCallsStreamingHandler()
  50. {
  51. $calledA = false;
  52. $a = function (array $req) use (&$calledA) { $calledA = true; };
  53. $calledB = false;
  54. $b = function (array $req) use (&$calledB) { $calledB = true; };
  55. $s = Middleware::wrapStreaming($a, $b);
  56. $s(['client' => ['stream' => true]]);
  57. $this->assertFalse($calledA);
  58. $this->assertTrue($calledB);
  59. }
  60. }