add: more tests
This commit is contained in:
parent
ce7f620984
commit
fcd76cf130
|
@ -34,10 +34,9 @@ class CsvAgentCost
|
|||
|
||||
function calculate_agents_cost(array $array, array $agents_time, float $minute_cost)
|
||||
{
|
||||
$minute_cost = utils::round_up_to_two_decimals($minute_cost);
|
||||
foreach ($agents_time as $name => $agent_time) {
|
||||
//Identify agents involved in the issue
|
||||
$agent_cost = utils::round_up_to_two_decimals($agent_time / 60 * $minute_cost);
|
||||
$agent_cost = utils::round_up_to_two_decimals($agent_time * $minute_cost / 60);
|
||||
$array[$name] = gmdate('H:i:s', $agent_time);
|
||||
$array[$name . ' costo'] = $agent_cost;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,10 @@ namespace App\Http\Controllers\CsvController\CsvCostCalc;
|
|||
|
||||
class CsvCostCalc
|
||||
{
|
||||
/**
|
||||
* @var CsvMinuteCostCalc|(CsvMinuteCostCalc&object&\PHPUnit\Framework\MockObject\MockObject)|(CsvMinuteCostCalc&\PHPUnit\Framework\MockObject\MockObject)|(object&\PHPUnit\Framework\MockObject\MockObject)|\PHPUnit\Framework\MockObject\MockObject
|
||||
*/
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->internal_percentage_to_deduct = getenv('PRICE_INTERNAL_PERCENTAGE_TO_DEDUCT');
|
||||
|
|
|
@ -28,7 +28,7 @@ class CsvMinuteCostCalc
|
|||
|
||||
private function calculate_minute_cost(int $hourly_cost)
|
||||
{
|
||||
return utils::round_up_to_two_decimals($hourly_cost / 60);
|
||||
return $hourly_cost / 60;
|
||||
}
|
||||
|
||||
private function extract_string_from_label($label)
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\CsvController\CsvCostCalc;
|
||||
use function Utils\round_up_to_two_decimals;
|
||||
use App\Utils\utils;
|
||||
|
||||
class CsvTotalCostCalc
|
||||
{
|
||||
private $CsvMinuteCostCalc;
|
||||
public function __construct()
|
||||
{
|
||||
$this->CsvMinuteCostCalc = new CsvMinuteCostCalc();
|
||||
|
@ -15,7 +14,7 @@ class CsvTotalCostCalc
|
|||
function total_time_cost(array $array, int $total_time)
|
||||
{
|
||||
$minute_cost = $this->CsvMinuteCostCalc->select_correct_cost($array['Request By'], $array['Priority']);
|
||||
$total_cost = round_up_to_two_decimals($total_time / 60 * $minute_cost);
|
||||
$total_cost = utils::round_up_to_two_decimals($total_time * ($minute_cost / 60));
|
||||
$array['Tempo totale'] = gmdate('H:i:s', $total_time);
|
||||
$array['Costo totale'] = $total_cost;
|
||||
return $array;
|
||||
|
|
|
@ -15,7 +15,7 @@ class CsvDataHandling
|
|||
$this->csv_cost_calc = new CsvCostCalc();
|
||||
}
|
||||
|
||||
private function handle_agent_issue($agents_issue_time, $time)
|
||||
function handle_agent_issue($agents_issue_time, $time)
|
||||
{
|
||||
$time_agent = $time['user_name'];
|
||||
!array_key_exists($time_agent, $agents_issue_time) && $agents_issue_time[$time_agent] = 0;
|
||||
|
@ -23,7 +23,7 @@ class CsvDataHandling
|
|||
return $agents_issue_time;
|
||||
}
|
||||
|
||||
private function get_issue_total_time(array $issue_time)
|
||||
function get_issue_total_time(array $issue_time)
|
||||
{
|
||||
$total_issue_time = 0;
|
||||
$agents_issue_time = [];
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
<?php
|
||||
phpinfo();
|
||||
?>
|
|
@ -89,9 +89,9 @@ class CsvAgentCostTest extends TestCase
|
|||
// Ricordarsi che all'esecuzione della funzione, è gia stata dedotta la percentuale interna
|
||||
$expected = [
|
||||
'Agent A' => '02:00:00',
|
||||
'Agent A costo' => 619.21, // 120 minutes * 5.0
|
||||
'Agent A costo' => 618.67, // 120 minutes * 5.0
|
||||
'Agent B' => '03:00:00',
|
||||
'Agent B costo' => 928.8, // 180 minutes * 5.0
|
||||
'Agent B costo' => 928.0, // 180 minutes * 5.0
|
||||
];
|
||||
|
||||
$result = $this->csv_agent_cost->calculate_agents_cost($array, $agents_time, $minute_cost);
|
||||
|
@ -143,7 +143,7 @@ class CsvAgentCostTest extends TestCase
|
|||
|
||||
public function testSelectCorrectCostCalculatesCostCorrectly()
|
||||
{
|
||||
// percentuale dedotta impostata nel setup
|
||||
// percentuale dedotta impostata nel setup di questa classe
|
||||
// Costo orario per cliente con issue high, 75.5€/h
|
||||
$array = [
|
||||
'Progetto' => 'Progetto Test',
|
||||
|
@ -172,7 +172,7 @@ class CsvAgentCostTest extends TestCase
|
|||
$expected_result = $array;
|
||||
$expected_result = array_merge($array, [
|
||||
'agentTest' => '00:10:00',
|
||||
'agentTest costo' => 11.4
|
||||
'agentTest costo' => 11.34
|
||||
]);
|
||||
|
||||
// Call the method being tested
|
||||
|
|
|
@ -2,14 +2,44 @@
|
|||
|
||||
namespace Tests\Http\Controllers\CsvController\CsvCostCalc;
|
||||
|
||||
use App\Http\Controllers\CsvController\CsvCostCalc\CsvSumTotalsCalc;
|
||||
use App\Http\Controllers\CsvController\CsvCostCalc\CsvMinuteCostCalc;
|
||||
use App\Http\Controllers\CsvController\CsvCostCalc\CsvTotalCostCalc;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CsvSumTotalsCalcTest extends TestCase
|
||||
{
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->CsvTotalCostCalc = new CsvTotalCostCalc();
|
||||
$this->mockCsvMinuteCostCalc = $this->createMock(CsvMinuteCostCalc::class);
|
||||
$this->CsvTotalCostCalc->CsvMinuteCostCalc = $this->mockCsvMinuteCostCalc;
|
||||
}
|
||||
public function testSum_costs()
|
||||
{
|
||||
$array = [
|
||||
'Request By'=>'RequestBy/Client',
|
||||
'Priority'=>'Priority/High',
|
||||
];
|
||||
|
||||
$total_time = 3600;
|
||||
|
||||
//Valori presi dalla classe di test di CsvMinuteCostCalcTest
|
||||
$this->mockCsvMinuteCostCalc
|
||||
->expects($this->once())
|
||||
->method('select_correct_cost')
|
||||
->with('RequestBy/Client', 'Priority/High')
|
||||
->willReturn(1.26); // Mocked return value
|
||||
|
||||
$expected = [
|
||||
'Request By'=>'RequestBy/Client',
|
||||
'Priority'=>'Priority/High',
|
||||
'Tempo totale'=>'01:00:00',
|
||||
'Costo totale'=>75.61,
|
||||
];
|
||||
|
||||
$result = $this->CsvTotalCostCalc->total_time_cost($array, $total_time);
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,14 +2,45 @@
|
|||
|
||||
namespace Tests\Http\Controllers\CsvController\CsvCostCalc;
|
||||
|
||||
use App\Http\Controllers\CsvController\CsvCostCalc\CsvMinuteCostCalc;
|
||||
use App\Http\Controllers\CsvController\CsvCostCalc\CsvTotalCostCalc;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CsvTotalCostCalcTest extends TestCase
|
||||
{
|
||||
|
||||
public function testTotal_time_cost()
|
||||
{
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->CsvTotalCostCalc = new CsvTotalCostCalc();
|
||||
$this->mockCsvMinuteCostCalc = $this->createMock(CsvMinuteCostCalc::class);
|
||||
$this->CsvTotalCostCalc->CsvMinuteCostCalc = $this->mockCsvMinuteCostCalc;
|
||||
}
|
||||
public function testSum_costs()
|
||||
{
|
||||
$array = [
|
||||
'Request By'=>'RequestBy/Client',
|
||||
'Priority'=>'Priority/High',
|
||||
];
|
||||
|
||||
$total_time = 3600;
|
||||
|
||||
//Valori presi dalla classe di test di CsvMinuteCostCalcTest
|
||||
$this->mockCsvMinuteCostCalc
|
||||
->expects($this->once())
|
||||
->method('select_correct_cost')
|
||||
->with('RequestBy/Client', 'Priority/High')
|
||||
->willReturn(1.26); // Mocked return value
|
||||
|
||||
$expected = [
|
||||
'Request By'=>'RequestBy/Client',
|
||||
'Priority'=>'Priority/High',
|
||||
'Tempo totale'=>'01:00:00',
|
||||
'Costo totale'=>75.61,
|
||||
];
|
||||
|
||||
$result = $this->CsvTotalCostCalc->total_time_cost($array, $total_time);
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,9 +7,93 @@ use PHPUnit\Framework\TestCase;
|
|||
|
||||
class CsvDataHandlingTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->csv_data_handling = new CsvDataHandling();
|
||||
$this->mockCsvMinuteCostCalc = $this->createMock(CsvMinuteCostCalc::class);
|
||||
|
||||
}
|
||||
|
||||
public function handleAgentIssue()
|
||||
{
|
||||
// Arrange
|
||||
$agents_issue_time = [];
|
||||
$time = ['user_name' => 'agent1', 'time' => 5];
|
||||
|
||||
// Act
|
||||
$result = $this->csv_data_handling->handle_agent_issue($agents_issue_time, $time);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(['agent1' => 5], $result);
|
||||
|
||||
// Act with another entry for the same agent
|
||||
$time = ['user_name' => 'agent1', 'time' => 3];
|
||||
$result = $this->csv_data_handling->handle_agent_issue($result, $time);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(['agent1' => 8], $result);
|
||||
|
||||
// Act with a different agent
|
||||
$time = ['user_name' => 'agent2', 'time' => 10];
|
||||
$result = $this->csv_data_handling->handle_agent_issue($result, $time);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(['agent1' => 8, 'agent2' => 10], $result);
|
||||
}
|
||||
|
||||
public function getIssueTotalTime()
|
||||
{
|
||||
// Arrange
|
||||
$issue_time = [
|
||||
['user_name' => 'agent1', 'time' => 5],
|
||||
['user_name' => 'agent1', 'time' => 3],
|
||||
['user_name' => 'agent2', 'time' => 10],
|
||||
];
|
||||
|
||||
// Act
|
||||
list($total_time, $agents_issue_time) = $this->csv_data_handling->get_issue_total_time($issue_time);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(18, $total_time);
|
||||
$this->assertEquals(['agent1' => 8, 'agent2' => 10], $agents_issue_time);
|
||||
}
|
||||
|
||||
public function testHandle_csv_time()
|
||||
{
|
||||
// Arrange
|
||||
$array = [
|
||||
'total_time_cost' => 0,
|
||||
'agent_cost' => 0,
|
||||
];
|
||||
|
||||
$issue = [
|
||||
['id' => 1, 'title' => 'Issue 1'],
|
||||
['id' => 2, 'title' => 'Issue 2'],
|
||||
];
|
||||
|
||||
$issue_time = [
|
||||
['user_name' => 'agent1', 'time' => 5],
|
||||
['user_name' => 'agent1', 'time' => 3],
|
||||
['user_name' => 'agent2', 'time' => 10],
|
||||
];
|
||||
|
||||
$company_agents = [
|
||||
['name' => 'agent1', 'cost_per_hour' => 50],
|
||||
['name' => 'agent2', 'cost_per_hour' => 60],
|
||||
];
|
||||
|
||||
$this->csv_data_handling->method('total_time_cost')
|
||||
->with($array, 18)
|
||||
->willReturn(['total_time_cost' => 1800]);
|
||||
|
||||
$this->csv_data_handling->method('agent_cost_calc')
|
||||
->with($array, $company_agents, ['agent1' => 8, 'agent2' => 10])
|
||||
->willReturn(['agent_cost' => 800]);
|
||||
|
||||
// Act
|
||||
$result = $this->csv_data_handling->handle_csv_time($array, $issue, $issue_time, $company_agents);
|
||||
|
||||
// Assert
|
||||
$this->assertEquals(['total_time_cost' => 1800, 'agent_cost' => 800], $result);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,13 +8,80 @@ use PHPUnit\Framework\TestCase;
|
|||
class CsvDataInitTest extends TestCase
|
||||
{
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->csv_data_init = new CsvDataInit();
|
||||
}
|
||||
|
||||
public function testInitialize_csv_data()
|
||||
{
|
||||
|
||||
|
||||
$issue = [
|
||||
'repository' => ['name' => 'test'],
|
||||
'number' => 0,
|
||||
'title' => 'test',
|
||||
'html_url' => 'https://git.test.test/issue/0',
|
||||
'created_at' => '2024-08-14-11:00:00',
|
||||
'closed_at' => '2024-08-14-11:00:00',
|
||||
'labels' => [
|
||||
['name' => 'Priority/Low'],
|
||||
['name' => 'RequestBy/Customer'],
|
||||
['name' => 'Status/Resolved'],
|
||||
],
|
||||
'Kind' => '',
|
||||
'RequestBy' => '',
|
||||
'Priority' => '',
|
||||
];
|
||||
|
||||
$result = $this->csv_data_init->initialize_csv_data($issue);
|
||||
|
||||
$expected = [
|
||||
'Progetto' => 'test',
|
||||
'#' => 0,
|
||||
'Titolo' => 'test',
|
||||
'URL' => 'https://git.test.test/issue/0',
|
||||
'Aperto_il' => '2024-08-14-11:00:00',
|
||||
'Chiuso_il' => '2024-08-14-11:00:00',
|
||||
'Etichette' => 'Priority/Low,RequestBy/Customer,Status/Resolved,',
|
||||
'Kind' => '',
|
||||
'Request By' => '',
|
||||
'Priority' => ''
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
}
|
||||
|
||||
public function testHandle_labels()
|
||||
{
|
||||
$array = [];
|
||||
$issue_labels = [
|
||||
[
|
||||
'name' => 'Kind: Bug'
|
||||
],
|
||||
[
|
||||
'name' => 'RequestBy: John Doe'
|
||||
],
|
||||
[
|
||||
'name' => 'Priority: High'
|
||||
],
|
||||
[
|
||||
'name' => 'Other Label'
|
||||
]
|
||||
];
|
||||
|
||||
// Call the handle_labels function
|
||||
$result = $this->csv_data_init->handle_labels($array, $issue_labels);
|
||||
|
||||
// Assert the expected output
|
||||
$this->assertArrayHasKey('Kind', $result);
|
||||
$this->assertEquals('Kind: Bug', $result['Kind']);
|
||||
|
||||
$this->assertArrayHasKey('Request By', $result);
|
||||
$this->assertEquals('RequestBy: John Doe', $result['Request By']);
|
||||
|
||||
$this->assertArrayHasKey('Priority', $result);
|
||||
$this->assertEquals('Priority: High', $result['Priority']);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue