Files
FOSSBilling/tests-legacy/modules/Stats/ServiceTest.php
T
ac00bf1bda Refactor dashboard and enhance charts with transaction support (#3581)
* Support optional `transaction.ipn_hash` with repair

* Refactor dashboard tables and product sales card

* Refactor dashboard client/avatar and activity tables

* Replace ApexCharts with Chart.js sparklines

* Preserve decimal income and enhance chart tooltips

* Normalize admin navigation URIs

* Fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* refactor(Stats): split _genFlotArray into typed _genFlotArrayInt and _genFlotArrayFloat helpers

Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/fb8520de-2038-4e33-af3e-3520d999c939

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

* fix(Invoice): use processTransaction return value in markAsPaidByAdmin instead of returning true unconditionally

Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/fc7131ac-81aa-416b-af57-7d65d9bd20c3

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

* fix(Invoice): remove runtime DDL migration from ServiceTransaction; simplify supportsTransactionIpnHash to schema-check only

Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/e4b07e3f-7660-4fba-84c0-0105d6b2a318

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
2026-05-18 08:57:37 +01:00

364 lines
11 KiB
PHP

<?php
declare(strict_types=1);
namespace Box\Mod\Stats;
use PHPUnit\Framework\Attributes\Group;
#[Group('Core')]
final class ServiceTest extends \BBTestCase
{
protected ?Service $service;
public function setUp(): void
{
$this->service = new Service();
}
public function testGetOrdersStatuses(): void
{
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('counter')
->willReturn([]);
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$this->service->setDi($di);
$result = $this->service->getOrdersStatuses([]);
$this->assertIsArray($result);
}
public function testGetProductSummary(): void
{
$data = [];
$resultMock = $this->createMock(\Doctrine\DBAL\Result::class);
$resultMock->expects($this->atLeastOnce())
->method('fetchAllAssociative')
->willReturn([]);
$dbalMock = $this->createMock(\Doctrine\DBAL\Connection::class);
$dbalMock->expects($this->atLeastOnce())
->method('executeQuery')
->willReturn($resultMock);
$di = $this->getDi();
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$result = $this->service->getProductSummary($data);
$this->assertIsArray($result);
}
public function testGetSummary(): void
{
$resultMock = $this->createMock(\Doctrine\DBAL\Result::class);
$resultMock->expects($this->atLeastOnce())
->method('fetchOne')
->willReturn(null);
$dbalMock = $this->createMock(\Doctrine\DBAL\Connection::class);
$dbalMock->expects($this->atLeastOnce())
->method('executeQuery')
->willReturn($resultMock);
$di = $this->getDi();
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$expected = [
'clients_total' => null,
'clients_today' => null,
'clients_yesterday' => null,
'clients_this_month' => null,
'clients_last_month' => null,
'orders_total' => null,
'orders_today' => null,
'orders_yesterday' => null,
'orders_this_month' => null,
'orders_last_month' => null,
'invoices_total' => null,
'invoices_today' => null,
'invoices_yesterday' => null,
'invoices_this_month' => null,
'invoices_last_month' => null,
'tickets_total' => null,
'tickets_today' => null,
'tickets_yesterday' => null,
'tickets_this_month' => null,
'tickets_last_month' => null,
];
$result = $this->service->getSummary();
$this->assertIsArray($result);
$this->assertEquals($expected, $result);
}
public function testGetSummaryIncome(): void
{
$resultMock = $this->createMock(\Doctrine\DBAL\Result::class);
$resultMock->expects($this->atLeastOnce())
->method('fetchOne')
->willReturn(null);
$dbalMock = $this->createMock(\Doctrine\DBAL\Connection::class);
$dbalMock->expects($this->atLeastOnce())
->method('executeQuery')
->willReturn($resultMock);
$di = $this->getDi();
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$expected = [
'total' => null,
'today' => null,
'yesterday' => null,
'this_month' => null,
'last_month' => null,
];
$result = $this->service->getSummaryIncome();
$this->assertIsArray($result);
$this->assertEquals($expected, $result);
}
public function testGetProductSales(): void
{
$res = ['testProduct' => 1];
$resultMock = $this->createMock(\Doctrine\DBAL\Result::class);
$resultMock->expects($this->atLeastOnce())
->method('fetchAllKeyValue')
->willReturn($res);
$dbalMock = $this->createMock(\Doctrine\DBAL\Connection::class);
$dbalMock->expects($this->atLeastOnce())
->method('executeQuery')
->willReturn($resultMock);
$di = $this->getDi();
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$data = [
'date_from' => 'yesterday',
'date_to' => 'now',
];
$result = $this->service->getProductSales($data);
$this->assertIsArray($result);
}
public function testIncomeAndRefundStats(): void
{
$res = [
[
'refund' => 0,
'income' => 0,
],
];
$resultMock = $this->createMock(\Doctrine\DBAL\Result::class);
$resultMock->expects($this->atLeastOnce())
->method('fetchAllAssociative')
->willReturn($res);
$dbalMock = $this->createMock(\Doctrine\DBAL\Connection::class);
$dbalMock->expects($this->atLeastOnce())
->method('executeQuery')
->willReturn($resultMock);
$di = $this->getDi();
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$result = $this->service->incomeAndRefundStats([]);
$this->assertIsArray($result);
$this->assertEquals($res[0], $result);
}
public function testGetRefunds(): void
{
$resultMock = $this->createMock(\Doctrine\DBAL\Result::class);
$resultMock->expects($this->atLeastOnce())
->method('fetchAllKeyValue')
->willReturn([]);
$dbalMock = $this->createMock(\Doctrine\DBAL\Connection::class);
$dbalMock->expects($this->atLeastOnce())
->method('executeQuery')
->willReturn($resultMock);
$di = $this->getDi();
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$data = [
'date_from' => 'yesterday',
'date_to' => 'now',
];
$result = $this->service->getRefunds($data);
$this->assertIsArray($result);
}
public function testGetIncome(): void
{
$resultMock = $this->createMock(\Doctrine\DBAL\Result::class);
$resultMock->expects($this->atLeastOnce())
->method('fetchAllKeyValue')
->willReturn([]);
$dbalMock = $this->createMock(\Doctrine\DBAL\Connection::class);
$dbalMock->expects($this->atLeastOnce())
->method('executeQuery')
->willReturn($resultMock);
$di = $this->getDi();
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$data = [
'date_from' => 'yesterday',
'date_to' => 'now',
];
$result = $this->service->getIncome($data);
$this->assertIsArray($result);
}
public function testGetIncomePreservesDecimals(): void
{
$resultMock = $this->createMock(\Doctrine\DBAL\Result::class);
$resultMock->expects($this->once())
->method('fetchAllKeyValue')
->willReturn([
'2026-05-14' => '11.99',
'2026-05-15' => '0.50',
]);
$dbalMock = $this->createMock(\Doctrine\DBAL\Connection::class);
$dbalMock->expects($this->once())
->method('executeQuery')
->willReturn($resultMock);
$di = $this->getDi();
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$result = $this->service->getIncome([
'date_from' => '2026-05-13',
'date_to' => '2026-05-16',
]);
$this->assertSame([
[strtotime('2026-05-14') * 1000, 11.99],
[strtotime('2026-05-15') * 1000, 0.5],
], $result);
}
public function testGetClientCountries(): void
{
$resultMock = $this->createMock(\Doctrine\DBAL\Result::class);
$resultMock->expects($this->atLeastOnce())
->method('fetchAllKeyValue')
->willReturn([]);
$dbalMock = $this->createMock(\Doctrine\DBAL\Connection::class);
$dbalMock->expects($this->atLeastOnce())
->method('executeQuery')
->willReturn($resultMock);
$di = $this->getDi();
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$result = $this->service->getClientCountries([]);
$this->assertIsArray($result);
}
public function testGetSalesByCountry(): void
{
$resultMock = $this->createMock(\Doctrine\DBAL\Result::class);
$resultMock->expects($this->atLeastOnce())
->method('fetchAllKeyValue')
->willReturn([]);
$dbalMock = $this->createMock(\Doctrine\DBAL\Connection::class);
$dbalMock->expects($this->atLeastOnce())
->method('executeQuery')
->willReturn($resultMock);
$di = $this->getDi();
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$result = $this->service->getSalesByCountry([]);
$this->assertIsArray($result);
}
public function testGetTableStatsKeepsCountsAsIntegers(): void
{
$resultMock = $this->createMock(\Doctrine\DBAL\Result::class);
$resultMock->expects($this->once())
->method('fetchAllKeyValue')
->willReturn([
'2026-05-14' => '3',
'2026-05-15' => '1',
]);
$dbalMock = $this->createMock(\Doctrine\DBAL\Connection::class);
$dbalMock->expects($this->once())
->method('executeQuery')
->willReturn($resultMock);
$di = $this->getDi();
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$result = $this->service->getTableStats('invoice', [
'date_from' => '2026-05-13',
'date_to' => '2026-05-16',
]);
$this->assertSame([
[strtotime('2026-05-14') * 1000, 3],
[strtotime('2026-05-15') * 1000, 1],
], $result);
}
public function testGetTableStats(): void
{
$resultMock = $this->createMock(\Doctrine\DBAL\Result::class);
$resultMock->expects($this->atLeastOnce())
->method('fetchAllKeyValue')
->willReturn([]);
$dbalMock = $this->createMock(\Doctrine\DBAL\Connection::class);
$dbalMock->expects($this->atLeastOnce())
->method('executeQuery')
->willReturn($resultMock);
$di = $this->getDi();
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$data = [
'date_from' => 'yesterday',
'date_to' => 'now',
];
$result = $this->service->getTableStats('client', $data);
$this->assertIsArray($result);
}
}