-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from liberu-ecommerce/sweep/add-unit-tests-rat…
…ing-review Add Unit Tests for Rating and Review Controllers and Models
- Loading branch information
Showing
4 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
} |