Skip to content

Commit

Permalink
Fix: Failed returning Responsable from Middleware
Browse files Browse the repository at this point in the history
 - This is a fix for issue #24156 which states that. When we return Implementation of Responsable from middleware it fails.
  • Loading branch information
TBlindaruk committed May 13, 2018
1 parent 35a8805 commit d36bafd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Illuminate/Pipeline/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use RuntimeException;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract;

class Pipeline implements PipelineContract
Expand Down Expand Up @@ -145,9 +146,13 @@ protected function carry()
$parameters = [$passable, $stack];
}

return method_exists($pipe, $this->method)
$response = method_exists($pipe, $this->method)
? $pipe->{$this->method}(...$parameters)
: $pipe(...$parameters);

return $response instanceof Responsable
? $response->toResponse($this->container->make('Illuminate\Http\Request'))
: $response;
};
};
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Pipeline/PipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Contracts\Support\Responsable;

class PipelineTest extends TestCase
{
Expand Down Expand Up @@ -62,6 +63,23 @@ function ($piped) {
unset($_SERVER['__test.pipe.one']);
}

public function testPipelineUsageWithResponsableObjects()
{
$result = (new Pipeline(new \Illuminate\Container\Container))
->send('foo')
->through([new PipelineTestPipeResponsable])
->then(
function ($piped) {
return $piped;
}
);

$this->assertEquals('bar', $result);
$this->assertEquals('foo', $_SERVER['__test.pipe.responsable']);

unset($_SERVER['__test.pipe.responsable']);
}

public function testPipelineUsageWithCallable()
{
$function = function ($piped, $next) {
Expand Down Expand Up @@ -160,6 +178,14 @@ public function differentMethod($piped, $next)
}
}

class PipeResponsable implements Responsable
{
public function toResponse($request)
{
return 'bar';
}
}

class PipelineTestPipeTwo
{
public function __invoke($piped, $next)
Expand All @@ -170,6 +196,16 @@ public function __invoke($piped, $next)
}
}

class PipelineTestPipeResponsable
{
public function handle($piped, $next)
{
$_SERVER['__test.pipe.responsable'] = $piped;

return new PipeResponsable;
}
}

class PipelineTestParameterPipe
{
public function handle($piped, $next, $parameter1 = null, $parameter2 = null)
Expand Down

0 comments on commit d36bafd

Please sign in to comment.