Skip to content

Commit a4d0bbd

Browse files
committed
Improved exceptions and tests for them
1 parent 57a5d96 commit a4d0bbd

File tree

9 files changed

+84
-4
lines changed

9 files changed

+84
-4
lines changed

src/Exceptions/FileNotFound.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace YlsIdeas\FeatureFlags\Exceptions;
4+
5+
final class FileNotFound extends \RuntimeException
6+
{
7+
public function __construct(string $message, protected string $path, int $code = 0)
8+
{
9+
parent::__construct($message, $code);
10+
}
11+
12+
public function getPath(): string
13+
{
14+
return $this->path;
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace YlsIdeas\FeatureFlags\Exceptions;
4+
5+
class GatewayConfigurationMissing extends \RuntimeException
6+
{
7+
public function __construct(string $message, protected string $gateway, int $code = 0)
8+
{
9+
parent::__construct($message, $code);
10+
}
11+
12+
public function getGateway(): string
13+
{
14+
return $this->gateway;
15+
}
16+
}

src/Exceptions/UnableToLoadFlags.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace YlsIdeas\FeatureFlags\Exceptions;
4+
5+
final class UnableToLoadFlags extends \RuntimeException
6+
{
7+
public function __construct(string $message, protected string $path, int $code = 0)
8+
{
9+
parent::__construct($message, $code);
10+
}
11+
12+
public function getPath(): string
13+
{
14+
return $this->path;
15+
}
16+
}

src/Manager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use YlsIdeas\FeatureFlags\Events\FeatureSwitchedOff;
2121
use YlsIdeas\FeatureFlags\Events\FeatureSwitchedOn;
2222
use YlsIdeas\FeatureFlags\Exceptions\FeatureExpired;
23+
use YlsIdeas\FeatureFlags\Exceptions\GatewayConfigurationMissing;
2324
use YlsIdeas\FeatureFlags\Gateways\DatabaseGateway;
2425
use YlsIdeas\FeatureFlags\Gateways\GateGateway;
2526
use YlsIdeas\FeatureFlags\Gateways\InMemoryGateway;
@@ -334,7 +335,7 @@ protected function buildRedisGateway(array $config): RedisGateway
334335
protected function buildGateGateway(array $config, string $name): GateGateway
335336
{
336337
if (! ($config['gate'] ?? false)) {
337-
throw new \RuntimeException(sprintf('No gate is configured for gateway `%s`', $name));
338+
throw new GatewayConfigurationMissing(sprintf('No gate is configured for gateway `%s`', $name), $name);
338339
}
339340

340341
return new GateGateway(

src/Support/FeaturesFileDiscoverer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Contracts\Foundation\Application;
66
use Illuminate\Support\Str;
7+
use YlsIdeas\FeatureFlags\Exceptions\FileNotFound;
78

89
/**
910
* @see \YlsIdeas\FeatureFlags\Tests\Support\FeaturesFileDiscovererTest
@@ -30,6 +31,6 @@ public function find(): string
3031
return $path;
3132
}
3233

33-
throw new \RuntimeException(sprintf('`%s` file could not be found.', $this->file));
34+
throw (new FileNotFound(sprintf('`%s` file could not be found.', $this->file), $path));
3435
}
3536
}

src/Support/FileLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Contracts\Container\Container;
66
use YlsIdeas\FeatureFlags\Contracts\InMemoryLoader;
7+
use YlsIdeas\FeatureFlags\Exceptions\UnableToLoadFlags;
78

89
/**
910
* @see \YlsIdeas\FeatureFlags\Tests\Support\FileLoaderTest
@@ -18,7 +19,7 @@ public function load(): array
1819
{
1920
$callable = require($file = $this->discoverer->find());
2021
if (! is_callable($callable)) {
21-
throw new \RuntimeException(sprintf('File `%s` does not return a callable', $file));
22+
throw new UnableToLoadFlags(sprintf('File `%s` does not return a callable', $file), $file);
2223
}
2324

2425
return $this->container->call($callable);

tests/Support/FeaturesFileDiscovererTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Filesystem\Filesystem;
77
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
88
use PHPUnit\Framework\TestCase;
9+
use YlsIdeas\FeatureFlags\Exceptions\FileNotFound;
910
use YlsIdeas\FeatureFlags\Support\FeaturesFileDiscoverer;
1011

1112
class FeaturesFileDiscovererTest extends TestCase
@@ -116,7 +117,8 @@ public function test_it_throws_an_exception_if_no_file_is_discovered(): void
116117

117118
$discoverer = new FeaturesFileDiscoverer($app, '.features.php');
118119

119-
$this->expectException(\RuntimeException::class);
120+
$this->expectException(FileNotFound::class);
121+
$this->expectExceptionMessage('.features.php` file could not be found.');
120122

121123
$discoverer->find();
122124
}

tests/Support/FileLoaderTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Contracts\Container\Container;
66
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
77
use PHPUnit\Framework\TestCase;
8+
use YlsIdeas\FeatureFlags\Exceptions\UnableToLoadFlags;
89
use YlsIdeas\FeatureFlags\Support\FeaturesFileDiscoverer;
910
use YlsIdeas\FeatureFlags\Support\FileLoader;
1011

@@ -36,4 +37,29 @@ public function test_it_loads_features_from_php_file(): void
3637

3738
$this->assertSame($features, $results);
3839
}
40+
41+
public function test_it_throws_an_exception_if_a_callable_isnt_returned(): void
42+
{
43+
$discoverer = \Mockery::mock(FeaturesFileDiscoverer::class);
44+
$container = \Mockery::mock(Container::class);
45+
46+
$features = [
47+
'my-feature' => true,
48+
];
49+
50+
$discoverer->shouldReceive('find')
51+
->once()
52+
->andReturn(__DIR__ . '/../fixtures/features-non-callable.php');
53+
54+
$loader = new FileLoader($discoverer, $container);
55+
56+
$this->expectException(UnableToLoadFlags::class);
57+
$this->expectExceptionMessage(
58+
'File `' .
59+
__DIR__ . '/../fixtures/features-non-callable.php' .
60+
'` does not return a callable'
61+
);
62+
63+
$loader->load();
64+
}
3965
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

0 commit comments

Comments
 (0)