Created
October 5, 2017 06:03
-
-
Save deividaspetraitis/e24d5151bce9102df78c3fc2adda01bd to your computer and use it in GitHub Desktop.
Custom Request object is overridden by default Illuminate\Http\Request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// .... | |
$app = new App\Application( | |
realpath(__DIR__.'/../') | |
); | |
// .... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App; | |
class Application extends \Laravel\Lumen\Application | |
{ | |
use Concerns\RoutesRequests; // override with our new trait | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
// ... | |
"autoload": { | |
"psr-4": { | |
"App\\": "app/", | |
"Modules\\": "Modules/", | |
"Tests\\": "tests/" // <---- add this | |
}, | |
"files": [ | |
"app/helpers.php" | |
] | |
} | |
// ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Tests\Concerns; | |
use Symfony\Component\HttpFoundation\Request as SymfonyRequest; | |
trait MakesHttpRequests | |
{ | |
use \Laravel\Lumen\Testing\Concerns\MakesHttpRequests; | |
/** | |
* Call the given URI and return the Response. | |
* | |
* @param string $method | |
* @param string $uri | |
* @param array $parameters | |
* @param array $cookies | |
* @param array $files | |
* @param array $server | |
* @param string $content | |
* @return \Illuminate\Http\Response | |
*/ | |
public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null) | |
{ | |
$this->currentUri = $this->prepareUrlForRequest($uri); | |
$symfonyRequest = SymfonyRequest::create( | |
$this->currentUri, $method, $parameters, | |
$cookies, $files, $server, $content | |
); | |
return $this->response =app()->prepareResponse( | |
app()->handle(app('request')::createFromBase($symfonyRequest)) | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Concerns; | |
use \Illuminate\Http\Request; | |
use Symfony\Component\HttpFoundation\Request as SymfonyRequest; | |
trait RoutesRequests | |
{ | |
use \Laravel\Lumen\Concerns\RoutesRequests; | |
/** | |
* Parse the incoming request and return the method and path info. | |
* | |
* @param \Symfony\Component\HttpFoundation\Request|null $request | |
* @return array | |
*/ | |
protected function parseIncomingRequest($request) | |
{ | |
if (! $request) { | |
$request = Request::capture(); | |
} | |
$this->instance(get_class($request), $this->prepareRequest($request)); | |
return [$request->getMethod(), '/'.trim($request->getPathInfo(), '/')]; | |
} | |
/** | |
* Prepare the given request instance for use with the application. | |
* | |
* @param \Symfony\Component\HttpFoundation\Request $request | |
* @return \Illuminate\Http\Request | |
*/ | |
public function prepareRequest(SymfonyRequest $request) | |
{ | |
if (! $request instanceof Request) { | |
$request = Request::createFromBase($request); | |
} | |
$request->setUserResolver(function ($guard = null) { | |
return $this->make('auth')->guard($guard)->user(); | |
})->setRouteResolver(function () { | |
return $this->currentRoute; | |
}); | |
return $request; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Tests\Concerns\MakesHttpRequests; | |
use Laravel\Lumen\Testing\DatabaseMigrations; | |
use Laravel\Lumen\Testing\DatabaseTransactions; | |
abstract class TestCase extends Laravel\Lumen\Testing\TestCase | |
{ | |
use DatabaseMigrations, // creates tables, etc. | |
MakesHttpRequests; // override with our new trait | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment