|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace spec\Http\Client\Common\Plugin; |
| 4 | + |
| 5 | +use Http\Client\Exception\NetworkException; |
| 6 | +use Http\Promise\FulfilledPromise; |
| 7 | +use Http\Promise\RejectedPromise; |
| 8 | +use Psr\Http\Message\RequestInterface; |
| 9 | +use Psr\Http\Message\ResponseInterface; |
| 10 | +use Symfony\Component\Stopwatch\Stopwatch; |
| 11 | +use PhpSpec\ObjectBehavior; |
| 12 | + |
| 13 | +class StopwatchPluginSpec extends ObjectBehavior |
| 14 | +{ |
| 15 | + function let(Stopwatch $stopwatch) |
| 16 | + { |
| 17 | + $this->beConstructedWith($stopwatch); |
| 18 | + } |
| 19 | + |
| 20 | + function it_is_initializable(Stopwatch $stopwatch) |
| 21 | + { |
| 22 | + $this->shouldHaveType('Http\Client\Common\Plugin\StopwatchPlugin'); |
| 23 | + } |
| 24 | + |
| 25 | + function it_is_a_plugin() |
| 26 | + { |
| 27 | + $this->shouldImplement('Http\Client\Common\Plugin'); |
| 28 | + } |
| 29 | + |
| 30 | + function it_records_event(Stopwatch $stopwatch, RequestInterface $request, ResponseInterface $response) |
| 31 | + { |
| 32 | + $request->getMethod()->willReturn('GET'); |
| 33 | + $request->getRequestTarget()->willReturn('/'); |
| 34 | + |
| 35 | + $stopwatch->start('GET /', 'php_http.request')->shouldBeCalled(); |
| 36 | + $stopwatch->stop('GET /', 'php_http.request')->shouldBeCalled(); |
| 37 | + |
| 38 | + $next = function (RequestInterface $request) use ($response) { |
| 39 | + return new FulfilledPromise($response->getWrappedObject()); |
| 40 | + }; |
| 41 | + |
| 42 | + $this->handleRequest($request, $next, function () {}); |
| 43 | + } |
| 44 | + |
| 45 | + function it_records_event_on_error(Stopwatch $stopwatch, RequestInterface $request) |
| 46 | + { |
| 47 | + $request->getMethod()->willReturn('GET'); |
| 48 | + $request->getRequestTarget()->willReturn('/'); |
| 49 | + |
| 50 | + $stopwatch->start('GET /', 'php_http.request')->shouldBeCalled(); |
| 51 | + $stopwatch->stop('GET /', 'php_http.request')->shouldBeCalled(); |
| 52 | + |
| 53 | + $next = function (RequestInterface $request) { |
| 54 | + return new RejectedPromise(new NetworkException('', $request)); |
| 55 | + }; |
| 56 | + |
| 57 | + $this->handleRequest($request, $next, function () {}); |
| 58 | + } |
| 59 | +} |
0 commit comments