Skip to main content

Manage Activities and Assign Guides

Premium
25 min read

Comments & Discussion

RA
Richard A. Hoyle ✓ Link copied!

Having a bit of an issue with your use statement and the activity::class it seems to have a mismatch,

 
With the Activity::class being
Activity::class => CompanyActivityPolicy::class,
and the use I thank you want to be the following:
use App\Policies\CompanyActivityPolicy;
not
use App\Policies\ActivityPolicy;
Right?
PK
Povilas Korop ✓ Link copied!

Yes, correct, well spotted! Fixed now.

RA
Richard A. Hoyle ✓ Link copied!

Sorry but I just realized that you never created the Activity Model or at least I can’t find it.

I can see where you added

 
public function price(): Attribute
{
return Attribute::make(
get: fn($value) => $value / 100,
set: fn($value) => $value * 100,
);
}
to it but not where you carated it in the first place.
PK
Povilas Korop ✓ Link copied!

All the database and models have been created in the first lesson.

D
david ✓ Link copied!

The existing price values are divided by 100 in front-end as you mention after adding accesor/mutator. Creating new activities, the price value is multiplied by 100 and saved in the DB, but in front-end the value is the same as the one entered. what is the goal for the new values??

RA
Richard A. Hoyle ✓ Link copied!

Question since I am using the Uuid for the user and the user is being referenced in the guide_id and again in the StoreActivityRequest.php file do I need to replace it with the foreignUuid ?

PK
Povilas Korop ✓ Link copied!

Yes, most likely.

KD
Kwesi Danquah ✓ Link copied!

For some reason the below test is throwing an error and i cant seem to figure out why.

public function test_can_upload_image() { Storage::fake('activities');

$company = Company::factory()->create();
$user = User::factory()->companyOwner()->create(['company_id' => $company->id]);
$guide = User::factory()->guide()->create();
 
$file = UploadedFile::fake()->image('avatar.jpg');
 
$this->actingAs($user)->post(route('companies.activities.store', $company), [
'name' => 'activity',
'description' => 'description',
'start_time' => '2023-09-01 10:00',
'price' => 9999,
'guide_id' => $guide->id,
'image' => $file,
]);
 
Storage::disk('activities')->assertExists($file->hashName());
Storage::disk('activities')->assertExists('thumbs/'.$file->hashName());
}

I get the follwing error

FAILED Tests\Feature\CompanyActivityTest > can upload image
Unable to find a file or directory at path [o6Kjk8xnngz9MFO2D5E3DwJcneAEp3xpPXy23HPA.jpg]. Failed asserting that false is true.

at vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php:119 115▕ 116▕ $paths = Arr::wrap($path); 117▕ 118▕ foreach ($paths as $path) { ➜ 119▕ PHPUnit::assertTrue( 120▕ $this->exists($path), "Unable to find a file or directory at path [{$path}]." 121▕ ); 122▕ 123▕ if (! is_null($content)) {

+1 vendor frames

2 tests/Feature/CompanyActivityTest.php:86

V
Vitalii ✓ Link copied!

With Storage::fake('activities') we would fake a so-called disk named 'activities'. In our filesystems.php there's no disk declared as 'activities'.

In our CompanyActivityController we're saving to the subdirectory 'activities' on 'public' disk, so for making our test work, we should fake this disk:

Storage::fake('public');

Also use this disk then for the assertions:

Storage::disk('public')->assertExists('/activities/'.$file->hashName());
T
teebee ✓ Link copied!

Hi all,

Awesome tutorial, I'm learning a bunch of new stuff, however I can get pass testing phase in this step. From 14 tests ony 4 are passing, and I see 2 recurring issues:

Call to undefined relationship [activities] on model [App\Models\Company].

and

FAILED Tests\Feature\CompanyActivityTest > admin can edit activity for company ────────────────────────────────────────────────
Error Error
Class "Database\Factories\Factory" not found

at database\factories\ActivityFactory.php:9 5▕ use App\Models\Company; 6▕ use Illuminate\Support\Carbon; 7▕ 8▕ ➜ 9▕ class ActivityFactory extends Factory 10▕ { 11▕ public function definition(): array 12▕ { 13▕ return [

1 database\factories\ActivityFactory.php:9 2 vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php:829

Have I missed some steps (I have been doing this for the last few hours, and it is a posibility, although, I went with copy/past last time to ensure no typing error sneaks in) ?

N
Nerijus ✓ Link copied!

For the first error you are missing the activities() relationship on the Company model. For the second, maybe you are missing HasFactory trait on the model.

T
teebee ✓ Link copied!

I understood that, but I'm unable to find the step in this tutorial where it sais I should do that.

T
teebee ✓ Link copied!

The second issue us due to not having an import declared on ActivityFactory.php because, again is not mentioned anywhere in here that is should be.

use Illuminate\Database\Eloquent\Factories\Factory;

I'm down to my last issue, and this one I have no clue on how to solve it:

FAILED Tests\Feature\CompanyActivityTest > guides are shown only for specific company in edit form AssertionFailedError
The response is not a view.

at tests\Feature\CompanyActivityTest.php:147 143▕ $guide2 = User::factory()->guide()->create(['company_id' => $company2->id]); 144▕ 145▕ $response = $this->actingAs($user)->get(route('companies.activities.edit', [$company, $activity])); 146▕ ➜ 147▕ $response->assertViewHas('guides', function (Collection $guides) use ($guide) { 148▕ return $guide->name === $guides[$guide->id]; 149▕ }); 150▕ 151▕ $response->assertViewHas('guides', function (Collection $guides) use ($guide2) {

Tests: 1 failed, 13 passed (29 assertions) Duration: 3.92s

Any suggestion on how I can overcome this obstacle?

Much appreciated!

N
Nerijus ✓ Link copied!

When you create a model using an artisan command the trait is always added. Also, when using factories you must know about this trait.

T
teebee ✓ Link copied!

I did create the model using artisan. I was not aware that completing this cours has a pre-requisite on knowing stuff (that I don't know) by heart especially as I did not worked with factorie untill now.

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.