added it_get_all_invoices_from_admin, it_get_single_user_invoice_page test

This commit is contained in:
Peter Papp
2021-03-07 09:50:04 +01:00
parent da7aee2790
commit 5660fcd4dc
6 changed files with 159 additions and 112 deletions
@@ -5,9 +5,9 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Resources\InvoiceAdminCollection; use App\Http\Resources\InvoiceAdminCollection;
use App\Http\Resources\InvoiceResource; use App\Http\Resources\InvoiceResource;
use App\Invoice; use App\Models\Invoice;
use App\Services\StripeService; use App\Services\StripeService;
use App\Setting; use App\Models\Setting;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class InvoiceController extends Controller class InvoiceController extends Controller
@@ -37,7 +37,7 @@ class InvoiceController extends Controller
* *
* @param $customer * @param $customer
* @param $token * @param $token
* @return InvoiceResource * @return InvoiceResource|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/ */
public function show($customer, $token) public function show($customer, $token)
{ {
+19 -18
View File
@@ -2,7 +2,7 @@
namespace App\Http\Resources; namespace App\Http\Resources;
use App\User; use App\Models\User;
use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\JsonResource;
use Laravel\Cashier\Cashier; use Laravel\Cashier\Cashier;
@@ -16,7 +16,8 @@ class InvoiceAdminResource extends JsonResource
*/ */
public function toArray($request) public function toArray($request)
{ {
$user = User::where('stripe_id', $this['customer'])->first(); $user = User::where('stripe_id', $this['customer'])
->first();
return [ return [
'data' => [ 'data' => [
@@ -29,7 +30,7 @@ class InvoiceAdminResource extends JsonResource
'created_at_formatted' => format_date($this['created']), 'created_at_formatted' => format_date($this['created']),
'created_at' => $this['created'], 'created_at' => $this['created'],
'order' => $this['number'], 'order' => $this['number'],
'user_id' => $user ? $user->id : null, 'user_id' => $user->id ?? null,
'client' => [ 'client' => [
'billing_address' => $this['customer_address'], 'billing_address' => $this['customer_address'],
'billing_name' => $this['customer_name'], 'billing_name' => $this['customer_name'],
@@ -42,24 +43,24 @@ class InvoiceAdminResource extends JsonResource
'description' => $this['lines']['data'][0]['description'], 'description' => $this['lines']['data'][0]['description'],
], ],
'seller' => null, 'seller' => null,
] ],
], $this->mergeWhen($user, function () use ($user) {
$this->mergeWhen($user, function () use ($user) { return [
return [ 'relationships' => [
'relationships' => [ 'user' => [
'user' => [ 'data' => [
'data' => [ 'id' => $user->id,
'id' => (string)$user->id, 'type' => 'user',
'type' => 'user', 'attributes' => [
'attributes' => [ 'name' => $user->name,
'name' => $user->name, 'avatar' => $user->avatar,
'avatar' => $user->avatar, ]
] ]
] ]
] ]
] ];
]; }),
}), ],
]; ];
} }
} }
+16 -8
View File
@@ -333,7 +333,10 @@ class StripeService
*/ */
public function deletePlan($slug) public function deletePlan($slug)
{ {
$this->stripe->plans()->delete($slug); $this
->stripe
->plans()
->delete($slug);
} }
/** /**
@@ -344,20 +347,22 @@ class StripeService
*/ */
public function getUserInvoices($user) public function getUserInvoices($user)
{ {
return $user->invoices(); return $user
->invoices();
} }
/** /**
* Get user invoice by id * Get user invoice by id
* *
* @param $customer
* @param $id * @param $id
* @return \Laravel\Cashier\Invoice|null * @return \Laravel\Cashier\Invoice|null
*/ */
public function getUserInvoice($customer, $id) public function getUserInvoice($customer, $id)
{ {
$user = User::where('stripe_id', $customer)->firstOrFail(); return User::whereStripeId($customer)
->firstOrFail()
return $user->findInvoice($id); ->findInvoice($id);
} }
/** /**
@@ -367,8 +372,11 @@ class StripeService
*/ */
public function getInvoices() public function getInvoices()
{ {
return $this->stripe->invoices()->all([ return $this
'limit' => 20 ->stripe
]); ->invoices()
->all([
'limit' => 20
]);
} }
} }
+1 -5
View File
@@ -46,11 +46,7 @@ Route::group(['prefix' => 'pages'], function () {
}); });
// Invoices // Invoices
// TODO: testy Route::get('/invoices', [InvoiceController::class, 'index']);
Route::group(['prefix' => 'invoices'], function () {
Route::get('/{token}', [InvoiceController::class, 'show']);
Route::get('/', [InvoiceController::class, 'index']);
});
// Settings // Settings
Route::group(['prefix' => 'settings'], function () { Route::group(['prefix' => 'settings'], function () {
+1 -1
View File
@@ -29,7 +29,7 @@ Route::group(['middleware' => ['auth:api', 'auth.shared', 'auth.master', 'scope:
}); });
// Get user invoice // 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']); Route::get('/invoice/{customer}/{token}', [InvoiceController::class, 'show']);
}); });
+119 -77
View File
@@ -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); ])->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 '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
]);
}
} }