2015-12-19 16:36:44 +00:00
|
|
|
<?php
|
|
|
|
|
2017-08-05 18:55:02 +00:00
|
|
|
namespace Tests\Unit;
|
2017-02-14 06:53:02 +00:00
|
|
|
|
2015-12-19 17:08:03 +00:00
|
|
|
use App\Services\RESTfulService;
|
2015-12-19 16:36:44 +00:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
2015-12-19 17:08:03 +00:00
|
|
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
2016-05-30 16:19:52 +00:00
|
|
|
use Mockery as m;
|
2017-08-05 18:55:02 +00:00
|
|
|
use Tests\TestCase;
|
2015-12-19 16:36:44 +00:00
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
class RESTfulAPIServiceTest extends TestCase
|
2015-12-19 16:36:44 +00:00
|
|
|
{
|
2017-08-05 16:56:11 +00:00
|
|
|
use WithoutMiddleware;
|
2015-12-19 16:36:44 +00:00
|
|
|
|
2017-08-05 18:55:02 +00:00
|
|
|
protected function tearDown()
|
2015-12-19 16:36:44 +00:00
|
|
|
{
|
2017-08-05 18:55:02 +00:00
|
|
|
m::close();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function a_uri_can_be_constructed()
|
|
|
|
{
|
|
|
|
/** @var Client $client */
|
|
|
|
$client = m::mock(Client::class);
|
|
|
|
$api = new RESTfulService('bar', null, 'http://foo.com', $client);
|
2015-12-19 16:36:44 +00:00
|
|
|
$this->assertEquals('http://foo.com/get/param?key=bar', $api->buildUrl('get/param'));
|
|
|
|
$this->assertEquals('http://foo.com/get/param?baz=moo&key=bar', $api->buildUrl('/get/param?baz=moo'));
|
|
|
|
$this->assertEquals('http://baz.com/?key=bar', $api->buildUrl('http://baz.com/'));
|
|
|
|
}
|
|
|
|
|
2017-08-05 18:55:02 +00:00
|
|
|
/** @test */
|
|
|
|
public function a_request_can_be_made()
|
2015-12-19 16:36:44 +00:00
|
|
|
{
|
2017-08-05 18:55:02 +00:00
|
|
|
/** @var Client $client */
|
2016-05-30 16:19:52 +00:00
|
|
|
$client = m::mock(Client::class, [
|
2015-12-19 16:36:44 +00:00
|
|
|
'get' => new Response(200, [], '{"foo":"bar"}'),
|
|
|
|
'post' => new Response(200, [], '{"foo":"bar"}'),
|
|
|
|
'delete' => new Response(200, [], '{"foo":"bar"}'),
|
|
|
|
'put' => new Response(200, [], '{"foo":"bar"}'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$api = new RESTfulService('foo', null, 'http://foo.com', $client);
|
|
|
|
|
|
|
|
$this->assertObjectHasAttribute('foo', $api->get('/'));
|
|
|
|
$this->assertObjectHasAttribute('foo', $api->post('/'));
|
|
|
|
$this->assertObjectHasAttribute('foo', $api->put('/'));
|
|
|
|
$this->assertObjectHasAttribute('foo', $api->delete('/'));
|
|
|
|
}
|
|
|
|
}
|