diff --git a/src/Exceptions/FileNotFound.php b/src/Exceptions/FileNotFound.php new file mode 100644 index 0000000..5ee6302 --- /dev/null +++ b/src/Exceptions/FileNotFound.php @@ -0,0 +1,16 @@ +path; + } +} diff --git a/src/Exceptions/GatewayConfigurationMissing.php b/src/Exceptions/GatewayConfigurationMissing.php new file mode 100644 index 0000000..2ddaf8b --- /dev/null +++ b/src/Exceptions/GatewayConfigurationMissing.php @@ -0,0 +1,16 @@ +gateway; + } +} diff --git a/src/Exceptions/UnableToLoadFlags.php b/src/Exceptions/UnableToLoadFlags.php new file mode 100644 index 0000000..16525f7 --- /dev/null +++ b/src/Exceptions/UnableToLoadFlags.php @@ -0,0 +1,16 @@ +path; + } +} diff --git a/src/Manager.php b/src/Manager.php index 8afc391..d83fc97 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -20,6 +20,7 @@ use YlsIdeas\FeatureFlags\Events\FeatureSwitchedOff; use YlsIdeas\FeatureFlags\Events\FeatureSwitchedOn; use YlsIdeas\FeatureFlags\Exceptions\FeatureExpired; +use YlsIdeas\FeatureFlags\Exceptions\GatewayConfigurationMissing; use YlsIdeas\FeatureFlags\Gateways\DatabaseGateway; use YlsIdeas\FeatureFlags\Gateways\GateGateway; use YlsIdeas\FeatureFlags\Gateways\InMemoryGateway; @@ -334,7 +335,7 @@ protected function buildRedisGateway(array $config): RedisGateway protected function buildGateGateway(array $config, string $name): GateGateway { if (! ($config['gate'] ?? false)) { - throw new \RuntimeException(sprintf('No gate is configured for gateway `%s`', $name)); + throw new GatewayConfigurationMissing(sprintf('No gate is configured for gateway `%s`', $name), $name); } return new GateGateway( diff --git a/src/Support/FeaturesFileDiscoverer.php b/src/Support/FeaturesFileDiscoverer.php index 137b04e..6987d58 100644 --- a/src/Support/FeaturesFileDiscoverer.php +++ b/src/Support/FeaturesFileDiscoverer.php @@ -4,6 +4,7 @@ use Illuminate\Contracts\Foundation\Application; use Illuminate\Support\Str; +use YlsIdeas\FeatureFlags\Exceptions\FileNotFound; /** * @see \YlsIdeas\FeatureFlags\Tests\Support\FeaturesFileDiscovererTest @@ -30,6 +31,6 @@ public function find(): string return $path; } - throw new \RuntimeException(sprintf('`%s` file could not be found.', $this->file)); + throw (new FileNotFound(sprintf('`%s` file could not be found.', $this->file), $path)); } } diff --git a/src/Support/FileLoader.php b/src/Support/FileLoader.php index 9001985..a52f298 100644 --- a/src/Support/FileLoader.php +++ b/src/Support/FileLoader.php @@ -4,6 +4,7 @@ use Illuminate\Contracts\Container\Container; use YlsIdeas\FeatureFlags\Contracts\InMemoryLoader; +use YlsIdeas\FeatureFlags\Exceptions\UnableToLoadFlags; /** * @see \YlsIdeas\FeatureFlags\Tests\Support\FileLoaderTest @@ -18,7 +19,7 @@ public function load(): array { $callable = require($file = $this->discoverer->find()); if (! is_callable($callable)) { - throw new \RuntimeException(sprintf('File `%s` does not return a callable', $file)); + throw new UnableToLoadFlags(sprintf('File `%s` does not return a callable', $file), $file); } return $this->container->call($callable); diff --git a/tests/Support/FeaturesFileDiscovererTest.php b/tests/Support/FeaturesFileDiscovererTest.php index b30ff27..6e1888a 100644 --- a/tests/Support/FeaturesFileDiscovererTest.php +++ b/tests/Support/FeaturesFileDiscovererTest.php @@ -6,6 +6,7 @@ use Illuminate\Filesystem\Filesystem; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use PHPUnit\Framework\TestCase; +use YlsIdeas\FeatureFlags\Exceptions\FileNotFound; use YlsIdeas\FeatureFlags\Support\FeaturesFileDiscoverer; class FeaturesFileDiscovererTest extends TestCase @@ -116,7 +117,8 @@ public function test_it_throws_an_exception_if_no_file_is_discovered(): void $discoverer = new FeaturesFileDiscoverer($app, '.features.php'); - $this->expectException(\RuntimeException::class); + $this->expectException(FileNotFound::class); + $this->expectExceptionMessage('.features.php` file could not be found.'); $discoverer->find(); } diff --git a/tests/Support/FileLoaderTest.php b/tests/Support/FileLoaderTest.php index f65e08c..10a4f9a 100644 --- a/tests/Support/FileLoaderTest.php +++ b/tests/Support/FileLoaderTest.php @@ -5,6 +5,7 @@ use Illuminate\Contracts\Container\Container; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use PHPUnit\Framework\TestCase; +use YlsIdeas\FeatureFlags\Exceptions\UnableToLoadFlags; use YlsIdeas\FeatureFlags\Support\FeaturesFileDiscoverer; use YlsIdeas\FeatureFlags\Support\FileLoader; @@ -36,4 +37,29 @@ public function test_it_loads_features_from_php_file(): void $this->assertSame($features, $results); } + + public function test_it_throws_an_exception_if_a_callable_isnt_returned(): void + { + $discoverer = \Mockery::mock(FeaturesFileDiscoverer::class); + $container = \Mockery::mock(Container::class); + + $features = [ + 'my-feature' => true, + ]; + + $discoverer->shouldReceive('find') + ->once() + ->andReturn(__DIR__ . '/../fixtures/features-non-callable.php'); + + $loader = new FileLoader($discoverer, $container); + + $this->expectException(UnableToLoadFlags::class); + $this->expectExceptionMessage( + 'File `' . + __DIR__ . '/../fixtures/features-non-callable.php' . + '` does not return a callable' + ); + + $loader->load(); + } } diff --git a/tests/fixtures/features-non-callable.php b/tests/fixtures/features-non-callable.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/tests/fixtures/features-non-callable.php @@ -0,0 +1 @@ +