Skip to content

Commit

Permalink
Merge pull request #1 from Basster/feature/http-stuff
Browse files Browse the repository at this point in the history
support status code and headers for JsonSerializeResponse as well.
  • Loading branch information
Basster authored Sep 30, 2019
2 parents c43abff + a82858b commit af5995a
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 35 deletions.
1 change: 0 additions & 1 deletion .php_cs.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
'no_alternative_syntax' => true,
'php_unit_set_up_tear_down_visibility' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'no_superfluous_phpdoc_tags' => true,
'return_assignment' => true,
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
'php_unit_test_annotation' => ['style' => 'annotation'],
Expand Down
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ before_script:
- phpenv config-add travis.php.ini
- composer update --prefer-source -o --no-interaction

script: vendor/bin/phpunit -c phpunit.xml.dist
script:
- composer run-script psalm
- vendor/bin/phpunit -c phpunit.xml.dist
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@
"symfony/routing": "^4.3",
"symfony/event-dispatcher": "^4.3",
"symfony/http-kernel": "^4.3",
"friendsofphp/php-cs-fixer": "^2.15"
"friendsofphp/php-cs-fixer": "^2.15",
"vimeo/psalm": "^3.5"
},
"suggest": {
"symfony/event-dispatcher": "Required when using any of the event handlers.",
"symfony/serializer": "Required when using the \\Basster\\LazyResponseBundle\\Response\\JsonSerializeResponse",
"twig/twig": "Required when using the \\Basster\\LazyResponseBundle\\Response\\TwigResponse",
"symfony/routing": "Required when using the \\Basster\\LazyResponseBundle\\Response\\RedirectResponse"
}
},
"scripts":{
"fix": "php-cs-fixer fix --config .php_cs.dist.php",
"psalm": "psalm"
}
}
55 changes: 55 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<LessSpecificReturnType errorLevel="info" />

<!-- level 3 issues - slightly lazy code writing, but provably low false-negatives -->

<DeprecatedMethod errorLevel="info" />
<DeprecatedProperty errorLevel="info" />
<DeprecatedClass errorLevel="info" />
<DeprecatedConstant errorLevel="info" />
<DeprecatedFunction errorLevel="info" />
<DeprecatedInterface errorLevel="info" />
<DeprecatedTrait errorLevel="info" />

<InternalMethod errorLevel="info" />
<InternalProperty errorLevel="info" />
<InternalClass errorLevel="info" />

<MissingClosureReturnType errorLevel="info" />
<MissingReturnType errorLevel="info" />
<MissingPropertyType errorLevel="info" />
<InvalidDocblock errorLevel="info" />
<MisplacedRequiredParam errorLevel="info" />

<PropertyNotSetInConstructor errorLevel="info" />
<MissingConstructor errorLevel="info" />
<MissingClosureParamType errorLevel="info" />
<MissingParamType errorLevel="info" />

<RedundantCondition errorLevel="info" />

<DocblockTypeContradiction errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="info" />

<UnresolvableInclude errorLevel="info" />

<RawObjectIteration errorLevel="info" />

<InvalidStringClass errorLevel="info" />
</issueHandlers>
</psalm>
41 changes: 41 additions & 0 deletions src/Response/AbstractLazyHttpResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);

namespace Basster\LazyResponseBundle\Response;

use Symfony\Component\HttpFoundation\Response;

/**
* Class AbstractHttpResponse.
*/
abstract class AbstractLazyHttpResponse implements LazyResponseInterface
{
/**
* @var int
*/
protected $status;

/**
* @var array
*/
protected $headers;

/**
* AbstractLazyHttpResponse constructor.
*/
public function __construct(int $status = Response::HTTP_OK, array $headers = [])
{
$this->status = $status;
$this->headers = $headers;
}

public function getStatus(): int
{
return $this->status;
}

public function getHeaders(): array
{
return $this->headers;
}
}
5 changes: 5 additions & 0 deletions src/Response/Handler/AbstractLazyResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ abstract protected function isSupported(LazyResponseInterface $controllerResult)

abstract protected function generateResponse(LazyResponseInterface $controllerResult): Response;

/**
* @param mixed $controllerResult
*
* @return bool
*/
private function isSupportedLazyResponse($controllerResult): bool
{
return $controllerResult instanceof LazyResponseInterface && $this->isSupported($controllerResult);
Expand Down
10 changes: 8 additions & 2 deletions src/Response/Handler/JsonSerializeResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ public function __construct(SerializerInterface $serializer)
}

/**
* @param JsonSerializeResponse|LazyResponseInterface $controllerResult
* @param JsonSerializeResponse $controllerResult
*
* @psalm-suppress MoreSpecificImplementedParamType
*/
protected function generateResponse(LazyResponseInterface $controllerResult): Response
{
return new JsonResponse($this->serializer->serialize($controllerResult->getData(), 'json'));
return new JsonResponse(
$this->serializer->serialize($controllerResult->getData(), 'json'),
$controllerResult->getStatus(),
$controllerResult->getHeaders(),
);
}

protected function isSupported(LazyResponseInterface $controllerResult): bool
Expand Down
6 changes: 4 additions & 2 deletions src/Response/Handler/RedirectResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ protected function isSupported(LazyResponseInterface $controllerResult): bool
}

/**
* @param LazyResponseInterface|RedirectResponse $controllerResult
* @param RedirectResponse $controllerResult
*
* @psalm-suppress MoreSpecificImplementedParamType
*/
protected function generateResponse(LazyResponseInterface $controllerResult): Response
{
return new SymfonyRedirectResponse(
$this->router->generate(
$controllerResult->getRouteName(),
$controllerResult->getRouteParams()
),
),
$controllerResult->getStatusCode(),
$controllerResult->getHeaders()
);
Expand Down
4 changes: 3 additions & 1 deletion src/Response/Handler/TwigResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ protected function isSupported(LazyResponseInterface $controllerResult): bool
}

/**
* @param LazyResponseInterface|TemplateResponse $controllerResult
* @param TemplateResponse $controllerResult
*
* @psalm-suppress MoreSpecificImplementedParamType
*
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
Expand Down
17 changes: 15 additions & 2 deletions src/Response/JsonSerializeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,32 @@

namespace Basster\LazyResponseBundle\Response;

use Symfony\Component\HttpFoundation\Response;

/**
* Class JsonSerializeResponse.
*/
final class JsonSerializeResponse implements LazyResponseInterface
final class JsonSerializeResponse extends AbstractLazyHttpResponse
{
/** @var mixed */
private $data;

public function __construct($data)
/**
* JsonSerializeResponse constructor.
*
* @param mixed $data
* @param int $status
* @param array $headers
*/
public function __construct($data, int $status = Response::HTTP_OK, array $headers = [])
{
parent::__construct($status, $headers);
$this->data = $data;
}

/**
* @return mixed
*/
public function getData()
{
return $this->data;
Expand Down
25 changes: 2 additions & 23 deletions src/Response/TemplateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Class TemplateResponse.
*/
final class TemplateResponse implements LazyResponseInterface
final class TemplateResponse extends AbstractLazyHttpResponse
{
/**
* @var string
Expand All @@ -20,32 +20,11 @@ final class TemplateResponse implements LazyResponseInterface
*/
private $data;

/**
* @var int
*/
private $status;

/**
* @var array
*/
private $headers;

public function __construct(string $template, array $data, int $status = Response::HTTP_OK, array $headers = [])
{
parent::__construct($status, $headers);
$this->template = $template;
$this->data = $data;
$this->status = $status;
$this->headers = $headers;
}

public function getStatus(): int
{
return $this->status;
}

public function getHeaders(): array
{
return $this->headers;
}

public function getTemplate(): string
Expand Down
2 changes: 1 addition & 1 deletion tests/Response/Handler/AbstractLazyResponseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function setUp(): void
*/
public function subscribesKernelViewEvent(): void
{
$events = \call_user_func($this->getHandlerClassName() . '::getSubscribedEvents');
$events = $this->getHandlerClassName()::getSubscribedEvents();
self::assertArrayHasKey('kernel.view', $events);
}

Expand Down
28 changes: 28 additions & 0 deletions tests/Response/Handler/JsonSerializeResponseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ public function serializeControllerResultDataWhenSupported(): void
$this->serializer->serialize($data, 'json')->shouldHaveBeenCalled();
}

/**
* @test
*/
public function passStatusCodeToResponse(): void
{
$created = 201;
$controllerResult = new JsonSerializeResponse([], $created);
$event = $this->createViewEvent($controllerResult);

$this->handler->handleLazyResponse($event);

self::assertSame($created, $event->getResponse()->getStatusCode());
}

/**
* @test
*/
public function passHeadersToResponse(): void
{
$headers = ['X-Foo' => 'bar'];
$controllerResult = new JsonSerializeResponse([], 200, $headers);
$event = $this->createViewEvent($controllerResult);

$this->handler->handleLazyResponse($event);

self::assertSame($headers['X-Foo'], $event->getResponse()->headers->get('X-Foo'));
}

protected function getHandlerClassName(): string
{
return JsonSerializeResponseHandler::class;
Expand Down

0 comments on commit af5995a

Please sign in to comment.