Skip to content

Commit

Permalink
Merge branch '1.6'
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Apr 3, 2018
2 parents 278288a + 74a913f commit 5e86de7
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 53 deletions.
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service_name: travis-ci
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Ignore following folder/file.
/tests export-ignore
/.coveralls.yml export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.php_cs export-ignore
Expand Down
14 changes: 13 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ php:
- 7.2

env:
global:
- coverage=no
matrix:
- setup=basic
- setup=lowest
Expand All @@ -18,9 +20,19 @@ before_script:
- if [[ $setup = 'basic' ]]; then travis_retry composer install --prefer-source --no-interaction; fi
- if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-source --no-interaction --prefer-stable; fi
- if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-source --no-interaction --prefer-lowest --prefer-stable; fi
- if [[ $setup = 'coveralls' ]]; then travis_retry composer require "satooshi/php-coveralls=~1.0" --prefer-dist --no-interaction --dev; fi

script:
- vendor/bin/phpunit -c phpunit.xml --testdox
- if [[ $coverage = 'yes' ]]; then vendor/bin/phpunit -c phpunit.xml --coverage-clover build/logs/clover.xml; fi
- if [[ $coverage = 'no' ]]; then vendor/bin/phpunit -c phpunit.xml; fi

after_script:
- if [[ $setup = 'coveralls' ]]; then php vendor/bin/coveralls -v; fi

matrix:
include:
- php: 7.1
env: setup=coveralls coverage=yes
allow_failures:
- env: setup=coveralls coverage=yes
fast_finish: true
10 changes: 10 additions & 0 deletions CHANGELOG-1.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

This changelog references the relevant changes (bug and security fixes) done to `laravie/codex`.

## 1.6.0

Released: 2018-04-03

### Changes

* Bump minimum PHP to 7.0+.
* `Laravie\Codex\Endpoint` now implements `Laravie\Codex\Contracts\Endpoint`.
* Improves tests.

## 1.5.0

Released: 2018-03-12
Expand Down
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function uses(string $service, ?string $version = null): Contracts\Reques
throw new InvalidArgumentException("Resource [{$service}] for version [{$version}] is not available.");
}

return new $class($this);
return $this->via(new $class($this));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Testing/FakeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function call(string $method = 'GET', $headers = [], $body = ''): self
->andReturnUsing(function ($m, $u, $h, $b) {
Assert::assertSame((string) $u, $this->expectedEndpoint);

return $this->message;
return $this->message();
});

return $this;
Expand Down
91 changes: 41 additions & 50 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Laravie\Codex\Testing\FakeRequest;
use Laravie\Codex\TestCase\Acme\Client;

class ClientTest extends TestCase
Expand All @@ -24,6 +25,7 @@ public function it_has_proper_signature()
$this->assertInstanceOf('Laravie\Codex\Client', $stub);
$this->assertSame('v1', $stub->getApiVersion());
$this->assertSame('https://acme.laravie/', $stub->getApiEndpoint());
$this->assertSame([], $stub->queries());
}

/** @test */
Expand All @@ -41,17 +43,12 @@ public function it_can_set_custom_endpoint()
/** @test */
public function it_can_send_api_request_on_version_one()
{
$http = m::mock('Http\Client\Common\HttpMethodsClient');
$message = m::mock('Psr\Http\Message\ResponseInterface');

$http->shouldReceive('send')->once()
->with('GET', m::type('GuzzleHttp\Psr7\Uri'), [], '')
->andReturn($message);

$message->shouldReceive('getStatusCode')->andReturn(200)
->shouldReceive('getBody')->andReturn('{"success":true}');
$faker = FakeRequest::create()
->call('GET', [], '')
->expectEndpointIs('https://acme.laravie/v1/welcome')
->shouldResponseWith(200, '{"success":true}');

$response = (new Client($http, 'abc'))
$response = (new Client($faker->http(), 'abc'))
->uses('Welcome')
->show();

Expand All @@ -65,17 +62,12 @@ public function it_can_send_api_request_on_version_one()
/** @test */
public function it_can_send_api_request_on_version_two()
{
$http = m::mock('Http\Client\Common\HttpMethodsClient');
$message = m::mock('Psr\Http\Message\ResponseInterface');

$http->shouldReceive('send')->once()
->with('GET', m::type('GuzzleHttp\Psr7\Uri'), ['Authorization' => 'Bearer abc'], '')
->andReturn($message);

$message->shouldReceive('getStatusCode')->andReturn(200)
->shouldReceive('getBody')->andReturn('{"success":true}');
$faker = FakeRequest::create()
->call('GET', ['Authorization' => 'Bearer abc'], '')
->expectEndpointIs('https://acme.laravie/v2/welcome')
->shouldResponseWith(200, '{"success":true}');

$response = (new Client($http, 'abc'))
$response = (new Client($faker->http(), 'abc'))
->useVersion('v2')
->uses('Welcome')
->show();
Expand All @@ -90,18 +82,14 @@ public function it_can_send_api_request_on_version_two()
/** @test */
public function it_can_send_api_request_by_sending_stream_data()
{
$http = m::mock('Http\Client\Common\HttpMethodsClient');
$message = m::mock('Psr\Http\Message\ResponseInterface');
$stream = m::mock('Psr\Http\Message\StreamInterface');

$http->shouldReceive('send')->once()
->with('POST', m::type('GuzzleHttp\Psr7\Uri'), [], $stream)
->andReturn($message);

$message->shouldReceive('getStatusCode')->andReturn(200)
->shouldReceive('getBody')->andReturn('{"success":true}');
$faker = FakeRequest::create()
->call('POST', [], $stream)
->expectEndpointIs('https://acme.laravie/v1/welcome')
->shouldResponseWith(200, '{"success":true}');

$response = (new Client($http, 'abc'))->uses('Welcome')->ping($stream);
$response = (new Client($faker->http(), 'abc'))->uses('Welcome')->ping($stream);

$this->assertSame(200, $response->getStatusCode());
$this->assertSame('{"success":true}', $response->getBody());
Expand All @@ -113,21 +101,17 @@ public function it_can_send_api_request_by_sending_stream_data()
/** @test */
public function it_can_send_api_request_by_sending_json_data()
{
$http = m::mock('Http\Client\Common\HttpMethodsClient');
$message = m::mock('Psr\Http\Message\ResponseInterface');

$headers = ['Content-Type' => 'application/json'];
$payload = ['meta' => ['foo', 'bar']];

$http->shouldReceive('send')->once()
->with('POST', m::type('GuzzleHttp\Psr7\Uri'), $headers, '{"meta":["foo","bar"]}')
->andReturn($message);

$message->shouldReceive('getStatusCode')->andReturn(200)
->shouldReceive('getBody')->andReturn('{"success":true}');
$faker = FakeRequest::create()
->call('POST', $headers, json_encode($payload))
->expectEndpointIs('https://acme.laravie/v1/welcome')
->shouldResponseWith(200, '{"success":true}');

$response = (new Client($http, 'abc'))
$response = (new Client($faker->http(), 'abc'))
->uses('Welcome')
->ping(['meta' => ['foo', 'bar']], $headers);
->ping($payload, $headers);

$this->assertSame(200, $response->getStatusCode());
$this->assertSame('{"success":true}', $response->getBody());
Expand All @@ -139,17 +123,12 @@ public function it_can_send_api_request_by_sending_json_data()
/** @test */
public function it_can_send_api_request_by_providing_endpoint()
{
$http = m::mock('Http\Client\Common\HttpMethodsClient');
$message = m::mock('Psr\Http\Message\ResponseInterface');
$faker = FakeRequest::create()
->call('GET', [], '')
->expectEndpointIs('https://acme.laravie/v1/welcome')
->shouldResponseWith(200, '{"success":true}');

$http->shouldReceive('send')->once()
->with('GET', m::type('GuzzleHttp\Psr7\Uri'), [], '')
->andReturn($message);

$message->shouldReceive('getStatusCode')->andReturn(200)
->shouldReceive('getBody')->andReturn('{"success":true}');

$response = (new Client($http, 'abc'))
$response = (new Client($faker->http(), 'abc'))
->uses('Welcome')
->pong();

Expand Down Expand Up @@ -181,4 +160,16 @@ public function it_cant_find_unknown_resource()

$response = (new Client($http, 'abc'))->uses('Foobar');
}

/**
* @test
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Resource [Foobar] for version [v1] is not available.
*/
public function it_cant_find_unknown_resource_using_old_method()
{
$http = m::mock('Http\Client\Common\HttpMethodsClient');

$response = (new Client($http, 'abc'))->resource('Foobar');
}
}
50 changes: 50 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Mockery as m;
use Laravie\Codex\Response;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\ResponseInterface;
use Laravie\Codex\Exceptions\HttpException;

class ResponseTest extends TestCase
{
Expand Down Expand Up @@ -65,6 +67,54 @@ public function it_can_return_parent_methods()
$this->assertSame('1.1', $stub->getProtocolVersion());
}

/** @test */
public function it_can_get_body_from_stream()
{
$stream = m::mock(StreamInterface::class);
$api = m::mock(ResponseInterface::class);

$api->shouldReceive('getBody')->andReturn($stream);
$stream->shouldReceive('__toString')->andReturn('Text from stream');

$stub = new Response($api);

$this->assertSame('Text from stream', $stub->getBody());
}

/** @test */
public function it_can_use_validate_with()
{
$api = m::mock(ResponseInterface::class);

$api->shouldReceive('getStatusCode')->andReturn(200);

$stub = (new Response($api))->validateWith(function ($code, $response) {
if ($code === 404) {
throw new HttpException($response, '404 File not found.');
}
});

$this->assertInstanceOf(Response::class, $stub);
}

/**
* @test
* @expectedException \Laravie\Codex\Exceptions\HttpException
* @expectedExceptionMessage 404 File not found.
*/
public function it_can_use_validate_with_can_throws_exception()
{
$api = m::mock(ResponseInterface::class);

$api->shouldReceive('getStatusCode')->andReturn(404);

(new Response($api))->validateWith(function ($code, $response) {
if ($code === 404) {
throw new HttpException($response, '404 File not found.');
}
});
}

/**
* @test
* @expectedException \BadMethodCallException
Expand Down

0 comments on commit 5e86de7

Please sign in to comment.