Show the admin/suppliers/create method. Maybe you are failing validation ?
Laravel 9 testing, im getting tabel is empty
Hi all i have created some test and im not testing that the database has data in a feature test so im just passing static data see assertDatabaseHas "some data in it" but i keep on getting table is empty, what im I doing wrong?
public function test_if_user_can_create_a_supplier()
{
$supplier = [
'name' => 'Acme Inc.',
'trading_as' => 'Acme',
'vat_number' => '123456789',
'email' => '[email protected]',
'currency' => 'USD',
'account_number' => '987654321',
'phone' => '+1 (123) 456-7890',
'fax' => '+1 (123) 456-7891',
'mobile' => '+1 (123) 456-7892',
'website' => 'https://www.acme.com',
];
$adminUser = AdminUser::find(1);
$response = $this->actingAs($adminUser)->post(
'admin/suppliers/create',
$supplier
);
$response->assertStatus(302);
$this->assertDatabaseHas(
'suppliers',
$supplier
);
}
hi @sinnbeck here is the method
/**
* Show the form for creating a new resource.
*
* @throws AuthorizationException
*
* @return Factory|View
*/
public function create()
{
$this->authorize('admin.supplier.create');
return view('admin.supplier.create');
}
@Dirk313 That is a the page for creating the data. I assume you meant to post to the store() method ? Wrong url ?
$response = $this->actingAs($adminUser)->post(
'admin/suppliers',
$supplier
);
The strange things is even if i post to the store route i still get The table is empty.
if i do this
$response = $this->actingAs($adminUser)->post(
'admin/suppliers',
$supplier
);
or
$response = $this->actingAs($adminUser)->post(
'admin/suppliers/store',
$supplier
);
it does not get the data
@sinnbeck her is all my routes
Route::middleware(['auth:' . config('admin-auth.defaults.guard'), 'admin'])->group(static function () {
Route::prefix('admin')->namespace('App\Http\Controllers\Admin')->name('admin/')->group(static function () {
Route::prefix('suppliers')->name('suppliers/')->group(static function () {
Route::get('/', 'SuppliersController@index')->name('index');
Route::get('/create', 'SuppliersController@create')->name('create');
Route::post('/', 'SuppliersController@store')->name('store');
Route::get('/{supplier}/edit', 'SuppliersController@edit')->name('edit');
Route::post('/bulk-destroy', 'SuppliersController@bulkDestroy')->name('bulk-destroy');
Route::post('/{supplier}', 'SuppliersController@update')->name('update');
Route::delete('/{supplier}', 'SuppliersController@destroy')->name('destroy');
});
});
});
@Dirk313 Then you are using the wrong route in your test. As I said earlier its
$response = $this->actingAs($adminUser)->post(
'admin/suppliers', //no /store
$supplier
);
@Sinnbeck Yes sorry i made a typo i have fixed my post ment to add
when i change it to this i still get empty table
$response = $this->actingAs($adminUser)->post(
'admin/suppliers',
$supplier
);
Sorry im trying to fit the routes nicely but it doesn't fit well
@Dirk313 Its fine, I can read them :)
@sinnbeck When i do the change i still get empty table
$response = $this->actingAs($adminUser)->post(
'admin/suppliers',
$supplier
);
@Dirk313 And
Could you please show the correct method?
@Sinnbeck O goodness sorry meant to send it,
/**
* Store a newly created resource in storage.
*
* @param StoreSupplier $request
*
* @return array|RedirectResponse|Redirector
*/
public function store(StoreSupplier $request)
{
// Sanitize input
$sanitized = $request->getSanitized();
// Store the Supplier
$supplier = Supplier::create($sanitized);
if ($request->ajax()) {
if ($sanitized['save']) {
return [
'redirect' => url('admin/suppliers/' . $supplier->id . '/edit'),
'message' => trans('brackets/admin-ui::admin.operation.succeeded'),
];
} else {
return [
'redirect' => url('admin/suppliers'),
'message' => trans('brackets/admin-ui::admin.operation.succeeded'),
];
}
}
return redirect('admin/suppliers');
}
@Dirk313 No worries :) We are getting there. Can you also show StoreSupplier
@Sinnbeck cool Here is my store request : )
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
'name' => ['required', 'string'],
'trading_as' => ['nullable', 'string'],
'vat_number' => ['nullable', 'string'],
'email' => ['required', 'string'],
'currency' => ['required', 'string'],
'account_number' => ['nullable', 'string'],
'phone' => ['required', 'string'],
'fax' => ['nullable', 'string'],
'mobile' => ['nullable', 'string'],
'website' => ['nullable', 'string', 'required','regex:/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i'],
'save' => 'sometimes|boolean',
];
}
I need to work on my indentations hehe
@Dirk313 Its ok :) I can read it just fine
I havent spotted the issue yet. So lets check other things
Can you try adding this to the test?
$response->assertSessionHasNoErrors(); //add this
$response->assertStatus(302);
@sinnbeck Sorry unfortionatly the same "empty table"
$response = $this->actingAs($adminUser)->post(
'admin/suppliers',
$supplier
);
$response->assertSessionHasNoErrors();
$response->assertStatus(302);
$this->assertDatabaseHas(
'suppliers',
$supplier
);
@Dirk313 So if you remove this it is successful ?
/*
$this->assertDatabaseHas(
'suppliers',
$supplier
);
*/
If it works like that, we need to see how far the request gets. Maybe its a permission issue (middleware)
Try this and see if it triggers
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
dd('RULES ARE HIT'); //add this
return [
'name' => ['required', 'string'],
'trading_as' => ['nullable', 'string'],
'vat_number' => ['nullable', 'string'],
'email' => ['required', 'string'],
'currency' => ['required', 'string'],
'account_number' => ['nullable', 'string'],
'phone' => ['required', 'string'],
'fax' => ['nullable', 'string'],
'mobile' => ['nullable', 'string'],
'website' => ['nullable', 'string', 'required','regex:/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i'],
'save' => 'sometimes|boolean',
];
}
You can also try adding this
public function test_if_user_can_create_a_supplier()
{
$supplier = [
'name' => 'Acme Inc.',
'trading_as' => 'Acme',
'vat_number' => '123456789',
'email' => '[email protected]',
'currency' => 'USD',
'account_number' => '987654321',
'phone' => '+1 (123) 456-7890',
'fax' => '+1 (123) 456-7891',
'mobile' => '+1 (123) 456-7892',
'website' => 'https://www.acme.com',
];
$this->withoutExceptionHandling(); //this
$adminUser = AdminUser::find(1);
$response = $this->actingAs($adminUser)->post(
'admin/suppliers/create',
$supplier
);
$response->assertStatus(302);
$this->assertDatabaseHas(
'suppliers',
$supplier
);
}
Another possibility is that you dont have an admin user
$adminUser = AdminUser::find(1);
dd($adminUser); //is this null ?
@sinnbeck if i use it like this it works, but the duplication is not allowed to be used and i need to simplify it
public function test_if_user_can_create_a_supplier()
{
$supplier = Supplier::create(
$supplier = [
'name' => 'Acme Inc.',
'trading_as' => 'Acme',
'vat_number' => '123456789',
'email' => '[email protected]',
'currency' => 'USD',
'account_number' => '987654321',
'phone' => '+1 (123) 456-7890',
'fax' => '+1 (123) 456-7891',
'mobile' => '+1 (123) 456-7892',
'website' => 'https://www.acme.com',
]
);
$adminUser = AdminUser::find(1);
$response = $this->actingAs($adminUser)->post(
'admin/suppliers'
);
$response->assertStatus(302);
$this->assertDatabaseHas(
'suppliers',
[
'name' => 'Acme Inc.',
'trading_as' => 'Acme',
'vat_number' => '123456789',
'email' => '[email protected]',
'currency' => 'USD',
'account_number' => '987654321',
'phone' => '+1 (123) 456-7890',
'fax' => '+1 (123) 456-7891',
'mobile' => '+1 (123) 456-7892',
'website' => 'https://www.acme.com',
]
);
}
@Dirk313 Yeah that does not test anything.. :)
@Sinnbeck hehehe but it works LOL, this is so frustrating haha
@Dirk313 Yes. You are creating data and then testing if the data you just created exists. So you might as well just skip the whole request part as you arent really testing it
But I have given you a few ways to debug it. Let me know what you find out
@sinnbeck you are right, it doesn't help me trying it that way, as you said it doesn't really test anything, I will see what i can do and will the post the results, Thank you so much for your help Sir it means allot
@sinnbeck I managed to get the create test and it passed : )
public function test_if_user_can_create_a_supplier()
{
//Make fake data, don't persist to database.
$supplier = Supplier::factory()->make()->setAppends([])->makeHidden(['created_at', 'updated_at']);
//Create an Admin User and assign the Administrator role to this new user
$adminUser = factory(AdminUser::class)->create();
$adminUser->roles()->sync(Role::where('name', 'Administrator')->first());
//Make the request as the new authenticated Administrator user with the admin guard.
$this->actingAs($adminUser, config('admin-auth.defaults.guard'))
->json(
'POST',
route('admin/suppliers/store'),
$supplier->toArray()
)
->assertStatus(302)
->assertRedirectToRoute('admin/suppliers/index');
$this->assertDatabaseHas(
'suppliers',
$supplier->toArray()
);
}
✓ if user can create a supplier
@sinnbeck but now im sitting with this issue to test if n supplier can be deleted when a run the following code i get Missing required parameter for [Route: admin/suppliers/destroy] [URI: admin/suppliers/{supplier}] [Missing parameter: supplier].
public function test_if_a_user_can_delete_a_supplier()
{
//Make fake data, don't persist to database.
$supplier = Supplier::factory()->make()->setAppends([])->makeHidden(['created_at', 'updated_at']);
//Create an Admin User and assign the Administrator role to this new user
$adminUser = factory(AdminUser::class)->create();
$adminUser->roles()->sync(Role::where('name', 'Administrator')->first());
$this->actingAs($adminUser, config('admin-auth.defaults.guard'))
->json(
'DELETE',
route('admin/suppliers/destroy'),
$supplier->toArray()
)
->assertStatus(302)
->assertRedirectToRoute('admin/suppliers/index');
$this->assertDatabaseMissing(
'suppliers',
$supplier->toArray()
);
}
Moved my answer to your new question. Maybe close out this thread with Sinnbeck too ;)
@webrobert Hi i have also tried that but got the same error
'DELETE',
route('admin/suppliers/destroy', $supplier),
$supplier->toArray()
Please or to participate in this conversation.