Skip to content

Commit 44126d0

Browse files
authored
Merge pull request #6 from Landerstraeten/add-bad-request-problem
Add Bad Request problem
2 parents 401ebab + 1f3db7b commit 44126d0

5 files changed

Lines changed: 90 additions & 37 deletions

File tree

.travis.yml

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,27 @@
11
language: php
22

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
3+
php:
4+
- 7.2
5+
- 7.3
6+
- 7.4
7+
- nightly
108

119
cache:
1210
directories:
13-
- .composer/cache
11+
- ~/.cache/composer
1412

1513
matrix:
1614
fast_finish: true
1715
allow_failures:
1816
- php: nightly
1917

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
18+
before_install:
19+
- phpenv config-rm xdebug.ini
20+
- alias composer=composer\ --no-interaction && composer selfupdate
21+
- composer global require hirak/prestissimo
22+
23+
install:
24+
- travis_retry composer update --no-progress --profile --no-scripts --no-suggest $DEPENDENCIES
4225

43-
allow_failures:
44-
- php: nightly
26+
script:
27+
- vendor/bin/grumphp run

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ throw new ApiProblemException(
4040
- [NotFoundProblem](#notfoundproblem)
4141
- [UnauthorizedProblem](#unauthorizedproblem)
4242
- [ValidationApiProblem](#validationapiproblem)
43+
- [BadRequestProblem](#badrequestproblem)
4344

4445
#### ExceptionApiProblem
4546

@@ -188,10 +189,27 @@ new ValidationApiProblem(new ConstraintViolationList([
188189
}
189190
````
190191

192+
#### BadRequestProblem
193+
194+
```php
195+
use Phpro\ApiProblem\Http\BadRequestProblem;
196+
197+
new BadRequestProblem('Bad request. Bad!.');
198+
```
199+
200+
```json
201+
{
202+
"status": 400,
203+
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
204+
"title": "Bad Request",
205+
"detail": "Bad request. Bad!"
206+
}
207+
````
208+
191209

192210
### Creating your own problem
193211

194-
Creating problem sounds scarry right!?
212+
Creating problem sounds scary right!?
195213
Since the RFC is very loose, we made the interface as easy as possible:
196214

197215
```php
@@ -238,7 +256,7 @@ class MyProblem implements DebuggableApiProblemInterface
238256
return [
239257
'type' => 'about:blank',
240258
'status' => '99',
241-
'title' => 'Got 99 problems but a glitch aint one!',
259+
'title' => 'Got 99 problems but a glitch ain\'t one!',
242260
];
243261
}
244262

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"type": "library",
55
"require-dev": {
66
"friendsofphp/php-cs-fixer": "^2.12",
7-
"phpro/grumphp": "^0.14.1",
8-
"phpspec/phpspec": "^4.3",
9-
"symfony/validator": "^4.1",
7+
"phpro/grumphp": "^0.17",
8+
"phpspec/phpspec": "^6.0",
9+
"symfony/validator": "^4.4",
1010
"sebastian/comparator": "^1.2.4"
1111
},
1212
"license": "MIT",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace spec\Phpro\ApiProblem\Http;
6+
7+
use Phpro\ApiProblem\Http\BadRequestProblem;
8+
use Phpro\ApiProblem\Http\HttpApiProblem;
9+
use PhpSpec\ObjectBehavior;
10+
11+
class BadRequestProblemSpec extends ObjectBehavior
12+
{
13+
public function let(): void
14+
{
15+
$this->beConstructedWith('a reason');
16+
}
17+
18+
public function it_is_initializable(): void
19+
{
20+
$this->shouldHaveType(BadRequestProblem::class);
21+
}
22+
23+
public function it_is_an_http_api_problem(): void
24+
{
25+
$this->shouldHaveType(HttpApiProblem::class);
26+
}
27+
28+
public function it_can_parse_to_array(): void
29+
{
30+
$this->toArray()->shouldBe([
31+
'status' => 400,
32+
'type' => HttpApiProblem::TYPE_HTTP_RFC,
33+
'title' => HttpApiProblem::getTitleForStatusCode(400),
34+
'detail' => 'a reason',
35+
]);
36+
}
37+
}

src/Http/BadRequestProblem.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phpro\ApiProblem\Http;
6+
7+
class BadRequestProblem extends HttpApiProblem
8+
{
9+
public function __construct(string $detail)
10+
{
11+
parent::__construct(400, [
12+
'detail' => $detail,
13+
]);
14+
}
15+
}

0 commit comments

Comments
 (0)