85 lines
2.4 KiB
PHP
85 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Service;
|
|
|
|
use App\Entity\Accounting\Transaction;
|
|
use App\Entity\Money;
|
|
use App\Entity\Tipjar;
|
|
use App\Service\AccountingService;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
|
use Zenstruck\Foundry\Test\ResetDatabase;
|
|
|
|
class AccountingServiceTest extends KernelTestCase
|
|
{
|
|
use ResetDatabase;
|
|
|
|
private AccountingService $accountingService;
|
|
private EntityManagerInterface $entityManager;
|
|
|
|
public function setUp(): void
|
|
{
|
|
self::bootKernel();
|
|
|
|
$this->accountingService = static::getContainer()->get(AccountingService::class);
|
|
$this->entityManager = static::getContainer()->get(EntityManagerInterface::class);
|
|
}
|
|
|
|
public function getBalancedAccountings(): array
|
|
{
|
|
$tipjars = [];
|
|
|
|
for ($i = 0; $i < 10; ++$i) {
|
|
$tipjar = new Tipjar();
|
|
$tipjar->setName(\sprintf('TEST_TIPJAR_%d', $i));
|
|
|
|
$this->entityManager->persist($tipjar);
|
|
|
|
$tipjars[] = $tipjar;
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
|
|
for ($i = 0; $i < \count($tipjars) * 5; ++$i) {
|
|
$origin = $i % 2 ? $tipjars[\random_int(0, 5)] : $tipjars[\random_int(5, 9)];
|
|
$target = $i % 2 ? $tipjars[\random_int(5, 9)] : $tipjars[\random_int(0, 5)];
|
|
|
|
$trx = new Transaction();
|
|
$trx->setMoney(new Money(random_int(100, 999), 'EUR'));
|
|
$trx->setOrigin($origin->getAccounting());
|
|
$trx->setTarget($target->getAccounting());
|
|
|
|
$this->entityManager->persist($trx);
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
|
|
return \array_map(function (Tipjar $tipjar) {
|
|
return $tipjar->getAccounting();
|
|
}, $tipjars);
|
|
}
|
|
|
|
public function testBalanceSerieAggregatesData()
|
|
{
|
|
$accountings = $this->getBalancedAccountings();
|
|
|
|
$period = new \DatePeriod(
|
|
(new \DateTime('now'))->sub(new \DateInterval('PT1H')),
|
|
new \DateInterval('PT10M'),
|
|
new \DateTime('now')
|
|
);
|
|
|
|
foreach ($accountings as $accounting) {
|
|
$series = $this->accountingService->calcBalanceSeries($accounting, $period);
|
|
|
|
$this->assertCount(6, $series);
|
|
|
|
foreach ($series as $point) {
|
|
$this->assertInstanceOf(Money::class, $point);
|
|
}
|
|
|
|
$this->assertNotEquals(0, end($series)->amount);
|
|
}
|
|
}
|
|
}
|