Skip to content

Commit

Permalink
Merge pull request #470 from bhimbho/chore/test-for-all-written-feature
Browse files Browse the repository at this point in the history
chore: test for statistics, sales and revenue
  • Loading branch information
timiajayi authored Aug 9, 2024
2 parents dd470aa + 7bc1a5c commit cbb5ce1
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 8 deletions.
10 changes: 4 additions & 6 deletions app/Http/Controllers/Api/V1/User/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function index()
->get()
->flatMap(function ($product) {
return $product->orders->map(function ($order) {
return $order->quantity * $order->amount;
return $order->total_amount;
});
})->sum();

Expand All @@ -37,15 +37,14 @@ public function index()
}])
->get()
->sum('orders_count');

$lastMonthRevenue = $userProducts
->with(['orders' => function ($query) use ($currentMonth, $lastMonth) {
$query->whereBetween('created_at', [$lastMonth, $currentMonth]);
}])
->get()
->flatMap(function ($product) {
return $product->orders->map(function ($order) {
return $order->quantity * $order->amount;
return $order->total_amount;
});
})->sum();

Expand Down Expand Up @@ -149,9 +148,8 @@ public function user_analytics()
return response()->json([
'message' => 'User analytics retrieved successfully',
'status_code' => Response::HTTP_OK,
'data' => [
$revenueByMonth,
]
'data' => $revenueByMonth,

]);
}

Expand Down
4 changes: 2 additions & 2 deletions database/seeders/AdminSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public function run(): void
[
'name' => "Super Admin",
'role' => "admin",
'password' => Hash::make("bulldozer"),
'password' => Hash::make("@Bulldozer01"),
'is_verified' => 1,
]
]
);

$admin->profile()->create([
Expand Down
149 changes: 149 additions & 0 deletions tests/Feature/UserDashboardTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace Tests\Feature;

use App\Models\Order;
use App\Models\Product;
use App\Models\User;
use Carbon\Carbon;
use Tests\TestCase;

class UserDashboardTest extends TestCase
{
public function test_user_cards_data_is_returned_in_right_format()
{
$this->actingAs(User::factory()->create());
$response = $this->get('/api/v1/user-statistics');

$response->assertSuccessful();
$response->assertJson([
'message' => 'Dashboard retrieved successfully',
'status_code' => 200,
'data' => [
'revenue' => [],
'subscriptions' => [],
'orders' => [],
'active_users' => [],
]
]);
}

public function test_accurate_percentage_and_amount_is_returned_for_revenue_data()
{
$user = User::factory()->create();
$this->actingAs($user);
$product = Product::factory()->create([
'price' => 1000,
'user_id' => $user->id,
]);

$order = Order::factory()->create([
'product_id' => $product->product_id,
'quantity' => 2,
'total_amount' => 2000,
]);
$order->created_at = Carbon::now()->subMonth();
$order->save();
Order::factory()->create([
'product_id' => $product->product_id,
'quantity' => 3,
'total_amount' => 3000,
]);
$response = $this->get('/api/v1/user-statistics');
$response->assertJsonFragment([
"current_month" => 3000,
"previous_month" => 2000,
"percentage_difference" => "50%"
]);
}

public function test_accurate_data_is_returned_for_graph_usage()
{
$user = User::factory()->create();
$this->actingAs($user);
$product = Product::factory()->create([
'price' => 1000,
'user_id' => $user->id,
]);

$order = Order::factory()->create([
'product_id' => $product->product_id,
'quantity' => 2,
'total_amount' => 2000,
]);
$order->created_at = Carbon::now()->subMonth();
$order->save();
Order::factory()->create([
'product_id' => $product->product_id,
'quantity' => 3,
'total_amount' => 3000,
]);
$response = $this->get('/api/v1/user-analytics');
$response->assertJson(
[
'message' => 'User analytics retrieved successfully',
'status_code' => 200,
'data' => [
'Jan' => 0,
'Feb' => 0,
'Mar' => 0,
'Apr' => 0,
'May' => 0,
'Jun' => 0,
'Jul' => 2000.00,
'Aug' => 3000.00,
'Sep' => 0,
'Oct' => 0,
'Nov' => 0,
'Dec' => 0,
]
]
);
}

public function test_accurate_data_is_returned_for_recent_sales()
{
$user = User::factory()->create();
$this->actingAs($user);
$product = Product::factory()->create([
'price' => 1000,
'user_id' => $user->id,
]);
$order = Order::factory()->create([
'product_id' => $product->product_id,
'quantity' => 2,
'total_amount' => 2000,
]);
$order->created_at = Carbon::now()->subMonth();
$order->save();
$order1 = Order::factory()->create([
'product_id' => $product->product_id,
'quantity' => 3,
'total_amount' => 3000,
]);
$response = $this->get('/api/v1/user-sales');
$response->assertSuccessful();
$response->assertJson(
[
'message' => 'Recent sales retrieved successfully',
'status_code' => 200,
'data' => [
[
'id' => $order->id,
'user_id' => $order->user_id,
'quantity' => 2,
'total_amount' => '2000.00',
'user' => []
],
[
'id' => $order1->id,
'user_id' => $order1->user_id,
'quantity' => 3,
'total_amount' => '3000.00',
'user' => []
],
]
]
);
}
}

0 comments on commit cbb5ce1

Please sign in to comment.