Skip to content

Commit a8d4780

Browse files
authored
Merge pull request #3 from AllanKoder/resource-reviews-tests
Finished feature tests for resource reviews and improved factory for resources
2 parents 4845b95 + 567ded8 commit a8d4780

6 files changed

Lines changed: 163 additions & 15 deletions

File tree

database/factories/CommentFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function definition(): array
7070

7171
public function configure()
7272
{
73+
// TODO: Consider making the increment part of the dispatch event?
7374
return $this->afterCreating(function (Comment $comment) {
7475
if ($comment->root_comment_id) {
7576
Comment::where('id', $comment->root_comment_id)

database/factories/ComputerScienceResourceFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function definition(): array
2828
return [
2929
'name' => fake()->name(),
3030
'description' => fake()->realText(),
31-
'user_id' => User::all()->random()->id,
31+
'user_id' => User::inRandomOrder()->first() ?? User::factory()->create(),
3232
'image_url' => 'https://cdn.iconscout.com/icon/free/png-256/free-leetcode-logo-icon-download-in-svg-png-gif-file-formats--technology-social-media-company-vol-1-pack-logos-icons-3030025.png',
3333
'page_url' => fake()->url(),
3434

tests/Feature/CommentsTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ public function test_cannot_comment_on_non_existent_resource()
8484
/**
8585
* Test that commenting works on all commentable types defined in config.
8686
*/
87-
// TODO:
8887
public function test_can_comment_all_commentable_types()
8988
{
9089
$user = User::factory()->create();

tests/Feature/ResourceReviewsTest.php

Lines changed: 117 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,134 @@
22

33
namespace Tests\Feature;
44

5+
use App\Models\ComputerScienceResource;
6+
use App\Models\User;
57
use Illuminate\Foundation\Testing\RefreshDatabase;
6-
use Illuminate\Foundation\Testing\WithFaker;
78
use Tests\TestCase;
9+
use Tests\TestResources\ResourceReviewTestResource;
810

911
class ResourceReviewsTest extends TestCase
1012
{
11-
/**
12-
* A basic feature test example.
13-
*/
13+
use RefreshDatabase;
14+
1415
public function test_example(): void
1516
{
1617
$response = $this->get('/');
17-
1818
$response->assertStatus(200);
1919
}
2020

21-
// Test this:
22-
// Cannot post any form of invalid data
23-
// Do 3 tests of invalid data testing
24-
21+
public function test_invalid_data_missing_required_fields(): void
22+
{
23+
$user = User::factory()->create();
24+
$resource = ComputerScienceResource::factory()->create();
25+
26+
$invalidData = ResourceReviewTestResource::fake();
27+
unset($invalidData['title'], $invalidData['description']);
28+
29+
$response = $this->actingAs($user)
30+
->post(route('reviews.store', $resource), $invalidData);
31+
32+
$response->assertSessionHasErrors(['title', 'description']);
33+
}
34+
35+
public function test_invalid_data_invalid_rating(): void
36+
{
37+
$user = User::factory()->create();
38+
$resource = ComputerScienceResource::factory()->create();
39+
40+
$invalidData = ResourceReviewTestResource::fake();
41+
$invalidData['community'] = 0;
42+
43+
$response = $this->actingAs($user)
44+
->post(route('reviews.store', $resource), $invalidData);
45+
46+
$response->assertSessionHasErrors(['community']);
47+
}
48+
49+
public function test_invalid_data_invalid_pros_field(): void
50+
{
51+
$user = User::factory()->create();
52+
$resource = ComputerScienceResource::factory()->create();
53+
54+
$invalidData = ResourceReviewTestResource::fake();
55+
$invalidData['pros'] = 'Not an array';
56+
57+
$response = $this->actingAs($user)
58+
->post(route('reviews.store', $resource), $invalidData);
59+
60+
$response->assertSessionHasErrors(['pros']);
61+
}
62+
63+
public function test_resource_review_can_be_posted(): void
64+
{
65+
$user = User::factory()->create();
66+
$resource = ComputerScienceResource::factory()->create();
67+
68+
$data = ResourceReviewTestResource::fake();
69+
70+
$response = $this->actingAs($user)
71+
->post(route('reviews.store', $resource), $data);
72+
73+
$response->assertRedirect(route('resources.show', ['computerScienceResource' => $resource->id]))
74+
->assertSessionHas('success', 'Review created successfully!');
75+
76+
$this->assertDatabaseHas('resource_reviews', [
77+
'computer_science_resource_id' => $resource->id,
78+
'title' => $data['title']
79+
]);
80+
}
81+
82+
public function test_resource_average_has_changed(): void
83+
{
84+
$user = User::factory()->create();
85+
$resource = ComputerScienceResource::factory()->create();
86+
87+
$data = ResourceReviewTestResource::fake();
2588

26-
// test that the resource's average has changed
89+
$this->actingAs($user)
90+
->post(route('reviews.store', $resource), $data);
2791

28-
// Test that the resource review can be posted
29-
//
92+
$this->assertDatabaseHas('resource_review_summaries', [
93+
'computer_science_resource_id' => $resource->id,
94+
'community' => $data['community'],
95+
'teaching_clarity' => $data['teaching_clarity'],
96+
'engagement' => $data['engagement'],
97+
'practicality' => $data['practicality'],
98+
'user_friendliness' => $data['user_friendliness'],
99+
'updates' => $data['updates'],
100+
'review_count' => 1,
101+
]);
102+
}
103+
104+
public function test_resource_review_average_updates_correctly(): void
105+
{
106+
$resource = ComputerScienceResource::factory()->create();
107+
$total = [
108+
'community' => 0,
109+
'teaching_clarity' => 0,
110+
'engagement' => 0,
111+
'practicality' => 0,
112+
'user_friendliness' => 0,
113+
'updates' => 0,
114+
];
115+
116+
$reviewCount = 5;
117+
118+
for ($i = 0; $i < $reviewCount; $i++) {
119+
$user = User::factory()->create();
120+
$data = ResourceReviewTestResource::fake();
121+
122+
// Add to total for averaging later
123+
foreach (array_keys($total) as $key) {
124+
$total[$key] += $data[$key];
125+
}
126+
127+
$this->actingAs($user)->post(route('reviews.store', $resource), $data);
128+
}
129+
130+
$this->assertDatabaseHas('resource_review_summaries', array_merge([
131+
'computer_science_resource_id' => $resource->id,
132+
'review_count' => $reviewCount,
133+
], $total));
134+
}
30135
}

tests/TestResources/ComputerScienceResourceTestResource.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\TestResources;
44

55
use App\Models\ComputerScienceResource;
6+
use Event;
67
use Illuminate\Http\Request;
78
use Illuminate\Http\Resources\Json\JsonResource;
89

@@ -26,7 +27,8 @@ public function toArray(Request $request): array
2627

2728
public static function fake(): array
2829
{
29-
$model = ComputerScienceResource::factory()->create();
30+
// Create the model with disabled events
31+
$model = Event::fakeFor(fn() => ComputerScienceResource::factory()->create());
3032

3133
// Transform it to API form
3234
$formData = (new self($model))->toArray(request());
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Tests\TestResources;
4+
5+
use App\Models\ResourceReview;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Http\Resources\Json\JsonResource;
8+
use Event;
9+
10+
class ResourceReviewTestResource extends JsonResource
11+
{
12+
public function toArray(Request $request): array
13+
{
14+
return [
15+
'title' => $this->title,
16+
'description'=> $this->description,
17+
'community'=> $this->community,
18+
'teaching_clarity'=> $this->teaching_clarity,
19+
'engagement'=> $this->engagement,
20+
'practicality'=> $this->practicality,
21+
'user_friendliness'=> $this->user_friendliness,
22+
'updates'=> $this->updates,
23+
'pros'=> $this->pros,
24+
'cons'=> $this->cons,
25+
];
26+
}
27+
28+
public static function fake(): array
29+
{
30+
// Create the model with disabled events
31+
$model = Event::fakeFor(fn() => ResourceReview::factory()->create());
32+
33+
// Transform it to API form
34+
$formData = (new self($model))->toArray(request());
35+
36+
// Delete after getting the array to avoid polluting the DB
37+
$model->delete();
38+
39+
return $formData;
40+
}
41+
}

0 commit comments

Comments
 (0)