Files
crmPRO/tests/Domain/Tasks/WorkTimeRepositoryTest.php
Jacek Pyziak 5161d0f979 Refactor work time management and billing summary
- Introduced a new `TasksController` to handle work time logic, moving it from the factory layer to the domain layer.
- Created `WorkTimeRepository` to encapsulate data access for work time tasks, including methods to retrieve clients with unsettled tasks and calculate task times.
- Updated the work time view to display a consolidated billing summary with improved UI elements and AJAX functionality for closing tasks.
- Added new styles for the billing summary section in `style.scss`.
- Implemented tests for the `TasksController` and `WorkTimeRepository` to ensure functionality and correctness.
- Established a refactoring plan for future improvements and migrations within the CRM system.
2026-02-06 21:32:11 +01:00

41 lines
1.9 KiB
PHP

<?php
require_once __DIR__ . '/../../../autoload/Domain/Tasks/class.WorkTimeRepository.php';
use Domain\Tasks\WorkTimeRepository;
function assert_true( $condition, $message )
{
if ( !$condition )
throw new Exception( $message );
}
function run_work_time_repository_tests()
{
$statuses = WorkTimeRepository::getUnsettledTaskStatuses();
assert_true( $statuses === [ 1, 3 ], 'Expected unsettled statuses to include do sprawdzenia and do rozliczenia.' );
$rows = [
[ 'id' => 11, 'name' => 'Task A', 'pay_rate' => '100.00', 'month' => '2026-01' ],
[ 'id' => 11, 'name' => 'Task A', 'pay_rate' => '100.00', 'month' => '2026-01' ], // duplicate
[ 'id' => 11, 'name' => 'Task A', 'pay_rate' => '100.00', 'month' => '2026-02' ],
[ 'id' => 12, 'name' => 'Task B', 'pay_rate' => null, 'month' => '2026-02' ],
[ 'id' => 13, 'name' => 'Task C', 'pay_rate' => null ] // invalid row, no month
];
$calls = [];
$tasks = WorkTimeRepository::buildClientTasksByMonth( $rows, function( $task_id, $month ) use ( &$calls ) {
$calls[] = $task_id . ':' . $month;
return $task_id * 10;
} );
assert_true( isset( $tasks['2026-01'] ), 'Expected month 2026-01 in result.' );
assert_true( isset( $tasks['2026-02'] ), 'Expected month 2026-02 in result.' );
assert_true( count( $tasks['2026-01'] ) === 1, 'Expected duplicate tasks to be deduplicated per month.' );
assert_true( count( $tasks['2026-02'] ) === 2, 'Expected two unique tasks in month 2026-02.' );
assert_true( $tasks['2026-01'][0]['time'] === 110, 'Expected task time to come from provider.' );
assert_true( $tasks['2026-02'][0]['time'] === 110, 'Expected month-specific provider call for the same task.' );
assert_true( $tasks['2026-02'][1]['time'] === 120, 'Expected provider value for second task.' );
assert_true( count( $calls ) === 3, 'Expected provider to be called once for each unique task+month pair.' );
}