Created
January 26, 2011 23:47
-
-
Save wilmoore/797771 to your computer and use it in GitHub Desktop.
Re-factor a setBirthDate method for cleaner code
This file contains hidden or 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
public function setBirthDate($birthDate) { | |
// Allow setting with DateTime object or string date | |
// TODO: Check if object is a subclass of DateTime, watch out for namespaces | |
$this->birthDate = is_object($birthDate) ? $birthDate : (!empty($birthDate) ? new \DateTime($birthDate) : null); | |
return $this; | |
} |
This file contains hidden or 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
public function setBirthDate($birthDate) { | |
if (empty($birthDate)) { return $this; } | |
return ($birthDate instanceof \DateTime) | |
? $this->setBirthDateFromDateTime($birthDate) | |
: $this->setBirthDateFromString($birthDate); | |
} | |
public function setBirthDateFromDateTime(\DateTime $birthDate) { | |
$this->birthDate = $birthDate; | |
return $this; | |
} | |
public function setBirthDateFromString($birthDate) { | |
if (false === date_create($birthDate)) { | |
throw new exception\InvalidArgumentException(sprintf('Invalid date string specified: "%s"', $birthDate)); | |
} | |
$this->birthDate = new \DateTime($birthDate); | |
return $this; | |
} |
This file contains hidden or 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
/** | |
* @test | |
*/ | |
public function Can_Set_Birth_Date_From_DateTime() { | |
$birthDate= new \DateTime('2003-02-03'); | |
$patient = new Patient(); | |
$patient->setBirthDateFromDateTime($birthDate); | |
$this->assertEquals($birthDate, $patient->getBirthDate()); | |
} | |
/** | |
* @test | |
*/ | |
public function Can_Set_Birth_Date_From_DateTime_Inferred_By_Type() { | |
$birthDate= new \DateTime('2003-02-03'); | |
$patient = new Patient(); | |
$patient->setBirthDate($birthDate); | |
$this->assertEquals($birthDate, $patient->getBirthDate()); | |
} | |
/** | |
* @test | |
*/ | |
public function Can_Set_Birth_Date_From_String_Inferred_By_Type() { | |
$birthDate= '2003-02-03'; | |
$patient = new Patient(); | |
$patient->setBirthDate($birthDate); | |
$this->assertEquals(new \DateTime($birthDate), $patient->getBirthDate()); | |
} | |
/** | |
* @test | |
* @expectedException ets\model\data\exception\InvalidArgumentException | |
*/ | |
public function Set_Birth_Date_Inferred_By_Type_Given_A_Non_DateTime_Non_String_Should_Throw_Exception() { | |
$birthDate= (integer) 500; | |
$patient = new Patient(); | |
$patient->setBirthDate($birthDate); | |
} | |
/** | |
* @test | |
* @expectedException ErrorException | |
*/ | |
public function Non_DateTime_Given_To_SetBirthDateFromDateTime_Throws_ErrorException() { | |
$birthDate= new \stdClass(); | |
$patient = new Patient(); | |
$patient->setBirthDateFromDateTime($birthDate); | |
} | |
/** | |
* @test | |
*/ | |
public function Can_Set_Birth_Date_From_String() { | |
$birthDate= '2003-02-03'; | |
$patient = new Patient(); | |
$patient->setBirthDateFromString($birthDate); | |
$this->assertEquals(new \DateTime($birthDate), $patient->getBirthDate()); | |
} | |
/** | |
* @test | |
* @expectedException ets\model\data\exception\InvalidArgumentException | |
*/ | |
public function Non_Parseable_String_Given_To_SetBirthDateFromString_Throws_Exception() { | |
$birthDate= 'LOL......LOL'; | |
$patient = new Patient(); | |
$patient->setBirthDateFromString($birthDate); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment