diff --git a/tests/EndpointTest.php b/tests/EndpointTest.php index 264554b..9e2f805 100644 --- a/tests/EndpointTest.php +++ b/tests/EndpointTest.php @@ -2,6 +2,7 @@ namespace Laravie\Codex\TestCase; +use GuzzleHttp\Psr7\Uri; use Laravie\Codex\Endpoint; use PHPUnit\Framework\TestCase; @@ -73,6 +74,23 @@ public function it_can_build_basic_endpoint_with_query_string() /** @test */ public function it_can_build_basic_endpoint_appended_query_string() + { + $uri = new Uri('https://laravel.com/docs?search=controller&page=3'); + $endpoint = new Endpoint($uri); + + $this->assertSame('https', $endpoint->getScheme()); + $this->assertSame('laravel.com', $endpoint->getHost()); + $this->assertSame('https://laravel.com', $endpoint->getUri()); + $this->assertSame(['docs'], $endpoint->getPath()); + $this->assertSame(['search' => 'controller', 'page' => '3'], $endpoint->getQuery()); + + $this->assertInstanceOf('GuzzleHttp\Psr7\Uri', $endpoint->get()); + $this->assertSame('https://laravel.com/docs?search=controller&page=3', (string) $endpoint->get()); + $this->assertSame('https://laravel.com/docs?search=controller&page=3', (string) $endpoint); + } + + /** @test */ + public function it_can_build_basic_endpoint_from_uri_instance() { $endpoint = (new Endpoint('https://laravel.com', 'docs')) ->addQuery(['search' => 'controller', 'page' => 3]); diff --git a/tests/Support/HttpClientTest.php b/tests/Support/HttpClientTest.php index e13c107..46dbc8b 100644 --- a/tests/Support/HttpClientTest.php +++ b/tests/Support/HttpClientTest.php @@ -3,11 +3,16 @@ namespace Laravie\Codex\TestCase\Support; use Mockery as m; +use GuzzleHttp\Psr7\Uri; +use Laravie\Codex\Request; use Laravie\Codex\Endpoint; use Laravie\Codex\Response; use PHPUnit\Framework\TestCase; -use Psr\Http\Message\UriInterface; use Laravie\Codex\Support\HttpClient; +use Psr\Http\Message\StreamInterface; +use Laravie\Codex\Testing\FakeRequest; +use Psr\Http\Message\ResponseInterface; +use Laravie\Codex\Contracts\Response as ResponseContract; class HttpClientTest extends TestCase { @@ -19,41 +24,48 @@ class HttpClientTest extends TestCase protected function tearDown() { m::close(); - } - - /** @test */ - public function it_can_return_same_instance_when_given_contract() - { - $endpoint = m::mock(Endpoint::class); - $stub = $this->convertUriToEndpoint($endpoint); - $this->assertSame($endpoint, $stub); + unset($this->http); } /** @test */ - public function it_can_return_endpoint_when_given_uri() + public function it_can_send_http_request() { - $endpoint = m::mock(UriInterface::class); + $headers = ['Accept' => 'application/json']; + $payloads = ['search' => 'codex']; - $endpoint->shouldReceive('getQuery')->andReturn('foo=bar'); + $faker = FakeRequest::create() + ->call('GET', $headers, '') + ->expectEndpointIs('https://laravel.com/docs/5.5?search=codex') + ->shouldResponseWith(200, '{"status":"success"}'); - $stub = $this->convertUriToEndpoint($endpoint); + $this->http = $faker->http(); - $this->assertInstanceOf(Endpoint::class, $stub); - $this->assertSame(['foo' => 'bar'], $stub->getQuery()); + $response = $this->send('GET', (new Endpoint('https://laravel.com', ['docs', '5.5']))->get(), $headers, $payloads); + + $this->assertSame(200, $response->getStatusCode()); + $this->assertSame('success', $response->toArray()['status']); } /** @test */ - public function it_can_return_endpoint_when_given_string() + public function it_can_stream_http_request() { - $endpoint = m::mock(UriInterface::class); + $headers = ['Content-Type' => 'application/json']; + $payloads = ['search' => 'codex']; + + $stream = m::mock(StreamInterface::class); + + $faker = FakeRequest::create() + ->call('POST', $headers, $stream) + ->expectEndpointIs('https://laravel.com/codex/') + ->shouldResponseWith(200, '{"status":"success"}'); + + $this->http = $faker->http(); - $stub = $this->convertUriToEndpoint('https://laravel.com/docs/5.4?search=controller'); + $response = $this->stream('POST', (new Endpoint('https://laravel.com/codex'))->get(), $headers, $stream); - $this->assertInstanceOf(Endpoint::class, $stub); - $this->assertSame('https://laravel.com', $stub->getUri()); - $this->assertSame(['docs', '5.4'], $stub->getPath()); - $this->assertSame(['search' => 'controller'], $stub->getQuery()); + $this->assertSame(200, $response->getStatusCode()); + $this->assertSame('success', $response->toArray()['status']); } /**