Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #14 from shochdoerfer/feature/http-message-util
Browse files Browse the repository at this point in the history
Add fig/http-message-util to fix #12
  • Loading branch information
shochdoerfer authored Feb 11, 2017
2 parents 6e3c6d2 + 9597e5b commit 5fdcb75
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 21 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"psr/http-message": "^1.0",
"willdurand/negotiation": "^2.0",
"twig/twig": "^1.24",
"bitexpert/adroit": "^0.6.0"
"bitexpert/adroit": "^0.6.0",
"fig/http-message-util": "^1.1"
},
"require-dev": {
"phpunit/php-code-coverage": "^4.0.1",
Expand Down
52 changes: 51 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/bitExpert/Adrenaline/Adrenaline.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use bitExpert\Pathfinder\RouteBuilder;
use bitExpert\Pathfinder\Router;
use bitExpert\Pathfinder\RoutingResult;
use Fig\Http\Message\RequestMethodInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response;
Expand Down Expand Up @@ -297,7 +298,7 @@ public function post(string $name, string $path, $target, array $matchers = [])
*/
public function put(string $name, string $path, $target, array $matchers = []) : self
{
$route = $this->createRoute(['PUT'], $name, $path, $target, $matchers);
$route = $this->createRoute([RequestMethodInterface::METHOD_PUT], $name, $path, $target, $matchers);
$this->addRoute($route);
return $this;
}
Expand All @@ -313,7 +314,7 @@ public function put(string $name, string $path, $target, array $matchers = []) :
*/
public function delete(string $name, string $path, $target, array $matchers = []) : self
{
$route = $this->createRoute(['DELETE'], $name, $path, $target, $matchers);
$route = $this->createRoute([RequestMethodInterface::METHOD_DELETE], $name, $path, $target, $matchers);
$this->addRoute($route);
return $this;
}
Expand All @@ -329,7 +330,7 @@ public function delete(string $name, string $path, $target, array $matchers = []
*/
public function options(string $name, string $path, $target, array $matchers = []) : self
{
$route = $this->createRoute(['OPTIONS'], $name, $path, $target, $matchers);
$route = $this->createRoute([RequestMethodInterface::METHOD_OPTIONS], $name, $path, $target, $matchers);
$this->addRoute($route);
return $this;
}
Expand All @@ -345,7 +346,7 @@ public function options(string $name, string $path, $target, array $matchers = [
*/
public function patch(string $name, string $path, $target, array $matchers = []) : self
{
$route = $this->createRoute(['PATCH'], $name, $path, $target, $matchers);
$route = $this->createRoute([RequestMethodInterface::METHOD_PATCH], $name, $path, $target, $matchers);
$this->addRoute($route);
return $this;
}
Expand Down
3 changes: 2 additions & 1 deletion src/bitExpert/Adrenaline/Responder/JsonResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace bitExpert\Adrenaline\Responder;

use bitExpert\Adroit\Domain\Payload;
use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ResponseInterface;

/**
Expand Down Expand Up @@ -53,7 +54,7 @@ public function __invoke(Payload $payload, ResponseInterface $response) : Respon
}

/** @var \bitExpert\Adrenaline\Domain\DomainPayload $payload */
$status = $payload->getStatus() ?: 200;
$status = $payload->getStatus() ?: StatusCodeInterface::STATUS_OK;

return $response->withStatus($status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace bitExpert\Adrenaline\Responder;

use bitExpert\Adroit\Domain\Payload;
use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ResponseInterface;

/**
Expand Down Expand Up @@ -105,7 +106,7 @@ public function __invoke(Payload $payload, ResponseInterface $response) : Respon
}

/** @var \bitExpert\Adrenaline\Domain\DomainPayload $payload */
$status = $payload->getStatus() ?: 200;
$status = $payload->getStatus() ?: StatusCodeInterface::STATUS_OK;
return $response->withStatus($status);
}
}
3 changes: 2 additions & 1 deletion src/bitExpert/Adrenaline/Responder/TwigResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace bitExpert\Adrenaline\Responder;

use bitExpert\Adroit\Domain\Payload;
use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ResponseInterface;

/**
Expand Down Expand Up @@ -95,7 +96,7 @@ public function __invoke(Payload $payload, ResponseInterface $response) : Respon
}

/** @var \bitExpert\Adrenaline\Domain\DomainPayload $payload */
$status = $payload->getStatus() ?: 200;
$status = $payload->getStatus() ?: StatusCodeInterface::STATUS_OK;

return $response->withStatus($status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace bitExpert\Adrenaline\Responder;

use bitExpert\Adrenaline\Domain\DomainPayload;
use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response;

Expand Down Expand Up @@ -41,12 +42,12 @@ protected function setUp()
*/
public function responseCodeIsPassedToResponseObject()
{
$responder = new HttpStatusCodeResponder(200);
$responder = new HttpStatusCodeResponder(StatusCodeInterface::STATUS_OK);
$domainPayload = new DomainPayload('test');
/** @var ResponseInterface $response */
$response = $responder($domainPayload, $this->response);

$this->assertSame(200, $response->getStatusCode());
$this->assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
}

/**
Expand Down
11 changes: 6 additions & 5 deletions tests/bitExpert/Adrenaline/Responder/JsonResponderUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace bitExpert\Adrenaline\Responder;

use bitExpert\Adrenaline\Domain\DomainPayload;
use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response;

Expand Down Expand Up @@ -55,7 +56,7 @@ public function responderConvertsModelToJson()
$response->getBody()->rewind();

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
$this->assertEquals(['application/json'], $response->getHeader('Content-Type'));
$this->assertJson($response->getBody()->getContents());
}
Expand All @@ -72,7 +73,7 @@ public function additionalHttpHeadersGetAppendedToResponse()
$response = $responder($domainPayload, $this->response);

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
$this->assertEquals(['application/json'], $response->getHeader('Content-Type'));
$this->assertTrue($response->hasHeader('X-Sender'));
}
Expand All @@ -96,12 +97,12 @@ public function contentTypeCantBeChanged()
*/
public function respectsStatusCodeSetInPayload()
{
$domainPayload = (new DomainPayload('test'))->withStatus(400);
$domainPayload = (new DomainPayload('test'))->withStatus(StatusCodeInterface::STATUS_BAD_REQUEST);
$responder = $this->responder;
/** @var ResponseInterface $response */
$response = $responder($domainPayload, $this->response);

$this->assertEquals(400, $response->getStatusCode());
$this->assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $response->getStatusCode());
}

/**
Expand All @@ -114,6 +115,6 @@ public function usesOkStatusCodeIfNoneSetInPayload()
/** @var ResponseInterface $response */
$response = $responder($domainPayload, $this->response);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace bitExpert\Adrenaline\Responder;

use bitExpert\Adrenaline\Domain\DomainPayload;
use Fig\Http\Message\StatusCodeInterface;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamWrapper;
Expand Down Expand Up @@ -165,15 +166,15 @@ public function mergesAdditionalHeadersToResponse()
/**
* @test
*/
public function returnsStatus200PerDefault()
public function returnsStatusOkPerDefault()
{
vfsStream::newFile('test.html')->at($this->root);

$payload = (new DomainPayload('test', [StaticHtmlFileResponder::ATTRIBUTE_FILE => 'test']));

/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $this->responder->__invoke($payload, $this->response);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace bitExpert\Adrenaline\Responder;

use bitExpert\Adrenaline\Domain\DomainPayload;
use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response;

Expand Down Expand Up @@ -93,7 +94,7 @@ public function callingBuildResponseWithAPresetTemplateWillReturnResponse()
$response->getBody()->rewind();

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
$this->assertEquals(['text/html'], $response->getHeader('Content-Type'));
$this->assertEquals('<html>', $response->getBody()->getContents());
}
Expand All @@ -113,7 +114,7 @@ public function additionalHttpHeadersGetAppendedToResponse()
$response = $responder($this->domainPayload, $this->response);

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
$this->assertEquals(['text/html'], $response->getHeader('Content-Type'));
$this->assertTrue($response->hasHeader('X-Sender'));
}
Expand Down Expand Up @@ -159,6 +160,6 @@ public function usesOkStatusCodeIfNoneSetInPayload()
/** @var ResponseInterface $response */
$response = $responder($domainPayload, $this->response);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
}
}

0 comments on commit 5fdcb75

Please sign in to comment.