-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added server key based host_id detector (#7)
Co-authored-by: Pavel Agaletskiy <pavel.agaletskiy@lamoda.ru>
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 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
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lamoda\MultiEnv\HostDetector; | ||
|
||
use Lamoda\MultiEnv\Model\HostId; | ||
|
||
final class HostIdMapper implements HostDetectorInterface | ||
{ | ||
/** | ||
* @var HostDetectorInterface | ||
*/ | ||
private $inner; | ||
/** | ||
* @var string[] | ||
*/ | ||
private $hostIdMap; | ||
|
||
/** | ||
* @param HostDetectorInterface $inner | ||
* @param string[] $hostIdMap | ||
*/ | ||
public function __construct(HostDetectorInterface $inner, array $hostIdMap = []) | ||
{ | ||
$this->inner = $inner; | ||
$this->hostIdMap = $hostIdMap; | ||
} | ||
|
||
public function getCurrentHost(): HostId | ||
{ | ||
$innerHostId = $this->inner->getCurrentHost(); | ||
$value = $this->hostIdMap[(string)$innerHostId] ?? ''; | ||
|
||
return new HostId($value); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lamoda\MultiEnvTests\Unit\HostDetector; | ||
|
||
use Lamoda\MultiEnv\HostDetector\HostDetectorInterface; | ||
use Lamoda\MultiEnv\HostDetector\HostIdMapper; | ||
use Lamoda\MultiEnv\Model\HostId; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class HostIdMapperTest extends TestCase | ||
{ | ||
/** | ||
* @var HostDetectorInterface | ||
*/ | ||
private $inner; | ||
|
||
/** | ||
* @var HostIdMapper | ||
*/ | ||
private $mapper; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->inner = $this->createMock(HostDetectorInterface::class); | ||
$this->mapper = new HostIdMapper($this->inner, [ | ||
'host_from_1' => 'host_to_1', | ||
'host_from_2' => 'host_to_2', | ||
]); | ||
} | ||
|
||
/** | ||
* @dataProvider dataGetCurrentHost | ||
*/ | ||
public function testGetCurrentHost(HostId $innerResponse, HostId $expected): void | ||
{ | ||
$this->inner | ||
->method('getCurrentHost') | ||
->willReturn($innerResponse); | ||
|
||
$result = $this->mapper->getCurrentHost(); | ||
|
||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public function dataGetCurrentHost(): iterable | ||
{ | ||
yield [ | ||
new HostId(''), | ||
new HostId(''), | ||
]; | ||
|
||
yield [ | ||
new HostId('host_from_1'), | ||
new HostId('host_to_1'), | ||
]; | ||
|
||
yield [ | ||
new HostId('unknown'), | ||
new HostId(''), | ||
]; | ||
} | ||
} |