Skip to content

Commit

Permalink
Improved exceptions and tests for them
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfox committed Mar 11, 2023
1 parent 57a5d96 commit a4d0bbd
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/Exceptions/FileNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace YlsIdeas\FeatureFlags\Exceptions;

final class FileNotFound extends \RuntimeException
{
public function __construct(string $message, protected string $path, int $code = 0)
{
parent::__construct($message, $code);
}

public function getPath(): string

Check warning on line 12 in src/Exceptions/FileNotFound.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/FileNotFound.php#L12

Added line #L12 was not covered by tests
{
return $this->path;

Check warning on line 14 in src/Exceptions/FileNotFound.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/FileNotFound.php#L14

Added line #L14 was not covered by tests
}
}
16 changes: 16 additions & 0 deletions src/Exceptions/GatewayConfigurationMissing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace YlsIdeas\FeatureFlags\Exceptions;

class GatewayConfigurationMissing extends \RuntimeException
{
public function __construct(string $message, protected string $gateway, int $code = 0)
{
parent::__construct($message, $code);
}

public function getGateway(): string

Check warning on line 12 in src/Exceptions/GatewayConfigurationMissing.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/GatewayConfigurationMissing.php#L12

Added line #L12 was not covered by tests
{
return $this->gateway;

Check warning on line 14 in src/Exceptions/GatewayConfigurationMissing.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/GatewayConfigurationMissing.php#L14

Added line #L14 was not covered by tests
}
}
16 changes: 16 additions & 0 deletions src/Exceptions/UnableToLoadFlags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace YlsIdeas\FeatureFlags\Exceptions;

final class UnableToLoadFlags extends \RuntimeException
{
public function __construct(string $message, protected string $path, int $code = 0)
{
parent::__construct($message, $code);
}

public function getPath(): string

Check warning on line 12 in src/Exceptions/UnableToLoadFlags.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/UnableToLoadFlags.php#L12

Added line #L12 was not covered by tests
{
return $this->path;

Check warning on line 14 in src/Exceptions/UnableToLoadFlags.php

View check run for this annotation

Codecov / codecov/patch

src/Exceptions/UnableToLoadFlags.php#L14

Added line #L14 was not covered by tests
}
}
3 changes: 2 additions & 1 deletion src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion src/Support/FeaturesFileDiscoverer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Str;
use YlsIdeas\FeatureFlags\Exceptions\FileNotFound;

/**
* @see \YlsIdeas\FeatureFlags\Tests\Support\FeaturesFileDiscovererTest
Expand All @@ -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));
}
}
3 changes: 2 additions & 1 deletion src/Support/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion tests/Support/FeaturesFileDiscovererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Support/FileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
}
1 change: 1 addition & 0 deletions tests/fixtures/features-non-callable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php

0 comments on commit a4d0bbd

Please sign in to comment.