MockHandlerTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace GuzzleHttp\Tests\Ring\Client;
  3. use GuzzleHttp\Ring\Client\MockHandler;
  4. use GuzzleHttp\Ring\Future\FutureArray;
  5. use React\Promise\Deferred;
  6. class MockHandlerTest extends \PHPUnit_Framework_TestCase
  7. {
  8. public function testReturnsArray()
  9. {
  10. $mock = new MockHandler(['status' => 200]);
  11. $response = $mock([]);
  12. $this->assertEquals(200, $response['status']);
  13. $this->assertEquals([], $response['headers']);
  14. $this->assertNull($response['body']);
  15. $this->assertNull($response['reason']);
  16. $this->assertNull($response['effective_url']);
  17. }
  18. public function testReturnsFutures()
  19. {
  20. $deferred = new Deferred();
  21. $future = new FutureArray(
  22. $deferred->promise(),
  23. function () use ($deferred) {
  24. $deferred->resolve(['status' => 200]);
  25. }
  26. );
  27. $mock = new MockHandler($future);
  28. $response = $mock([]);
  29. $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
  30. $this->assertEquals(200, $response['status']);
  31. }
  32. public function testReturnsFuturesWithThenCall()
  33. {
  34. $deferred = new Deferred();
  35. $future = new FutureArray(
  36. $deferred->promise(),
  37. function () use ($deferred) {
  38. $deferred->resolve(['status' => 200]);
  39. }
  40. );
  41. $mock = new MockHandler($future);
  42. $response = $mock([]);
  43. $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
  44. $this->assertEquals(200, $response['status']);
  45. $req = null;
  46. $promise = $response->then(function ($value) use (&$req) {
  47. $req = $value;
  48. $this->assertEquals(200, $req['status']);
  49. });
  50. $this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
  51. $this->assertEquals(200, $req['status']);
  52. }
  53. public function testReturnsFuturesAndProxiesCancel()
  54. {
  55. $c = null;
  56. $deferred = new Deferred();
  57. $future = new FutureArray(
  58. $deferred->promise(),
  59. function () {},
  60. function () use (&$c) {
  61. $c = true;
  62. return true;
  63. }
  64. );
  65. $mock = new MockHandler($future);
  66. $response = $mock([]);
  67. $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
  68. $response->cancel();
  69. $this->assertTrue($c);
  70. }
  71. /**
  72. * @expectedException \InvalidArgumentException
  73. * @expectedExceptionMessage Response must be an array or FutureArrayInterface. Found
  74. */
  75. public function testEnsuresMockIsValid()
  76. {
  77. $mock = new MockHandler('foo');
  78. $mock([]);
  79. }
  80. }