Two logic changes in Car.php, and 2 unit tests needed modification
Last active
May 6, 2019 22:14
-
-
Save tchalko/a35a2899c35fa48557f99065d4d38fc8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Car { | |
public $make; | |
public $year; | |
public $mileage; | |
public $coverage_name; | |
public $coverage_terms; | |
public $coverage_miles; | |
public $condition; | |
public $suffix1; | |
public $suffix2; | |
public $base_warranty_term; | |
public $base_warranty_miles; | |
public $validation_result; | |
public $errors; | |
//===================== | |
// public functions | |
//===================== | |
// Retrieve json file by simulating API call | |
public function getCoverageArray($url) { | |
$results = $this->apiCall($url); | |
return $results; | |
} | |
// Validate make input from the command line | |
public function validateMake($make, $base_warranty, &$valid_makes){ | |
foreach ($base_warranty as $item) { | |
if ($item["make"] == $make) { | |
return true; | |
} | |
array_push($valid_makes,$item["make"]); | |
} | |
return false; | |
} | |
// Validate year input from the command line | |
public function validateYear($year, $years, &$valid_years){ | |
foreach ($years as $item) { | |
if ($item["modelyear"] == $year) { | |
return true; | |
} | |
array_push($valid_years,$item["modelyear"]); | |
} | |
return false; | |
} | |
// Validate mileage input from the command line | |
public function validateMileage($mileage){ | |
return (($mileage>MIN_INPUT_MILEAGE) && | |
($mileage<MAX_INPUT_MILEAGE) && | |
($mileage%INC_MILEAGE==0)) ? true : false; | |
} | |
// Get basic info from the command line | |
public function getCarInputValues($base_warranty, $years){ | |
return $this->getInputValues($base_warranty, $years); | |
} | |
// Set base warranty info for the car | |
public function getBaseWarrantyInfo($base_warranty) | |
{ | |
foreach ($base_warranty as $item) { | |
if ($item["make"] != $this->make) | |
continue; | |
else { | |
$this->base_warranty_miles = $item["miles"]; | |
$this->base_warranty_term = $item["term"]; | |
return true; | |
} | |
} | |
} | |
// Checks the age condition of the car | |
public function getCondition(){ | |
return ($this->mileage < $this->base_warranty_miles) ? "NEW" : "USED"; | |
} | |
// Sets the correct suffix1 for the car | |
public function getSuffix1($years) { | |
foreach ($years as $item) { | |
if ($item["modelyear"]== $this->year) { | |
$formatted_suffix1 = sprintf("%02d", $item["suffix1"]); | |
return $formatted_suffix1; | |
} | |
} | |
array_push($this->errors, "Car model year is not on our list"); | |
return "--"; | |
} | |
// Sets the correct suffix2 for the car | |
public function getSuffix2($issue_mileage) { | |
foreach ($issue_mileage as $item) { | |
if ($this->mileage > $item["max"]) | |
continue; | |
else | |
return $item["suffix2"]; | |
} | |
array_push($this->errors, "Car mileage is not on our list"); | |
return "-"; | |
} | |
// Checks if the miles of the coverage contract expire before the base warranty miles of the | |
// vehicle has expired. | |
public function checkContractMileage($coverage){ | |
// get number miles left on warranty | |
$num_miles_left_on_base_warranty = $this->base_warranty_miles - $this->mileage; | |
// if this is negative, adjust it to 0, to show we have no miles left under the base warranty | |
if ($num_miles_left_on_base_warranty < 0) | |
$num_miles_left_on_base_warranty = 0; | |
if ($coverage['miles'] <= $num_miles_left_on_base_warranty) { | |
array_push($this->errors, "Miles expires before warranty"); | |
return false; | |
} | |
return true; | |
} | |
// Checks if the term of the coverage contract expire before the base warranty term of the | |
// vehicle has expired. | |
public function checkContractTerms($coverage){ | |
$current_year = date("Y"); | |
// get car's age in months | |
$car_age = ($current_year - $this->year)*12; | |
// get number months left on warranty | |
$num_months_left_on_base_warranty = $this->base_warranty_term - $car_age; | |
// if this is negative, adjust it to 0, to show we have no time left on the base warranty | |
if ($num_months_left_on_base_warranty < 0) | |
$num_months_left_on_base_warranty = 0; | |
if ($coverage['terms'] <= $num_months_left_on_base_warranty) { | |
array_push($this->errors, "Term expires before warranty"); | |
return false; | |
} | |
return true; | |
} | |
// Checks if car will be too old before contract expires | |
public function checkAge($coverage){ | |
$current_year = date("Y"); | |
// Get future age of car, in months, at the end of the term | |
$future_age = (12*($current_year - $this->year)) + $coverage['terms']; | |
// if future age is > 147 months, this car will be too old | |
if ($future_age > 147){ | |
array_push($this->errors, "Vehicle age will be too old before contract ends"); | |
return false; | |
} | |
return true; | |
} | |
// Checks if car will have too many miles before contract expires | |
public function checkMileage($coverage){ | |
$current_year = date("Y"); | |
$current_month = date("m"); | |
// first get car current average miles per month | |
if ($this->year < $current_year) | |
$average_monthly_miles = $this->mileage / (12*($current_year - $this->year)); | |
else | |
$average_monthly_miles = $this->mileage / $current_month; | |
// next get mileage expected during the term | |
$miles_expected_during_contract = $average_monthly_miles * $coverage["terms"]; | |
// finally get total miles expected | |
$total_expected_miles = $miles_expected_during_contract + $this->mileage; | |
// if total expected miles is > MAX_MILEAGE miles, this car will be too high mileage | |
if ($total_expected_miles > MAX_MILEAGE) { | |
array_push($this->errors, "Vehicle mileage will be too high before contract ends"); | |
return false; | |
} | |
return true; | |
} | |
// Returns values to print of general car information, | |
public function getPrintValues(){ | |
$string_to_output = | |
$this->make.' '.$this->year.' '.$this->mileage.' '.$this->condition.' "'.$this->coverage_name.'" suffix1:'.$this->suffix1.' suffix2:'.$this->suffix2.' '; | |
return $string_to_output; | |
} | |
// Returns values to print of validation results | |
public function getPrintValidationResults(){ | |
$num_errors = count($this->errors); | |
if ($num_errors == 0) | |
$string_to_output = "RESULT: SUCCESS"; | |
else { | |
$string_to_output = "RESULT: FAILURE array('"; | |
$cnt=0; | |
foreach($this->errors as $value) { | |
$string_to_output = $string_to_output.((++$cnt == $num_errors) ? "$value')" : "$value', '"); | |
} | |
} | |
return $string_to_output; | |
} | |
//===================== | |
// private functions | |
//===================== | |
// simulate API call to get json file. | |
private function apiCall($url) { | |
$strJson = file_get_contents($url); //implemented with file_get_contents since file is in same directory | |
$result = json_decode($strJson, true); | |
return $result; | |
} | |
// Retrieve car's make from the command line | |
private function getCarMake($base_warranty){ | |
$valid_input = false; | |
$valid_makes = array(); | |
while (!$valid_input) { | |
echo "Enter car make: "; | |
$make = rtrim(fgets(STDIN), "\r\n"); | |
$valid_input = $this->validateMake($make, $base_warranty, $valid_makes); | |
if (!$valid_input) { | |
echo "Invalid car make input, select from [ "; | |
while (count($valid_makes) > 0) { | |
echo array_shift($valid_makes) . " "; | |
} | |
echo "]. Please try again\n"; | |
} | |
} | |
return $make; | |
} | |
// Retrieve car's year from the command line | |
private function getCarYear($years){ | |
$valid_input = false; | |
$valid_years = array(); | |
while (!$valid_input) { | |
echo "Enter car year: "; | |
$year = rtrim(fgets(STDIN), "\r\n"); | |
$valid_input = $this->validateYear($year, $years, $valid_years); | |
if (!$valid_input){ | |
echo "Invalid car year input, select from [ "; | |
while (count($valid_years) > 0) { | |
echo array_shift($valid_years) . " "; | |
} | |
echo "]. Please try again\n"; | |
} | |
} | |
return $year; | |
} | |
// Retrieve car's issue mileage from the command line | |
private function getCarMileage(){ | |
$valid_input = false; | |
while (!$valid_input) { | |
echo "Enter car issue mileage: "; | |
$mileage = rtrim(fgets(STDIN), "\r\n"); | |
$valid_input = $this->validateMileage($mileage); | |
if (!$valid_input) | |
echo "Invalid car issue mileage input, select from [ ".MIN_INPUT_MILEAGE."-".MAX_INPUT_MILEAGE." ] in ".INC_MILEAGE." mile increments. Please try again\n"; | |
} | |
return $mileage; | |
} | |
// Retrieve car make, year, and issue mileage from the command line | |
private function getInputValues($base_warranty, $years){ | |
$this->make = $this->getCarMake($base_warranty); | |
$this->year = $this->getCarYear($years); | |
$this->mileage = $this->getCarMileage(); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PHPUnit\Framework\TestCase; | |
const MAX_INPUT_MILEAGE = 150000; | |
const MIN_INPUT_MILEAGE = 0; | |
const INC_MILEAGE = 1000; | |
const MAX_MILEAGE = 153000; | |
class CarTest extends TestCase | |
{ | |
public $years = array( | |
array("modelyear" => 2003, "suffix1" => 15), | |
array("modelyear" => 2004, "suffix1" => 14), | |
array("modelyear" => 2005, "suffix1" => 13), | |
array("modelyear" => 2006, "suffix1" => 12), | |
array("modelyear" => 2007, "suffix1" => 11), | |
array("modelyear" => 2008, "suffix1" => 10), | |
array("modelyear" => 2009, "suffix1" => 9), | |
array("modelyear" => 2010, "suffix1" => 8), | |
array("modelyear" => 2011, "suffix1" => 7), | |
array("modelyear" => 2012, "suffix1" => 6), | |
array("modelyear" => 2013, "suffix1" => 5), | |
array("modelyear" => 2014, "suffix1" => 4), | |
array("modelyear" => 2015, "suffix1" => 3), | |
array("modelyear" => 2016, "suffix1" => 2), | |
array("modelyear" => 2017, "suffix1" => 1), | |
array("modelyear" => 2018, "suffix1" => 0), | |
array("modelyear" => 2019, "suffix1" => 0)); | |
// mileage of the vehicle at the time the contract is rated | |
public $issue_mileage = array( | |
array("min" => 0, "max" => 12000, "suffix2" => "A"), | |
array("min" => 12001, "max" => 24000, "suffix2" => "A"), | |
array("min" => 24001, "max" => 36000, "suffix2" => "B"), | |
array("min" => 36001, "max" => 48000, "suffix2" => "C"), | |
array("min" => 48001, "max" => 60000, "suffix2" => "D"), | |
array("min" => 60001, "max" => 72000, "suffix2" => "E"), | |
array("min" => 72001, "max" => 84000, "suffix2" => "F"), | |
array("min" => 84001, "max" => 96000, "suffix2" => "G"), | |
array("min" => 96001, "max" => 108000, "suffix2" => "H"), | |
array("min" => 108001, "max" => 120000, "suffix2" => "I"), | |
array("min" => 120001, "max" => 132000, "suffix2" => "J"), | |
array("min" => 132001, "max" => 144000, "suffix2" => "K"), | |
array("min" => 144001, "max" => 150000, "suffix2" => "L") | |
); | |
public $base_warranty = array( | |
array("make" => "BMW", "term" => 36, "miles" => 48000), | |
array("make" => "Volkswagen", "term" => 72, "miles" => 100000) | |
); | |
public $coverage = array( | |
array("name" => "3 Months/3,000 Miles", "terms" => 3, "miles" => 3000), | |
array("name" => "6 Months/12,000 Miles", "terms" => 6, "miles" => 12000), | |
array("name" => "12 Months/24,000 Miles", "terms" => 12, "miles" => 24000), | |
array("name" => "24 Months/30,000 Miles", "terms" => 24, "miles" => 30000), | |
array("name" => "24 Months/36,000 Miles", "terms" => 24, "miles" => 36000), | |
array("name" => "36 Months/36,000 Miles", "terms" => 36, "miles" => 36000), | |
array("name" => "36 Months/45,000 Miles", "terms" => 36, "miles" => 45000), | |
array("name" => "36 Months/50,000 Miles", "terms" => 36, "miles" => 50000), | |
array("name" => "48 Months/50,000 Miles", "terms" => 48, "miles" => 50000), | |
array("name" => "48 Months/60,000 Miles", "terms" => 48, "miles" => 60000), | |
array("name" => "60 Months/72,000 Miles", "terms" => 60, "miles" => 72000), | |
array("name" => "60 Months/75,000 Miles", "terms" => 60, "miles" => 75000), | |
array("name" => "72 Months/100,000 Miles", "terms" => 72, "miles" => 100000), | |
array("name" => "84 Months/84,000 Miles", "terms" => 84, "miles" => 84000), | |
array("name" => "84 Months/96,000 Miles", "terms" => 84, "miles" => 96000), | |
array("name" => "100 Months/100,000 Miles", "terms" => 100, "miles" => 100000), | |
array("name" => "100 Months/120,000 Miles", "terms" => 100, "miles" => 120000), | |
array("name" => "120 Months/120,000 Miles", "terms" => 120, "miles" => 120000) | |
); | |
protected $car; | |
protected function setUp(): void { | |
$this->car = new Car; | |
} | |
public function testCoverageArrayReturnedCorrectly(){ | |
$url = "src/devTest.json"; | |
$this->assertEquals($this->coverage, $this->car->getCoverageArray($url)); | |
} | |
public function testCarMakeIsValid(){ | |
$make = "BMC"; | |
$valid_makes = array(); | |
$this->assertEquals(false, $this->car->validateMake($make, $this->base_warranty, $valid_makes)); | |
$this->assertEquals(2, count($valid_makes)); | |
$make = "BMW"; | |
$valid_makes = array(); | |
$this->assertEquals(true, $this->car->validateMake($make, $this->base_warranty, $valid_makes)); | |
} | |
public function testCarYearIsValid(){ | |
$valid_years = array(); | |
$year = 2002; | |
$this->assertEquals(false, $this->car->validateYear($year, $this->years, $valid_years)); | |
$this->assertEquals(17, count($valid_years)); | |
$valid_years = array(); | |
$year = 2018; | |
$this->assertEquals(true, $this->car->validateYear($year, $this->years, $valid_years)); | |
} | |
public function testCarMileageIsValid(){ | |
$mileage = 90000; | |
$this->assertEquals(true, $this->car->validateMileage($mileage)); | |
$mileage = 90500; | |
$this->assertEquals(false, $this->car->validateMileage($mileage)); | |
} | |
public function testRetrievalOfBaseWarrantyInformation(){ | |
$this->car->make = "BMW" ; | |
$this->assertEquals(true, $this->car->getBaseWarrantyInfo($this->base_warranty)); | |
$this->assertEquals(48000, $this->car->base_warranty_miles); | |
$this->assertEquals(36, $this->car->base_warranty_term); | |
} | |
public function testConditionReturnsNewAndUsed(){ | |
$this->car->mileage = 52000 ; | |
$this->car->base_warranty_miles= 48000; | |
$this->assertEquals("USED", $this->car->getCondition()); | |
$this->car->mileage = 44000 ; | |
$this->assertEquals("NEW", $this->car->getCondition()); | |
} | |
public function testSuffix1returnsValidValue() { | |
$this->car->year = 2019; | |
$this->assertEquals("00", $this->car->getSuffix1($this->years)); | |
$this->car->year = 2012; | |
$this->assertEquals("06", $this->car->getSuffix1($this->years)); | |
$this->car->year = 2007; | |
$this->assertEquals("11", $this->car->getSuffix1($this->years)); | |
} | |
public function testSuffix2returnsValidValue() { | |
$this->car->mileage = 44000 ; | |
$this->assertEquals("C", $this->car->getSuffix2($this->issue_mileage)); | |
} | |
public function testCheckIfContractMilesExpiresBeforeBaseWarrantyMiles() { | |
$this->car->errors = []; | |
$this->car->mileage = 36000; | |
$this->car->base_warranty_miles = 48000; | |
$this->assertEquals(true, $this->car->checkContractMileage($this->coverage[7])); | |
$this->assertEquals(0, count($this->car->errors)); | |
$this->car->mileage = 40000; | |
$this->car->base_warranty_miles = 48000; | |
$this->assertEquals(false, $this->car->checkContractMileage($this->coverage[0])); | |
$this->assertEquals(1, count($this->car->errors)); | |
} | |
public function testCheckIfContractTermExpiresBeforeBaseWarrantyTerm() { | |
$this->car->errors = []; | |
$this->car->year = 2018; | |
$this->car->base_warranty_term= 36; | |
$this->assertEquals(true, $this->car->checkContractTerms($this->coverage[8])); | |
$this->assertEquals(0, count($this->car->errors)); | |
$this->car->year = 2017; | |
$this->car->base_warranty_term= 36; | |
$this->assertEquals(false, $this->car->checkContractTerms($this->coverage[0])); | |
$this->assertEquals(1, count($this->car->errors)); | |
} | |
public function testCheckIfCarWillBeTooOldBeforeContractExpires() { | |
$this->car->errors = []; | |
$this->car->year = 2018; | |
$this->assertEquals(true, $this->car->checkAge($this->coverage[8])); | |
$this->assertEquals(0, count($this->car->errors)); | |
$this->car->year = 2010; | |
$this->assertEquals(false, $this->car->checkAge($this->coverage[8])); | |
$this->assertEquals(1, count($this->car->errors)); | |
} | |
public function testCheckIfCarWillHaveTooManyMilesBeforeContractExpires() { | |
$this->car->errors = []; | |
$this->car->year = 2018; | |
$this->car->mileage = 1000; | |
$this->assertEquals(true, $this->car->checkMileage($this->coverage[8])); | |
$this->assertEquals(0, count($this->car->errors)); | |
$this->car->errors = []; | |
$this->car->year = 2010; | |
$this->car->mileage = 80000; | |
$this->assertEquals(false, $this->car->checkMileage($this->coverage[17])); | |
$this->assertEquals(1, count($this->car->errors)); | |
} | |
public function testPrintedOutputIsValid(){ | |
$this->car->make = "BMW"; | |
$this->car->year ="2018"; | |
$this->car->mileage = "1000"; | |
$this->car->condition ="NEW"; | |
$this->car->coverage_name = "3 Months/3,000 Miles"; | |
$this->car->suffix1 = "00"; | |
$this->car->suffix2 = "A"; | |
$this->expectOutputString($this->car->getPrintValues()); | |
print 'BMW 2018 1000 NEW "3 Months/3,000 Miles" suffix1:00 suffix2:A '; | |
} | |
public function testPrintedValidationOutputIsValidSuccess(){ | |
$this->car->errors = array(); | |
$this->expectOutputString($this->car->getPrintValidationResults()); | |
print 'RESULT: SUCCESS'; | |
} | |
public function testPrintedValidationOutputIsValidFailure(){ | |
$this->car->errors = array(); | |
array_push($this->car->errors,'Term expires before warranty'); | |
$this->expectOutputString($this->car->getPrintValidationResults()); | |
print "RESULT: FAILURE array('Term expires before warranty')"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment