-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
109 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
*/ | ||
interface TimezoneAwareInterface | ||
{ | ||
public function getTimezone(): ?string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
*/ | ||
trait TimezoneAwareTrait | ||
{ | ||
/** | ||
* @var string|null | ||
*/ | ||
private $timezone; | ||
|
||
final public function getTimezone(): ?string | ||
{ | ||
return $this->timezone; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -23,30 +25,60 @@ | |
/** | ||
* @author Emmanuel Vella <[email protected]> | ||
*/ | ||
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') | ||
|
@@ -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); | ||
|
||
|