Skip to content

Commit

Permalink
Merge pull request laravel#16594 from misenhower/make-test-methods-ch…
Browse files Browse the repository at this point in the history
…ainable

[5.3] Make test methods chainable
  • Loading branch information
taylorotwell authored Nov 30, 2016
2 parents 654e2ba + f151541 commit 5bc846f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ trait ImpersonatesUsers
*/
public function actingAs(UserContract $user, $driver = null)
{
$this->be($user, $driver);

return $this;
return $this->be($user, $driver);
}

/**
* Set the currently logged in user for the application.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string|null $driver
* @return void
* @return $this
*/
public function be(UserContract $user, $driver = null)
{
$this->app['auth']->guard($driver)->setUser($user);

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ protected function notSeeInDatabase($table, array $data, $connection = null)
* Seed a given database connection.
*
* @param string $class
* @return void
* @return $this
*/
public function seed($class = 'DatabaseSeeder')
{
$this->artisan('db:seed', ['--class' => $class]);

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ protected function makeRequest($method, $uri, $parameters = [], $cookies = [], $
/**
* Clean the crawler and the subcrawlers values to reset the page context.
*
* @return void
* @return $this
*/
protected function resetPageContext()
{
$this->crawler = null;

$this->subCrawlers = [];

return $this;
}

/**
Expand Down Expand Up @@ -201,7 +203,7 @@ protected function seeRouteIs($route, $parameters = [])
*
* @param string $uri
* @param string|null $message
* @return void
* @return $this
*
* @throws \Illuminate\Foundation\Testing\HttpException
*/
Expand All @@ -219,6 +221,8 @@ protected function assertPageLoaded($uri, $message = null)

throw new HttpException($message, null, $responseException);
}

return $this;
}

/**
Expand Down Expand Up @@ -645,7 +649,7 @@ protected function storeInput($element, $text)
* Assert that a filtered Crawler returns nodes.
*
* @param string $filter
* @return void
* @return $this
*
* @throws \InvalidArgumentException
*/
Expand All @@ -658,6 +662,8 @@ protected function assertFilterProducesResults($filter)
"Nothing matched the filter [{$filter}] CSS query provided for [{$this->currentUri}]."
);
}

return $this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function withSession(array $data)
* Set the session to the given array.
*
* @param array $data
* @return void
* @return $this
*/
public function session(array $data)
{
Expand All @@ -32,38 +32,44 @@ public function session(array $data)
foreach ($data as $key => $value) {
$this->app['session']->put($key, $value);
}

return $this;
}

/**
* Start the session for the application.
*
* @return void
* @return $this
*/
protected function startSession()
{
if (! $this->app['session']->isStarted()) {
$this->app['session']->start();
}

return $this;
}

/**
* Flush all of the current session data.
*
* @return void
* @return $this
*/
public function flushSession()
{
$this->startSession();

$this->app['session']->flush();

return $this;
}

/**
* Assert that the session has a given value.
*
* @param string|array $key
* @param mixed $value
* @return void
* @return $this
*/
public function seeInSession($key, $value = null)
{
Expand All @@ -77,7 +83,7 @@ public function seeInSession($key, $value = null)
*
* @param string|array $key
* @param mixed $value
* @return void
* @return $this
*/
public function assertSessionHas($key, $value = null)
{
Expand All @@ -90,13 +96,15 @@ public function assertSessionHas($key, $value = null)
} else {
PHPUnit::assertEquals($value, $this->app['session.store']->get($key));
}

return $this;
}

/**
* Assert that the session has a given list of values.
*
* @param array $bindings
* @return void
* @return $this
*/
public function assertSessionHasAll(array $bindings)
{
Expand All @@ -107,13 +115,15 @@ public function assertSessionHasAll(array $bindings)
$this->assertSessionHas($key, $value);
}
}

return $this;
}

/**
* Assert that the session does not have a given key.
*
* @param string|array $key
* @return void
* @return $this
*/
public function assertSessionMissing($key)
{
Expand All @@ -124,14 +134,16 @@ public function assertSessionMissing($key)
} else {
PHPUnit::assertFalse($this->app['session.store']->has($key), "Session has unexpected key: $key");
}

return $this;
}

/**
* Assert that the session has errors bound.
*
* @param string|array $bindings
* @param mixed $format
* @return void
* @return $this
*/
public function assertSessionHasErrors($bindings = [], $format = null)
{
Expand All @@ -148,15 +160,19 @@ public function assertSessionHasErrors($bindings = [], $format = null)
PHPUnit::assertContains($value, $errors->get($key, $format));
}
}

return $this;
}

/**
* Assert that the session has old input.
*
* @return void
* @return $this
*/
public function assertHasOldInput()
{
$this->assertSessionHas('_old_input');

return $this;
}
}

0 comments on commit 5bc846f

Please sign in to comment.