Skip to content

Commit

Permalink
Add TimezoneAwareInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed May 29, 2020
1 parent 1498a47 commit aeea05d
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ jobs:
- uses: actions/checkout@master
- name: PHPStan
uses: "docker://oskarstark/phpstan-ga"
env:
REQUIRE_DEV: true
with:
args: analyse
22 changes: 22 additions & 0 deletions src/Timezone/TimezoneAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\IntlBundle\Timezone;

/**
* @author Javier Spagnoletti <phansys@gmail.com>
*/
interface TimezoneAwareInterface
{
public function getTimezone(): ?string;
}
32 changes: 32 additions & 0 deletions src/Timezone/TimezoneAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\IntlBundle\Timezone;

/**
* Basic Implementation of TimezoneAwareInterface.
*
* @author Javier Spagnoletti <phansys@gmail.com>
*/
trait TimezoneAwareTrait
{
/**
* @var string|null
*/
private $timezone;

final public function getTimezone(): ?string
{
return $this->timezone;
}
}
2 changes: 1 addition & 1 deletion src/Timezone/TimezoneDetectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface TimezoneDetectorInterface
/**
* Get the appropriate timezone.
*
* @return string
* @return string|null
*/
public function getTimezone();
}
11 changes: 9 additions & 2 deletions src/Timezone/UserBasedTimezoneDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,22 @@ public function __construct(TokenStorageInterface $securityContext)
public function getTimezone()
{
if (!$token = $this->securityContext->getToken()) {
return;
return null;
}

if (!$user = $token->getUser()) {
return;
return null;
}

if ($user instanceof TimezoneAwareInterface) {
return $user->getTimezone();
}

// NEXT_MAJOR: Remove this check when `User` implement `TimezoneAwareInterface`
if ($user instanceof User) {
return $user->getTimezone();
}

return null;
}
}
54 changes: 43 additions & 11 deletions tests/Timezone/UserBasedTimezoneDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
namespace Sonata\IntlBundle\Tests\Timezone;

use PHPUnit\Framework\TestCase;
use Sonata\IntlBundle\Timezone\TimezoneAwareInterface;
use Sonata\IntlBundle\Timezone\TimezoneAwareTrait;
use Sonata\IntlBundle\Timezone\UserBasedTimezoneDetector;
use Sonata\UserBundle\Model\User;
use Sonata\UserBundle\SonataUserBundle;
Expand All @@ -23,30 +25,60 @@
/**
* @author Emmanuel Vella <vella.emmanuel@gmail.com>
*/
class UserBasedTimezoneDetectorTest extends TestCase
final class UserBasedTimezoneDetectorTest extends TestCase
{
protected function setUp(): void
{
if (!class_exists(SonataUserBundle::class)) {
$this->markTestSkipped('SonataUserBundle must be installed to run this test.');
}
}

public static function timezoneProvider()
public static function timezoneProvider(): iterable
{
return [
['Europe/Paris'],
[null],
];
}

/**
* @dataProvider timezoneProvider
*/
public function testUserTimezoneDetection(?string $timezone): void
{
$user = new class($timezone) implements TimezoneAwareInterface {
use TimezoneAwareTrait;

public function __construct(?string $timezone)
{
$this->timezone = $timezone;
}
};

$token = $this->createMock(TokenInterface::class);
$token
->expects($this->once())
->method('getUser')
->willReturn($user)
;

$storage = $this->createMock(TokenStorageInterface::class);

$storage
->expects($this->once())
->method('getToken')
->willReturn($token)
;

$timezoneDetector = new UserBasedTimezoneDetector($storage);
$this->assertSame($timezone, $timezoneDetector->getTimezone());
}

/**
* @dataProvider timezoneProvider
*
* @group legacy
*/
public function testDetectsTimezoneForUser($timezone)
public function testDetectsTimezoneForUser(?string $timezone): void
{
if (!class_exists(SonataUserBundle::class)) {
$this->markTestSkipped('SonataUserBundle must be installed to run this test.');
}

$user = $this->createMock(User::class);
$user
->method('getTimezone')
Expand All @@ -70,7 +102,7 @@ public function testDetectsTimezoneForUser($timezone)
$this->assertSame($timezone, $timezoneDetector->getTimezone());
}

public function testTimezoneNotDetected()
public function testTimezoneNotDetected(): void
{
$storage = $this->createMock(TokenStorageInterface::class);

Expand Down

0 comments on commit aeea05d

Please sign in to comment.