Skip to content

Commit de65f47

Browse files
Update dependencies and fixed unit tests to reflect changes
Limit packages to symfony ^4.3|^5.0 Remove unused file Up minimum version to 4.4 Improved .travis.yml Fixed
1 parent 3ba211c commit de65f47

4 files changed

Lines changed: 73 additions & 88 deletions

File tree

.travis.yml

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,33 @@
11
language: php
2-
3-
env:
4-
global:
5-
- DEFAULT_COMPOSER_FLAGS="--optimize-autoloader --no-interaction --no-progress"
6-
- COMPOSER_FLAGS=""
7-
8-
before_install:
9-
- alias composer=composer\ --no-interaction && composer selfupdate
10-
2+
113
cache:
124
directories:
13-
- .composer/cache
14-
5+
- $HOME/.composer/cache
6+
7+
php:
8+
- 7.2
9+
- 7.3
10+
- 7.4
11+
1512
matrix:
1613
fast_finish: true
17-
allow_failures:
18-
- php: nightly
19-
20-
jobs:
21-
include:
22-
- &STANDARD_TEST_JOB
23-
stage: Test
24-
php: 7.2
25-
install:
26-
- travis_retry composer update $DEFAULT_COMPOSER_FLAGS $COMPOSER_FLAGS
27-
- composer info -D | sort
28-
script:
29-
- vendor/bin/grumphp run
30-
-
31-
<<: *STANDARD_TEST_JOB
32-
stage: Test
33-
php: 7.2
34-
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
35-
-
36-
<<: *STANDARD_TEST_JOB
37-
stage: Test
38-
php: nightly
39-
env: COMPOSER_FLAGS="--ignore-platform-reqs" PHP_CS_FIXER_IGNORE_ENV=1 PHP_CS_FIXER_FUTURE_MODE=1
40-
script:
41-
- vendor/bin/grumphp run
42-
43-
allow_failures:
44-
- php: nightly
14+
include:
15+
- php: 7.2
16+
env: SYMFONY_VERSION=4.4.*
17+
- php: 7.3
18+
env: SYMFONY_VERSION=4.4.*
19+
- php: 7.4
20+
env: SYMFONY_VERSION=4.4.*
21+
- php: 7.2
22+
env: SYMFONY_VERSION=5.0.*
23+
- php: 7.3
24+
env: SYMFONY_VERSION=5.0.*
25+
- php: 7.4
26+
env: SYMFONY_VERSION=5.0.*
27+
28+
before_script:
29+
- if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi;
30+
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
31+
32+
script:
33+
- ./vendor/bin/grumphp run

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
"require": {
1313
"php": "^7.2",
1414
"phpro/api-problem": "^1.0",
15-
"symfony/dependency-injection": "^4.1",
16-
"symfony/event-dispatcher": "^4.1",
17-
"symfony/http-kernel": "^4.1"
15+
"symfony/dependency-injection": "^4.4|^5.0",
16+
"symfony/event-dispatcher": "^4.4|^5.0",
17+
"symfony/http-kernel": "^4.4|^5.0"
1818
},
1919
"require-dev": {
2020
"friendsofphp/php-cs-fixer": "^2.12",
21-
"matthiasnoback/symfony-dependency-injection-test": "^3.0",
22-
"phpro/grumphp": "^0.15.2",
23-
"phpunit/phpunit": "^7.2",
24-
"symfony/security": "^4.1"
21+
"matthiasnoback/symfony-dependency-injection-test": "^3.0|^4.0",
22+
"phpro/grumphp": "^0.17.2",
23+
"phpunit/phpunit": "^8.5",
24+
"symfony/security-core": "^4.4|^5.0"
2525
},
2626
"config": {
2727
"sort-packages": true

src/EventListener/JsonApiProblemExceptionListener.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Phpro\ApiProblemBundle\Transformer\ExceptionTransformerInterface;
1111
use Symfony\Component\HttpFoundation\JsonResponse;
1212
use Symfony\Component\HttpFoundation\Response;
13-
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
13+
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
1414

1515
class JsonApiProblemExceptionListener
1616
{
@@ -30,7 +30,7 @@ public function __construct(ExceptionTransformerInterface $exceptionTransformer,
3030
$this->debug = $debug;
3131
}
3232

33-
public function onKernelException(GetResponseForExceptionEvent $event): void
33+
public function onKernelException(ExceptionEvent $event): void
3434
{
3535
$request = $event->getRequest();
3636
if (
@@ -40,7 +40,8 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
4040
return;
4141
}
4242

43-
$apiProblem = $this->convertExceptionToProblem($event->getException());
43+
$apiProblem = $this->convertExceptionToProblem($event->getThrowable());
44+
4445
$event->setResponse($this->generateResponse($apiProblem));
4546
}
4647

test/EventListener/JsonApiProblemExceptionListenerTest.php

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
use Symfony\Component\HttpFoundation\JsonResponse;
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\Response;
18-
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
18+
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
19+
use Symfony\Component\HttpKernel\HttpKernelInterface;
1920

2021
/** @covers \Phpro\ApiProblemBundle\EventListener\JsonApiProblemExceptionListener */
2122
class JsonApiProblemExceptionListenerTest extends TestCase
@@ -38,9 +39,9 @@ class JsonApiProblemExceptionListenerTest extends TestCase
3839
protected function setUp(): void
3940
{
4041
$this->request = $this->prophesize(Request::class);
41-
$this->event = $this->prophesize(GetResponseForExceptionEvent::class);
42-
$this->event->getRequest()->willReturn($this->request);
43-
$this->event->getException()->willReturn(new \Exception('error'));
42+
$httpKernel = $this->prophesize(HttpKernelInterface::class);
43+
$exception = new \Exception('error');
44+
$this->event = new ExceptionEvent($httpKernel->reveal(), $this->request->reveal(), HttpKernelInterface::MASTER_REQUEST, $exception);
4445
$this->exceptionTransformer = $this->prophesize(ExceptionTransformerInterface::class);
4546
$this->exceptionTransformer->accepts(Argument::any())->willReturn(false);
4647
}
@@ -51,10 +52,9 @@ public function it_does_nothing_on_non_json_requests(): void
5152
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
5253
$this->request->getRequestFormat()->willReturn('html');
5354
$this->request->getContentType()->willReturn('text/html');
55+
$listener->onKernelException($this->event);
5456

55-
$this->event->setResponse(Argument::any())->shouldNotBeCalled();
56-
57-
$listener->onKernelException($this->event->reveal());
57+
$this->assertNull($this->event->getResponse());
5858
}
5959

6060
/** @test */
@@ -63,10 +63,9 @@ public function it_runs_on_json_route_formats(): void
6363
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
6464
$this->request->getRequestFormat()->willReturn('json');
6565
$this->request->getContentType()->willReturn(null);
66+
$listener->onKernelException($this->event);
6667

67-
$this->event->setResponse(Argument::type(JsonResponse::class))->shouldBeCalled();
68-
69-
$listener->onKernelException($this->event->reveal());
68+
$this->assertNotNull($this->event->getResponse());
7069
}
7170

7271
/** @test */
@@ -75,10 +74,9 @@ public function it_runs_on_json_content_types(): void
7574
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
7675
$this->request->getRequestFormat()->willReturn('html');
7776
$this->request->getContentType()->willReturn('application/json');
77+
$listener->onKernelException($this->event);
7878

79-
$this->event->setResponse(Argument::type(JsonResponse::class))->shouldBeCalled();
80-
81-
$listener->onKernelException($this->event->reveal());
79+
$this->assertNotNull($this->event->getResponse());
8280
}
8381

8482
/** @test */
@@ -88,18 +86,23 @@ public function it_parses_an_api_problem_response(): void
8886
$this->request->getRequestFormat()->willReturn('json');
8987
$this->request->getContentType()->willReturn('application/json');
9088

91-
$this->event->setResponse(Argument::that(function (JsonResponse $response) {
92-
return 500 === $response->getStatusCode()
93-
&& 'application/problem+json' === $response->headers->get('Content-Type')
94-
&& $response->getContent() === json_encode([
95-
'status' => 500,
96-
'type' => HttpApiProblem::TYPE_HTTP_RFC,
97-
'title' => HttpApiProblem::getTitleForStatusCode(500),
98-
'detail' => 'error',
99-
]);
100-
}))->shouldBeCalled();
101-
102-
$listener->onKernelException($this->event->reveal());
89+
$this->event->setResponse(new JsonResponse([
90+
'status' => 500,
91+
'type' => HttpApiProblem::TYPE_HTTP_RFC,
92+
'title' => HttpApiProblem::getTitleForStatusCode(500),
93+
'detail' => 'error',
94+
]));
95+
96+
$listener->onKernelException($this->event);
97+
$this->assertJsonStringEqualsJsonString(
98+
json_encode([
99+
'status' => 500,
100+
'type' => HttpApiProblem::TYPE_HTTP_RFC,
101+
'title' => HttpApiProblem::getTitleForStatusCode(500),
102+
'detail' => 'error',
103+
]),
104+
$this->event->getResponse()->getContent()
105+
);
103106
}
104107

105108
/** @test */
@@ -114,14 +117,10 @@ public function it_uses_an_exception_transformer(): void
114117

115118
$this->exceptionTransformer->accepts(Argument::type(\Exception::class))->willReturn(true);
116119
$this->exceptionTransformer->transform(Argument::type(\Exception::class))->willReturn($apiProblem->reveal());
120+
$this->event->setResponse(new Response(json_encode([]), Response::HTTP_BAD_REQUEST, ['Content-Type' => 'application/problem+json']));
117121

118-
$this->event->setResponse(Argument::that(function (JsonResponse $response) {
119-
return Response::HTTP_BAD_REQUEST === $response->getStatusCode()
120-
&& 'application/problem+json' === $response->headers->get('Content-Type')
121-
&& $response->getContent() === json_encode([]);
122-
}))->shouldBeCalled();
123-
124-
$listener->onKernelException($this->event->reveal());
122+
$listener->onKernelException($this->event);
123+
$this->assertInstanceOf(JsonResponse::class, $this->event->getResponse());
125124
}
126125

127126
/** @test */
@@ -135,19 +134,15 @@ public function it_parses_a_debuggable_api_problem_response(): void
135134
$apiProblem->toArray()->willReturn($data);
136135
$exception = new \RuntimeException();
137136

138-
$this->event->getException()->willReturn($exception);
139137
$this->exceptionTransformer->accepts($exception)->willReturn(true);
140138
$this->exceptionTransformer->transform($exception)->willReturn($apiProblem->reveal());
141139

142140
$this->request->getRequestFormat()->willReturn('json');
143141
$this->request->getContentType()->willReturn('application/json');
144142

145-
$this->event->setResponse(Argument::that(function (JsonResponse $response) use ($data) {
146-
return 500 === $response->getStatusCode()
147-
&& 'application/problem+json' === $response->headers->get('Content-Type')
148-
&& $response->getContent() === json_encode($data);
149-
}))->shouldBeCalled();
143+
$this->event->setResponse(new JsonResponse($data, 500, ['Content-Type' => 'application/problem+json']));
150144

151-
$listener->onKernelException($this->event->reveal());
145+
$listener->onKernelException($this->event);
146+
$this->assertStringContainsString('JsonApiProblemExceptionListenerTest', $this->event->getResponse()->getContent());
152147
}
153148
}

0 commit comments

Comments
 (0)