Skip to content

Commit

Permalink
Merge pull request #70 from Sybio/2.x-allow-feature-annotation-to-be-…
Browse files Browse the repository at this point in the history
…repeatable

feat: allow Feature annotation to be repeatable
  • Loading branch information
jdecool authored Aug 28, 2024
2 parents 996ee64 + 5084082 commit a29b63c
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Annotation/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @Annotation
* @NamedArgumentConstructor()
*/
#[\Attribute]
#[\Attribute(\Attribute::TARGET_ALL | \Attribute::IS_REPEATABLE)]
class Feature
{
/** @var string */
Expand Down
2 changes: 1 addition & 1 deletion src/DataCollector/FeatureCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(FeatureManager $manager, StorageInterface $storage)
$this->storage = $storage;
}

public function collect(Request $request, Response $response, \Throwable $exception = null): void
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
{
$features = $this->storage->all();
$activeFeatureCount = count(
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Feature implements FeatureInterface
/**
* Constructor
*/
public function __construct(string $key, bool $enabled, string $description = null)
public function __construct(string $key, bool $enabled, ?string $description = null)
{
$this->key = $key;
$this->enabled = $enabled;
Expand Down
32 changes: 32 additions & 0 deletions tests/Fixtures/App/TestBundle/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ public function annotationFooDisabled()
return new Response('DefaultController::annotationFooDisabledAction');
}

/**
* @Feature("foo", enabled=true)
* @Feature("bar", enabled=true)
*/
public function annotationFooEnabledBarEnabled()
{
return new Response('DefaultController::annotationFooEnabledBarEnabledAction');
}

/**
* @Feature("foo", enabled=true)
* @Feature("bar", enabled=false)
*/
public function annotationFooEnabledBarDisabled()
{
return new Response('DefaultController::annotationFooEnabledBarDisabledAction');
}

#[Feature(name: 'foo')]
public function attributeFooEnabled()
{
Expand All @@ -73,4 +91,18 @@ public function attributeFooDisabled()
{
return new Response('DefaultController::attributeFooDisabledAction');
}

#[Feature(name: 'foo', enabled: true)]
#[Feature(name: 'bar', enabled: true)]
public function attributeFooEnabledBarEnabled()
{
return new Response('DefaultController::attributeFooEnabledBarEnabledAction');
}

#[Feature(name: 'foo', enabled: true)]
#[Feature(name: 'bar', enabled: false)]
public function attributeFooEnabledBarDisabled()
{
return new Response('DefaultController::attributeFooEnabledBarDisabledAction');
}
}
17 changes: 17 additions & 0 deletions tests/Fixtures/App/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ annotation_configuration_disabled:
path: /annotation/disabled
defaults: { _controller: Novaway\Bundle\FeatureFlagBundle\Tests\Fixtures\App\TestBundle\Controller\DefaultController::annotationFooDisabled }

annotation_configuration_bar_enabled_foo_enabled:
path: /annotation/bar/enabled/foo/enabled
defaults: { _controller: Novaway\Bundle\FeatureFlagBundle\Tests\Fixtures\App\TestBundle\Controller\DefaultController::annotationFooEnabledBarEnabled }

annotation_configuration_bar_enabled_foo_disabled:
path: /annotation/bar/enabled/foo/disabled
defaults: { _controller: Novaway\Bundle\FeatureFlagBundle\Tests\Fixtures\App\TestBundle\Controller\DefaultController::annotationFooEnabledBarDisabled }


attribute_class_disabled:
path: /attribute/class/disabled
defaults: { _controller: Novaway\Bundle\FeatureFlagBundle\Tests\Fixtures\App\TestBundle\Controller\AttributeClassDisabledController }
Expand All @@ -43,3 +52,11 @@ attribute_configuration_enabled:
attribute_configuration_disabled:
path: /attribute/disabled
defaults: { _controller: Novaway\Bundle\FeatureFlagBundle\Tests\Fixtures\App\TestBundle\Controller\DefaultController::attributeFooDisabled }

attribute_configuration_bar_enabled_foo_enabled:
path: /attribute/bar/enabled/foo/enabled
defaults: { _controller: Novaway\Bundle\FeatureFlagBundle\Tests\Fixtures\App\TestBundle\Controller\DefaultController::attributeFooEnabledBarEnabled }

attribute_configuration_bar_enabled_foo_disabled:
path: /attribute/bar/enabled/foo/disabled
defaults: { _controller: Novaway\Bundle\FeatureFlagBundle\Tests\Fixtures\App\TestBundle\Controller\DefaultController::attributeFooEnabledBarDisabled }
48 changes: 48 additions & 0 deletions tests/Functional/Controller/DefaultControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ public function testAnnotationFooDisabledAction()
static::assertSame(404, static::$client->getResponse()->getStatusCode());
}

/**
* @requires PHP >= 8.0
*/
public function testTwoAnnotationsFooEnabledBarEnabledHasNoAccessBecauseFeatureFooIsEnabledButFeatureBarIsDisabled()
{
static::$client->request('GET', '/annotation/bar/enabled/foo/enabled');

static::assertSame(404, static::$client->getResponse()->getStatusCode());
}

/**
* @requires PHP >= 8.0
*/
public function testTwoAnnotationsFooEnabledBarDisabledHasAccessBecauseFeatureFooIsEnabledAndFeatureBarIsDisabled()
{
$crawler = static::$client->request('GET', '/annotation/bar/enabled/foo/disabled');

static::assertSame(200, static::$client->getResponse()->getStatusCode());
static::assertGreaterThan(
0,
$crawler->filter('html:contains("DefaultController::annotationFooEnabledBarDisabledAction")')->count(),
);
}

/**
* @requires PHP >= 8.0
*/
Expand All @@ -106,4 +130,28 @@ public function testAttributeFooDisabledAction()

static::assertSame(404, static::$client->getResponse()->getStatusCode());
}

/**
* @requires PHP >= 8.0
*/
public function testTwoAttributesFooEnabledBarEnabledHasNoAccessBecauseFeatureFooIsEnabledButFeatureBarIsDisabled()
{
static::$client->request('GET', '/attribute/bar/enabled/foo/enabled');

static::assertSame(404, static::$client->getResponse()->getStatusCode());
}

/**
* @requires PHP >= 8.0
*/
public function testTwoAttributesFooEnabledBarDisabledHasAccessBecauseFeatureFooIsEnabledAndFeatureBarIsDisabled()
{
$crawler = static::$client->request('GET', '/attribute/bar/enabled/foo/disabled');

static::assertSame(200, static::$client->getResponse()->getStatusCode());
static::assertGreaterThan(
0,
$crawler->filter('html:contains("DefaultController::attributeFooEnabledBarDisabledAction")')->count(),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function setUp(): void
$this->featureManager = $this->createMock(FeatureManager::class);

$this->expressionLanguage = new class($this->featureManager) extends ExpressionLanguage {
public function __construct($featureManager, CacheItemPoolInterface $cache = null, array $providers = [])
public function __construct($featureManager, ?CacheItemPoolInterface $cache = null, array $providers = [])
{
array_unshift($providers, new FeatureFlagExpressionLanguageProvider($featureManager));

Expand Down

0 comments on commit a29b63c

Please sign in to comment.