Skip to content

Commit

Permalink
Merge 0802a43 into d2c55d3
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon committed Mar 10, 2020
2 parents d2c55d3 + 0802a43 commit 7f83235
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 39 deletions.
21 changes: 3 additions & 18 deletions src/Domain/Clock/ClockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,9 @@

interface ClockInterface
{
/**
* @return int
*/
public function currentTime();
public function currentTime(): int;

/**
* @param string $string
* @param int $time
*
* @return int
*/
public function stringToTime($string, $time = null);
public function stringToTime(string $string, int $time = null): int;

/**
* @param string $format
* @param string $time
*
* @return int
*/
public function createTimestampFromFormat($format, $time);
public function createTimestampFromFormat(string $format, string $time): int;
}
25 changes: 4 additions & 21 deletions src/Domain/Clock/SystemClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,14 @@

final class SystemClock implements ClockInterface
{
/**
* @return int
*/
public function currentTime()
public function currentTime(): int
{
return time();
}

/**
* @param string $string
* @param int $time
*
* @return false|int
* @throws ClockException
*/
public function stringToTime($string, $time = null)
public function stringToTime(string $string, int $time = null): int
{
$time = $time ?? time();
$time = $time ?? $this->currentTime();
$timestamp = strtotime($string, $time);

if ($timestamp === false) {
Expand All @@ -40,14 +30,7 @@ public function stringToTime($string, $time = null)
return $timestamp;
}

/**
* @param string $format
* @param string $time
*
* @return int
* @throws ClockException
*/
public function createTimestampFromFormat($format, $time)
public function createTimestampFromFormat(string $format, string $time): int
{
$datetime = DateTime::createFromFormat($format, $time);

Expand Down
69 changes: 69 additions & 0 deletions tests/Unit/Domain/Clock/SystemClockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace TYPO3\Surf\Tests\Unit\Domain\Clock;

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

use PHPUnit\Framework\TestCase;
use TYPO3\Surf\Domain\Clock\ClockException;
use TYPO3\Surf\Domain\Clock\SystemClock;

class SystemClockTest extends TestCase
{
/**
* @var SystemClock
*/
protected $subject;

protected function setUp()
{
$this->subject = new SystemClock();
}

/**
* @test
*/
public function currentTime(): void
{
$this->assertInternalType('int', $this->subject->currentTime());
}

/**
* @test
*/
public function stringToTime(): void
{
$this->assertInternalType('int', $this->subject->stringToTime('yesterday'));
}

/**
* @test
*/
public function stringToTimeThrowsException(): void
{
$this->expectException(ClockException::class);
$this->subject->stringToTime('foobarbaz');
}

/**
* @test
*/
public function createTimestampFromFormat(): void
{
$this->assertInternalType('int', $this->subject->createTimestampFromFormat('d.m.Y', '20.12.2002'));
}

/**
* @test
*/
public function createTimestampFromFormatThrowsException(): void
{
$this->expectException(ClockException::class);
$this->assertInternalType('int', $this->subject->createTimestampFromFormat('d.m.Y', 'foobarbaz'));
}
}

0 comments on commit 7f83235

Please sign in to comment.