Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/WalletsTest.php
<?php | <?php | ||||
namespace Tests\Feature\Controller; | namespace Tests\Feature\Controller; | ||||
use App\Http\Controllers\API\V4\WalletsController; | use App\Http\Controllers\API\V4\WalletsController; | ||||
use App\Payment; | |||||
use App\Providers\PaymentProvider; | |||||
use App\Transaction; | use App\Transaction; | ||||
use Carbon\Carbon; | |||||
use Tests\TestCase; | use Tests\TestCase; | ||||
class WalletsTest extends TestCase | class WalletsTest extends TestCase | ||||
{ | { | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
Show All 9 Lines | class WalletsTest extends TestCase | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->deleteTestUser('wallets-controller@kolabnow.com'); | $this->deleteTestUser('wallets-controller@kolabnow.com'); | ||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test fetching pdf receipt | |||||
*/ | |||||
public function testReceiptDownload(): void | |||||
{ | |||||
$user = $this->getTestUser('wallets-controller@kolabnow.com'); | |||||
$john = $this->getTestUser('john@klab.org'); | |||||
$wallet = $user->wallets()->first(); | |||||
// Unauth access not allowed | |||||
$response = $this->get("api/v4/wallets/{$wallet->id}/receipts/2020-05"); | |||||
$response->assertStatus(401); | |||||
$response = $this->actingAs($john)->get("api/v4/wallets/{$wallet->id}/receipts/2020-05"); | |||||
$response->assertStatus(403); | |||||
// Invalid receipt id (current month) | |||||
$receiptId = date('Y-m'); | |||||
$response = $this->actingAs($user)->get("api/v4/wallets/{$wallet->id}/receipts/{$receiptId}"); | |||||
$response->assertStatus(404); | |||||
// Invalid receipt id | |||||
$receiptId = '1000-03'; | |||||
$response = $this->actingAs($user)->get("api/v4/wallets/{$wallet->id}/receipts/{$receiptId}"); | |||||
$response->assertStatus(404); | |||||
// Valid receipt id | |||||
$year = intval(date('Y')) - 1; | |||||
$receiptId = "$year-12"; | |||||
$filename = \config('app.name') . " Receipt for $year-12"; | |||||
$response = $this->actingAs($user)->get("api/v4/wallets/{$wallet->id}/receipts/{$receiptId}"); | |||||
$response->assertStatus(200); | |||||
$response->assertHeader('content-type', 'application/pdf'); | |||||
$response->assertHeader('content-disposition', 'attachment; filename="' . $filename . '"'); | |||||
$response->assertHeader('content-length'); | |||||
$length = $response->headers->get('content-length'); | |||||
$content = $response->content(); | |||||
$this->assertStringStartsWith("%PDF-1.3\n", $content); | |||||
$this->assertEquals(strlen($content), $length); | |||||
} | |||||
/** | |||||
* Test fetching list of receipts | |||||
*/ | |||||
public function testReceipts(): void | |||||
{ | |||||
$user = $this->getTestUser('wallets-controller@kolabnow.com'); | |||||
$john = $this->getTestUser('john@klab.org'); | |||||
$wallet = $user->wallets()->first(); | |||||
$wallet->payments()->delete(); | |||||
// Unauth access not allowed | |||||
$response = $this->get("api/v4/wallets/{$wallet->id}/receipts"); | |||||
$response->assertStatus(401); | |||||
$response = $this->actingAs($john)->get("api/v4/wallets/{$wallet->id}/receipts"); | |||||
$response->assertStatus(403); | |||||
// Empty list expected | |||||
$response = $this->actingAs($user)->get("api/v4/wallets/{$wallet->id}/receipts"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertCount(5, $json); | |||||
$this->assertSame('success', $json['status']); | |||||
$this->assertSame([], $json['list']); | |||||
$this->assertSame(1, $json['page']); | |||||
$this->assertSame(0, $json['count']); | |||||
$this->assertSame(false, $json['hasMore']); | |||||
// Insert a payment to the database | |||||
$date = Carbon::create(intval(date('Y')) - 1, 4, 30); | |||||
$payment = Payment::create([ | |||||
'id' => 'AAA1', | |||||
'status' => PaymentProvider::STATUS_PAID, | |||||
'type' => PaymentProvider::TYPE_ONEOFF, | |||||
'description' => 'Paid in April', | |||||
'wallet_id' => $wallet->id, | |||||
'provider' => 'stripe', | |||||
'amount' => 1111, | |||||
]); | |||||
$payment->updated_at = $date; | |||||
$payment->save(); | |||||
$response = $this->actingAs($user)->get("api/v4/wallets/{$wallet->id}/receipts"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertCount(5, $json); | |||||
$this->assertSame('success', $json['status']); | |||||
$this->assertSame([$date->format('Y-m')], $json['list']); | |||||
$this->assertSame(1, $json['page']); | |||||
$this->assertSame(1, $json['count']); | |||||
$this->assertSame(false, $json['hasMore']); | |||||
} | |||||
/** | |||||
* Test fetching wallet transactions | * Test fetching wallet transactions | ||||
*/ | */ | ||||
public function testTransactions(): void | public function testTransactions(): void | ||||
{ | { | ||||
$package_kolab = \App\Package::where('title', 'kolab')->first(); | $package_kolab = \App\Package::where('title', 'kolab')->first(); | ||||
$user = $this->getTestUser('wallets-controller@kolabnow.com'); | $user = $this->getTestUser('wallets-controller@kolabnow.com'); | ||||
$user->assignPackage($package_kolab); | $user->assignPackage($package_kolab); | ||||
$john = $this->getTestUser('john@klab.org'); | $john = $this->getTestUser('john@klab.org'); | ||||
▲ Show 20 Lines • Show All 113 Lines • Show Last 20 Lines |