php - Unit test in Laravel9 is failing
one text
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' => 'cas@example.com',
'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?
Source