if (! function_exists('calculatePercentage')) {
/**
* Calculates the percentage between the given values.
*
* @param int|string $previous
* @param int|string $current
*
* @return string
*/
function calculatePercentage($previous, $current)
{
if (! isset($previous, $current) || $current === 0) {
return null;
}
return $previous / $current;
}
}
public function test_the_calculate_percentage_helper()
{
$values = [
['previous' => 0, 'current' => 0, 'expects' => null],
['previous' => 0, 'current' => 1, 'expects' => 0],
['previous' => 1, 'current' => 0, 'expects' => null],
['previous' => 1, 'current' => 1, 'expects' => 1],
['previous' => 0, 'current' => -1, 'expects' => 0],
['previous' => -1, 'current' => 0, 'expects' => null],
['previous' => -1, 'current' => -1, 'expects' => 1],
['previous' => 0.50, 'current' => 0.30, 'expects' => 1.6666666666666667],
['previous' => 0.50, 'current' => -0.30, 'expects' => -1.6666666666666667],
['previous' => 4466.43, 'current' => 4212.79, 'expects' => 1.0602071311411203],
['previous' => 1068.00, 'current' => 535.00, 'expects' => 1.9962616822429906],
['previous' => 76.39, 'current' => 76.51, 'expects' => 0.9984315775715592],
['previous' => 135, 'current' => 360, 'expects' => 0.375],
];
foreach ($values as $value) {
$percentage = calculatePercentage($value['previous'], $value['current']);
$this->assertSame($value['expects'], $percentage);
}
}