Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions database/factories/CommentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function definition(): array

public function configure()
{
// TODO: Consider making the increment part of the dispatch event?
return $this->afterCreating(function (Comment $comment) {
if ($comment->root_comment_id) {
Comment::where('id', $comment->root_comment_id)
Expand Down
2 changes: 1 addition & 1 deletion database/factories/ComputerScienceResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function definition(): array
return [
'name' => fake()->name(),
'description' => fake()->realText(),
'user_id' => User::all()->random()->id,
'user_id' => User::inRandomOrder()->first() ?? User::factory()->create(),
'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',
'page_url' => fake()->url(),

Expand Down
1 change: 0 additions & 1 deletion tests/Feature/CommentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public function test_cannot_comment_on_non_existent_resource()
/**
* Test that commenting works on all commentable types defined in config.
*/
// TODO:
public function test_can_comment_all_commentable_types()
{
$user = User::factory()->create();
Expand Down
129 changes: 117 additions & 12 deletions tests/Feature/ResourceReviewsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,134 @@

namespace Tests\Feature;

use App\Models\ComputerScienceResource;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Tests\TestResources\ResourceReviewTestResource;

class ResourceReviewsTest extends TestCase
{
/**
* A basic feature test example.
*/
use RefreshDatabase;

public function test_example(): void
{
$response = $this->get('/');

$response->assertStatus(200);
}

// Test this:
// Cannot post any form of invalid data
// Do 3 tests of invalid data testing

public function test_invalid_data_missing_required_fields(): void
{
$user = User::factory()->create();
$resource = ComputerScienceResource::factory()->create();

$invalidData = ResourceReviewTestResource::fake();
unset($invalidData['title'], $invalidData['description']);

$response = $this->actingAs($user)
->post(route('reviews.store', $resource), $invalidData);

$response->assertSessionHasErrors(['title', 'description']);
}

public function test_invalid_data_invalid_rating(): void
{
$user = User::factory()->create();
$resource = ComputerScienceResource::factory()->create();

$invalidData = ResourceReviewTestResource::fake();
$invalidData['community'] = 0;

$response = $this->actingAs($user)
->post(route('reviews.store', $resource), $invalidData);

$response->assertSessionHasErrors(['community']);
}

public function test_invalid_data_invalid_pros_field(): void
{
$user = User::factory()->create();
$resource = ComputerScienceResource::factory()->create();

$invalidData = ResourceReviewTestResource::fake();
$invalidData['pros'] = 'Not an array';

$response = $this->actingAs($user)
->post(route('reviews.store', $resource), $invalidData);

$response->assertSessionHasErrors(['pros']);
}

public function test_resource_review_can_be_posted(): void
{
$user = User::factory()->create();
$resource = ComputerScienceResource::factory()->create();

$data = ResourceReviewTestResource::fake();

$response = $this->actingAs($user)
->post(route('reviews.store', $resource), $data);

$response->assertRedirect(route('resources.show', ['computerScienceResource' => $resource->id]))
->assertSessionHas('success', 'Review created successfully!');

$this->assertDatabaseHas('resource_reviews', [
'computer_science_resource_id' => $resource->id,
'title' => $data['title']
]);
}

public function test_resource_average_has_changed(): void
{
$user = User::factory()->create();
$resource = ComputerScienceResource::factory()->create();

$data = ResourceReviewTestResource::fake();

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

// Test that the resource review can be posted
//
$this->assertDatabaseHas('resource_review_summaries', [
'computer_science_resource_id' => $resource->id,
'community' => $data['community'],
'teaching_clarity' => $data['teaching_clarity'],
'engagement' => $data['engagement'],
'practicality' => $data['practicality'],
'user_friendliness' => $data['user_friendliness'],
'updates' => $data['updates'],
'review_count' => 1,
]);
}

public function test_resource_review_average_updates_correctly(): void
{
$resource = ComputerScienceResource::factory()->create();
$total = [
'community' => 0,
'teaching_clarity' => 0,
'engagement' => 0,
'practicality' => 0,
'user_friendliness' => 0,
'updates' => 0,
];

$reviewCount = 5;

for ($i = 0; $i < $reviewCount; $i++) {
$user = User::factory()->create();
$data = ResourceReviewTestResource::fake();

// Add to total for averaging later
foreach (array_keys($total) as $key) {
$total[$key] += $data[$key];
}

$this->actingAs($user)->post(route('reviews.store', $resource), $data);
}

$this->assertDatabaseHas('resource_review_summaries', array_merge([
'computer_science_resource_id' => $resource->id,
'review_count' => $reviewCount,
], $total));
}
}
4 changes: 3 additions & 1 deletion tests/TestResources/ComputerScienceResourceTestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\TestResources;

use App\Models\ComputerScienceResource;
use Event;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

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

public static function fake(): array
{
$model = ComputerScienceResource::factory()->create();
// Create the model with disabled events
$model = Event::fakeFor(fn() => ComputerScienceResource::factory()->create());

// Transform it to API form
$formData = (new self($model))->toArray(request());
Expand Down
41 changes: 41 additions & 0 deletions tests/TestResources/ResourceReviewTestResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Tests\TestResources;

use App\Models\ResourceReview;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Event;

class ResourceReviewTestResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'title' => $this->title,
'description'=> $this->description,
'community'=> $this->community,
'teaching_clarity'=> $this->teaching_clarity,
'engagement'=> $this->engagement,
'practicality'=> $this->practicality,
'user_friendliness'=> $this->user_friendliness,
'updates'=> $this->updates,
'pros'=> $this->pros,
'cons'=> $this->cons,
];
}

public static function fake(): array
{
// Create the model with disabled events
$model = Event::fakeFor(fn() => ResourceReview::factory()->create());

// Transform it to API form
$formData = (new self($model))->toArray(request());

// Delete after getting the array to avoid polluting the DB
$model->delete();

return $formData;
}
}