diff --git a/phpunit.php b/phpunit.php index 0296f3da1..b79562db6 100644 --- a/phpunit.php +++ b/phpunit.php @@ -4,6 +4,9 @@ require __DIR__ . '/vendor/autoload.php'; use Cake\Chronos\Chronos; +use Cake\Chronos\Date; +use Cake\Chronos\MutableDate; +use Cake\Chronos\MutableDateTime; /* |-------------------------------------------------------------------------- @@ -18,3 +21,6 @@ date_default_timezone_set('UTC'); Chronos::setTestNow(Chronos::now()); +MutableDateTime::setTestNow(MutableDateTime::now()); +Date::setTestNow(Date::now()); +MutableDate::setTestNow(MutableDate::now()); diff --git a/src/Viserio/Cookie/AbstractCookie.php b/src/Viserio/Cookie/AbstractCookie.php index 0883fd2ac..e05eaf263 100644 --- a/src/Viserio/Cookie/AbstractCookie.php +++ b/src/Viserio/Cookie/AbstractCookie.php @@ -2,6 +2,7 @@ declare(strict_types=1); namespace Viserio\Cookie; +use Cake\Chronos\Chronos; use DateTime; use DateTimeInterface; use Viserio\Contracts\Cookie\Cookie as CookieContract; @@ -396,7 +397,7 @@ protected function appendFormattedNameAndValuePartIfSet(array $cookieStringParts $name = urlencode($this->name) . '='; if ((string) $this->getValue() === '') { - $cookieStringParts[] .= $name . 'deleted; Expires=' . gmdate('D, d-M-Y H:i:s T', time() - 31536001); + $cookieStringParts[] .= $name . 'deleted; Expires=' . gmdate('D, d-M-Y H:i:s T', Chronos::now()->timestamp - 31536001); } else { $cookieStringParts[] .= $name . urlencode($this->getValue()); diff --git a/src/Viserio/Cookie/CookieJar.php b/src/Viserio/Cookie/CookieJar.php index 3c2adcacb..ab04239f8 100644 --- a/src/Viserio/Cookie/CookieJar.php +++ b/src/Viserio/Cookie/CookieJar.php @@ -2,6 +2,7 @@ declare(strict_types=1); namespace Viserio\Cookie; +use Cake\Chronos\Chronos; use Narrowspark\Arr\Arr; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -64,7 +65,7 @@ public function create( ): CookieContract { list($path, $domain, $secure) = $this->getPathAndDomain($path, $domain, $secure); - $time = ($minutes === 0) ? 0 : time() + ($minutes * 60); + $time = ($minutes === 0) ? 0 : Chronos::now()->timestamp + ($minutes * 60); return new Cookie($name, $value, $time, $path, $domain, $secure, $httpOnly, $sameSite); } diff --git a/src/Viserio/Cookie/Tests/CookieJarTest.php b/src/Viserio/Cookie/Tests/CookieJarTest.php index 8e971b434..84967810a 100644 --- a/src/Viserio/Cookie/Tests/CookieJarTest.php +++ b/src/Viserio/Cookie/Tests/CookieJarTest.php @@ -2,6 +2,7 @@ declare(strict_types=1); namespace Viserio\Cookie\Tests; +use Cake\Chronos\Chronos; use Viserio\Cookie\CookieJar; class CookieJarTest extends \PHPUnit_Framework_TestCase @@ -27,7 +28,7 @@ public function testCookiesAreCreatedWithProperOptions() $c3 = $cookie->delete('color'); $this->assertNull($c3->getValue()); - $this->assertNotEquals($c3->getExpiresTime()->getTimestamp(), time()); + $this->assertNotEquals($c3->getExpiresTime()->getTimestamp(), Chronos::now()->timestamp); } public function testCookiesAreCreatedWithProperOptionsUsingDefaultPathAndDomain() diff --git a/src/Viserio/Cookie/Tests/CookieTest.php b/src/Viserio/Cookie/Tests/CookieTest.php index d91bd9ca1..4756c8cbc 100644 --- a/src/Viserio/Cookie/Tests/CookieTest.php +++ b/src/Viserio/Cookie/Tests/CookieTest.php @@ -2,6 +2,7 @@ declare(strict_types=1); namespace Viserio\Cookie\Tests; +use Cake\Chronos\Chronos; use DateTime; use Viserio\Cookie\Cookie; @@ -337,7 +338,7 @@ public function testToString() $this->assertEquals( 'foo=deleted; Expires=' . gmdate( 'D, d-M-Y H:i:s T', - time() - 31536001 + Chronos::now()->timestamp - 31536001 ) . '; Path=/admin; Domain=myfoodomain.com; Max-Age=1; HttpOnly', $cookie->__toString(), '->__toString() returns string representation of a cleared cookie if value is NULL' @@ -345,7 +346,7 @@ public function testToString() $cookie = new Cookie('foo'); $this->assertEquals( - 'foo=deleted; Expires=' . gmdate('D, d-M-Y H:i:s T', time() - 31536001) . '; Path=/', + 'foo=deleted; Expires=' . gmdate('D, d-M-Y H:i:s T', Chronos::now()->timestamp - 31536001) . '; Path=/', $cookie->__toString() ); } diff --git a/src/Viserio/Cookie/composer.json b/src/Viserio/Cookie/composer.json index dfd6a59c7..e8e550ec0 100644 --- a/src/Viserio/Cookie/composer.json +++ b/src/Viserio/Cookie/composer.json @@ -19,6 +19,7 @@ ], "require": { "php" : "7.0.0 - 7.0.5 || ^7.0.7", + "cakephp/chronos" : "^1.0", "psr/http-message" : "^1.0", "viserio/cotracts" : "self.version" }, diff --git a/src/Viserio/Queue/Connectors/AbstractQueue.php b/src/Viserio/Queue/Connectors/AbstractQueue.php index d6290017d..489cf45af 100644 --- a/src/Viserio/Queue/Connectors/AbstractQueue.php +++ b/src/Viserio/Queue/Connectors/AbstractQueue.php @@ -2,6 +2,7 @@ declare(strict_types=1); namespace Viserio\Queue\Connectors; +use Cake\Chronos\Chronos; use Closure; use DateTimeInterface; use Exception; @@ -105,7 +106,7 @@ protected function getSeconds($delay): int */ protected function getTime(): int { - return time(); + return Chronos::now()->timestamp; } /** diff --git a/src/Viserio/Queue/Jobs/AbstractJob.php b/src/Viserio/Queue/Jobs/AbstractJob.php index 374a5093b..c67ac25b1 100644 --- a/src/Viserio/Queue/Jobs/AbstractJob.php +++ b/src/Viserio/Queue/Jobs/AbstractJob.php @@ -2,6 +2,7 @@ declare(strict_types=1); namespace Viserio\Queue\Jobs; +use Cake\Chronos\Chronos; use DateTime; use Narrowspark\Arr\Arr; use Viserio\Contracts\Container\Traits\ContainerAwareTrait; @@ -198,6 +199,6 @@ protected function getSeconds($delay): int */ protected function getTime(): int { - return time(); + return Chronos::now()->timestamp; } } diff --git a/src/Viserio/Session/Handler/CookieSessionHandler.php b/src/Viserio/Session/Handler/CookieSessionHandler.php index c52a1d9e3..38bb511f7 100644 --- a/src/Viserio/Session/Handler/CookieSessionHandler.php +++ b/src/Viserio/Session/Handler/CookieSessionHandler.php @@ -69,7 +69,7 @@ public function read($sessionId) if (isset($decoded[$sessionId])) { $data = $decoded[$sessionId]; - if (isset($data['expires']) && time() <= $data['expires']) { + if (isset($data['expires']) && Chronos::now()->timestamp <= $data['expires']) { return $data['data']; } } diff --git a/src/Viserio/Session/Middleware/VerifyCsrfTokenMiddleware.php b/src/Viserio/Session/Middleware/VerifyCsrfTokenMiddleware.php index 393b1433c..db6a6275a 100644 --- a/src/Viserio/Session/Middleware/VerifyCsrfTokenMiddleware.php +++ b/src/Viserio/Session/Middleware/VerifyCsrfTokenMiddleware.php @@ -2,6 +2,7 @@ declare(strict_types=1); namespace Viserio\Session\Middleware; +use Cake\Chronos\Chronos; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Schnittstabil\Csrf\TokenService\TokenService; @@ -39,7 +40,7 @@ public function __construct(SessionManager $manager) $this->tokenService = new TokenService( $config->get('session::key'), - $config->get('session::csrf.livetime', time() + 60 * 120), + $config->get('session::csrf.livetime', Chronos::now()->timestamp + 60 * 120), $config->get('session::csrf.algo', 'SHA512') ); } @@ -104,7 +105,7 @@ protected function addCookieToResponse(ResponseInterface $response): ResponseInt $setCookie = new Cookie( 'XSRF-TOKEN', $this->tokenService->generate(), - $config->get('session::csrf.livetime', time() + 60 * 120), + $config->get('session::csrf.livetime', Chronos::now()->timestamp + 60 * 120), $config->get('path'), $config->get('domain'), $config->get('secure', false), diff --git a/src/Viserio/Session/Tests/StoreTest.php b/src/Viserio/Session/Tests/StoreTest.php index 65c079fe9..228eb2a5c 100644 --- a/src/Viserio/Session/Tests/StoreTest.php +++ b/src/Viserio/Session/Tests/StoreTest.php @@ -2,6 +2,7 @@ declare(strict_types=1); namespace Viserio\Session\Tests; +use Cake\Chronos\Chronos; use Defuse\Crypto\Key; use Narrowspark\TestingHelper\Traits\MockeryTrait; use ReflectionClass; @@ -205,7 +206,7 @@ public function testStartMethodResetsIdRegenerationTrace() $session->start(); $this->assertNotEquals($regenerationTrace, $session->getRegenerationTrace()); - $this->assertGreaterThanOrEqual(time() - 1, $session->getRegenerationTrace()); + $this->assertGreaterThanOrEqual(Chronos::now()->timestamp - 1, $session->getRegenerationTrace()); } public function testStartMethodGeneratesFingerprint() diff --git a/src/Viserio/Session/composer.json b/src/Viserio/Session/composer.json index f0443f493..f6ba5cb18 100644 --- a/src/Viserio/Session/composer.json +++ b/src/Viserio/Session/composer.json @@ -20,8 +20,8 @@ "require": { "php" : "7.0.0 - 7.0.5 || ^7.0.7", "container-interop/container-interop" : "^1.1", - "narrowspark/arr" : "^1.0", "cakephp/chronos" : "^1.0", + "narrowspark/arr" : "^1.0", "psr/http-message" : "^1.0", "schnittstabil/csrf-tokenservice" : "^2.0", "viserio/config" : "self.version",