Sie sollten Ihre Methode in sich geschlossene wie machen so
use Request;
/**
* returns each section of current url in an array
*
* @return array
*/
public function getUrlPath(Request $request)
{
return explode("/", $request->path());
}
Sie können die Request
als Parameter an der enthaltenden Klasse wie das hinzufügen:
use Request; //it is a facade https://laravel.com/docs/5.3/facades
class MyRequestHandler
{
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function getUrlPath()
{
return explode("/", $this->request->path());
}
}
als Test ist wie folgt aus:
public function testGetUrlPath(){
$expected = ['url','to','path'];
$request = Request::create(implode('/', $expected)); // https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L313
$class = new MyRequestHandler($request);
// Getting results of function so we can test that it has some properties which were supposed to have been set.
$result = $class->getUrlPath();
$this->assertEquals($expected, $result);
}
Es wäre wahrscheinlich besser, wenn Sie an die Hilfsmethode denken, die in sich abgeschlossen ist, w it Abhängigkeiten als Parameter zur Verfügung gestellt. Es wird ermöglichen, dass es leicht testbar ist. 'public function getUrlPathElements ($ path)' würde es erlauben, es direkt ohne Laravel-Routing-Mechanismus zu testen. –
Meinst du die Methode wie in dieser Frage? http://stackoverflow.com/questions/29781103/how-to-test-laravel-5-controllers-methods –