fix: env vars handled via conf file, add: functional tests

This commit is contained in:
Michael 2024-08-17 18:06:06 +02:00
parent 3d08b14f11
commit 21c4330f16
39 changed files with 416 additions and 172 deletions

View file

@ -2,40 +2,132 @@
namespace Tests;
use App\Http\Controllers\GiteaApiController\GiteaExport;
use Tests\TestCase;
use Laravel\Lumen\Testing\DatabaseMigrations;
use Laravel\Lumen\Testing\DatabaseTransactions;
class CsvGenerationTest extends TestCase
{
public function test_export_contains_correct_data()
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';
}
$res = $this->post('/backend', array(
'organization' => 'GruppoCO',
'password' => getenv('APP_PASSWORD')
));
$res->assertResponseOk();
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);
$beginningDateYear = '2024' ;
$beginningDateMonth = '04';
$endDateYear = '2024';
$endDateMonth = '05';
$issueType = 'closed';
$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);
}
$generatedCsv = $this->post('/export', array(
'from_year' => $beginningDateYear,
'from_month' => $beginningDateMonth,
'to_year' => $endDateYear,
'to_month' => $endDateMonth,
'issues_type' => $issueType,
));
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;
}
$res->assertResponseOK();
//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);
}
}
$generatedCsvHash = md5_file($generatedCsv);
$testFileHash = '07078b8e24768453b1e45236b13d347d';
//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);
}
}
assertEquals($generatedCsvHash, $testFileHash);
//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()
{
$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);
$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);
//Check if total sum is the same both on the csv and the env var
$this->test_total_cost($parsed_generated_csv);
//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]);
//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);
//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);
//TODO RECALCULATE EACH AGENT COST WITH TEST PRICE
}
}