Files
michaelschiemer/tests/Unit/Framework/FeatureFlags/FeatureFlagManagerTest.php
Michael Schiemer fc3d7e6357 feat(Production): Complete production deployment infrastructure
- Add comprehensive health check system with multiple endpoints
- Add Prometheus metrics endpoint
- Add production logging configurations (5 strategies)
- Add complete deployment documentation suite:
  * QUICKSTART.md - 30-minute deployment guide
  * DEPLOYMENT_CHECKLIST.md - Printable verification checklist
  * DEPLOYMENT_WORKFLOW.md - Complete deployment lifecycle
  * PRODUCTION_DEPLOYMENT.md - Comprehensive technical reference
  * production-logging.md - Logging configuration guide
  * ANSIBLE_DEPLOYMENT.md - Infrastructure as Code automation
  * README.md - Navigation hub
  * DEPLOYMENT_SUMMARY.md - Executive summary
- Add deployment scripts and automation
- Add DEPLOYMENT_PLAN.md - Concrete plan for immediate deployment
- Update README with production-ready features

All production infrastructure is now complete and ready for deployment.
2025-10-25 19:18:37 +02:00

182 lines
6.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Framework\FeatureFlags;
use App\Framework\FeatureFlags\FeatureFlag;
use App\Framework\FeatureFlags\FeatureFlagContext;
use App\Framework\FeatureFlags\FeatureFlagManager;
use App\Framework\FeatureFlags\FeatureFlagRepository;
use App\Framework\FeatureFlags\FeatureFlagStatus;
use App\Framework\Core\ValueObjects\Timestamp;
describe('FeatureFlagManager', function () {
beforeEach(function () {
// Create in-memory repository for testing
$this->repository = new class implements FeatureFlagRepository {
private array $flags = [];
public function find(string $name): ?FeatureFlag
{
return $this->flags[$name] ?? null;
}
public function findAll(): array
{
return array_values($this->flags);
}
public function save(FeatureFlag $flag): void
{
$this->flags[$flag->name] = $flag;
}
public function delete(string $name): void
{
unset($this->flags[$name]);
}
public function exists(string $name): bool
{
return isset($this->flags[$name]);
}
};
$this->manager = new FeatureFlagManager($this->repository);
});
it('enables a feature flag', function () {
$this->manager->enable('test-feature', 'Test description');
expect($this->manager->isEnabled('test-feature'))->toBeTrue();
expect($this->manager->exists('test-feature'))->toBeTrue();
});
it('disables a feature flag', function () {
$this->manager->enable('test-feature');
$this->manager->disable('test-feature');
expect($this->manager->isDisabled('test-feature'))->toBeTrue();
});
it('returns false for non-existent flag', function () {
expect($this->manager->isEnabled('non-existent'))->toBeFalse();
});
it('sets conditional feature flag', function () {
$this->manager->setConditional('user-feature', ['user_id' => [1, 2, 3]]);
$matchingContext = (new FeatureFlagContext())->withUserId('2');
$nonMatchingContext = (new FeatureFlagContext())->withUserId('99');
expect($this->manager->isEnabled('user-feature', $matchingContext))->toBeTrue();
expect($this->manager->isEnabled('user-feature', $nonMatchingContext))->toBeFalse();
});
it('sets percentage rollout', function () {
$this->manager->setPercentageRollout('rollout-feature', 50);
$flag = $this->manager->getFlag('rollout-feature');
expect($flag)->not->toBeNull();
expect($flag->conditions)->toHaveKey('percentage');
expect($flag->conditions['percentage'])->toBe(50);
});
it('validates percentage range', function () {
expect(fn () => $this->manager->setPercentageRollout('invalid', 150))
->toThrow(\InvalidArgumentException::class);
expect(fn () => $this->manager->setPercentageRollout('invalid', -10))
->toThrow(\InvalidArgumentException::class);
});
it('sets user-specific flag', function () {
$this->manager->setForUsers('user-only', [1, 2, 3]);
$flag = $this->manager->getFlag('user-only');
expect($flag->conditions)->toHaveKey('user_id');
expect($flag->conditions['user_id'])->toBe([1, 2, 3]);
});
it('sets environment-specific flag', function () {
$this->manager->setForEnvironment('env-feature', 'production');
$prodContext = (new FeatureFlagContext())->withEnvironment('production');
$devContext = (new FeatureFlagContext())->withEnvironment('development');
expect($this->manager->isEnabled('env-feature', $prodContext))->toBeTrue();
expect($this->manager->isEnabled('env-feature', $devContext))->toBeFalse();
});
it('sets expiration', function () {
$this->manager->enable('temp-feature');
$expiry = Timestamp::fromTimestamp(time() + 3600);
$this->manager->setExpiration('temp-feature', $expiry);
$flag = $this->manager->getFlag('temp-feature');
expect($flag->expiresAt)->toBe($expiry);
});
it('throws exception when setting expiration on non-existent flag', function () {
$expiry = Timestamp::fromTimestamp(time() + 3600);
expect(fn () => $this->manager->setExpiration('non-existent', $expiry))
->toThrow(\InvalidArgumentException::class);
});
it('deletes a feature flag', function () {
$this->manager->enable('delete-me');
expect($this->manager->exists('delete-me'))->toBeTrue();
$this->manager->deleteFlag('delete-me');
expect($this->manager->exists('delete-me'))->toBeFalse();
});
it('gets all feature flags', function () {
$this->manager->enable('flag1');
$this->manager->enable('flag2');
$this->manager->disable('flag3');
$allFlags = $this->manager->getAllFlags();
expect($allFlags)->toHaveCount(3);
});
it('generates status summary', function () {
$this->manager->enable('enabled1');
$this->manager->enable('enabled2');
$this->manager->disable('disabled1');
$this->manager->setConditional('conditional1', ['user_id' => [1]]);
$summary = $this->manager->getStatusSummary();
expect($summary['total'])->toBe(4);
expect($summary['enabled'])->toBe(2);
expect($summary['disabled'])->toBe(1);
expect($summary['conditional'])->toBe(1);
});
it('uses default context when none provided', function () {
$defaultContext = (new FeatureFlagContext())->withUserId('5');
$manager = new FeatureFlagManager($this->repository, $defaultContext);
$manager->setConditional('user-feature', ['user_id' => [5, 6, 7]]);
// Should use default context
expect($manager->isEnabled('user-feature'))->toBeTrue();
});
it('counts expired flags in summary', function () {
$this->manager->enable('expired-feature');
$pastExpiry = Timestamp::fromTimestamp(time() - 3600);
$this->manager->setExpiration('expired-feature', $pastExpiry);
$summary = $this->manager->getStatusSummary();
expect($summary['expired'])->toBe(1);
});
});