Skip to main content

Comments & Discussion

RA
Richard A. Hoyle ✓ Link copied!

This is a weird one at least for me 7 passed and 1 failed that one was the first one. Any suggestions?

 
php artisan test --filter=CompanyGuideTest
 
FAIL Tests\Feature\CompanyGuideTest
company owner can view his companies guides 0.30s
company owner cannot view other companies guides 0.03s
company owner can create guide to his company 0.05s
company owner cannot create guide to other company 0.03s
company owner can edit guide for his company 0.03s
company owner cannot edit guide for other company 0.03s
company owner can delete guide for his company 0.03s
company owner cannot delete guide for other company 0.03s
──────────────────────────────────────────────────────────────────────────────────────────────────────────
FAILED Tests\Feature\CompanyGuideTest > company owner can view his companies guides
Expected: \n
\n
\n
\n
\n
\n
\n
 
To contain: Mr. Albin Hackett PhD
 
at tests\Feature\CompanyGuideTest.php:24
20
21 $response = $this->actingAs($user)->get(route('companies.guides.index', $company->id));
22
23 $response->assertOk()
24 ->assertSeeText($secondUser->name);
25 }
26
27 public function test_company_owner_cannot_view_other_companies_guides()
28 {
 
 
Tests: 1 failed, 7 passed (15 assertions)
Duration: 0.68s

PS the /n goes on for quite a wile like 20 to 30 lines.

RA
Richard A. Hoyle ✓ Link copied!

could the problem be the fact that I don't have a second user? but would't that be supplied by the UserFactory or the CompanyFactory ?

AA
Ali Al Qahtani ✓ Link copied!

In CompanyGuideController index method

should query the guide role id like this:

$guides = $company->users()->where('role_id', RoleEnum::GUIDE->value)->get();

J
jwinder ✓ Link copied!

Either its me, or shouldnt the last two tests be written like this?

public function test_company_owner_can_delete_guides_from_their_company() { $company = Company::factory()->create();

$user = User::factory()->companyOwner()->create(['company_id' => $company->id]);
$guide = User::factory()->guide()->create(['company_id' => $company->id]);
 
$response = $this->actingAs($user)->delete(route('companies.guides.destroy', [$company->id,$guide->id]));
 
 
$response->assertRedirect(route('companies.guides.index', $company->id));
 
$this->assertSoftDeleted('users', [
'name' => $guide->name,
'email' => $guide->email
]);
}
 
 
public function test_company_owner_cannot_delete_guides_from_another_company()
{
$company = Company::factory()->create();
$companyTwo = Company::factory()->create();
 
$user = User::factory()->companyOwner()->create(['company_id' => $company->id]);
$guide = User::factory()->guide()->create(['company_id' => $companyTwo->id]);
 
$response = $this->actingAs($user)->delete(route('companies.guides.destroy', [$companyTwo->id,$guide->id]));
 
$response->assertForbidden();
}
AA
Ali Al Qahtani ✓ Link copied!

In resources/views/companies/guides/edit.blade.php

Undefined variable $user should be $guide

N
Nerijus ✓ Link copied!

Updated lesson. Thanks.

LA
Luis Antonio Parrado ✓ Link copied!

in resources/views/companies/guides/edit.blade.php: line 12 the action route for the form must be companies.guides.update instead of companies.users.update.

PK
Povilas Korop ✓ Link copied!

Thank you, well noticed, fixed it!

J
joschuba ✓ Link copied!

In the tests of this section: Wouldn't it be more explicit to check, if the company owner can edit a guide and not himself?

Before:

public function test_company_owner_can_edit_guide_for_his_company()
{
$company = Company::factory()->create();
$user = User::factory()->companyOwner()->create(['company_id' => $company->id]);
 
$response = $this->actingAs($user)->put(route('companies.guides.update', [$company->id, $user->id]), [
'name' => 'updated user',
'email' => 'test@update.com',
]);
 
$response->assertRedirect(route('companies.guides.index', $company->id));
 
$this->assertDatabaseHas('users', [
'name' => 'updated user',
'email' => 'test@update.com',
'company_id' => $company->id,
]);
}

After:

public function test_company_owner_can_edit_guide_for_his_company()
{
$company = Company::factory()->create();
$user = User::factory()->companyOwner()->create(['company_id' => $company->id]);
$guide = User::factory()->guide()->create(['company_id' => $company->id]);
 
$response = $this->actingAs($user)->put(route('companies.guides.update', [$company->id, $guide->id]), [
'name' => 'updated user',
'email' => 'test@update.com',
]);
 
$response->assertRedirect(route('companies.guides.index', $company->id));
 
$this->assertDatabaseHas('users', [
'name' => 'updated user',
'email' => 'test@update.com',
'company_id' => $company->id,
]);
}

The same for the other. Plus and an additional point: It should be tested, if the actual data of the user/guide is removed, that was created with the factory. With assertDatabaseMissing() the test would would always pass, because the given data was never in the database. And because auf the introduction of soft deletes a few chapters back, it should also be replaced by assertSoftDeleted() .

Before:

public function test_company_owner_can_delete_guide_for_his_company()
{
$company = Company::factory()->create();
$user = User::factory()->companyOwner()->create(['company_id' => $company->id]);
 
$response = $this->actingAs($user)->delete(route('companies.guides.update', [$company->id, $user->id]));
 
$response->assertRedirect(route('companies.guides.index', $company
D
david ✓ Link copied!

Hi, is there a different in using:

$response = $this->actingAs($user)->get(route('companies.guides.index', $company->id));

or

$response = $this->actingAs($user)->get(route('companies.guides.index', $company));

in both cases the test passes without errors, how laravel interprets them? thanks

N
Nerijus ✓ Link copied!

It is how route model binding wourks and has nothing to do with tests itself. You can read more about it in the laravel

T
teebee ✓ Link copied!

StoreGuideRequest is missing use statement for Illuminate\Validation\Rules. This should be explicit for the users that do not use LSP.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.