generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04fcdb7
commit 451cc0d
Showing
37 changed files
with
854 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
use Illuminate\Foundation\Auth\User; | ||
use Spatie\ErrorSolutions\Tests\Laravel\Exceptions\AlwaysFalseSolutionProvider; | ||
use Spatie\ErrorSolutions\Tests\Laravel\Exceptions\AlwaysTrueSolutionProvider; | ||
use Spatie\ErrorSolutions\Contracts\BaseSolution; | ||
use Spatie\ErrorSolutions\Solutions\SolutionProviders\BadMethodCallSolutionProvider; | ||
use Spatie\ErrorSolutions\Solutions\SolutionProviders\SolutionProviderRepository; | ||
use Spatie\ErrorSolutions\Laravel\Solutions\SolutionProviders\MissingAppKeySolutionProvider; | ||
|
||
it('returns possible solutions', function () { | ||
$repository = new SolutionProviderRepository(); | ||
|
||
$repository->registerSolutionProvider(AlwaysTrueSolutionProvider::class); | ||
$repository->registerSolutionProvider(AlwaysFalseSolutionProvider::class); | ||
|
||
$solutions = $repository->getSolutionsForThrowable(new Exception()); | ||
|
||
$this->assertNotNull($solutions); | ||
expect($solutions)->toHaveCount(1); | ||
expect($solutions[0] instanceof BaseSolution)->toBeTrue(); | ||
}); | ||
|
||
it('returns possible solutions when registered together', function () { | ||
$repository = new SolutionProviderRepository(); | ||
|
||
$repository->registerSolutionProviders([ | ||
AlwaysTrueSolutionProvider::class, | ||
AlwaysFalseSolutionProvider::class, | ||
]); | ||
|
||
$solutions = $repository->getSolutionsForThrowable(new Exception()); | ||
|
||
$this->assertNotNull($solutions); | ||
expect($solutions)->toHaveCount(1); | ||
expect($solutions[0] instanceof BaseSolution)->toBeTrue(); | ||
}); | ||
|
||
it('can suggest bad method call exceptions', function () { | ||
if (version_compare(app()->version(), '5.6.3', '<')) { | ||
$this->markTestSkipped('Laravel version < 5.6.3 do not support bad method call solutions'); | ||
} | ||
|
||
try { | ||
collect([])->faltten(); | ||
} catch (Exception $exception) { | ||
$solution = new BadMethodCallSolutionProvider(); | ||
|
||
expect($solution->canSolve($exception))->toBeTrue(); | ||
} | ||
}); | ||
|
||
it('can propose a solution for bad method call exceptions on collections', function () { | ||
try { | ||
collect([])->frist(fn ($item) => null); | ||
} catch (Exception $exception) { | ||
$solution = new BadMethodCallSolutionProvider(); | ||
|
||
expect($solution->getSolutions($exception)[0]->getSolutionDescription())->toBe('Did you mean Illuminate\Support\Collection::first() ?'); | ||
} | ||
}); | ||
|
||
it('can propose a solution for bad method call exceptions on models', function () { | ||
try { | ||
$user = new User(); | ||
$user->sarve(); | ||
} catch (Exception $exception) { | ||
$solution = new BadMethodCallSolutionProvider(); | ||
|
||
expect($solution->getSolutions($exception)[0]->getSolutionDescription())->toBe('Did you mean Illuminate\Foundation\Auth\User::save() ?'); | ||
} | ||
}); | ||
|
||
it('can propose a solution for missing app key exceptions', function () { | ||
$exception = new RuntimeException('No application encryption key has been specified.'); | ||
|
||
$solution = new MissingAppKeySolutionProvider(); | ||
|
||
expect($solution->getSolutions($exception)[0]->getSolutionActionDescription())->toBe('Generate your application encryption key using `php artisan key:generate`.'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Spatie\ErrorSolutions\Tests\Laravel\Exceptions; | ||
|
||
use Spatie\ErrorSolutions\Contracts\BaseSolution; | ||
use Spatie\ErrorSolutions\Contracts\HasSolutionsForThrowable; | ||
use Throwable; | ||
|
||
class AlwaysFalseSolutionProvider implements HasSolutionsForThrowable | ||
{ | ||
public function canSolve(Throwable $throwable): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function getSolutions(Throwable $throwable): array | ||
{ | ||
return [new BaseSolution('Base Solution')]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Spatie\ErrorSolutions\Tests\Laravel\Exceptions; | ||
|
||
use Spatie\ErrorSolutions\Contracts\BaseSolution; | ||
use Spatie\ErrorSolutions\Contracts\HasSolutionsForThrowable; | ||
use Throwable; | ||
|
||
class AlwaysTrueSolutionProvider implements HasSolutionsForThrowable | ||
{ | ||
public function canSolve(Throwable $throwable): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function getSolutions(Throwable $throwable): array | ||
{ | ||
return [new BaseSolution('Base Solution')]; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/Laravel/Solutions/InvalidRouteActionSolutionProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Route; | ||
use Illuminate\Support\Str; | ||
use Spatie\ErrorSolutions\Tests\Laravel\stubs\Controllers\TestTypoController; | ||
use Spatie\LaravelIgnition\Solutions\SolutionProviders\InvalidRouteActionSolutionProvider; | ||
use Spatie\ErrorSolutions\Laravel\Support\Composer\ComposerClassMap; | ||
|
||
beforeEach(function () { | ||
app()->bind( | ||
ComposerClassMap::class, | ||
function () { | ||
return new ComposerClassMap(__DIR__.'/../../../vendor/autoload.php'); | ||
} | ||
); | ||
}); | ||
|
||
it('can solve the exception', function () { | ||
$canSolve = app(InvalidRouteActionSolutionProvider::class)->canSolve(getInvalidRouteActionException()); | ||
|
||
expect($canSolve)->toBeTrue(); | ||
}); | ||
|
||
it('can recommend changing the routes method', function () { | ||
Route::get('/test', TestTypoController::class); | ||
|
||
/** @var \Spatie\Ignition\Contracts\Solution $solution */ | ||
$solution = app(InvalidRouteActionSolutionProvider::class)->getSolutions(getInvalidRouteActionException())[0]; | ||
|
||
expect(Str::contains($solution->getSolutionDescription(), 'Did you mean `TestTypoController`'))->toBeTrue(); | ||
}); | ||
|
||
it('wont recommend another controller class if the names are too different', function () { | ||
Route::get('/test', TestTypoController::class); | ||
|
||
$invalidController = 'UnrelatedTestTypoController'; | ||
|
||
/** @var \Spatie\Ignition\Contracts\Solution $solution */ | ||
$solution = app(InvalidRouteActionSolutionProvider::class)->getSolutions(getInvalidRouteActionException($invalidController))[0]; | ||
|
||
expect(Str::contains($solution->getSolutionDescription(), 'Did you mean `TestTypoController`'))->toBeFalse(); | ||
}); | ||
|
||
// Helpers | ||
function getInvalidRouteActionException(string $controller = 'TestTypooController'): UnexpectedValueException | ||
{ | ||
return new UnexpectedValueException("Invalid route action: [{$controller}]"); | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/Laravel/Solutions/LazyLoadingViolationSolutionProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
use Illuminate\Database\LazyLoadingViolationException; | ||
use Illuminate\Foundation\Auth\User; | ||
use Spatie\LaravelIgnition\Solutions\SolutionProviders\LazyLoadingViolationSolutionProvider; | ||
|
||
it('can solve lazy loading violations', function () { | ||
$canSolve = app(LazyLoadingViolationSolutionProvider::class) | ||
->canSolve(new LazyLoadingViolationException(new User(), 'posts')); | ||
|
||
expect($canSolve)->toBeTrue(); | ||
|
||
$canSolve = app(LazyLoadingViolationSolutionProvider::class) | ||
->canSolve(new Exception('generic exception')); | ||
|
||
expect($canSolve)->toBeFalse(); | ||
}); | ||
|
||
// Helpers | ||
function it_can_provide_the_solution_for_lazy_loading_exceptions() | ||
{ | ||
$solutions = app(LazyLoadingViolationSolutionProvider::class) | ||
->getSolutions(new LazyLoadingViolationException(new User(), 'posts')); | ||
|
||
expect($solutions)->toHaveCount(1); | ||
} |
Oops, something went wrong.