Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
fixes #390 (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
prisis committed Sep 21, 2016
1 parent 75db7d8 commit 68975c4
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 12 deletions.
6 changes: 6 additions & 0 deletions phpunit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
|--------------------------------------------------------------------------
Expand All @@ -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());
3 changes: 2 additions & 1 deletion src/Viserio/Cookie/AbstractCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand Down
3 changes: 2 additions & 1 deletion src/Viserio/Cookie/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Viserio/Cookie/Tests/CookieJarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions src/Viserio/Cookie/Tests/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
declare(strict_types=1);
namespace Viserio\Cookie\Tests;

use Cake\Chronos\Chronos;
use DateTime;
use Viserio\Cookie\Cookie;

Expand Down Expand Up @@ -337,15 +338,15 @@ 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'
);

$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()
);
}
Expand Down
1 change: 1 addition & 0 deletions src/Viserio/Cookie/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
3 changes: 2 additions & 1 deletion src/Viserio/Queue/Connectors/AbstractQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
declare(strict_types=1);
namespace Viserio\Queue\Connectors;

use Cake\Chronos\Chronos;
use Closure;
use DateTimeInterface;
use Exception;
Expand Down Expand Up @@ -105,7 +106,7 @@ protected function getSeconds($delay): int
*/
protected function getTime(): int
{
return time();
return Chronos::now()->timestamp;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Viserio/Queue/Jobs/AbstractJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -198,6 +199,6 @@ protected function getSeconds($delay): int
*/
protected function getTime(): int
{
return time();
return Chronos::now()->timestamp;
}
}
2 changes: 1 addition & 1 deletion src/Viserio/Session/Handler/CookieSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Viserio/Session/Middleware/VerifyCsrfTokenMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')
);
}
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion src/Viserio/Session/Tests/StoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/Viserio/Session/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 68975c4

Please sign in to comment.