Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/Admin/StatsTest.php
<?php | <?php | ||||
namespace Tests\Feature\Controller\Admin; | namespace Tests\Feature\Controller\Admin; | ||||
use App\Payment; | |||||
use App\Providers\PaymentProvider; | |||||
use Tests\TestCase; | use Tests\TestCase; | ||||
class StatsTest extends TestCase | class StatsTest extends TestCase | ||||
{ | { | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
self::useAdminUrl(); | self::useAdminUrl(); | ||||
Payment::truncate(); | |||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
Payment::truncate(); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test charts (GET /api/v4/stats/chart/<chart>) | * Test charts (GET /api/v4/stats/chart/<chart>) | ||||
*/ | */ | ||||
public function testChart(): void | public function testChart(): void | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | public function testChart(): void | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('All Users - last year', $json['title']); | $this->assertSame('All Users - last year', $json['title']); | ||||
$this->assertCount(54, $json['data']['labels']); | $this->assertCount(54, $json['data']['labels']); | ||||
$this->assertCount(1, $json['data']['datasets']); | $this->assertCount(1, $json['data']['datasets']); | ||||
} | } | ||||
/** | |||||
* Test income chart currency handling | |||||
*/ | |||||
public function testChartIncomeCurrency(): void | |||||
{ | |||||
$admin = $this->getTestUser('jeroen@jeroen.jeroen'); | |||||
$john = $this->getTestUser('john@kolab.org'); | |||||
$user = $this->getTestUser('test-stats@' . \config('app.domain')); | |||||
$wallet = $user->wallets()->first(); | |||||
$wallet->currency = 'EUR'; | |||||
$wallet->save(); | |||||
$johns_wallet = $john->wallets()->first(); | |||||
// Create some test payments | |||||
Payment::create([ | |||||
'id' => 'test1', | |||||
'description' => '', | |||||
'status' => PaymentProvider::STATUS_PAID, | |||||
'amount' => 1000, // EUR | |||||
'type' => PaymentProvider::TYPE_ONEOFF, | |||||
'wallet_id' => $wallet->id, | |||||
'provider' => 'mollie', | |||||
'currency' => 'EUR', | |||||
'currency_amount' => 1000, | |||||
]); | |||||
Payment::create([ | |||||
'id' => 'test2', | |||||
'description' => '', | |||||
'status' => PaymentProvider::STATUS_PAID, | |||||
'amount' => 2000, // EUR | |||||
'type' => PaymentProvider::TYPE_RECURRING, | |||||
'wallet_id' => $wallet->id, | |||||
'provider' => 'mollie', | |||||
'currency' => 'EUR', | |||||
'currency_amount' => 2000, | |||||
]); | |||||
Payment::create([ | |||||
'id' => 'test3', | |||||
'description' => '', | |||||
'status' => PaymentProvider::STATUS_PAID, | |||||
'amount' => 3000, // CHF | |||||
'type' => PaymentProvider::TYPE_ONEOFF, | |||||
'wallet_id' => $johns_wallet->id, | |||||
'provider' => 'mollie', | |||||
'currency' => 'EUR', | |||||
'currency_amount' => 2800, | |||||
]); | |||||
Payment::create([ | |||||
'id' => 'test4', | |||||
'description' => '', | |||||
'status' => PaymentProvider::STATUS_PAID, | |||||
'amount' => 4000, // CHF | |||||
'type' => PaymentProvider::TYPE_RECURRING, | |||||
'wallet_id' => $johns_wallet->id, | |||||
'provider' => 'mollie', | |||||
'currency' => 'CHF', | |||||
'currency_amount' => 4000, | |||||
]); | |||||
Payment::create([ | |||||
'id' => 'test5', | |||||
'description' => '', | |||||
'status' => PaymentProvider::STATUS_OPEN, | |||||
'amount' => 5000, // CHF | |||||
'type' => PaymentProvider::TYPE_ONEOFF, | |||||
'wallet_id' => $johns_wallet->id, | |||||
'provider' => 'mollie', | |||||
'currency' => 'CHF', | |||||
'currency_amount' => 5000, | |||||
]); | |||||
Payment::create([ | |||||
'id' => 'test6', | |||||
'description' => '', | |||||
'status' => PaymentProvider::STATUS_FAILED, | |||||
'amount' => 6000, // CHF | |||||
'type' => PaymentProvider::TYPE_ONEOFF, | |||||
'wallet_id' => $johns_wallet->id, | |||||
'provider' => 'mollie', | |||||
'currency' => 'CHF', | |||||
'currency_amount' => 6000, | |||||
]); | |||||
// 'income' chart | |||||
$response = $this->actingAs($admin)->get("api/v4/stats/chart/income"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame('Income in CHF - last 8 weeks', $json['title']); | |||||
$this->assertSame('bar', $json['type']); | |||||
$this->assertCount(8, $json['data']['labels']); | |||||
$this->assertSame(date('Y-W'), $json['data']['labels'][7]); | |||||
// 7000 CHF + 3000 EUR = | |||||
$expected = 7000 + intval(round(3000 * \App\Utils::exchangeRate('EUR', 'CHF'))); | |||||
$this->assertCount(1, $json['data']['datasets']); | |||||
$this->assertSame($expected / 100, $json['data']['datasets'][0]['values'][7]); | |||||
} | |||||
} | } |