Skip to content

Commit

Permalink
Merge pull request #1719 from dingo/feature/laravel-7
Browse files Browse the repository at this point in the history
Laravel 7 support
  • Loading branch information
specialtactics authored Mar 13, 2020
2 parents 1ff4d3a + 0df41c5 commit b093fae
Show file tree
Hide file tree
Showing 45 changed files with 736 additions and 424 deletions.
7 changes: 2 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ env:
- setup=basic
- xdebug=true

# HHVM is no longer supported on Ubuntu Precise. Please consider using Trusty with `dist: trusty`.
dist: trusty

cache:
directories:
- $HOME/.composer/cache

matrix:
include:
- php: 7.1
env: xdebug=false
- php: 7.2
env: xdebug=false
- php: 7.3
env: xdebug=false
- php: 7.4
env: xdebug=false

before_install:
- if [[ $xdebug = 'true' ]] ; then phpenv config-rm xdebug.ini; fi
Expand Down
30 changes: 15 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@
"email": "[email protected]"
}],
"require": {
"php": "^7.1",
"dingo/blueprint": "^0.3",
"illuminate/routing": "^5.5 || ^6.0",
"illuminate/support": "^5.5 || ^6.0",
"php": "^7.2.5",
"dingo/blueprint": "^0.4",
"illuminate/routing": "^5.5 || ^6.0 || ^7.0",
"illuminate/support": "^5.5 || ^6.0 || ^7.0",
"league/fractal": "^0.17"
},
"require-dev": {
"phpdocumentor/reflection-docblock": "3.3.2",
"friendsofphp/php-cs-fixer": "~2",
"illuminate/auth": "^5.5 || ^6.0",
"illuminate/cache": "^5.5 || ^6.0",
"illuminate/console": "^5.5 || ^6.0",
"illuminate/database": "^5.5 || ^6.0",
"illuminate/events": "^5.5 || ^6.0",
"illuminate/filesystem": "^5.5 || ^6.0",
"illuminate/log": "^5.5 || ^6.0",
"illuminate/pagination": "^5.5 || ^6.0",
"laravel/lumen-framework": "^5.5 || ^6.0",
"illuminate/auth": "^5.5 || ^6.0 || ^7.0",
"illuminate/cache": "^5.5 || ^6.0 || ^7.0",
"illuminate/console": "^5.5 || ^6.0 || ^7.0",
"illuminate/database": "^5.5 || ^6.0 || ^7.0",
"illuminate/events": "^5.5 || ^6.0 || ^7.0",
"illuminate/filesystem": "^5.5 || ^6.0 || ^7.0",
"illuminate/log": "^5.5 || ^6.0 || ^7.0",
"illuminate/pagination": "^5.5 || ^6.0 || ^7.0",
"laravel/lumen-framework": "^5.5 || ^6.0 || ^7.0",
"mockery/mockery": "~1.0",
"phpunit/phpunit": "^4.8.35 || ^5.4.3 || ^6.5",
"phpunit/phpunit": "^4.8.35 || ^5.4.3 || ^6.5 || ^8.5",
"squizlabs/php_codesniffer": "~2.0",
"tymon/jwt-auth": "1.0.*"
},
Expand Down Expand Up @@ -70,4 +70,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
syntaxCheck="true"
verbose="true"
>
<testsuites>
Expand Down
6 changes: 2 additions & 4 deletions src/Contract/Debug/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

namespace Dingo\Api\Contract\Debug;

use Exception;

interface ExceptionHandler
{
/**
* Handle an exception.
*
* @param \Exception $exception
* @param \Throwable|\Exception $exception
*
* @return \Illuminate\Http\Response
*/
public function handle(Exception $exception);
public function handle($exception);
}
68 changes: 36 additions & 32 deletions src/Exception/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

namespace Dingo\Api\Exception;

use Dingo\Api\Http\Request;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Str;
use Illuminate\Http\Response;
use Dingo\Api\Contract\Debug\ExceptionHandler;
use Dingo\Api\Contract\Debug\MessageBagErrors;
use Exception;
use Illuminate\Contracts\Debug\ExceptionHandler as IlluminateExceptionHandler;
use Illuminate\Http\Response;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use ReflectionFunction;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\Response as BaseResponse;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;

class Handler implements ExceptionHandler, IlluminateExceptionHandler
{
Expand Down Expand Up @@ -47,14 +51,14 @@ class Handler implements ExceptionHandler, IlluminateExceptionHandler
/**
* The parent Illuminate exception handler instance.
*
* @var \Illuminate\Contracts\Debug\ExceptionHandler
* @var IlluminateExceptionHandler
*/
protected $parentHandler;

/**
* Create a new exception handler instance.
*
* @param \Illuminate\Contracts\Debug\ExceptionHandler $parentHandler
* @param IlluminateExceptionHandler $parentHandler
* @param array $format
* @param bool $debug
*
Expand All @@ -70,51 +74,51 @@ public function __construct(IlluminateExceptionHandler $parentHandler, array $fo
/**
* Report or log an exception.
*
* @param \Exception $exception
* @param Throwable $exception
*
* @return void
*/
public function report(Exception $exception)
public function report(Throwable $throwable)
{
$this->parentHandler->report($exception);
$this->parentHandler->report($throwable);
}

/**
* Determine if the exception should be reported.
*
* @param \Exception $e
* @param Throwable $e
*
* @return bool
*/
public function shouldReport(Exception $e)
public function shouldReport(Throwable $e)
{
return true;
}

/**
* Render an exception into an HTTP response.
*
* @param \Dingo\Api\Http\Request $request
* @param \Exception $exception
* @param Request $request
* @param Throwable $exception
*
* @throws \Exception
* @throws Exception
*
* @return mixed
*/
public function render($request, Exception $exception)
public function render($request, Throwable $exception)
{
return $this->handle($exception);
}

/**
* Render an exception to the console.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \Exception $exception
* @param OutputInterface $output
* @param Throwable $exception
*
* @return mixed
*/
public function renderForConsole($output, Exception $exception)
public function renderForConsole($output, Throwable $exception)
{
return $this->parentHandler->renderForConsole($output, $exception);
}
Expand All @@ -136,14 +140,14 @@ public function register(callable $callback)
/**
* Handle an exception if it has an existing handler.
*
* @param \Exception $exception
* @param Throwable|Exception $exception
*
* @return \Illuminate\Http\Response
* @return Response
*/
public function handle(Exception $exception)
public function handle($exception)
{
// Convert Eloquent's 500 ModelNotFoundException into a 404 NotFoundHttpException
if ($exception instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
if ($exception instanceof ModelNotFoundException) {
$exception = new NotFoundHttpException($exception->getMessage(), $exception);
}

Expand All @@ -167,13 +171,13 @@ public function handle(Exception $exception)
/**
* Handle a generic error response if there is no handler available.
*
* @param \Exception $exception
* @param Throwable $exception
*
* @throws \Exception
* @throws Throwable
*
* @return \Illuminate\Http\Response
* @return Response
*/
protected function genericResponse(Exception $exception)
protected function genericResponse(Throwable $exception)
{
$replacements = $this->prepareReplacements($exception);

Expand All @@ -193,11 +197,11 @@ protected function genericResponse(Exception $exception)
/**
* Get the status code from the exception.
*
* @param \Exception $exception
* @param Throwable $exception
*
* @return int
*/
protected function getStatusCode(Exception $exception)
protected function getStatusCode(Throwable $exception)
{
if ($exception instanceof ValidationException) {
return $exception->status;
Expand All @@ -209,23 +213,23 @@ protected function getStatusCode(Exception $exception)
/**
* Get the headers from the exception.
*
* @param \Exception $exception
* @param Throwable $exception
*
* @return array
*/
protected function getHeaders(Exception $exception)
protected function getHeaders(Throwable $exception)
{
return $exception instanceof HttpExceptionInterface ? $exception->getHeaders() : [];
}

/**
* Prepare the replacements array by gathering the keys and values.
*
* @param \Exception $exception
* @param Throwable $exception
*
* @return array
*/
protected function prepareReplacements(Exception $exception)
protected function prepareReplacements(Throwable $exception)
{
$statusCode = $this->getStatusCode($exception);

Expand Down Expand Up @@ -322,7 +326,7 @@ protected function newResponseArray()
/**
* Get the exception status code.
*
* @param \Exception $exception
* @param Exception $exception
* @param int $defaultStatusCode
*
* @return int
Expand Down
10 changes: 8 additions & 2 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function __construct($content, $status = 200, $headers = [], Binding $bin
*/
public static function makeFromExisting(IlluminateResponse $old)
{
$new = static::create($old->getOriginalContent(), $old->getStatusCode());
$new = new static($old->getOriginalContent(), $old->getStatusCode());

$new->headers = $old->headers;

Expand All @@ -112,7 +112,7 @@ public static function makeFromJson(JsonResponse $json)
$content = json_decode($json->getContent(), true);
}

$new = static::create($content, $json->getStatusCode());
$new = new static($content, $json->getStatusCode());

$new->headers = $json->headers;

Expand Down Expand Up @@ -196,6 +196,12 @@ public function setContent($content)
// then we most likely have an object that cannot be type cast. In that
// case we'll simply leave the content as null and set the original
// content value and continue.
if (! empty($content) && is_object($content) && ! $this->shouldBeJson($content)) {
$this->original = $content;

return $this;
}

try {
return parent::setContent($content);
} catch (UnexpectedValueException $exception) {
Expand Down
40 changes: 24 additions & 16 deletions tests/Auth/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,45 @@

namespace Dingo\Api\Tests\Auth;

use Mockery as m;
use Dingo\Api\Auth\Auth;
use Dingo\Api\Contract\Auth\Provider;
use Dingo\Api\Http\Request;
use Dingo\Api\Routing\Route;
use Dingo\Api\Routing\Router;
use PHPUnit\Framework\TestCase;
use Dingo\Api\Tests\BaseTestCase;
use Illuminate\Container\Container;
use Dingo\Api\Contract\Auth\Provider;
use Mockery as m;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class AuthTest extends TestCase
class AuthTest extends BaseTestCase
{
public function setUp()
/**
* @var Container
*/
protected $container;
/**
* @var Router|m\LegacyMockInterface|m\MockInterface
*/
protected $router;
/**
* @var Auth
*/
protected $auth;

public function setUp(): void
{
parent::setUp();

$this->container = new Container;
$this->router = m::mock(Router::class);
$this->auth = new Auth($this->router, $this->container, []);
}

public function tearDown()
{
m::close();
}

/**
* @expectedException \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*/
public function testExceptionThrownWhenAuthorizationHeaderNotSet()
{
$this->expectException(UnauthorizedHttpException::class);

$this->router->shouldReceive('getCurrentRoute')->once()->andReturn($route = m::mock(Route::class));
$this->router->shouldReceive('getCurrentRequest')->once()->andReturn($request = Request::create('foo', 'GET'));

Expand All @@ -43,11 +52,10 @@ public function testExceptionThrownWhenAuthorizationHeaderNotSet()
$this->auth->authenticate();
}

/**
* @expectedException \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*/
public function testExceptionThrownWhenProviderFailsToAuthenticate()
{
$this->expectException(UnauthorizedHttpException::class);

$this->router->shouldReceive('getCurrentRoute')->once()->andReturn($route = m::mock(Route::class));
$this->router->shouldReceive('getCurrentRequest')->once()->andReturn($request = Request::create('foo', 'GET'));

Expand Down
Loading

0 comments on commit b093fae

Please sign in to comment.