Repository: pheature-flags/pheature-flags (php)

Issue:
[mezzio-toggle] Make as optional the installation of some pheature packages
Epic: #24 Mezzio Toggle
Package: https://github.com/pheature-flags/mezzio-toggle

Description:

Right now, if you want to run Pheature Flags using the Mezzio Toggle, you need to install all packages of the Pheature framework. We should make them optional, adding some checks to the `ToggleConfigProvider` to add services only if they've been previously installed.

Pull request:
[#261][pheature-flags] fix dependency issues between some packages an psr-11 package
Closes #261 

Patch:
diff --git a/packages/dbal-toggle/.php-version b/packages/dbal-toggle/.php-version
new file mode 100644
index 0000000..37722eb
--- /dev/null
+++ b/packages/dbal-toggle/.php-version
@@ -0,0 +1,1 @@
+7.4
diff --git a/packages/toggle-crud-psr11-factories/src/Container/ConfigProvider.php b/packages/toggle-crud-psr11-factories/src/Container/ConfigProvider.php
index 8ef37c0..f8c3f7e 100644
--- a/packages/toggle-crud-psr11-factories/src/Container/ConfigProvider.php
+++ b/packages/toggle-crud-psr11-factories/src/Container/ConfigProvider.php
@@ -4,7 +4,7 @@ declare(strict_types=1);
 
 namespace Pheature\Crud\Psr11\Toggle\Container;
 
-use Pheature\Community\Mezzio\ToggleConfigFactory;
+use Pheature\Crud\Psr11\Toggle\ToggleConfigFactory;
 use Pheature\Crud\Psr11\Toggle\ToggleConfig;
 
 final class ConfigProvider
diff --git a/packages/mezzio-toggle/src/ToggleConfigFactory.php b/packages/toggle-crud-psr11-factories/src/ToggleConfigFactory.php
similarity index 50%
rename from packages/mezzio-toggle/src/ToggleConfigFactory.php
rename to packages/toggle-crud-psr11-factories/src/ToggleConfigFactory.php
index be1799e..31af5f2 100644
--- a/packages/mezzio-toggle/src/ToggleConfigFactory.php
+++ b/packages/toggle-crud-psr11-factories/src/ToggleConfigFactory.php
@@ -2,22 +2,22 @@
 
 declare(strict_types=1);
 
-namespace Pheature\Community\Mezzio;
+namespace Pheature\Crud\Psr11\Toggle;
 
-use Pheature\Crud\Psr11\Toggle\ToggleConfig;
 use Psr\Container\ContainerInterface;
-use UnexpectedValueException;
+use Webmozart\Assert\Assert;
 
 final class ToggleConfigFactory
 {
+    private const MISSING_CONFIG = '"pheature_flags" configuration not found in container';
+
     public function __invoke(ContainerInterface $container): ToggleConfig
     {
-        /** @var array<string, mixed> $config */
+        /** @var array<string, mixed>|mixed $config */
         $config = $container->get('config');
-
-        if (empty($config['pheature_flags'])) {
-            throw new UnexpectedValueException('"pheature_flags" configuration not found in container');
-        }
+        Assert::isArray($config);
+        Assert::keyExists($config, 'pheature_flags', self::MISSING_CONFIG);
+        Assert::isArray($config['pheature_flags'], self::MISSING_CONFIG);
 
         /** @var array<string, mixed> $pheatureConfig */
         $pheatureConfig = $config['pheature_flags'];
diff --git a/packages/toggle-crud-psr7-api/src/Container/ConfigProvider.php b/packages/toggle-crud-psr7-api/src/Container/ConfigProvider.php
index 6c5b957..729906b 100644
--- a/packages/toggle-crud-psr7-api/src/Container/ConfigProvider.php
+++ b/packages/toggle-crud-psr7-api/src/Container/ConfigProvider.php
@@ -4,11 +4,11 @@ declare(strict_types=1);
 
 namespace Pheature\Crud\Psr7\Toggle\Container;
 
-use Pheature\Crud\Psr11\Toggle\DeleteFeatureFactory;
-use Pheature\Crud\Psr11\Toggle\GetFeatureFactory;
-use Pheature\Crud\Psr11\Toggle\GetFeaturesFactory;
-use Pheature\Crud\Psr11\Toggle\PatchFeatureFactory;
-use Pheature\Crud\Psr11\Toggle\PostFeatureFactory;
+use Pheature\Crud\Psr7\Toggle\DeleteFeatureFactory;
+use Pheature\Crud\Psr7\Toggle\GetFeatureFactory;
+use Pheature\Crud\Psr7\Toggle\GetFeaturesFactory;
+use Pheature\Crud\Psr7\Toggle\PatchFeatureFactory;
+use Pheature\Crud\Psr7\Toggle\PostFeatureFactory;
 use Pheature\Crud\Psr7\Toggle\DeleteFeature;
 use Pheature\Crud\Psr7\Toggle\GetFeature;
 use Pheature\Crud\Psr7\Toggle\GetFeatures;
diff --git a/packages/toggle-crud-psr11-factories/src/DeleteFeatureFactory.php b/packages/toggle-crud-psr7-api/src/DeleteFeatureFactory.php
similarity index 95%
rename from packages/toggle-crud-psr11-factories/src/DeleteFeatureFactory.php
rename to packages/toggle-crud-psr7-api/src/DeleteFeatureFactory.php
index eddd4a2..d9fe7c9 100644
--- a/packages/toggle-crud-psr11-factories/src/DeleteFeatureFactory.php
+++ b/packages/toggle-crud-psr7-api/src/DeleteFeatureFactory.php
@@ -2,7 +2,7 @@
 
 declare(strict_types=1);
 
-namespace Pheature\Crud\Psr11\Toggle;
+namespace Pheature\Crud\Psr7\Toggle;
 
 use Pheature\Crud\Psr7\Toggle\DeleteFeature;
 use Pheature\Crud\Toggle\Handler\RemoveFeature;
diff --git a/packages/toggle-crud-psr11-factories/src/GetFeatureFactory.php b/packages/toggle-crud-psr7-api/src/GetFeatureFactory.php
similarity index 95%
rename from packages/toggle-crud-psr11-factories/src/GetFeatureFactory.php
rename to packages/toggle-crud-psr7-api/src/GetFeatureFactory.php
index a7b394d..23a8cb0 100644
--- a/packages/toggle-crud-psr11-factories/src/GetFeatureFactory.php
+++ b/packages/toggle-crud-psr7-api/src/GetFeatureFactory.php
@@ -2,7 +2,7 @@
 
 declare(strict_types=1);
 
-namespace Pheature\Crud\Psr11\Toggle;
+namespace Pheature\Crud\Psr7\Toggle;
 
 use Pheature\Core\Toggle\Read\FeatureFinder;
 use Pheature\Crud\Psr7\Toggle\GetFeature;
diff --git a/packages/toggle-crud-psr11-factories/src/GetFeaturesFactory.php b/packages/toggle-crud-psr7-api/src/GetFeaturesFactory.php
similarity index 95%
rename from packages/toggle-crud-psr11-factories/src/GetFeaturesFactory.php
rename to packages/toggle-crud-psr7-api/src/GetFeaturesFactory.php
index 4cc3490..a17082c 100644
--- a/packages/toggle-crud-psr11-factories/src/GetFeaturesFactory.php
+++ b/packages/toggle-crud-psr7-api/src/GetFeaturesFactory.php
@@ -2,7 +2,7 @@
 
 declare(strict_types=1);
 
-namespace Pheature\Crud\Psr11\Toggle;
+namespace Pheature\Crud\Psr7\Toggle;
 
 use Pheature\Core\Toggle\Read\FeatureFinder;
 use Pheature\Crud\Psr7\Toggle\GetFeatures;
diff --git a/packages/toggle-crud-psr11-factories/src/PatchFeatureFactory.php b/packages/toggle-crud-psr7-api/src/PatchFeatureFactory.php
similarity index 94%
rename from packages/toggle-crud-psr11-factories/src/PatchFeatureFactory.php
rename to packages/toggle-crud-psr7-api/src/PatchFeatureFactory.php
index a0eda2a..039cc49 100644
--- a/packages/toggle-crud-psr11-factories/src/PatchFeatureFactory.php
+++ b/packages/toggle-crud-psr7-api/src/PatchFeatureFactory.php
@@ -2,9 +2,8 @@
 
 declare(strict_types=1);
 
-namespace Pheature\Crud\Psr11\Toggle;
+namespace Pheature\Crud\Psr7\Toggle;
 
-use Pheature\Crud\Psr7\Toggle\PatchFeature;
 use Pheature\Crud\Toggle\Handler\SetStrategy;
 use Pheature\Crud\Toggle\Handler\DisableFeature;
 use Pheature\Crud\Toggle\Handler\EnableFeature;
diff --git a/packages/toggle-crud-psr11-factories/src/PostFeatureFactory.php b/packages/toggle-crud-psr7-api/src/PostFeatureFactory.php
similarity index 95%
rename from packages/toggle-crud-psr11-factories/src/PostFeatureFactory.php
rename to packages/toggle-crud-psr7-api/src/PostFeatureFactory.php
index 52275bc..a1f1a58 100644
--- a/packages/toggle-crud-psr11-factories/src/PostFeatureFactory.php
+++ b/packages/toggle-crud-psr7-api/src/PostFeatureFactory.php
@@ -2,7 +2,7 @@
 
 declare(strict_types=1);
 
-namespace Pheature\Crud\Psr11\Toggle;
+namespace Pheature\Crud\Psr7\Toggle;
 
 use Pheature\Crud\Psr7\Toggle\PostFeature;
 use Pheature\Crud\Toggle\Handler\CreateFeature;


Test patch:
diff --git a/packages/mezzio-toggle/test/ToggleConfigFactoryTest.php b/packages/mezzio-toggle/test/ToggleConfigFactoryTest.php
deleted file mode 100644
index 44dc610..0000000
--- a/packages/mezzio-toggle/test/ToggleConfigFactoryTest.php
+++ /dev/null
@@ -1,41 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace Pheature\Test\Community\Mezzio;
-
-use Pheature\Community\Mezzio\ToggleConfigFactory;
-use Pheature\Crud\Psr11\Toggle\ToggleConfig;
-use Pheature\Test\Community\Mezzio\Fixtures\TestContainerFactory;
-use PHPUnit\Framework\TestCase;
-use Psr\Container\NotFoundExceptionInterface;
-use UnexpectedValueException;
-
-final class ToggleConfigFactoryTest extends TestCase
-{
-    public function testItShouldThrowAnExceptionWhenNoConfigurationFoundInContainer(): void
-    {
-        $this->expectException(NotFoundExceptionInterface::class);
-        $container = TestContainerFactory::create();
-
-        (new ToggleConfigFactory())->__invoke($container);
-    }
-
-    public function testItShouldThrowAnExceptionWhenNoPheatureFlagsConfigurationFound(): void
-    {
-        $this->expectException(UnexpectedValueException::class);
-
-        $container = TestContainerFactory::createWithEmptyConfiguration();
-
-        (new ToggleConfigFactory())->__invoke($container);
-    }
-
-    public function testItCreatesAToggleConfig(): void
-    {
-        $container = TestContainerFactory::createWithPheatureFlagsConfiguration();
-
-        $actual = (new ToggleConfigFactory())->__invoke($container);
-
-        self::assertInstanceOf(ToggleConfig::class, $actual);
-    }
-}
diff --git a/packages/toggle-crud-psr11-factories/test/Container/ConfigProviderTest.php b/packages/toggle-crud-psr11-factories/test/Container/ConfigProviderTest.php
index 2da5789..e3149c9 100644
--- a/packages/toggle-crud-psr11-factories/test/Container/ConfigProviderTest.php
+++ b/packages/toggle-crud-psr11-factories/test/Container/ConfigProviderTest.php
@@ -4,7 +4,7 @@ declare(strict_types=1);
 
 namespace Pheature\Test\Crud\Psr11\Toggle\Container;
 
-use Pheature\Community\Mezzio\ToggleConfigFactory;
+use Pheature\Crud\Psr11\Toggle\ToggleConfigFactory;
 use Pheature\Crud\Psr11\Toggle\Container\ConfigProvider;
 use Pheature\Crud\Psr11\Toggle\ToggleConfig;
 use PHPUnit\Framework\TestCase;
diff --git a/packages/toggle-crud-psr11-factories/test/ToggleConfigFactoryTest.php b/packages/toggle-crud-psr11-factories/test/ToggleConfigFactoryTest.php
new file mode 100644
index 0000000..6584e53
--- /dev/null
+++ b/packages/toggle-crud-psr11-factories/test/ToggleConfigFactoryTest.php
@@ -0,0 +1,90 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Pheature\Test\Crud\Psr11\Toggle;
+
+use InvalidArgumentException;
+use Pheature\Crud\Psr11\Toggle\ToggleConfigFactory;
+use Pheature\Crud\Psr11\Toggle\ToggleConfig;
+use Pheature\Model\Toggle\EnableByMatchingIdentityId;
+use Pheature\Model\Toggle\EnableByMatchingSegment;
+use Pheature\Model\Toggle\IdentitySegment;
+use Pheature\Model\Toggle\SegmentFactory;
+use Pheature\Model\Toggle\StrategyFactory;
+use Pheature\Model\Toggle\StrictMatchingSegment;
+use PHPUnit\Framework\TestCase;
+use Psr\Container\ContainerInterface;
+use Psr\Container\NotFoundExceptionInterface;
+use UnexpectedValueException;
+
+final class ToggleConfigFactoryTest extends TestCase
+{
+    private const DEFAULT_CONFIG =[
+        'pheature_flags' => [
+            'driver' => 'inmemory',
+            'api_prefix' => '',
+            'api_enabled' => false,
+            'segment_types' => [
+                [
+                    'type' => IdentitySegment::NAME,
+                    'factory_id' => SegmentFactory::class
+                ],
+                [
+                    'type' => StrictMatchingSegment::NAME,
+                    'factory_id' => SegmentFactory::class
+                ]
+            ],
+            'strategy_types' => [
+                [
+                    'type' => EnableByMatchingSegment::NAME,
+                    'factory_id' => StrategyFactory::class
+                ],
+                [
+                    'type' => EnableByMatchingIdentityId::NAME,
+                    'factory_id' => StrategyFactory::class
+                ]
+            ],
+            'toggles' => [],
+        ]
+    ];
+
+    public function testItShouldThrowAnExceptionWhenNoConfigurationFoundInContainer(): void
+    {
+        $this->expectException(InvalidArgumentException::class);
+        $this->expectExceptionMessage('"pheature_flags" configuration not found in container');
+        $container = $this->createMock(ContainerInterface::class);
+        $container->expects(self::once())
+            ->method('get')
+            ->with('config')
+            ->willReturn([]);
+
+        (new ToggleConfigFactory())->__invoke($container);
+    }
+
+    public function testItShouldThrowAnExceptionWhenNoPheatureFlagsConfigurationFound(): void
+    {
+        $this->expectException(InvalidArgumentException::class);
+
+        $container = $this->createMock(ContainerInterface::class);
+        $container->expects(self::once())
+            ->method('get')
+            ->with('config')
+            ->willReturn(['pheature_flags' => []]);
+
+        (new ToggleConfigFactory())->__invoke($container);
+    }
+
+    public function testItCreatesAToggleConfig(): void
+    {
+        $container = $this->createMock(ContainerInterface::class);
+        $container->expects(self::once())
+            ->method('get')
+            ->with('config')
+            ->willReturn(self::DEFAULT_CONFIG);
+
+        $actual = (new ToggleConfigFactory())->__invoke($container);
+
+        self::assertInstanceOf(ToggleConfig::class, $actual);
+    }
+}
diff --git a/packages/toggle-crud-psr7-api/test/Container/ConfigProviderTest.php b/packages/toggle-crud-psr7-api/test/Container/ConfigProviderTest.php
index dc2b381..80b5005 100644
--- a/packages/toggle-crud-psr7-api/test/Container/ConfigProviderTest.php
+++ b/packages/toggle-crud-psr7-api/test/Container/ConfigProviderTest.php
@@ -4,11 +4,11 @@ declare(strict_types=1);
 
 namespace Pheature\Test\Crud\Psr7\Toggle\Container;
 
-use Pheature\Crud\Psr11\Toggle\DeleteFeatureFactory;
-use Pheature\Crud\Psr11\Toggle\GetFeatureFactory;
-use Pheature\Crud\Psr11\Toggle\GetFeaturesFactory;
-use Pheature\Crud\Psr11\Toggle\PatchFeatureFactory;
-use Pheature\Crud\Psr11\Toggle\PostFeatureFactory;
+use Pheature\Crud\Psr7\Toggle\DeleteFeatureFactory;
+use Pheature\Crud\Psr7\Toggle\GetFeatureFactory;
+use Pheature\Crud\Psr7\Toggle\GetFeaturesFactory;
+use Pheature\Crud\Psr7\Toggle\PatchFeatureFactory;
+use Pheature\Crud\Psr7\Toggle\PostFeatureFactory;
 use Pheature\Crud\Psr7\Toggle\Container\ConfigProvider;
 use Pheature\Crud\Psr7\Toggle\DeleteFeature;
 use Pheature\Crud\Psr7\Toggle\GetFeature;
diff --git a/packages/toggle-crud-psr11-factories/test/DeleteFeatureFactoryTest.php b/packages/toggle-crud-psr7-api/test/DeleteFeatureFactoryTest.php
similarity index 94%
rename from packages/toggle-crud-psr11-factories/test/DeleteFeatureFactoryTest.php
rename to packages/toggle-crud-psr7-api/test/DeleteFeatureFactoryTest.php
index fec3353..e5ddb93 100644
--- a/packages/toggle-crud-psr11-factories/test/DeleteFeatureFactoryTest.php
+++ b/packages/toggle-crud-psr7-api/test/DeleteFeatureFactoryTest.php
@@ -1,9 +1,9 @@
 <?php
 
-namespace Pheature\Test\Crud\Psr11\Toggle;
+namespace Pheature\Test\Crud\Psr7\Toggle;
 
 use Pheature\Core\Toggle\Write\FeatureRepository;
-use Pheature\Crud\Psr11\Toggle\DeleteFeatureFactory;
+use Pheature\Crud\Psr7\Toggle\DeleteFeatureFactory;
 use Pheature\Crud\Psr7\Toggle\DeleteFeature;
 use Pheature\Crud\Toggle\Handler\RemoveFeature;
 use PHPUnit\Framework\TestCase;
diff --git a/packages/toggle-crud-psr11-factories/test/GetFeatureFactoryTest.php b/packages/toggle-crud-psr7-api/test/GetFeatureFactoryTest.php
similarity index 93%
rename from packages/toggle-crud-psr11-factories/test/GetFeatureFactoryTest.php
rename to packages/toggle-crud-psr7-api/test/GetFeatureFactoryTest.php
index cfe2d68..a4af181 100644
--- a/packages/toggle-crud-psr11-factories/test/GetFeatureFactoryTest.php
+++ b/packages/toggle-crud-psr7-api/test/GetFeatureFactoryTest.php
@@ -1,9 +1,9 @@
 <?php
 
-namespace Pheature\Test\Crud\Psr11\Toggle;
+namespace Pheature\Test\Crud\Psr7\Toggle;
 
 use Pheature\Core\Toggle\Read\FeatureFinder;
-use Pheature\Crud\Psr11\Toggle\GetFeatureFactory;
+use Pheature\Crud\Psr7\Toggle\GetFeatureFactory;
 use Pheature\Crud\Psr7\Toggle\GetFeature;
 use PHPUnit\Framework\TestCase;
 use Psr\Container\ContainerInterface;
diff --git a/packages/toggle-crud-psr11-factories/test/GetFeaturesFactoryTest.php b/packages/toggle-crud-psr7-api/test/GetFeaturesFactoryTest.php
similarity index 93%
rename from packages/toggle-crud-psr11-factories/test/GetFeaturesFactoryTest.php
rename to packages/toggle-crud-psr7-api/test/GetFeaturesFactoryTest.php
index ad976e8..a9b8c00 100644
--- a/packages/toggle-crud-psr11-factories/test/GetFeaturesFactoryTest.php
+++ b/packages/toggle-crud-psr7-api/test/GetFeaturesFactoryTest.php
@@ -1,9 +1,9 @@
 <?php
 
-namespace Pheature\Test\Crud\Psr11\Toggle;
+namespace Pheature\Test\Crud\Psr7\Toggle;
 
 use Pheature\Core\Toggle\Read\FeatureFinder;
-use Pheature\Crud\Psr11\Toggle\GetFeaturesFactory;
+use Pheature\Crud\Psr7\Toggle\GetFeaturesFactory;
 use Pheature\Crud\Psr7\Toggle\GetFeatures;
 use PHPUnit\Framework\TestCase;
 use Psr\Container\ContainerInterface;
diff --git a/packages/toggle-crud-psr11-factories/test/PatchFeatureFactoryTest.php b/packages/toggle-crud-psr7-api/test/PatchFeatureFactoryTest.php
similarity index 95%
rename from packages/toggle-crud-psr11-factories/test/PatchFeatureFactoryTest.php
rename to packages/toggle-crud-psr7-api/test/PatchFeatureFactoryTest.php
index 8bad00d..457d065 100644
--- a/packages/toggle-crud-psr11-factories/test/PatchFeatureFactoryTest.php
+++ b/packages/toggle-crud-psr7-api/test/PatchFeatureFactoryTest.php
@@ -1,9 +1,9 @@
 <?php
 
-namespace Pheature\Test\Crud\Psr11\Toggle;
+namespace Pheature\Test\Crud\Psr7\Toggle;
 
 use Pheature\Core\Toggle\Write\FeatureRepository;
-use Pheature\Crud\Psr11\Toggle\PatchFeatureFactory;
+use Pheature\Crud\Psr7\Toggle\PatchFeatureFactory;
 use Pheature\Crud\Psr7\Toggle\PatchFeature;
 use Pheature\Crud\Toggle\Handler\SetStrategy;
 use Pheature\Crud\Toggle\Handler\DisableFeature;
diff --git a/packages/toggle-crud-psr11-factories/test/PostFeatureFactoryTest.php b/packages/toggle-crud-psr7-api/test/PostFeatureFactoryTest.php
similarity index 94%
rename from packages/toggle-crud-psr11-factories/test/PostFeatureFactoryTest.php
rename to packages/toggle-crud-psr7-api/test/PostFeatureFactoryTest.php
index 73344bf..787362d 100644
--- a/packages/toggle-crud-psr11-factories/test/PostFeatureFactoryTest.php
+++ b/packages/toggle-crud-psr7-api/test/PostFeatureFactoryTest.php
@@ -1,9 +1,9 @@
 <?php
 
-namespace Pheature\Test\Crud\Psr11\Toggle;
+namespace Pheature\Test\Crud\Psr7\Toggle;
 
 use Pheature\Core\Toggle\Write\FeatureRepository;
-use Pheature\Crud\Psr11\Toggle\PostFeatureFactory;
+use Pheature\Crud\Psr7\Toggle\PostFeatureFactory;
 use Pheature\Crud\Psr7\Toggle\PostFeature;
 use Pheature\Crud\Toggle\Handler\CreateFeature;
 use PHPUnit\Framework\TestCase;