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