Skip to content

Commit

Permalink
Merge branch '1.6' into 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
crynobone committed Apr 5, 2018
2 parents 9b2358d + 097d7f7 commit b4c1b82
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
18 changes: 18 additions & 0 deletions tests/EndpointTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Laravie\Codex\TestCase;

use GuzzleHttp\Psr7\Uri;
use Laravie\Codex\Endpoint;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -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]);
Expand Down
56 changes: 34 additions & 22 deletions tests/Support/HttpClientTest.php
Expand Up @@ -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
{
Expand All @@ -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']);
}

/**
Expand Down

0 comments on commit b4c1b82

Please sign in to comment.