Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/CurrencyRateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use FlexMindSoftware\CurrencyRate\Commands\CurrencyRateCommand;
use FlexMindSoftware\CurrencyRate\Drivers\UnitedStatesDriver;
use InvalidArgumentException;
use Illuminate\Support\Str;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand Down Expand Up @@ -41,9 +42,17 @@
return new CurrencyRateManager($app);
});

$this->app->bind(UnitedStatesDriver::class, fn ($app) => new UnitedStatesDriver());

Check failure on line 45 in src/CurrencyRateServiceProvider.php

View workflow job for this annotation

GitHub Actions / psalm

UnusedClosureParam

src/CurrencyRateServiceProvider.php:45:57: UnusedClosureParam: Param $app is never referenced in this method (see https://psalm.dev/188)
}

/**
* @return void
*/
public function packageBooted(): void
{
$this->validateDrivers();
}

protected function validateConfig(): void
{
$required = ['driver', 'table-name', 'drivers'];
Expand All @@ -56,4 +65,17 @@
}
}
}

protected function validateDrivers(): void
{
foreach (config('currency-rate.drivers', []) as $driver) {
$class = 'FlexMindSoftware\\CurrencyRate\\Drivers\\'.Str::studly($driver).'Driver';

if (! class_exists($class)) {
throw new InvalidArgumentException(
"Driver class [$class] for driver [$driver] does not exist."
);
}
}
}
}
37 changes: 37 additions & 0 deletions tests/DriverValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace FlexMindSoftware\CurrencyRate\Tests;

use FlexMindSoftware\CurrencyRate\CurrencyRateServiceProvider;
use InvalidArgumentException;

class DriverValidationTest extends TestCase
{
protected function getPackageProviders($app)
{
return [];
}

/**
* @test
*/
public function it_throws_exception_for_missing_driver_class(): void
{
$this->app['config']->set('currency-rate', [
'driver' => 'non-existing-driver',
'drivers' => ['non-existing-driver'],
'table-name' => 'currency_rates',
]);

$provider = new CurrencyRateServiceProvider($this->app);
$provider->packageRegistered();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Driver class [FlexMindSoftware\\CurrencyRate\\Drivers\\NonExistingDriverDriver] for driver [non-existing-driver] does not exist.');

$provider->packageBooted();
}
}

Loading