public function testConstruct()
{
// Unixtime
$now = time();
$time = new Time($now);
$this->assertEquals(date('Y-m-d', $now), $time->format('Y-m-d'));
// Strtotime
$time = new Time('now');
$this->assertEquals(date('Y-m-d', strtotime('now')), $time->format('Y-m-d'));
// Invalid strtotime
try {
$time = new Time('abcde');
$this->fail(sprintf('Expected exception %s.', \InvalidArgumentException::class));
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
throw $e;
} catch (\Exception $e) {
// Correctly thrown exception
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
}
// \Jyxo\Time\Time
$time = new Time(new Time($now));
$this->assertEquals(date('Y-m-d', $now), $time->format('Y-m-d'));
$time = new Time(new Time($now), date_default_timezone_get());
$this->assertEquals(date('Y-m-d', $now), $time->format('Y-m-d'));
// \DateTime
$dateTime = \DateTime::createFromFormat('U', (string) $now);
$time = new Time($dateTime);
$this->assertEquals($now, $time->format('U'));
$time = new Time($dateTime, date_default_timezone_get());
$this->assertEquals(date('Y-m-d', $now), $time->format('Y-m-d'));
// Invalid parameter
try {
$time = new Time([]);
$this->fail(sprintf('Expected exception %s.', \InvalidArgumentException::class));
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
throw $e;
} catch (\Exception $e) {
// Correctly thrown exception
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
}
try {
$time = new Time(new \stdClass());
$this->fail(sprintf('Expected exception %s.', \InvalidArgumentException::class));
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
throw $e;
} catch (\Exception $e) {
// Correctly thrown exception
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
}
try {
$tmp = new Time($time, new \stdClass());
$this->fail(sprintf('Expected exception %s.', \InvalidArgumentException::class));
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
throw $e;
} catch (\Exception $e) {
// Correctly thrown exception
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
}
try {
$tmp = new Time($dateTime, (object) ['foo' => 'bar']);
$this->fail(sprintf('Expected exception %s.', \InvalidArgumentException::class));
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
throw $e;
} catch (\Exception $e) {
// Correctly thrown exception
$this->assertInstanceOf(\InvalidArgumentException::class, $e);
}
}