Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Browser/WalletTest.php
<?php | <?php | ||||
namespace Tests\Browser; | namespace Tests\Browser; | ||||
use App\Payment; | |||||
use App\Providers\PaymentProvider; | |||||
use App\Transaction; | use App\Transaction; | ||||
use App\Wallet; | use App\Wallet; | ||||
use Carbon\Carbon; | |||||
use Tests\Browser; | use Tests\Browser; | ||||
use Tests\Browser\Pages\Dashboard; | use Tests\Browser\Pages\Dashboard; | ||||
use Tests\Browser\Pages\Home; | use Tests\Browser\Pages\Home; | ||||
use Tests\Browser\Pages\Wallet as WalletPage; | use Tests\Browser\Pages\Wallet as WalletPage; | ||||
use Tests\TestCaseDusk; | use Tests\TestCaseDusk; | ||||
class WalletTest extends TestCaseDusk | class WalletTest extends TestCaseDusk | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | public function testWallet(): void | ||||
$browser->click('@links .link-wallet') | $browser->click('@links .link-wallet') | ||||
->on(new WalletPage()) | ->on(new WalletPage()) | ||||
->assertSeeIn('#wallet .card-title', 'Account balance') | ->assertSeeIn('#wallet .card-title', 'Account balance') | ||||
->assertSeeIn('#wallet .card-text', 'Current account balance is -12,34 CHF'); | ->assertSeeIn('#wallet .card-text', 'Current account balance is -12,34 CHF'); | ||||
}); | }); | ||||
} | } | ||||
/** | /** | ||||
* Test Receipts tab | |||||
*/ | |||||
public function testReceipts(): void | |||||
{ | |||||
$user = $this->getTestUser('wallets-controller@kolabnow.com', ['password' => 'simple123']); | |||||
$wallet = $user->wallets()->first(); | |||||
$wallet->payments()->delete(); | |||||
// Log out John and log in the test user | |||||
$this->browse(function (Browser $browser) { | |||||
$browser->visit('/logout') | |||||
->waitForLocation('/login') | |||||
->on(new Home()) | |||||
->submitLogon('wallets-controller@kolabnow.com', 'simple123', true); | |||||
}); | |||||
// Assert Receipts tab content when there's no receipts available | |||||
$this->browse(function (Browser $browser) { | |||||
$browser->on(new Dashboard()) | |||||
->click('@links .link-wallet') | |||||
->on(new WalletPage()) | |||||
->assertSeeIn('@nav #tab-receipts', 'Receipts') | |||||
->with('@receipts-tab', function (Browser $browser) { | |||||
$browser->waitUntilMissing('.app-loader') | |||||
->assertSeeIn('p', 'There are no receipts for payments') | |||||
->assertDontSeeIn('p', 'Here you can download') | |||||
->assertMissing('select') | |||||
->assertMissing('button'); | |||||
}); | |||||
}); | |||||
// Create some sample payments | |||||
$receipts = []; | |||||
$date = Carbon::create(intval(date('Y')) - 1, 3, 30); | |||||
$payment = Payment::create([ | |||||
'id' => 'AAA1', | |||||
'status' => PaymentProvider::STATUS_PAID, | |||||
'type' => PaymentProvider::TYPE_ONEOFF, | |||||
'description' => 'Paid in March', | |||||
'wallet_id' => $wallet->id, | |||||
'provider' => 'stripe', | |||||
'amount' => 1111, | |||||
]); | |||||
$payment->updated_at = $date; | |||||
$payment->save(); | |||||
$receipts[] = $date->format('Y-m'); | |||||
$date = Carbon::create(intval(date('Y')) - 1, 4, 30); | |||||
$payment = Payment::create([ | |||||
'id' => 'AAA2', | |||||
'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(); | |||||
$receipts[] = $date->format('Y-m'); | |||||
// Assert Receipts tab with receipts available | |||||
$this->browse(function (Browser $browser) use ($receipts) { | |||||
$browser->refresh() | |||||
->on(new WalletPage()) | |||||
->assertSeeIn('@nav #tab-receipts', 'Receipts') | |||||
->with('@receipts-tab', function (Browser $browser) use ($receipts) { | |||||
$browser->waitUntilMissing('.app-loader') | |||||
->assertDontSeeIn('p', 'There are no receipts for payments') | |||||
->assertSeeIn('p', 'Here you can download') | |||||
->assertSeeIn('button', 'Download') | |||||
->assertElementsCount('select > option', 2) | |||||
->assertSeeIn('select > option:nth-child(1)', $receipts[1]) | |||||
->assertSeeIn('select > option:nth-child(2)', $receipts[0]); | |||||
// Download a receipt file | |||||
$browser->select('select', $receipts[0]) | |||||
->click('button') | |||||
->pause(2000); | |||||
$files = glob(__DIR__ . '/downloads/*.pdf'); | |||||
$filename = pathinfo($files[0], PATHINFO_BASENAME); | |||||
$this->assertTrue(strpos($filename, $receipts[0]) !== false); | |||||
$content = $browser->readDownloadedFile($filename, 0); | |||||
$this->assertStringStartsWith("%PDF-1.3\n", $content); | |||||
$browser->removeDownloadedFile($filename); | |||||
}); | |||||
}); | |||||
} | |||||
/** | |||||
* Test History tab | * Test History tab | ||||
*/ | */ | ||||
public function testHistory(): void | public function testHistory(): void | ||||
{ | { | ||||
$user = $this->getTestUser('wallets-controller@kolabnow.com', ['password' => 'simple123']); | $user = $this->getTestUser('wallets-controller@kolabnow.com', ['password' => 'simple123']); | ||||
// Log out John and log in the test user | // Log out John and log in the test user | ||||
$this->browse(function (Browser $browser) { | $this->browse(function (Browser $browser) { | ||||
Show All 12 Lines | public function testHistory(): void | ||||
$transactions = array_reverse($transactions); | $transactions = array_reverse($transactions); | ||||
$pages = array_chunk($transactions, 10 /* page size*/); | $pages = array_chunk($transactions, 10 /* page size*/); | ||||
$this->browse(function (Browser $browser) use ($pages, $wallet) { | $this->browse(function (Browser $browser) use ($pages, $wallet) { | ||||
$browser->on(new Dashboard()) | $browser->on(new Dashboard()) | ||||
->click('@links .link-wallet') | ->click('@links .link-wallet') | ||||
->on(new WalletPage()) | ->on(new WalletPage()) | ||||
->assertSeeIn('@nav #tab-history', 'History') | ->assertSeeIn('@nav #tab-history', 'History') | ||||
->click('@nav #tab-history') | |||||
->with('@history-tab', function (Browser $browser) use ($pages, $wallet) { | ->with('@history-tab', function (Browser $browser) use ($pages, $wallet) { | ||||
$browser->assertElementsCount('table tbody tr', 10) | $browser->waitUntilMissing('.app-loader') | ||||
->assertElementsCount('table tbody tr', 10) | |||||
->assertMissing('table td.email') | ->assertMissing('table td.email') | ||||
->assertSeeIn('#transactions-loader button', 'Load more'); | ->assertSeeIn('#transactions-loader button', 'Load more'); | ||||
foreach ($pages[0] as $idx => $transaction) { | foreach ($pages[0] as $idx => $transaction) { | ||||
$selector = 'table tbody tr:nth-child(' . ($idx + 1) . ')'; | $selector = 'table tbody tr:nth-child(' . ($idx + 1) . ')'; | ||||
$priceStyle = $transaction->type == Transaction::WALLET_AWARD ? 'text-success' : 'text-danger'; | $priceStyle = $transaction->type == Transaction::WALLET_AWARD ? 'text-success' : 'text-danger'; | ||||
$browser->assertSeeIn("$selector td.description", $transaction->shortDescription()) | $browser->assertSeeIn("$selector td.description", $transaction->shortDescription()) | ||||
->assertMissing("$selector td.selection button") | ->assertMissing("$selector td.selection button") | ||||
Show All 32 Lines |