|
| 1 | +<?php |
| 2 | + |
| 3 | +use Clue\React\Packagist\Api\Client; |
| 4 | +use React\Promise\Deferred; |
| 5 | +use Clue\React\Buzz\Message\Response; |
| 6 | +use Clue\React\Buzz\Message\Headers; |
| 7 | +use Clue\React\Buzz\Message\Body; |
| 8 | + |
| 9 | +class ClientTest extends TestCase |
| 10 | +{ |
| 11 | + private $browser; |
| 12 | + private $client; |
| 13 | + |
| 14 | + public function setUp() |
| 15 | + { |
| 16 | + $this->browser = $this->getMockBuilder('Clue\React\Buzz\Browser')->disableOriginalConstructor()->getMock(); |
| 17 | + |
| 18 | + $this->client = new Client($this->browser); |
| 19 | + } |
| 20 | + |
| 21 | + public function testGet() |
| 22 | + { |
| 23 | + $this->setupBrowser('packages/clue/zenity-react.json', $this->createResponsePromise('{"package":{"name":"clue\/zenity-react", "versions": {}}}')); |
| 24 | + |
| 25 | + $this->expectPromiseResolve($this->client->get('clue/zenity-react')); |
| 26 | + } |
| 27 | + |
| 28 | + public function testAll() |
| 29 | + { |
| 30 | + $this->setupBrowser('packages/list.json', $this->createResponsePromise('{"packageNames":["a/a", "b/b"]}')); |
| 31 | + |
| 32 | + $this->expectPromiseResolve($this->client->all()); |
| 33 | + } |
| 34 | + |
| 35 | + public function testAllVendor() |
| 36 | + { |
| 37 | + $this->setupBrowser('packages/list.json?vendor=a', $this->createResponsePromise('{"packageNames":["a/a"]}')); |
| 38 | + |
| 39 | + $this->expectPromiseResolve($this->client->all(array('vendor' => 'a'))); |
| 40 | + } |
| 41 | + |
| 42 | + public function testSearch() |
| 43 | + { |
| 44 | + $this->setupBrowser('search.json?q=zenity', $this->createResponsePromise('{"results":[{"name":"clue\/zenity-react","description":"Build graphical desktop (GUI) applications in PHP","url":"https:\/\/packagist.org\/packages\/clue\/zenity-react","downloads":57,"favers":0,"repository":"https:\/\/github.com\/clue\/reactphp-zenity"}],"total":1}')); |
| 45 | + |
| 46 | + $this->expectPromiseResolve($this->client->search('zenity')); |
| 47 | + } |
| 48 | + |
| 49 | + public function testSearchPagination() |
| 50 | + { |
| 51 | + $this->browser->expects($this->exactly(2)) |
| 52 | + ->method('get') |
| 53 | + ->will($this->onConsecutiveCalls( |
| 54 | + $this->createResponsePromise('{"results":[{"name":"clue\/zenity-react","description":"Build graphical desktop (GUI) applications in PHP","url":"https:\/\/packagist.org\/packages\/clue\/zenity-react","downloads":57,"favers":0,"repository":"https:\/\/github.com\/clue\/reactphp-zenity"}],"total":2, "next": ""}'), |
| 55 | + $this->createResponsePromise('{"results":[{"name":"clue\/zenity-react","description":"Build graphical desktop (GUI) applications in PHP","url":"https:\/\/packagist.org\/packages\/clue\/zenity-react","downloads":57,"favers":0,"repository":"https:\/\/github.com\/clue\/reactphp-zenity"}],"total":2}') |
| 56 | + )); |
| 57 | + |
| 58 | + $this->expectPromiseResolve($this->client->search('zenity')); |
| 59 | + } |
| 60 | + |
| 61 | + public function testHttpError() |
| 62 | + { |
| 63 | + $this->setupBrowser('packages/clue/invalid.json', $this->createRejectedPromise(new RuntimeException('error'))); |
| 64 | + |
| 65 | + $this->expectPromiseReject($this->client->get('clue/invalid')); |
| 66 | + } |
| 67 | + |
| 68 | + private function setupBrowser($expectedUrl, $promise) |
| 69 | + { |
| 70 | + $this->browser->expects($this->once()) |
| 71 | + ->method('get') |
| 72 | + ->with($this->equalTo('https://packagist.org/' . $expectedUrl), array()) |
| 73 | + ->will($this->returnValue($promise)); |
| 74 | + } |
| 75 | + |
| 76 | + private function createResponsePromise($fakeResponseBody) |
| 77 | + { |
| 78 | + $d = new Deferred(); |
| 79 | + $d->resolve(new Response('HTTP/1.0', 200, 'OK', new Headers(), new Body($fakeResponseBody))); |
| 80 | + return $d->promise(); |
| 81 | + } |
| 82 | + |
| 83 | + private function createRejectedPromise($reason) |
| 84 | + { |
| 85 | + $d = new Deferred(); |
| 86 | + $d->reject($reason); |
| 87 | + return $d->promise(); |
| 88 | + } |
| 89 | +} |
0 commit comments