gitea_issues_exporter/tests/CsvGenerationTest.php

134 lines
6.1 KiB
PHP
Raw Normal View History

2024-07-22 14:17:00 +02:00
<?php
namespace Tests;
use App\Http\Controllers\GiteaApiController\GiteaExport;
2024-07-22 14:17:00 +02:00
use Tests\TestCase;
class CsvGenerationTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->test_date_year = config('app.test_export_date_yyyy');
$this->test_date_month = config('app.test_export_date_mm');
$this->test_export_total_cost = config('app.test_export_total_cost');
$this->test_export_total_cost_minus_percentage = config('app.test_export_total_cost_minus_percentage');
$this->test_export_agents_cost = json_decode(config('app.test_export_agents_cost'));
$this->test_export_agent_percentage_deducted = json_decode(config('app.test_export_agent_percentage_deducted'));
$this->test_file_hash_without_agents = config('app.test_file_hash_without_agents');
$this->test_file_hash_with_agents = config('app.test_file_hash_with_agents');
$this->issueType = 'closed';
}
private function test_total_cost($parsed_generated_csv){
$total_cost_index = array_search('Costo totale',$parsed_generated_csv[0]);
$total_cost_column = array_column($parsed_generated_csv,$total_cost_index);
array_shift($total_cost_column);
array_pop($total_cost_column);
$total_cost_sum = 0;
foreach($total_cost_column as $row){
$total_cost_sum += (float) $row;
}
$this->assertEquals($this->test_export_total_cost, $total_cost_sum);
}
public function test_export_values_without_agents_cost()
{
$gitea_export = new GiteaExport();
$export_date =$this->test_date_year .'-'. $this->test_date_month .'-';
$gitea_export->export_issues($export_date . '01', $export_date . '31', ['state' => 'closed']);
$generated_csv_string = ob_get_contents();
$generated_csv = explode("\n", $generated_csv_string);
$parsed_generated_csv = array_map(function($row) {
return str_getcsv($row);
}, $generated_csv);
$generated_csv_hash = md5($generated_csv_string);
$this->assertEquals($generated_csv_hash, $this->test_file_hash_without_agents);
//Check if total sum is the same both on the csv and the env var
$this->test_total_cost($parsed_generated_csv);
}
function extract_agents_column($parsed_generated_csv, $test_export_agents_cost){
foreach ($test_export_agents_cost as $agent_name => $agent_total_cost){
$agent_position = array_search($agent_name,$parsed_generated_csv[0]);
if(!$agent_position) break;
$agents_columns[$agent_name] = [
'agent_time' => array_column($parsed_generated_csv,$agent_position),
'agent_cost' => array_column($parsed_generated_csv,$agent_position+1),
];
array_shift($agents_columns[$agent_name]['agent_time']);
array_shift($agents_columns[$agent_name]['agent_cost']);
}
return $agents_columns;
}
//Check for each agent, if their total costs is the same as the env reference
private function test_single_agent_cost($agents_columns, $parsed_generated_csv_header){
foreach($agents_columns as $agent_name => $agent_column){
$agent_total = end($agent_column['agent_cost']);
$this->assertEquals($this->test_export_agents_cost->$agent_name, $agent_total);
}
}
//Check for each agent, if their cost column sum is the same as their env total cost reference
private function test_single_agent_total_sum($agents_columns){
foreach($agents_columns as $agent_name => $agent_column){
$agent_total = 0;
foreach(array_slice($agent_column['agent_cost'], 0, -1) as $agent_cost){
$agent_total += (float)$agent_cost;
}
$test = (string) $agent_total;
$this->assertEquals($this->test_export_agents_cost->$agent_name, $test);
}
}
//Check if the total sum - % to deduct is the same as the agents cost total sum
private function test_agents_total_cost_equals_total_sum_minus_percentage($agents_columns){
$agents_total_cost_calculated = 0;
foreach($agents_columns as $agent_name => $agent_column){
$agent_cost_voice = (float)end($agent_column['agent_cost']);
$agents_total_cost_calculated = $agents_total_cost_calculated + $agent_cost_voice;
}
$this->assertEquals($this->test_export_total_cost_minus_percentage, $agents_total_cost_calculated);
}
//Check if the total sum - % to deduct is the same as the agents cost total sum
public function test_export_values_with_agents_cost()
2024-07-22 14:17:00 +02:00
{
$calculate_agent_cost = true;
$gitea_export = new GiteaExport(
$calculate_agent_cost,
);
$export_date =$this->test_date_year .'-'. $this->test_date_month .'-';
$gitea_export->export_issues($export_date . '01', $export_date . '31', ['state' => 'closed']);
$generated_csv_string = ob_get_contents();
$generated_csv = explode("\n", $generated_csv_string);
$parsed_generated_csv = array_map(function($row) {
return str_getcsv($row);
}, $generated_csv);
2024-07-22 14:17:00 +02:00
$agents_columns = $this->extract_agents_column($parsed_generated_csv, $this->test_export_agents_cost);
//Check if hash of generated csv and env reference are the same
$this->assertEquals(md5($generated_csv_string), $this->test_file_hash_with_agents);
2024-07-22 14:17:00 +02:00
//Check if total sum is the same both on the csv and the env var
$this->test_total_cost($parsed_generated_csv);
2024-07-22 14:17:00 +02:00
//Check for each agent, if their total costs is the same as the env reference
$this->test_single_agent_cost($agents_columns,$parsed_generated_csv[0]);
2024-07-22 14:17:00 +02:00
//Check for each agent, if their cost column sum is the same as their env total cost reference
$this->test_single_agent_total_sum($agents_columns);
2024-07-22 14:17:00 +02:00
//Check if the total sum - % to deduct is the same as the agents cost total sum
$this->test_agents_total_cost_equals_total_sum_minus_percentage($agents_columns);
2024-07-22 14:17:00 +02:00
//TODO RECALCULATE EACH AGENT COST WITH TEST PRICE
2024-07-22 14:17:00 +02:00
}
}