183 lines
5.6 KiB
PHP
183 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\unit;
|
|
|
|
use Exception;
|
|
use CodeIgniter\Test\CIUnitTestCase;
|
|
use App\Controllers\LeadsController;
|
|
|
|
class LeadsControllerTest extends CIUnitTestCase
|
|
{
|
|
protected $leadsController;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->leadsController = new LeadsController();
|
|
}
|
|
|
|
public function testGetDemographyDataBasicScenario()
|
|
{
|
|
echo('passed 0');
|
|
// Arrange
|
|
$members = [
|
|
// Header row
|
|
['Name', 'Age', 'Relationship', 'SI Enhancement'],
|
|
// Data rows
|
|
['John', '25', 'Self', '100000'],
|
|
['Jane', '23', 'Spouse', '100000'],
|
|
['Kid', '5', 'Child', '50000']
|
|
];
|
|
echo('passed 1');
|
|
$age_band_data = [
|
|
['0-17', '18-35', '36-45', 'Above 46']
|
|
];
|
|
echo('passed 2');
|
|
$members_heading = ['Name', 'Age', 'Relationship', 'SI Enhancement'];
|
|
$available_col = 'age';print_rr($available_col);
|
|
$col_index = 1;
|
|
echo('passed 3');
|
|
try{
|
|
echo('Inside try');
|
|
// Act
|
|
echo('passed 3.5');
|
|
$result = $this->leadsController->getDemographyData($members,$age_band_data,$members_heading,$available_col,$col_index);
|
|
|
|
echo('passed 4');
|
|
} catch (Exception $e) {
|
|
echo "Message: " . $e->getMessage() . "\n";
|
|
echo "File: " . $e->getFile() . "\n";
|
|
echo "Line: " . $e->getLine() . "\n";
|
|
// Optionally, print the full stack trace
|
|
echo "Stack trace: " . $e->getTraceAsString() . "\n";
|
|
}
|
|
var_dump($result);
|
|
// Assert
|
|
$this->assertIsArray($result);
|
|
$this->assertArrayHasKey('general', $result);
|
|
$this->assertArrayHasKey('100000', $result);
|
|
$this->assertArrayHasKey('50000', $result);
|
|
|
|
// Check counts for general category
|
|
$this->assertEquals(1, $result['general']['Self']['18-35']);
|
|
$this->assertEquals(1, $result['general']['Spouse']['18-35']);
|
|
$this->assertEquals(1, $result['general']['Child']['0-17']);
|
|
}
|
|
|
|
public function testGetDemographyDataWithDOB()
|
|
{
|
|
// Arrange
|
|
$members = [
|
|
// Header row
|
|
['Name', 'DOB', 'Relationship', 'SI Enhancement'],
|
|
// Data rows
|
|
['John', '01-Jan-1995', 'Self', '100000'],
|
|
['Jane', '01-Jan-1998', 'Spouse', '100000']
|
|
];
|
|
|
|
$age_band_data = [
|
|
['18-35', '36-45', '46+']
|
|
];
|
|
|
|
$members_heading = ['Name', 'DOB', 'Relationship', 'SI Enhancement'];
|
|
$available_col = 'dob';
|
|
$col_index = 1;
|
|
|
|
// Act
|
|
$result = $this->leadsController->getDemographyData(
|
|
$members,
|
|
$age_band_data,
|
|
$members_heading,
|
|
$available_col,
|
|
$col_index
|
|
);
|
|
|
|
// Assert
|
|
$this->assertIsArray($result);
|
|
$this->assertArrayHasKey('general', $result);
|
|
$this->assertArrayHasKey('100000', $result);
|
|
|
|
// Both members should be in 18-35 age band
|
|
$this->assertEquals(1, $result['general']['Self']['18-35']);
|
|
$this->assertEquals(1, $result['general']['Spouse']['18-35']);
|
|
}
|
|
|
|
public function testGetDemographyDataWithEmptyMembers()
|
|
{
|
|
// Arrange
|
|
$members = [
|
|
// Only header row
|
|
['Name', 'Age', 'Relationship', 'SI Enhancement']
|
|
];
|
|
|
|
$age_band_data = [
|
|
['0-17', '18-35', '36-45', '46+']
|
|
];
|
|
|
|
$members_heading = ['Name', 'Age', 'Relationship', 'SI Enhancement'];
|
|
$available_col = 'age';
|
|
$col_index = 1;
|
|
|
|
// Act
|
|
$result = $this->leadsController->getDemographyData(
|
|
$members,
|
|
$age_band_data,
|
|
$members_heading,
|
|
$available_col,
|
|
$col_index
|
|
);
|
|
|
|
// Assert
|
|
$this->assertIsArray($result);
|
|
$this->assertArrayHasKey('general', $result);
|
|
|
|
// Check that all totals are zero
|
|
$this->assertEquals(0, $result['general']['Grand Total']['Grand Total']);
|
|
}
|
|
|
|
public function testGetDemographyDataWithMultipleSIBands()
|
|
{
|
|
// Arrange
|
|
$members = [
|
|
// Header row
|
|
['Name', 'Age', 'Relationship', 'SI Enhancement'],
|
|
// Data rows with different SI amounts
|
|
['John', '25', 'Self', '100000'],
|
|
['Jane', '23', 'Spouse', '200000'],
|
|
['Kid1', '5', 'Child', '100000'],
|
|
['Kid2', '7', 'Child', '200000']
|
|
];
|
|
|
|
$age_band_data = [
|
|
['0-17', '18-35', '36-45', '46+']
|
|
];
|
|
|
|
$members_heading = ['Name', 'Age', 'Relationship', 'SI Enhancement'];
|
|
$available_col = 'age';
|
|
$col_index = 1;
|
|
|
|
// Act
|
|
$result = $this->leadsController->getDemographyData(
|
|
$members,
|
|
$age_band_data,
|
|
$members_heading,
|
|
$available_col,
|
|
$col_index
|
|
);
|
|
|
|
// Assert
|
|
$this->assertIsArray($result);
|
|
$this->assertArrayHasKey('100000', $result);
|
|
$this->assertArrayHasKey('200000', $result);
|
|
|
|
// Check counts for different SI bands
|
|
$this->assertEquals(1, $result['100000']['Self']['18-35']);
|
|
$this->assertEquals(1, $result['200000']['Spouse']['18-35']);
|
|
$this->assertEquals(1, $result['100000']['Child']['0-17']);
|
|
$this->assertEquals(1, $result['200000']['Child']['0-17']);
|
|
|
|
// Check general category totals
|
|
$this->assertEquals(2, $result['general']['Child']['0-17']);
|
|
$this->assertEquals(2, $result['general']['Child']['Grand Total']);
|
|
}
|
|
} |