Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Allow chaining of response assertions. #17330

Merged
merged 1 commit into from
Jan 13, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function fromBaseResponse($response)
* Assert that the response has the given status code.
*
* @param int $status
* @return void
* @return $this
*/
public function assertStatus($status)
{
Expand All @@ -49,13 +49,15 @@ public function assertStatus($status)
$actual === $status,
"Expected status code {$status} but received {$actual}."
);

return $this;
}

/**
* Assert whether the response is redirecting to a given URI.
*
* @param string $uri
* @return void
* @return $this
*/
public function assertRedirect($uri)
{
Expand All @@ -64,6 +66,8 @@ public function assertRedirect($uri)
);

PHPUnit::assertEquals(app('url')->to($uri), $this->headers->get('Location'));

return $this;
}

/**
Expand Down Expand Up @@ -96,11 +100,13 @@ public function assertHeader($headerName, $value = null)
*
* @param string $cookieName
* @param mixed $value
* @return void
* @return $this
*/
public function assertPlainCookie($cookieName, $value = null)
{
$this->assertCookie($cookieName, $value, false);

return $this;
}

/**
Expand Down Expand Up @@ -154,11 +160,13 @@ protected function getCookie($cookieName)
* Assert that the response is a superset of the given JSON.
*
* @param array $data
* @return void
* @return $this
*/
public function assertHasJson(array $data)
{
PHPUnit::assertArraySubset($data, $this->decodeResponseJson());

return $this;
}

/**
Expand Down Expand Up @@ -186,7 +194,7 @@ protected function decodeResponseJson()
*
* @param string|array $key
* @param mixed $value
* @return void
* @return $this
*/
public function assertViewHas($key, $value = null)
{
Expand All @@ -203,13 +211,15 @@ public function assertViewHas($key, $value = null)
} else {
PHPUnit::assertEquals($value, $this->original->$key);
}

return $this;
}

/**
* Assert that the response view has a given list of bound data.
*
* @param array $bindings
* @return void
* @return $this
*/
public function assertViewHasAll(array $bindings)
{
Expand All @@ -220,39 +230,45 @@ public function assertViewHasAll(array $bindings)
$this->assertViewHas($key, $value);
}
}

return $this;
}

/**
* Assert that the response view is missing a piece of bound data.
*
* @param string $key
* @return void
* @return $this
*/
public function assertViewMissing($key)
{
$this->ensureResponseHasView();

PHPUnit::assertArrayNotHasKey($key, $this->original->getData());

return $this;
}

/**
* Ensure that the response has a view as its original content.
*
* @return void
* @return $this
*/
protected function ensureResponseHasView()
{
if (! isset($this->original) || ! $this->original instanceof View) {
return PHPUnit::fail('The response is not a view.');
}

return $this;
}

/**
* Assert that the session has a given value.
*
* @param string|array $key
* @param mixed $value
* @return void
* @return $this
*/
public function assertSessionHas($key, $value = null)
{
Expand All @@ -268,13 +284,15 @@ public function assertSessionHas($key, $value = null)
} else {
PHPUnit::assertEquals($value, 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 @@ -285,13 +303,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 @@ -305,6 +325,8 @@ public function assertSessionMissing($key)
"Session has unexpected key [{$key}]."
);
}

return $this;
}

/**
Expand Down