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
}
}

View file

@ -3,18 +3,18 @@
namespace Tests\Http\Controllers\CsvController;
use App\Http\Controllers\CsvController\CsvController;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class CsvControllerTest extends TestCase
{
public function testCreate_csv()
{
}
public function testCreate_columns()
{
}
// public function testCreate_csv()
// {
//
// }
//
// public function testCreate_columns()
// {
//
// }
}

View file

@ -4,13 +4,15 @@ namespace Tests\Http\Controllers\CsvController\CsvCostCalc;
use App\Http\Controllers\CsvController\CsvCostCalc\CsvAgentCost;
use App\Http\Controllers\CsvController\CsvCostCalc\CsvMinuteCostCalc;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
use RuntimeException;
class CsvAgentCostTest extends TestCase
{
protected function setUp(): void
{
protected function setUp(): void
{parent::setUp();
parent::setUp();
$this->csv_agent_cost = new CsvAgentCost();
$this->mockCsvMinuteCostCalc = $this->createMock(CsvMinuteCostCalc::class);
$this->csv_agent_cost->CsvMinuteCostCalc = $this->mockCsvMinuteCostCalc;

View file

@ -1,25 +0,0 @@
<?php
namespace Tests\Http\Controllers\CsvController\CsvCostCalc;
use App\Http\Controllers\CsvController\CsvCostCalc\CsvCostCalc;
use PHPUnit\Framework\TestCase;
class CsvCostCalcTest extends TestCase
{
public function testTotal_time_cost()
{
}
public function testAgent_cost_calc()
{
}
public function testSum_costs()
{
}
}

View file

@ -3,7 +3,7 @@
namespace Tests\Http\Controllers\CsvController\CsvCostCalc;
use App\Http\Controllers\CsvController\CsvCostCalc\CsvMinuteCostCalc;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class CsvMinuteCostCalcTest extends TestCase
{
@ -12,7 +12,7 @@ class CsvMinuteCostCalcTest extends TestCase
protected function setUp(): void
{
parent::setUp();
$this->csv_minute_cost_calc = new CsvMinuteCostCalc(); // Replace CsvMinuteCostCalc with the actual class name
$this->csv_minute_cost_calc->partner_organization = 'PartnerOrg'; // Set up the partner organization
$this->csv_minute_cost_calc->price_partner = [

View file

@ -4,13 +4,14 @@ namespace Tests\Http\Controllers\CsvController\CsvCostCalc;
use App\Http\Controllers\CsvController\CsvCostCalc\CsvMinuteCostCalc;
use App\Http\Controllers\CsvController\CsvCostCalc\CsvTotalCostCalc;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class CsvSumTotalsCalcTest extends TestCase
{
protected function setUp(): void
{
protected function setUp(): void
{parent::setUp();
$this->CsvTotalCostCalc = new CsvTotalCostCalc();
$this->mockCsvMinuteCostCalc = $this->createMock(CsvMinuteCostCalc::class);
$this->CsvTotalCostCalc->CsvMinuteCostCalc = $this->mockCsvMinuteCostCalc;

View file

@ -4,14 +4,15 @@ namespace Tests\Http\Controllers\CsvController\CsvCostCalc;
use App\Http\Controllers\CsvController\CsvCostCalc\CsvMinuteCostCalc;
use App\Http\Controllers\CsvController\CsvCostCalc\CsvTotalCostCalc;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class CsvTotalCostCalcTest extends TestCase
{
protected function setUp(): void
{
protected function setUp(): void
{parent::setUp();
$this->CsvTotalCostCalc = new CsvTotalCostCalc();
$this->mockCsvMinuteCostCalc = $this->createMock(CsvMinuteCostCalc::class);
$this->CsvTotalCostCalc->CsvMinuteCostCalc = $this->mockCsvMinuteCostCalc;

View file

@ -6,12 +6,13 @@ use App\Http\Controllers\CsvController\CsvCostCalc\CsvAgentCost;
use App\Http\Controllers\CsvController\CsvCostCalc\CsvCostCalc;
use App\Http\Controllers\CsvController\CsvCostCalc\CsvMinuteCostCalc;
use App\Http\Controllers\CsvController\CsvDataHandling;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class CsvDataHandlingTest extends TestCase
{
protected function setUp(): void
{
protected function setUp(): void
{parent::setUp();
$this->mockCsvMinuteCostCalc = $this->createMock(CsvMinuteCostCalc::class);
$this->csv_data_handling = new CsvDataHandling();
$this->csv_data_handling->csv_cost_calc->CsvTotalCostCalc->CsvMinuteCostCalc = $this->mockCsvMinuteCostCalc;

View file

@ -3,13 +3,14 @@
namespace Tests\Http\Controllers\CsvController;
use App\Http\Controllers\CsvController\CsvDataInit;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class CsvDataInitTest extends TestCase
{
protected function setUp(): void
{
protected function setUp(): void
{parent::setUp();
$this->csv_data_init = new CsvDataInit();
}

View file

@ -3,7 +3,7 @@
namespace Tests\Http\Controllers\GiteaApiController;
use App\Http\Controllers\GiteaApiController\GiteaApiController;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class GiteaApiControllerTest extends TestCase
{

View file

@ -3,7 +3,7 @@
namespace Tests\Http\Controllers\GiteaApiController;
use App\Http\Controllers\GiteaApiController\GiteaExport;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class GiteaExportTest extends TestCase
{

View file

@ -3,7 +3,7 @@
namespace Tests\Http\Controllers\GiteaApiController;
use App\Http\Controllers\GiteaApiController\GiteaFetch;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class GiteaFetchTest extends TestCase
{

View file

@ -4,13 +4,14 @@ namespace Tests\Controllers;
use App\Http\Controllers\IssueValidationController;
use Exception;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class IssueValidationControllerTest extends TestCase
{
protected function setUp(): void
protected function setUp(): void
{
parent::setUp();
$this->issue_validation_controller = new IssueValidationController();
}
public function testValidDateConversion()

View file

@ -3,7 +3,7 @@
namespace Tests\Http\Controllers\ThirdPartyServices;
use App\Http\Controllers\ThirdPartyServices\EmailService;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class EmailServiceTest extends TestCase
{

View file

@ -3,7 +3,7 @@
namespace Tests\Http\Controllers\ThirdPartyServices;
use App\Http\Controllers\ThirdPartyServices\NextcloudService;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class NextcloudServiceTest extends TestCase
{

View file

@ -3,7 +3,7 @@
namespace Tests\Http\Controllers\ThirdPartyServices;
use App\Http\Controllers\ThirdPartyServices\OpenProjectService;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class OpenProjectServiceTest extends TestCase
{

View file

@ -3,7 +3,7 @@
namespace Tests\Http\Controllers\ThirdPartyServices;
use App\Http\Controllers\ThirdPartyServices\ThirdPartyServices;
use PHPUnit\Framework\TestCase;
use Tests\TestCase;
class ThirdPartyServicesTest extends TestCase
{

View file

@ -36,7 +36,7 @@ class PagesTest extends TestCase
{
$res = $this->post('/backend', array(
'organization' => 'GruppoCO',
'password' => getenv('APP_PASSWORD')
'password' => config('app.app_password')
));
$res->assertResponseOk();

View file

@ -7,7 +7,7 @@ class UnitTest extends TestCase
public function test_export_endpoint_works_for_logged_in_users()
{
$res = $this->post('/export', array(
'token' => getenv('GITEA_TOKEN'),
'token' => config('app.gitea_token'),
'from_month' => '10',
'from_year' => '2023',
'to_month' => '12',