Ok so I've been creating unit tests for my routes in my app. I have a test which I can't get to pass.
public function test_client_save_access_if_authorised()
{
$this->seed(TestSeederBase::class);
$user = User::find(1);
$response = $this->actingAs($user, 'web')
->withSession(['banned' => false])
->post('/client/save', [
'csrf_token' => csrf_token(),
'id' => 1,
'title' => 'Test Title',
'contact_1_title' => 'Another Title',
'contact_1_email' => '[email protected]',
'contact_1_phone_number' => '01234567890',
]);
$response->dumpHeaders();
$response->dumpSession();
$response->dump();
$response->assertStatus(200);
$task = JobWorkItem::where('job_number', 1)
->where('user_id', 1)
->first();
$this->assertSame(1, $task->job_number);
}
When this runs it fails with the assertStatus(200) line (I get a 302)
the code ran by this test is:
/**
* @param ClientRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store( ClientRequest $request )
{
$is_enabled = 0;
if (isset($request->is_enabled) && $request->is_enabled === 'on') {
$is_enabled = 1;
}
$client = Client::find($request->id);
$client->is_enabled = $is_enabled;
$client->title = $request->title;
$client->save();
// We need to get all contacts so we can build the request string
$all = ClientContact::where('client_id', $request->id)->get();
foreach ($all as $cc) {
if (isset($request->{'contact_' . $cc->id . '_title'})) {
$title = $request->{'contact_' . $cc->id . '_title'};
$email = $request->{'contact_' . $cc->id . '_email'};
$phone = $request->{'contact_' . $cc->id . '_phone_number'};
if ($title === '-' || $title === '') {
$cc->forceDelete();
} else {
$cc->title = $title;
$cc->email = $email;
$cc->phone_number = $phone;
$cc->save();
}
}
}
return back();
}
The full response after adding the $response dump lines is as follows:
/bin/php -c /etc/php/8.1/apache2/php.ini /hdd/projects/laravel.jobadmin.ecl/htdocs/vendor/phpunit/phpunit/phpunit --bootstrap /hdd/projects/laravel.jobadmin.ecl/htdocs/bootstrap/app.php --configuration /hdd/projects/laravel.jobadmin.ecl/htdocs/phpunit.xml --filter "/(Tests\\Eclipse\\Routes\\WebRoutesTest::test_client_save_access_if_authorised)( .*)?$/" --test-suffix WebRoutesTest.php /hdd/projects/laravel.jobadmin.ecl/htdocs/tests/Eclipse/Routes --teamcity
Testing started at 13:00 ...
PHPUnit 9.5.24 #StandWithUkraine
]8;;file:///hdd/projects/laravel.jobadmin.ecl/htdocs/vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php#L1520\^]8;;\ array:5 [
"cache-control" => array:1 [
0 => "no-cache, private"
]
"date" => array:1 [
0 => "Mon, 12 Sep 2022 12:00:35 GMT"
]
"location" => array:1 [
0 => "https://laravel.jobadmin.ecl"
]
"content-type" => array:1 [
0 => "text/html; charset=UTF-8"
]
"set-cookie" => array:2 [
0 => "XSRF-TOKEN=eyJpdiI6ImYxZGpOc09TQzQzdzFsOUkxdEszakE9PSIsInZhbHVlIjoiTlk3U2FuVXNvK0tvd0orZUJmcnlQcGw5MGlxejRxNnM1dkY0UXJDMXBPNjgzckpQaDg1aldwMFRidnQ3VGtXRm9Db1pHUS9xTnFXQ2xBZ2hVWWhqVDZKSVE2ekFVeVVRSEVPcEJvZFVFZFZ1WURqNFcwMGFjc0xlSDlGV2E5Q1MiLCJtYWMiOiI4Yjg5ZGE4YmVmZTVhOGVlODNjZTEwOWExOTI3Y2Y4ZTlmNjBkM2I5YWQ3ZjBjNWJjYjA3ZTdiYjQ4ZTAyZWVkIiwidGFnIjoiIn0%3D; expires=Mon, 12 Sep 2022 14:00:35 GMT; Max-Age=7200; path=/; domain=laravel.jobadmin.ecl; samesite=lax"
1 => "jobadmindev_session=eyJpdiI6Ik5BWFZvZVJUdFY2WG9qZ2s2OUFUM3c9PSIsInZhbHVlIjoiT0t2aUlJOVFLb2RQcXV4QnluWXVwd0JTTmp3NmVmYnpKOW5VZTAvYkpFd1E5aEpyRGNWM0o4cVJHTEFMbCttUURjUThoMXBTQWpTUndTWVIxYVloYWo5b2RuTTFRclArUk9NMjllTTh0V2NvZEJKWjlMNktaZ2dldmZTMkxQck4iLCJtYWMiOiJhZWZlZmVmOWY4MDVhMzAxMTQxMDNlMDFkZjAxYjkzNWMyMDkzOGYwMTMzZmY0NWQ1MjliZmJhODFiMWJhNDIwIiwidGFnIjoiIn0%3D; expires=Mon, 12 Sep 2022 14:00:35 GMT; Max-Age=7200; path=/; domain=laravel.jobadmin.ecl; httponly; samesite=lax"
]
]
]8;;file:///hdd/projects/laravel.jobadmin.ecl/htdocs/vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php#L1536\^]8;;\ array:4 [
"_token" => "osEZV5Vd06rR9GPEx5XIxyFoN9oCoU7z1JMpvybf"
"banned" => false
"password_hash_web" => "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi"
"_flash" => array:2 [
"old" => []
"new" => []
]
]
]8;;file:///hdd/projects/laravel.jobadmin.ecl/htdocs/vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php#L1507\^]8;;\ """
<!DOCTYPE html>\n
<html>\n
<head>\n
<meta charset="UTF-8" />\n
<meta http-equiv="refresh" content="0;url='https://laravel.jobadmin.ecl'" />\n
\n
<title>Redirecting to https://laravel.jobadmin.ecl</title>\n
</head>\n
<body>\n
Redirecting to <a href="https://laravel.jobadmin.ecl">https://laravel.jobadmin.ecl</a>.\n
</body>\n
</html>
"""
Expected response status code [200] but received 302.
Failed asserting that 200 is identical to 302.
/hdd/projects/laravel.jobadmin.ecl/htdocs/vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:179
/hdd/projects/laravel.jobadmin.ecl/htdocs/tests/Eclipse/Routes/WebRoutesTest.php:417
Time: 00:00.321, Memory: 44.50 MB
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Process finished with exit code 1
Any ideas how to pass this one? The result of the test is it is failing with a status 302 and not the 200 I expected.
An update: The function is running and the data is saved as expected. My function has a redirect back and I think this is my issue? How can I test this to be the case?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Bootstrap is not exclusively a CSS framework, but its most popular features are CSS-centric. These include a powerful grid, icons, buttons, map components, navigation bars, and more.
https://getbootstrap.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.