Skip to content

Commit

Permalink
[5.5] Add withMiddleware function in MakesHttpRequests (#22060)
Browse files Browse the repository at this point in the history
* Add withMiddleware function inMakesHttpRequests and add tests for with and without methods.

* Update MakesHttpRequests.php

* Update MakesHttpRequests.php

* Fix style.
  • Loading branch information
mathieutu authored and taylorotwell committed Nov 13, 2017
1 parent 7c3443b commit 47a543f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,27 @@ public function handle($request, $next)
return $this;
}

/**
* Enable the given middleware for the test.
*
* @param string|array $middleware
* @return $this
*/
public function withMiddleware($middleware = null)
{
if (is_null($middleware)) {
unset($this->app['middleware.disable']);

return $this;
}

foreach ((array) $middleware as $abstract) {
unset($this->app[$abstract]);
}

return $this;
}

/**
* Automatically follow any redirects returned from the response.
*
Expand Down
55 changes: 55 additions & 0 deletions tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Illuminate\Tests\Foundation\Http\Middleware;

use Orchestra\Testbench\TestCase;

class MakesHttpRequestsTest extends TestCase
{
public function testWithoutAndWithMiddleware()
{
$this->assertFalse($this->app->has('middleware.disable'));

$this->withoutMiddleware();
$this->assertTrue($this->app->has('middleware.disable'));
$this->assertTrue($this->app->make('middleware.disable'));

$this->withMiddleware();
$this->assertFalse($this->app->has('middleware.disable'));
}

public function testWithoutAndWithMiddlewareWithParameter()
{
$next = function ($request) {
return $request;
};

$this->assertFalse($this->app->has(MyMiddleware::class));
$this->assertEquals(
'fooWithMiddleware',
$this->app->make(MyMiddleware::class)->handle('foo', $next)
);

$this->withoutMiddleware(MyMiddleware::class);
$this->assertTrue($this->app->has(MyMiddleware::class));
$this->assertEquals(
'foo',
$this->app->make(MyMiddleware::class)->handle('foo', $next)
);

$this->withMiddleware(MyMiddleware::class);
$this->assertFalse($this->app->has(MyMiddleware::class));
$this->assertEquals(
'fooWithMiddleware',
$this->app->make(MyMiddleware::class)->handle('foo', $next)
);
}
}

class MyMiddleware
{
public function handle($request, $next)
{
return $next($request.'WithMiddleware');
}
}

0 comments on commit 47a543f

Please sign in to comment.