Skip to content

Commit

Permalink
Merge pull request #74 from liberu-ecommerce/sweep/add-unit-tests-rat…
Browse files Browse the repository at this point in the history
…ing-review

Add Unit Tests for Rating and Review Controllers and Models
  • Loading branch information
curtisdelicata committed May 30, 2024
2 parents 38272c2 + 51920b5 commit 3d8d2f5
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/Unit/RatingControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Tests\Unit;

use App\Http\Controllers\RatingController;
use App\Http\Requests\RatingRequest;
use App\Models\Rating;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Mockery;
use Tests\TestCase;

class RatingControllerTest extends TestCase
{
use RefreshDatabase;

public function testStore()
{
$mockRequest = Mockery::mock(RatingRequest::class);
$mockRequest->shouldReceive('product_id')->andReturn(1);
$mockRequest->shouldReceive('rating')->andReturn(5);

Auth::shouldReceive('id')->once()->andReturn(1);

$response = app(RatingController::class)->store($mockRequest);

$response->assertStatus(201);
$this->assertDatabaseHas('ratings', [
'user_id' => 1,
'product_id' => 1,
'rating' => 5,
]);
}

public function testCalculateAverageRating()
{
Rating::shouldReceive('calculateAverageRating')
->with(1)
->once()
->andReturn(4.5);

$response = app(RatingController::class)->calculateAverageRating(1);

$response->assertJson(['averageRating' => 4.5]);
}
}
29 changes: 29 additions & 0 deletions tests/Unit/RatingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Tests\Unit;

use App\Models\Rating;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class RatingTest extends TestCase
{
use RefreshDatabase;

public function testCalculateAverageRating()
{
$productId = 1;
$expectedAverage = 4.5;

Rating::unguard();
Rating::insert([
['product_id' => $productId, 'rating' => 5],
['product_id' => $productId, 'rating' => 4],
]);
Rating::reguard();

$averageRating = Rating::calculateAverageRating($productId);

$this->assertEquals($expectedAverage, $averageRating);
}
}
70 changes: 70 additions & 0 deletions tests/Unit/ReviewControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Tests\Unit;

use App\Http\Controllers\ReviewController;
use App\Http\Requests\ReviewRequest;
use App\Models\Review;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Mockery;
use Tests\TestCase;

class ReviewControllerTest extends TestCase
{
use RefreshDatabase;

public function testStore()
{
$mockRequest = Mockery::mock(ReviewRequest::class);
$mockRequest->shouldReceive('product_id')->andReturn(1);
$mockRequest->shouldReceive('rating')->andReturn(5);
$mockRequest->shouldReceive('review')->andReturn('Great product!');

Auth::shouldReceive('id')->once()->andReturn(1);

$response = app(ReviewController::class)->store($mockRequest);

$response->assertStatus(201);
$this->assertDatabaseHas('reviews', [
'user_id' => 1,
'product_id' => 1,
'rating' => 5,
'review' => 'Great product!',
'approved' => false,
]);
}

public function testApprove()
{
$review = Review::create([
'user_id' => 1,
'product_id' => 1,
'rating' => 5,
'review' => 'Great product!',
'approved' => false,
]);

$response = app(ReviewController::class)->approve($review->id);

$this->assertDatabaseHas('reviews', [
'id' => $review->id,
'approved' => true,
]);
}

public function testShow()
{
$review = Review::create([
'user_id' => 1,
'product_id' => 1,
'rating' => 5,
'review' => 'Great product!',
'approved' => true,
]);

$response = app(ReviewController::class)->show(1);

$response->assertJson([$review->toArray()]);
}
}
50 changes: 50 additions & 0 deletions tests/Unit/ReviewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Tests\Unit;

use App\Models\Review;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ReviewTest extends TestCase
{
use RefreshDatabase;

public function testApprove()
{
$review = Review::create([
'user_id' => 1,
'product_id' => 1,
'rating' => 5,
'review' => 'Excellent product!',
'approved' => false,
]);

$review->approve();

$this->assertTrue($review->approved);
$this->assertDatabaseHas('reviews', [
'id' => $review->id,
'approved' => true,
]);
}

public function testReject()
{
$review = Review::create([
'user_id' => 2,
'product_id' => 2,
'rating' => 4,
'review' => 'Good product, but has some issues.',
'approved' => true,
]);

$review->reject();

$this->assertFalse($review->approved);
$this->assertDatabaseHas('reviews', [
'id' => $review->id,
'approved' => false,
]);
}
}

0 comments on commit 3d8d2f5

Please sign in to comment.