Skip to content

Commit

Permalink
[7.x] Add new ApplicationNotAvailableException::validate() method
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Dec 4, 2024
1 parent c588d05 commit c562e98
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
8 changes: 2 additions & 6 deletions src/Concerns/HandlesDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ trait HandlesDatabases
*/
protected function setUpDatabaseRequirements(Closure $callback): void
{
if (\is_null($app = $this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}
ApplicationNotAvailableException::validate($app = $this->app, __METHOD__);

$app['events']->listen(DatabaseRefreshed::class, function () {
$this->defineDatabaseMigrationsAfterDatabaseRefreshed();
Expand Down Expand Up @@ -63,9 +61,7 @@ protected function setUpDatabaseRequirements(Closure $callback): void
*/
protected function usesSqliteInMemoryDatabaseConnection(?string $connection = null): bool
{
if (\is_null($app = $this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}
ApplicationNotAvailableException::validate($app = $this->app, __METHOD__);

/** @var \Illuminate\Contracts\Config\Repository $config */
$config = $app->make('config');
Expand Down
16 changes: 4 additions & 12 deletions src/Concerns/InteractsWithMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ protected function loadMigrationsFrom($paths): void
&& RefreshDatabaseState::$migrated === false
&& RefreshDatabaseState::$lazilyRefreshed === false
) {
if (\is_null($this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}
ApplicationNotAvailableException::validate($this->app, __METHOD__);

/** @var array<int, string>|string $paths */
load_migration_paths($this->app, $paths);
Expand All @@ -92,9 +90,7 @@ protected function loadMigrationsFrom($paths): void
*/
protected function loadMigrationsWithoutRollbackFrom($paths): void
{
if (\is_null($this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}
ApplicationNotAvailableException::validate($this->app, __METHOD__);

$migrator = new MigrateProcessor($this, $this->resolvePackageMigrationsOptions($paths));
$migrator->up();
Expand Down Expand Up @@ -146,9 +142,7 @@ protected function loadLaravelMigrations($database = []): void
*/
protected function loadLaravelMigrationsWithoutRollback($database = []): void
{
if (\is_null($this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}
ApplicationNotAvailableException::validate($this->app, __METHOD__);

$options = $this->resolveLaravelMigrationsOptions($database);
$options['--path'] = default_migration_path();
Expand Down Expand Up @@ -183,9 +177,7 @@ protected function runLaravelMigrations($database = []): void
*/
protected function runLaravelMigrationsWithoutRollback($database = []): void
{
if (\is_null($this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}
ApplicationNotAvailableException::validate($this->app, __METHOD__);

$migrator = new MigrateProcessor($this, $this->resolveLaravelMigrationsOptions($database));
$migrator->up();
Expand Down
10 changes: 2 additions & 8 deletions src/Concerns/InteractsWithTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ abstract protected static function resolvePhpUnitAttributesForMethod(string $cla
*/
protected function setUpTheTestEnvironmentUsingTestCase(): void
{
/** @phpstan-ignore-next-line */
if (\is_null($app = $this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}
ApplicationNotAvailableException::validate($app = $this->app, __METHOD__);

$this->resolvePhpUnitAttributes()
->flatten()
Expand All @@ -145,10 +142,7 @@ protected function setUpTheTestEnvironmentUsingTestCase(): void
*/
protected function tearDownTheTestEnvironmentUsingTestCase(): void
{
/** @phpstan-ignore-next-line */
if (\is_null($app = $this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}
ApplicationNotAvailableException::validate($app = $this->app, __METHOD__);

$this->resolvePhpUnitAttributes()
->flatten()
Expand Down
4 changes: 1 addition & 3 deletions src/Concerns/WithFactories.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ trait WithFactories
*/
protected function withFactories(string $path)
{
if (\is_null($this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}
ApplicationNotAvailableException::validate($this->app, __METHOD__);

return $this->loadFactoriesUsing($this->app, $path);
}
Expand Down
19 changes: 19 additions & 0 deletions src/Exceptions/ApplicationNotAvailableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@

namespace Orchestra\Testbench\Exceptions;

use Illuminate\Contracts\Foundation\Application;
use RuntimeException;

class ApplicationNotAvailableException extends RuntimeException
{
/**
* Determine if the application is available before throwing an exception.
*
* @param \Illuminate\Contracts\Foundation\Application|null $app
* @param string $method
* @return true
*
* @throws static
*/
public static function validate($app, string $method)
{
if (! $app instanceof Application) {
throw static::make($method);
}

return true;
}

/**
* Make new RuntimeException when application is not available.
*
Expand Down

0 comments on commit c562e98

Please sign in to comment.