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

Add Bad Request problem #6

Merged
merged 5 commits into from
Mar 27, 2020
Merged
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
47 changes: 15 additions & 32 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
language: php

env:
global:
- DEFAULT_COMPOSER_FLAGS="--optimize-autoloader --no-interaction --no-progress"
- COMPOSER_FLAGS=""

before_install:
- alias composer=composer\ --no-interaction && composer selfupdate
php:
- 7.2
- 7.3
- 7.4
- nightly

cache:
directories:
- .composer/cache
- ~/.cache/composer

matrix:
fast_finish: true
allow_failures:
- php: nightly

jobs:
include:
- &STANDARD_TEST_JOB
stage: Test
php: 7.2
install:
- travis_retry composer update $DEFAULT_COMPOSER_FLAGS $COMPOSER_FLAGS
- composer info -D | sort
script:
- vendor/bin/grumphp run
-
<<: *STANDARD_TEST_JOB
stage: Test
php: 7.2
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
-
<<: *STANDARD_TEST_JOB
stage: Test
php: nightly
env: COMPOSER_FLAGS="--ignore-platform-reqs" PHP_CS_FIXER_IGNORE_ENV=1 PHP_CS_FIXER_FUTURE_MODE=1
script:
- vendor/bin/grumphp run
before_install:
- phpenv config-rm xdebug.ini
- alias composer=composer\ --no-interaction && composer selfupdate
- composer global require hirak/prestissimo

install:
- travis_retry composer update --no-progress --profile --no-scripts --no-suggest $DEPENDENCIES

allow_failures:
- php: nightly
script:
- vendor/bin/grumphp run
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ throw new ApiProblemException(
- [NotFoundProblem](#notfoundproblem)
- [UnauthorizedProblem](#unauthorizedproblem)
- [ValidationApiProblem](#validationapiproblem)
- [BadRequestProblem](#badrequestproblem)

#### ExceptionApiProblem

Expand Down Expand Up @@ -188,10 +189,27 @@ new ValidationApiProblem(new ConstraintViolationList([
}
````

#### BadRequestProblem

```php
use Phpro\ApiProblem\Http\BadRequestProblem;

new BadRequestProblem('Bad request. Bad!.');
```

```json
{
"status": 400,
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
"title": "Bad Request",
"detail": "Bad request. Bad!"
}
````


### Creating your own problem

Creating problem sounds scarry right!?
Creating problem sounds scary right!?
Since the RFC is very loose, we made the interface as easy as possible:

```php
Expand Down Expand Up @@ -238,7 +256,7 @@ class MyProblem implements DebuggableApiProblemInterface
return [
'type' => 'about:blank',
'status' => '99',
'title' => 'Got 99 problems but a glitch aint one!',
'title' => 'Got 99 problems but a glitch ain\'t one!',
];
}

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"type": "library",
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.12",
"phpro/grumphp": "^0.14.1",
"phpspec/phpspec": "^4.3",
"symfony/validator": "^4.1",
"phpro/grumphp": "^0.17",
"phpspec/phpspec": "^6.0",
"symfony/validator": "^4.4",
"sebastian/comparator": "^1.2.4"
},
"license": "MIT",
Expand Down
37 changes: 37 additions & 0 deletions spec/Http/BadRequestProblemSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace spec\Phpro\ApiProblem\Http;

use Phpro\ApiProblem\Http\BadRequestProblem;
use Phpro\ApiProblem\Http\HttpApiProblem;
use PhpSpec\ObjectBehavior;

class BadRequestProblemSpec extends ObjectBehavior
{
public function let(): void
{
$this->beConstructedWith('a reason');
}

public function it_is_initializable(): void
{
$this->shouldHaveType(BadRequestProblem::class);
}

public function it_is_an_http_api_problem(): void
{
$this->shouldHaveType(HttpApiProblem::class);
}

public function it_can_parse_to_array(): void
{
$this->toArray()->shouldBe([
'status' => 400,
'type' => HttpApiProblem::TYPE_HTTP_RFC,
'title' => HttpApiProblem::getTitleForStatusCode(400),
'detail' => 'a reason',
]);
}
}
15 changes: 15 additions & 0 deletions src/Http/BadRequestProblem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Phpro\ApiProblem\Http;

class BadRequestProblem extends HttpApiProblem
{
public function __construct(string $detail)
{
parent::__construct(400, [
'detail' => $detail,
]);
}
}