Skip to content

Commit

Permalink
Add environment property to context
Browse files Browse the repository at this point in the history
  • Loading branch information
fenix20113 committed Sep 10, 2021
1 parent 9300303 commit 0a6e201
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Configuration/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

/**
* @method string|null getHostname()
* @method string|null getEnvironment()
* @method Context setHostname(string|null $hostname)
* @method Context setEnvironment(string|null $environment)
*/
interface Context
{
Expand Down
14 changes: 14 additions & 0 deletions src/Configuration/UnleashContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(
private ?string $sessionId = null,
private array $customContext = [],
?string $hostname = null,
private ?string $environment = null,
) {
$this->setHostname($hostname);
}
Expand All @@ -26,6 +27,11 @@ public function getCurrentUserId(): ?string
return $this->currentUserId;
}

public function getEnvironment(): ?string
{
return $this->environment;
}

public function getIpAddress(): ?string
{
return $this->ipAddress ?? $_SERVER['REMOTE_ADDR'] ?? null;
Expand Down Expand Up @@ -89,6 +95,13 @@ public function setSessionId(?string $sessionId): self
return $this;
}

public function setEnvironment(?string $environment): self
{
$this->environment = $environment;

return $this;
}

public function getHostname(): ?string
{
return $this->findContextValue('hostname') ?? (gethostname() ?: null);
Expand Down Expand Up @@ -124,6 +137,7 @@ public function findContextValue(string $fieldName): ?string
ContextField::USER_ID, Stickiness::USER_ID => $this->getCurrentUserId(),
ContextField::SESSION_ID, Stickiness::SESSION_ID => $this->getSessionId(),
ContextField::IP_ADDRESS => $this->getIpAddress(),
ContextField::ENVIRONMENT => $this->getEnvironment(),
default => $this->customContext[$fieldName] ?? null,
};
}
Expand Down
2 changes: 2 additions & 0 deletions src/Enum/ContextField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ final class ContextField

public const IP_ADDRESS = 'remoteAddress';

public const ENVIRONMENT = 'environment';

public const REMOTE_ADDRESS = self::IP_ADDRESS;
}
1 change: 1 addition & 0 deletions tests/ClientSpecificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ private function createContext(array $context): UnleashContext
$contextObject = (new UnleashContext())
->setCurrentUserId($context['userId'] ?? null)
->setSessionId($context['sessionId'] ?? null)
->setEnvironment($context['environment'] ?? null)
->setIpAddress($context['remoteAddress'] ?? '');

if (isset($context['properties'])) {
Expand Down
6 changes: 5 additions & 1 deletion tests/Configuration/UnleashContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testHasMatchingFieldValue()
{
unset($_SERVER['REMOTE_ADDR']);

$context = (new UnleashContext('123', '456', '789'))
$context = (new UnleashContext('123', '456', '789', [], null, 'dev'))
->setCustomProperty('someField', '012');

self::assertTrue($context->hasMatchingFieldValue(ContextField::USER_ID, [
Expand All @@ -77,6 +77,10 @@ public function testHasMatchingFieldValue()
'852',
'789',
]));
self::assertTrue($context->hasMatchingFieldValue(ContextField::ENVIRONMENT, [
'dev',
'production',
]));
self::assertTrue($context->hasMatchingFieldValue('someField', [
'753',
'012',
Expand Down
7 changes: 5 additions & 2 deletions tests/ContextProvider/DefaultUnleashContextProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,29 @@ public function testGetContext()
self::assertNull($instance->getContext()->getCurrentUserId());
self::assertNull($instance->getContext()->getIpAddress());
self::assertNull($instance->getContext()->getSessionId());
self::assertNull($instance->getContext()->getEnvironment());
self::assertNotSame($instance->getContext(), $instance->getContext());

$instance = new DefaultUnleashContextProvider(new UnleashContext('123', '456', '789'));
$instance = new DefaultUnleashContextProvider(new UnleashContext('123', '456', '789', [], null, 'dev'));
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::assertEquals('dev', $instance->getContext()->getEnvironment());
self::assertNotSame($instance->getContext(), $instance->getContext());
}

public function testSetDefaultContext()
{
$instance = new DefaultUnleashContextProvider();
$context = new UnleashContext('123', '456', '789');
$context = new UnleashContext('123', '456', '789', [], null, 'dev');
$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::assertEquals('dev', $instance->getContext()->getEnvironment());
self::assertNotSame($instance->getContext(), $instance->getContext());
}
}

0 comments on commit 0a6e201

Please sign in to comment.