-
Notifications
You must be signed in to change notification settings - Fork 11
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
1 parent
fdb5d28
commit d81e76a
Showing
10 changed files
with
290 additions
and
8 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,32 @@ | ||
<?php | ||
|
||
namespace Unleash\Client\ContextProvider; | ||
|
||
use JetBrains\PhpStorm\Pure; | ||
use Unleash\Client\Configuration\Context; | ||
use Unleash\Client\Configuration\UnleashContext; | ||
|
||
final class DefaultUnleashContextProvider implements UnleashContextProvider, SettableUnleashContextProvider | ||
{ | ||
public function __construct(private ?Context $defaultContext = null) | ||
{ | ||
} | ||
|
||
#[Pure] | ||
public function getContext(): Context | ||
{ | ||
return $this->defaultContext ? clone $this->defaultContext : new UnleashContext(); | ||
} | ||
|
||
/** | ||
* @todo remove in next major version | ||
* | ||
* @internal | ||
*/ | ||
public function setDefaultContext(Context $context): self | ||
{ | ||
$this->defaultContext = $context; | ||
|
||
return $this; | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
namespace Unleash\Client\ContextProvider; | ||
|
||
use JetBrains\PhpStorm\Deprecated; | ||
use Unleash\Client\Configuration\Context; | ||
|
||
/** | ||
* @todo Remove in next major version | ||
*/ | ||
#[Deprecated('This interface will be removed in next major version')] | ||
interface SettableUnleashContextProvider extends UnleashContextProvider | ||
{ | ||
public function setDefaultContext(Context $context): self; | ||
} |
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,12 @@ | ||
<?php | ||
|
||
namespace Unleash\Client\ContextProvider; | ||
|
||
use JetBrains\PhpStorm\Pure; | ||
use Unleash\Client\Configuration\Context; | ||
|
||
interface UnleashContextProvider | ||
{ | ||
#[Pure] | ||
public function getContext(): Context; | ||
} |
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
40 changes: 40 additions & 0 deletions
40
tests/ContextProvider/DefaultUnleashContextProviderTest.php
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,40 @@ | ||
<?php | ||
|
||
namespace Unleash\Client\Tests\ContextProvider; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Unleash\Client\Configuration\UnleashContext; | ||
use Unleash\Client\ContextProvider\DefaultUnleashContextProvider; | ||
|
||
final class DefaultUnleashContextProviderTest extends TestCase | ||
{ | ||
public function testGetContext() | ||
{ | ||
$instance = new DefaultUnleashContextProvider(); | ||
self::assertInstanceOf(UnleashContext::class, $instance->getContext()); | ||
self::assertNull($instance->getContext()->getCurrentUserId()); | ||
self::assertNull($instance->getContext()->getIpAddress()); | ||
self::assertNull($instance->getContext()->getSessionId()); | ||
self::assertNotSame($instance->getContext(), $instance->getContext()); | ||
|
||
$instance = new DefaultUnleashContextProvider(new UnleashContext('123', '456', '789')); | ||
self::assertInstanceOf(UnleashContext::class, $instance->getContext()); | ||
self::assertEquals('123', $instance->getContext()->getCurrentUserId()); | ||
self::assertEquals('456', $instance->getContext()->getIpAddress()); | ||
self::assertEquals('789', $instance->getContext()->getSessionId()); | ||
self::assertNotSame($instance->getContext(), $instance->getContext()); | ||
} | ||
|
||
public function testSetDefaultContext() | ||
{ | ||
$instance = new DefaultUnleashContextProvider(); | ||
$context = new UnleashContext('123', '456', '789'); | ||
$instance->setDefaultContext($context); | ||
|
||
self::assertInstanceOf(UnleashContext::class, $instance->getContext()); | ||
self::assertEquals('123', $instance->getContext()->getCurrentUserId()); | ||
self::assertEquals('456', $instance->getContext()->getIpAddress()); | ||
self::assertEquals('789', $instance->getContext()->getSessionId()); | ||
self::assertNotSame($instance->getContext(), $instance->getContext()); | ||
} | ||
} |
Oops, something went wrong.