From 5660fcd4dcecd07c3b062b12ddbe718ca7908413 Mon Sep 17 00:00:00 2001 From: Peter Papp Date: Sun, 7 Mar 2021 09:50:04 +0100 Subject: [PATCH] added it_get_all_invoices_from_admin, it_get_single_user_invoice_page test --- .../Controllers/Admin/InvoiceController.php | 6 +- app/Http/Resources/InvoiceAdminResource.php | 37 ++-- app/Services/StripeService.php | 24 ++- routes/admin.php | 6 +- routes/web.php | 2 +- tests/Feature/SubscriptionTest.php | 196 +++++++++++------- 6 files changed, 159 insertions(+), 112 deletions(-) diff --git a/app/Http/Controllers/Admin/InvoiceController.php b/app/Http/Controllers/Admin/InvoiceController.php index 381aa3f9..f861cf73 100644 --- a/app/Http/Controllers/Admin/InvoiceController.php +++ b/app/Http/Controllers/Admin/InvoiceController.php @@ -5,9 +5,9 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Http\Resources\InvoiceAdminCollection; use App\Http\Resources\InvoiceResource; -use App\Invoice; +use App\Models\Invoice; use App\Services\StripeService; -use App\Setting; +use App\Models\Setting; use Illuminate\Http\Request; class InvoiceController extends Controller @@ -37,7 +37,7 @@ class InvoiceController extends Controller * * @param $customer * @param $token - * @return InvoiceResource + * @return InvoiceResource|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ public function show($customer, $token) { diff --git a/app/Http/Resources/InvoiceAdminResource.php b/app/Http/Resources/InvoiceAdminResource.php index 3ddb775e..fb20fb3a 100644 --- a/app/Http/Resources/InvoiceAdminResource.php +++ b/app/Http/Resources/InvoiceAdminResource.php @@ -2,7 +2,7 @@ namespace App\Http\Resources; -use App\User; +use App\Models\User; use Illuminate\Http\Resources\Json\JsonResource; use Laravel\Cashier\Cashier; @@ -16,7 +16,8 @@ class InvoiceAdminResource extends JsonResource */ public function toArray($request) { - $user = User::where('stripe_id', $this['customer'])->first(); + $user = User::where('stripe_id', $this['customer']) + ->first(); return [ 'data' => [ @@ -29,7 +30,7 @@ class InvoiceAdminResource extends JsonResource 'created_at_formatted' => format_date($this['created']), 'created_at' => $this['created'], 'order' => $this['number'], - 'user_id' => $user ? $user->id : null, + 'user_id' => $user->id ?? null, 'client' => [ 'billing_address' => $this['customer_address'], 'billing_name' => $this['customer_name'], @@ -42,24 +43,24 @@ class InvoiceAdminResource extends JsonResource 'description' => $this['lines']['data'][0]['description'], ], 'seller' => null, - ] - ], - $this->mergeWhen($user, function () use ($user) { - return [ - 'relationships' => [ - 'user' => [ - 'data' => [ - 'id' => (string)$user->id, - 'type' => 'user', - 'attributes' => [ - 'name' => $user->name, - 'avatar' => $user->avatar, + ], + $this->mergeWhen($user, function () use ($user) { + return [ + 'relationships' => [ + 'user' => [ + 'data' => [ + 'id' => $user->id, + 'type' => 'user', + 'attributes' => [ + 'name' => $user->name, + 'avatar' => $user->avatar, + ] ] ] ] - ] - ]; - }), + ]; + }), + ], ]; } } diff --git a/app/Services/StripeService.php b/app/Services/StripeService.php index 90863f97..6277e1ec 100644 --- a/app/Services/StripeService.php +++ b/app/Services/StripeService.php @@ -333,7 +333,10 @@ class StripeService */ public function deletePlan($slug) { - $this->stripe->plans()->delete($slug); + $this + ->stripe + ->plans() + ->delete($slug); } /** @@ -344,20 +347,22 @@ class StripeService */ public function getUserInvoices($user) { - return $user->invoices(); + return $user + ->invoices(); } /** * Get user invoice by id * + * @param $customer * @param $id * @return \Laravel\Cashier\Invoice|null */ public function getUserInvoice($customer, $id) { - $user = User::where('stripe_id', $customer)->firstOrFail(); - - return $user->findInvoice($id); + return User::whereStripeId($customer) + ->firstOrFail() + ->findInvoice($id); } /** @@ -367,8 +372,11 @@ class StripeService */ public function getInvoices() { - return $this->stripe->invoices()->all([ - 'limit' => 20 - ]); + return $this + ->stripe + ->invoices() + ->all([ + 'limit' => 20 + ]); } } \ No newline at end of file diff --git a/routes/admin.php b/routes/admin.php index 93683be1..7690daf4 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -46,11 +46,7 @@ Route::group(['prefix' => 'pages'], function () { }); // Invoices -// TODO: testy -Route::group(['prefix' => 'invoices'], function () { - Route::get('/{token}', [InvoiceController::class, 'show']); - Route::get('/', [InvoiceController::class, 'index']); -}); +Route::get('/invoices', [InvoiceController::class, 'index']); // Settings Route::group(['prefix' => 'settings'], function () { diff --git a/routes/web.php b/routes/web.php index 7e758abb..c7ea1b12 100644 --- a/routes/web.php +++ b/routes/web.php @@ -29,7 +29,7 @@ Route::group(['middleware' => ['auth:api', 'auth.shared', 'auth.master', 'scope: }); // Get user invoice -Route::group(['middleware' => ['auth:api', 'auth.master', 'scope:master']], function () { +Route::group(['middleware' => ['auth:sanctum']], function () { Route::get('/invoice/{customer}/{token}', [InvoiceController::class, 'show']); }); diff --git a/tests/Feature/SubscriptionTest.php b/tests/Feature/SubscriptionTest.php index 58090095..560ea73b 100644 --- a/tests/Feature/SubscriptionTest.php +++ b/tests/Feature/SubscriptionTest.php @@ -273,28 +273,6 @@ class SubscriptionTest extends TestCase ]); } - /** - * - */ - public function it_get_user_invoices_from_admin() - { - $user = User::factory(User::class) - ->create($this->user); - - Sanctum::actingAs($user); - - $admin = User::factory(User::class) - ->create(['role' => 'admin']); - - Sanctum::actingAs($admin); - - $this->getJson("/api/admin/users/$user->id/invoices") - ->assertStatus(200) - ->assertJsonFragment([ - 'customer' => $this->user['stripe_id'] - ]); - } - /** * */ @@ -324,61 +302,6 @@ class SubscriptionTest extends TestCase ])->assertStatus(204); } - /** - * - */ - public function it_get_all_plans_from_admin() - { - $admin = User::factory(User::class) - ->create(['role' => 'admin']); - - Sanctum::actingAs($admin); - - $this->getJson('/api/admin/plans') - ->assertStatus(200); - } - - /** - * - */ - public function it_get_single_plan_from_admin() - { - $admin = User::factory(User::class) - ->create(['role' => 'admin']); - - Sanctum::actingAs($admin); - - $this->getJson('/api/admin/plans/' . $this->plan['data']['id']) - ->assertStatus(200); - } - - /** - * - */ - public function it_create_single_plan_from_admin() - { - $admin = User::factory(User::class) - ->create(['role' => 'admin']); - - Sanctum::actingAs($admin); - - $plan_name = 'test-plan-' . Str::random(16); - - $this->postJson('/api/admin/plans', [ - 'type' => 'plan', - 'attributes' => [ - 'name' => $plan_name, - 'price' => (string)rand(1, 99), - 'description' => 'Some random description', - 'capacity' => rand(1, 999), - ], - ]) - ->assertStatus(201) - ->assertJsonFragment([ - 'name' => $plan_name - ]); - } - /** * */ @@ -470,4 +393,123 @@ class SubscriptionTest extends TestCase 'id' => $user->id ]); } + + /** + * + */ + public function it_get_all_invoices_from_admin() + { + $admin = User::factory(User::class) + ->create(['role' => 'admin']); + + Sanctum::actingAs($admin); + + $this->getJson("/api/admin/invoices") + ->assertStatus(200); + } + + /** + * + */ + public function it_get_single_user_invoice_page_from_admin() + { + $user = User::factory(User::class) + ->create($this->user); + + Sanctum::actingAs($user); + + $invoices = $this->getJson('/api/user/invoices') + ->assertStatus(200) + ->assertJsonFragment([ + 'customer' => $this->user['stripe_id'] + ]); + + $admin = User::factory(User::class) + ->create(['role' => 'admin']); + + Sanctum::actingAs($admin); + + $invoice_id = json_decode($invoices->content(), true)['data'][0]['data']['id']; + + $this->get("/invoice/{$this->user['stripe_id']}/$invoice_id") + ->assertStatus(200) + ->assertSee('Invoice'); + } + + /** + * + */ + public function it_get_user_invoices_from_admin() + { + $user = User::factory(User::class) + ->create($this->user); + + Sanctum::actingAs($user); + + $admin = User::factory(User::class) + ->create(['role' => 'admin']); + + Sanctum::actingAs($admin); + + $this->getJson("/api/admin/users/$user->id/invoices") + ->assertStatus(200) + ->assertJsonFragment([ + 'customer' => $this->user['stripe_id'] + ]); + } + + /** + * + */ + public function it_get_all_plans_from_admin() + { + $admin = User::factory(User::class) + ->create(['role' => 'admin']); + + Sanctum::actingAs($admin); + + $this->getJson('/api/admin/plans') + ->assertStatus(200); + } + + /** + * + */ + public function it_get_single_plan_from_admin() + { + $admin = User::factory(User::class) + ->create(['role' => 'admin']); + + Sanctum::actingAs($admin); + + $this->getJson('/api/admin/plans/' . $this->plan['data']['id']) + ->assertStatus(200); + } + + /** + * + */ + public function it_create_single_plan_from_admin() + { + $admin = User::factory(User::class) + ->create(['role' => 'admin']); + + Sanctum::actingAs($admin); + + $plan_name = 'test-plan-' . Str::random(16); + + $this->postJson('/api/admin/plans', [ + 'type' => 'plan', + 'attributes' => [ + 'name' => $plan_name, + 'price' => (string)rand(1, 99), + 'description' => 'Some random description', + 'capacity' => rand(1, 999), + ], + ]) + ->assertStatus(201) + ->assertJsonFragment([ + 'name' => $plan_name + ]); + } }