1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/tests/Unit/Chart/ChartCurrencyTest.php

199 lines
5.3 KiB
PHP
Raw Normal View History

2022-01-20 10:09:08 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
2022-01-20 10:09:08 +01:00
*/
2022-01-20 10:09:08 +01:00
namespace Tests\Unit\Chart;
use App\DataMapper\ClientSettings;
use App\Models\Client;
2022-01-21 02:00:32 +01:00
use App\Models\Invoice;
2022-01-20 10:09:08 +01:00
use App\Services\Chart\ChartService;
2022-01-21 02:00:32 +01:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2022-01-20 10:09:08 +01:00
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers App\Services\Chart\ChartService
2022-01-20 10:09:08 +01:00
*/
class ChartCurrencyTest extends TestCase
{
use MockAccountData;
2022-01-21 02:00:32 +01:00
use DatabaseTransactions;
2022-01-20 10:09:08 +01:00
protected function setUp() :void
2022-01-20 10:09:08 +01:00
{
parent::setUp();
$this->makeTestData();
}
2022-01-21 02:00:32 +01:00
public function testRevenueValues()
{
Invoice::factory()->create([
'client_id' => $this->client->id,
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'paid_to_date' => 100,
'status_id' => 4,
'date' => now(),
'due_date'=> now(),
'number' => 'db_record',
2022-01-21 02:00:32 +01:00
]);
$this->assertDatabaseHas('invoices', ['number' => 'db_record']);
2022-01-21 02:00:32 +01:00
$cs = new ChartService($this->company);
2022-01-21 02:25:13 +01:00
// nlog($cs->getRevenueQuery(now()->subDays(20)->format('Y-m-d'), now()->addDays(100)->format('Y-m-d')));
2022-01-21 02:00:32 +01:00
$data = [
'start_date' => now()->subDays(30)->format('Y-m-d'),
'end_date' => now()->addDays(100)->format('Y-m-d'),
2022-01-21 02:00:32 +01:00
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/charts/totals', $data);
2022-01-21 02:00:32 +01:00
$response->assertStatus(200);
}
public function testgetCurrencyCodes()
{
$settings = ClientSettings::defaults();
$settings->currency_id = '1'; //USD
Client::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'settings' => $settings,
]);
$settings = ClientSettings::defaults();
$settings->currency_id = '2'; //GBP
Client::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'settings' => $settings,
]);
$cs = new ChartService($this->company);
2022-01-21 00:10:18 +01:00
$this->assertTrue(is_array($cs->getCurrencyCodes()));
2022-01-21 00:10:18 +01:00
$this->assertTrue(in_array('GBP', $cs->getCurrencyCodes()));
$this->assertTrue(in_array('USD', $cs->getCurrencyCodes()));
$this->assertFalse(in_array('AUD', $cs->getCurrencyCodes()));
}
2022-01-21 00:10:18 +01:00
2022-01-21 02:00:32 +01:00
public function testGetChartTotalsApi()
{
$data = [
'start_date' => now()->subDays(30)->format('Y-m-d'),
'end_date' => now()->format('Y-m-d'),
2022-01-21 02:00:32 +01:00
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/charts/totals', $data);
2022-01-21 02:00:32 +01:00
$response->assertStatus(200);
}
public function testClientServiceDataSetBuild()
{
$haystack = [
[
'currency_id' => null,
'amount' => 10,
],
[
'currency_id' => 1,
'amount' => 11,
],
[
'currency_id' => 2,
'amount' => 12,
],
[
'currency_id' => 3,
'amount' => 13,
],
];
$cs = new ChartService($this->company);
2022-01-21 02:25:13 +01:00
// nlog($cs->totals(now()->subYears(10), now()));
$this->assertTrue(is_array($cs->totals(now()->subYears(10), now())));
}
2022-01-21 00:10:18 +01:00
/* coalesces the company currency with the null currencies */
public function testFindNullValueinArray()
{
$haystack = [
[
'currency_id' => null,
'amount' => 10,
],
[
'currency_id' => 1,
'amount' => 11,
],
[
'currency_id' => 2,
'amount' => 12,
],
[
'currency_id' => 3,
'amount' => 13,
],
];
2022-01-21 00:10:18 +01:00
$company_currency_id = 1;
2022-01-21 00:10:18 +01:00
$c_key = array_search($company_currency_id, array_column($haystack, 'currency_id'));
2022-01-21 00:10:18 +01:00
$this->assertNotEquals($c_key, 2);
$this->assertEquals($c_key, 1);
2022-01-21 00:10:18 +01:00
$key = array_search(null, array_column($haystack, 'currency_id'));
2022-01-21 00:10:18 +01:00
$this->assertNotEquals($key, 39);
$this->assertEquals($key, 0);
2022-01-21 00:10:18 +01:00
$null_currency_amount = $haystack[$key]['amount'];
2022-01-21 00:10:18 +01:00
unset($haystack[$key]);
2022-01-21 00:10:18 +01:00
$haystack[$c_key]['amount'] += $null_currency_amount;
2022-01-21 00:10:18 +01:00
$this->assertEquals($haystack[$c_key]['amount'], 21);
}
2022-01-20 10:09:08 +01:00
public function testCollectionMerging()
{
$currencies = collect([1, 2, 3, 4, 5, 6]);
2022-01-20 10:09:08 +01:00
$expense_currencies = collect([4, 5, 6, 7, 8]);
2022-01-20 10:09:08 +01:00
$currencies = $currencies->merge($expense_currencies);
$this->assertEquals($currencies->count(), 11);
$currencies = $currencies->unique();
$this->assertEquals($currencies->count(), 8);
}
}