Skip to content

Commit

Permalink
Make castToDateTime and castToBirthday private
Browse files Browse the repository at this point in the history
  • Loading branch information
yguedidi committed Dec 19, 2017
1 parent 09ed53b commit 28af55b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 58 deletions.
62 changes: 31 additions & 31 deletions src/GraphNode/GraphNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,37 +199,6 @@ public function __toString()
return $this->asJson();
}

/**
* Casts a date value from Graph to DateTime.
*
* @param int|string $value
*
* @return \DateTime
*/
public function castToDateTime($value)
{
if (is_int($value)) {
$dt = new \DateTime();
$dt->setTimestamp($value);
} else {
$dt = new \DateTime($value);
}

return $dt;
}

/**
* Casts a birthday value from Graph to Birthday.
*
* @param string $value
*
* @return Birthday
*/
public function castToBirthday($value)
{
return new Birthday($value);
}

/**
* Getter for $graphNodeMap.
*
Expand Down Expand Up @@ -337,4 +306,35 @@ private function isIso8601DateString($string)

return preg_match($crazyInsaneRegexThatSomehowDetectsIso8601, $string) === 1;
}

/**
* Casts a date value from Graph to DateTime.
*
* @param int|string $value
*
* @return \DateTime
*/
private function castToDateTime($value)
{
if (is_int($value)) {
$dt = new \DateTime();
$dt->setTimestamp($value);
} else {
$dt = new \DateTime($value);
}

return $dt;
}

/**
* Casts a birthday value from Graph to Birthday.
*
* @param string $value
*
* @return Birthday
*/
private function castToBirthday($value)
{
return new Birthday($value);
}
}
34 changes: 7 additions & 27 deletions tests/GraphNode/GraphNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,25 @@ public static function provideDateTimeFieldNames()
/**
* @dataProvider provideValidDateTimeFieldValues
*/
public function testIsCastDateTimeFieldValueToDateTime($value, $message)
public function testIsCastDateTimeFieldValueToDateTime($value, $message, $prettyDate = null)
{
$graphNode = new GraphNode(['created_time' => $value]);

$this->assertInstanceOf(\DateTime::class, $graphNode->getField('created_time'), $message);

if ($prettyDate !== null) {
$this->assertEquals($prettyDate, $graphNode->getField('created_time')->format(\DateTime::RFC1036));
}
}

public static function provideValidDateTimeFieldValues()
{
yield ['1985-10-26T01:21:00+0000', 'Expected the valid ISO 8601 formatted date from Back To The Future to pass.'];
yield ['2014-07-15T03:44:53+0000', 'Expected the valid ISO 8601 formatted date to pass.', 'Tue, 15 Jul 14 03:44:53 +0000'];
yield ['1999-12-31', 'Expected the valid ISO 8601 formatted date to party like it\'s 1999.'];
yield ['2009-05-19T14:39Z', 'Expected the valid ISO 8601 formatted date to pass.'];
yield ['2014-W36', 'Expected the valid ISO 8601 formatted date to pass.'];
yield [1405547020, 'Expected the valid timestamp to pass.', 'Wed, 16 Jul 14 23:43:40 +0200'];
}

/**
Expand All @@ -100,32 +106,6 @@ public static function provideInvalidDateTimeFieldValues()
yield ['foo_time', 'Expected the invalid ISO 8601 format to fail.'];
}

public function testATimeStampCanBeConvertedToADateTimeObject()
{
$someTimeStampFromGraph = 1405547020;
$graphNode = new GraphNode();
$dateTime = $graphNode->castToDateTime($someTimeStampFromGraph);
$prettyDate = $dateTime->format(\DateTime::RFC1036);
$timeStamp = $dateTime->getTimestamp();

$this->assertInstanceOf(\DateTime::class, $dateTime);
$this->assertEquals('Wed, 16 Jul 14 23:43:40 +0200', $prettyDate);
$this->assertEquals(1405547020, $timeStamp);
}

public function testAGraphDateStringCanBeConvertedToADateTimeObject()
{
$someDateStringFromGraph = '2014-07-15T03:44:53+0000';
$graphNode = new GraphNode();
$dateTime = $graphNode->castToDateTime($someDateStringFromGraph);
$prettyDate = $dateTime->format(\DateTime::RFC1036);
$timeStamp = $dateTime->getTimestamp();

$this->assertInstanceOf(\DateTime::class, $dateTime);
$this->assertEquals('Tue, 15 Jul 14 03:44:53 +0000', $prettyDate);
$this->assertEquals(1405395893, $timeStamp);
}

public function testGettingGraphNodeAsAnArrayWillNotUncastTheDateTimeObject()
{
$graphNode = new GraphNode([
Expand Down

0 comments on commit 28af55b

Please sign in to comment.