Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbstractRequesterTest: Add a test #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "5.7.*|7.4.*",
"phpunit/phpunit": "^5.7|^7.4",
"byjg/restserver": "3.1.*"
},
"autoload": {
Expand Down
102 changes: 102 additions & 0 deletions tests/AbstractRequesterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Test;

use ByJG\ApiTools\AbstractRequester;
use ByJG\ApiTools\Base\Body;
use ByJG\ApiTools\Base\Schema;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class AbstractRequesterTest extends TestCase
{
/** @var MockObject|AbstractRequester */
private $requester;

/** @var MockObject|Schema */
private $schema;

protected function setUp()
{
parent::setUp();

$this->requester = $this->getMockForAbstractClass(AbstractRequester::class);

$this->schema = $this->getMockBuilder(Schema::class)
->disableOriginalConstructor()
->getMock();
}

public function testDefault()
{
// request body part of the schema
$requestBody = $this->getMockBuilder(Body::class)
->disableOriginalConstructor()
->getMock();
$requestBody->expects($this->once())
->method('match')
->with(null);

// response body part of the schema
$responseBody = $this->getMockBuilder(Body::class)
->disableOriginalConstructor()
->getMock();
$responseBody->expects($this->once())
->method('match')
->with(null);

// set up schema
$this->schema->method('getServerUrl')
->willReturn('https://api.example.com');
$this->schema->method('getBasePath')
->willReturn('/v1');
$this->schema->method('getRequestParameters')
->with(
'/v1/endpoint',
'POST'
)
->willReturn($requestBody);
$this->schema->method('getResponseParameters')
->with(
'/v1/endpoint',
'POST',
200
)
->willReturn($responseBody);

// set up abstract function to validate the request being sent
$this->requester->expects($this->once())
->method('handleRequest')
->with($this->isInstanceOf(Request::class))
/** @var Request $request */
->willReturnCallback(function ($request) {
// validate headers
$headers = $request->getHeaders();
$this->assertEquals($headers['Host'], ['api.example.com']);
$this->assertEquals($headers['Accept'], ['application/json']);
// validate method
$this->assertEquals('POST', $request->getMethod());
// validate URI
$uri = $request->getUri();
$this->assertEquals('https', $uri->getScheme());
$this->assertEquals('', $uri->getUserInfo());
$this->assertEquals('api.example.com', $uri->getHost());
$this->assertEquals('/endpoint', $uri->getPath());
$this->assertEquals('id=42', $uri->getQuery());
$this->assertEquals('', $uri->getFragment());

return new Response(200);
});

$this->requester->withSchema($this->schema);
$this->requester->withMethod('POST');
$this->requester->withPath('/endpoint');
$this->requester->withQuery(['id' => 42]);

$res = $this->requester->send();

$this->assertNull($res);
}
}