Skip to content

Commit

Permalink
Merge branch 'master' of github.com:dingo/api
Browse files Browse the repository at this point in the history
  • Loading branch information
thilanga committed Jan 3, 2018
2 parents cf243cf + 312781d commit c374e9d
Show file tree
Hide file tree
Showing 30 changed files with 122 additions and 79 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"league/fractal": "^0.17"
},
"require-dev": {
"phpdocumentor/reflection-docblock": "3.3.2",
"friendsofphp/php-cs-fixer": "~2",
"illuminate/auth": "^5.1",
"illuminate/cache": "^5.1",
Expand All @@ -30,8 +31,8 @@
"illuminate/log": "^5.1",
"illuminate/pagination": "^5.1",
"laravel/lumen-framework": "^5.1",
"mockery/mockery": "~0.9",
"phpunit/phpunit": "^4.8 || ^5.0",
"mockery/mockery": "~1.0",
"phpunit/phpunit": "^4.8.35 || ^5.4.3 || ^6.5",
"squizlabs/php_codesniffer": "~2.0",
"tymon/jwt-auth": "1.0.*"
},
Expand Down Expand Up @@ -69,4 +70,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
1 change: 0 additions & 1 deletion config/api.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


return [

/*
Expand Down
4 changes: 3 additions & 1 deletion src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,9 @@ protected function dispatch(InternalRequest $request)

if (! $response->isSuccessful() && ! $response->isRedirection()) {
throw new InternalHttpException($response);
} elseif (! $this->raw) {
}

if (! $this->raw) {
$response = $response->getOriginalContent();
}
} catch (HttpExceptionInterface $exception) {
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected function sendRequestThroughRouter(HttpRequest $request)
{
$this->app->instance('request', $request);

return (new Pipeline($this->app))->send($request)->through($this->middleware)->then(function ($request) {
return (new Pipeline($this->app))->send($request)->then(function ($request) {
return $this->router->dispatch($request);
});
}
Expand Down
49 changes: 45 additions & 4 deletions src/Routing/Adapter/Lumen.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ class Lumen implements Adapter
*/
protected $routes = [];

/**
* Array of merged old routes and API routes.
*
* @var array
*/
protected $mergedRoutes = [];

/**
* Routes already defined on the router.
*
* @var \Illuminate\Routing\RouteCollection
*/
protected $oldRoutes;

/**
* Indicates if the middleware has been removed from the application instance.
*
Expand Down Expand Up @@ -92,7 +106,7 @@ public function dispatch(Request $request, $version)

$this->removeMiddlewareFromApp();

$routeCollector = $this->routes[$version];
$routeCollector = $this->mergeOldRoutes($version);
$dispatcher = call_user_func($this->dispatcherResolver, $routeCollector);

$this->app->setDispatcher($dispatcher);
Expand All @@ -102,6 +116,28 @@ public function dispatch(Request $request, $version)
return $this->app->dispatch($request);
}

/**
* Merge the old application routes with the API routes.
*
* @param string $version
*
* @return array
*/
protected function mergeOldRoutes($version)
{
if (! isset($this->oldRoutes)) {
$this->oldRoutes = $this->app->router->getRoutes();
}
if (! isset($this->mergedRoutes[$version])) {
$this->mergedRoutes[$version] = $this->routes[$version];
foreach ($this->oldRoutes as $route) {
$this->mergedRoutes[$version]->addRoute($route['method'], $route['uri'], $route['action']);
}
}

return $this->mergedRoutes[$version];
}

/**
* Normalize the request URI so that Lumen can properly dispatch it.
*
Expand Down Expand Up @@ -225,9 +261,14 @@ protected function removeMiddlewareFromApp()
$reflection = new ReflectionClass($this->app);
$property = $reflection->getProperty('middleware');
$property->setAccessible(true);

$property->setValue($this->app, []);

$oldMiddlewares = $property->getValue($this->app);
$newMiddlewares = [];
foreach ($oldMiddlewares as $middle) {
if ((new ReflectionClass($middle))->hasMethod('terminate') && $middle != 'Dingo\Api\Http\Middleware\Request') {
$newMiddlewares = array_merge($newMiddlewares, [$middle]);
}
}
$property->setValue($this->app, $newMiddlewares);
$property->setAccessible(false);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Auth/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use Dingo\Api\Http\Request;
use Dingo\Api\Routing\Route;
use Dingo\Api\Routing\Router;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Illuminate\Container\Container;
use Dingo\Api\Contract\Auth\Provider;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class AuthTest extends PHPUnit_Framework_TestCase
class AuthTest extends TestCase
{
public function setUp()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Auth/Provider/AuthorizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Mockery as m;
use Dingo\Api\Routing\Route;
use Illuminate\Http\Request;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Dingo\Api\Tests\Stubs\AuthorizationProviderStub;

class AuthorizationTest extends PHPUnit_Framework_TestCase
class AuthorizationTest extends TestCase
{
public function tearDown()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Auth/Provider/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Mockery as m;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Dingo\Api\Auth\Provider\Basic;

class BasicTest extends PHPUnit_Framework_TestCase
class BasicTest extends TestCase
{
protected $auth;
protected $provider;
Expand Down
4 changes: 2 additions & 2 deletions tests/Auth/Provider/JWTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use Mockery as m;
use Illuminate\Http\Request;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Dingo\Api\Auth\Provider\JWT;
use Tymon\JWTAuth\Exceptions\JWTException;

class JWTTest extends PHPUnit_Framework_TestCase
class JWTTest extends TestCase
{
protected $auth;
protected $provider;
Expand Down
8 changes: 4 additions & 4 deletions tests/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Dingo\Api\Dispatcher;
use Illuminate\Http\Request;
use Dingo\Api\Routing\Router;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Dingo\Api\Tests\Stubs\UserStub;
use Illuminate\Container\Container;
use Illuminate\Filesystem\Filesystem;
Expand All @@ -21,7 +21,7 @@
use Dingo\Api\Transformer\Factory as TransformerFactory;
use Illuminate\Support\Facades\Request as RequestFacade;

class DispatcherTest extends PHPUnit_Framework_TestCase
class DispatcherTest extends TestCase
{
protected $container;

Expand Down Expand Up @@ -154,7 +154,7 @@ public function testInternalExceptionContainsResponseObject()
try {
$this->dispatcher->get('test');
} catch (InternalHttpException $exception) {
$this->assertInstanceOf('Illuminate\Http\Response', $exception->getResponse());
$this->assertInstanceOf(\Illuminate\Http\Response::class, $exception->getResponse());
$this->assertSame('test', $exception->getResponse()->getContent());
}
}
Expand Down Expand Up @@ -381,7 +381,7 @@ public function testRedirectResponseThrowsException()
});

$response = $this->dispatcher->get('redirect');
$this->assertInstanceOf('Illuminate\Http\RedirectResponse', $response);
$this->assertInstanceOf(\Illuminate\Http\RedirectResponse::class, $response);
$this->assertSame('redirect-test', $response->getTargetUrl());
}

Expand Down
18 changes: 9 additions & 9 deletions tests/Exception/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
use Mockery as m;
use RuntimeException;
use Illuminate\Http\Response;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Dingo\Api\Exception\Handler;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Dingo\Api\Http\Request as ApiRequest;
use Dingo\Api\Exception\ResourceException;
use Symfony\Component\HttpKernel\Exception\HttpException;

class HandlerTest extends PHPUnit_Framework_TestCase
class HandlerTest extends TestCase
{
protected $parentHandler;
protected $exceptionHandler;
Expand Down Expand Up @@ -67,7 +67,7 @@ public function testExceptionHandlerHandlesExceptionAndCreatesNewResponse()

$response = $this->exceptionHandler->handle($exception);

$this->assertInstanceOf('Illuminate\Http\Response', $response);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame('foo', $response->getContent());
$this->assertSame(404, $response->getStatusCode());
}
Expand All @@ -82,7 +82,7 @@ public function testExceptionHandlerHandlesExceptionWithRedirectResponse()

$response = $this->exceptionHandler->handle($exception);

$this->assertInstanceOf('Illuminate\Http\RedirectResponse', $response);
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertSame('foo', $response->getTargetUrl());
$this->assertSame(302, $response->getStatusCode());
}
Expand All @@ -97,7 +97,7 @@ public function testExceptionHandlerHandlesExceptionWithJsonResponse()

$response = $this->exceptionHandler->handle($exception);

$this->assertInstanceOf('Illuminate\Http\JsonResponse', $response);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertSame('{"foo":"bar"}', $response->getContent());
$this->assertSame(404, $response->getStatusCode());
}
Expand All @@ -108,7 +108,7 @@ public function testExceptionHandlerReturnsGenericWhenNoMatchingHandler()

$response = $this->exceptionHandler->handle($exception);

$this->assertInstanceOf('Illuminate\Http\Response', $response);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame('{"message":"bar","status_code":404}', $response->getContent());
$this->assertSame(404, $response->getStatusCode());
}
Expand All @@ -129,7 +129,7 @@ public function testUsingMultidimensionalArrayForGenericResponse()

$response = $this->exceptionHandler->handle($exception);

$this->assertInstanceOf('Illuminate\Http\Response', $response);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame('{"error":{"message":"bar","status_code":404}}', $response->getContent());
$this->assertSame(404, $response->getStatusCode());
}
Expand All @@ -150,7 +150,7 @@ public function testResourceExceptionErrorsAreIncludedInResponse()

$response = $this->exceptionHandler->handle($exception);

$this->assertInstanceOf('Illuminate\Http\Response', $response);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame('{"message":"bar","errors":{"foo":["bar"]},"code":10,"status_code":422}', $response->getContent());
$this->assertSame(422, $response->getStatusCode());
}
Expand All @@ -174,7 +174,7 @@ public function testHttpExceptionsWithNoMessageUseStatusCodeMessage()

$response = $this->exceptionHandler->handle($exception);

$this->assertInstanceOf('Illuminate\Http\Response', $response);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame('{"message":"404 Not Found","status_code":404}', $response->getContent());
$this->assertSame(404, $response->getStatusCode());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Middleware/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use Dingo\Api\Http\Request;
use Dingo\Api\Routing\Route;
use Dingo\Api\Routing\Router;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Illuminate\Container\Container;
use Dingo\Api\Tests\Stubs\RoutingAdapterStub;
use Illuminate\Routing\Route as IlluminateRoute;
use Dingo\Api\Http\Middleware\Auth as AuthMiddleware;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class AuthTest extends PHPUnit_Framework_TestCase
class AuthTest extends TestCase
{
protected $container;
protected $adapter;
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Middleware/RateLimitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Dingo\Api\Http\Response;
use Dingo\Api\Routing\Route;
use Dingo\Api\Routing\Router;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Illuminate\Cache\CacheManager;
use Dingo\Api\Http\InternalRequest;
use Illuminate\Container\Container;
Expand All @@ -17,7 +17,7 @@
use Dingo\Api\Exception\RateLimitExceededException;
use Symfony\Component\HttpKernel\Exception\HttpException;

class RateLimitTest extends PHPUnit_Framework_TestCase
class RateLimitTest extends TestCase
{
protected $container;
protected $router;
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Middleware/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Dingo\Api\Http\Request;
use Dingo\Api\Routing\Router;
use Dingo\Api\Http\Validation;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Dingo\Api\Exception\Handler;
use Dingo\Api\Http\RequestValidator;
use Dingo\Api\Http\Validation\Accept;
Expand All @@ -19,7 +19,7 @@
use Dingo\Api\Contract\Http\Request as RequestContract;
use Dingo\Api\Http\Middleware\Request as RequestMiddleware;

class RequestTest extends PHPUnit_Framework_TestCase
class RequestTest extends TestCase
{
protected $app;
protected $router;
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Parser/AcceptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Dingo\Api\Tests\Http\Parser;

use Dingo\Api\Http\Request;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Dingo\Api\Http\Parser\Accept;

class AcceptTest extends PHPUnit_Framework_TestCase
class AcceptTest extends TestCase
{
public function testParsingInvalidAcceptReturnsDefaults()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/RateLimit/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Dingo\Api\Tests\Http\RateLimit;

use Dingo\Api\Http\Request;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Illuminate\Cache\CacheManager;
use Illuminate\Container\Container;
use Dingo\Api\Http\RateLimit\Handler;
use Dingo\Api\Tests\Stubs\ThrottleStub;
use Dingo\Api\Http\RateLimit\Throttle\Route;

class HandlerTest extends PHPUnit_Framework_TestCase
class HandlerTest extends TestCase
{
protected $container;
protected $cache;
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/RateLimit/Throttle/AuthenticatedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use Mockery;
use Dingo\Api\Auth\Auth;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use Illuminate\Container\Container;
use Dingo\Api\Http\RateLimit\Throttle\Authenticated;

class AuthenticatedTest extends PHPUnit_Framework_TestCase
class AuthenticatedTest extends TestCase
{
public function testThrottleMatchesCorrectly()
{
Expand Down
Loading

0 comments on commit c374e9d

Please sign in to comment.