Skip to content

Commit

Permalink
[7.x] Add new Orchestra\Testbench\laravel_or_fail() function (#270)
Browse files Browse the repository at this point in the history
* [7.x] Add new `ApplicationNotAvailableException::validate()` method

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Dec 4, 2024
1 parent abefd18 commit bbe3b5c
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 39 deletions.
11 changes: 4 additions & 7 deletions src/Concerns/HandlesDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
use Illuminate\Database\Events\DatabaseRefreshed;
use Orchestra\Testbench\Attributes\DefineDatabase;
use Orchestra\Testbench\Attributes\WithMigration;
use Orchestra\Testbench\Exceptions\ApplicationNotAvailableException;
use Orchestra\Testbench\Features\TestingFeature;

use function Orchestra\Testbench\laravel_or_fail;

/**
* @internal
*/
Expand All @@ -21,9 +22,7 @@ trait HandlesDatabases
*/
protected function setUpDatabaseRequirements(Closure $callback): void
{
if (\is_null($app = $this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}
$app = laravel_or_fail($this->app);

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

/** @var \Illuminate\Contracts\Config\Repository $config */
$config = $app->make('config');
Expand Down
27 changes: 10 additions & 17 deletions src/Concerns/InteractsWithMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use InvalidArgumentException;
use Orchestra\Testbench\Attributes\ResetRefreshDatabaseState;
use Orchestra\Testbench\Database\MigrateProcessor;
use Orchestra\Testbench\Exceptions\ApplicationNotAvailableException;

use function Orchestra\Testbench\default_migration_path;
use function Orchestra\Testbench\laravel_or_fail;
use function Orchestra\Testbench\load_migration_paths;

/**
Expand Down Expand Up @@ -62,18 +62,17 @@ protected function tearDownInteractsWithMigrations(): void
*/
protected function loadMigrationsFrom($paths): void
{
$app = laravel_or_fail($this->app);

if (
(\is_string($paths) || Arr::isList($paths))
&& static::usesRefreshDatabaseTestingConcern()
&& RefreshDatabaseState::$migrated === false
&& RefreshDatabaseState::$lazilyRefreshed === false
) {
if (\is_null($this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}

/** @var array<int, string>|string $paths */
load_migration_paths($this->app, $paths);
load_migration_paths($app, $paths);

return;
}
Expand All @@ -92,16 +91,14 @@ protected function loadMigrationsFrom($paths): void
*/
protected function loadMigrationsWithoutRollbackFrom($paths): void
{
if (\is_null($this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}
$app = laravel_or_fail($this->app);

$migrator = new MigrateProcessor($this, $this->resolvePackageMigrationsOptions($paths));
$migrator->up();

array_unshift($this->cachedTestMigratorProcessors, $migrator);

$this->resetApplicationArtisanCommands($this->app);
$this->resetApplicationArtisanCommands($app);
}

/**
Expand Down Expand Up @@ -146,9 +143,7 @@ protected function loadLaravelMigrations($database = []): void
*/
protected function loadLaravelMigrationsWithoutRollback($database = []): void
{
if (\is_null($this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}
$app = laravel_or_fail($this->app);

$options = $this->resolveLaravelMigrationsOptions($database);
$options['--path'] = default_migration_path();
Expand All @@ -159,7 +154,7 @@ protected function loadLaravelMigrationsWithoutRollback($database = []): void

array_unshift($this->cachedTestMigratorProcessors, $migrator);

$this->resetApplicationArtisanCommands($this->app);
$this->resetApplicationArtisanCommands($app);
}

/**
Expand All @@ -183,16 +178,14 @@ protected function runLaravelMigrations($database = []): void
*/
protected function runLaravelMigrationsWithoutRollback($database = []): void
{
if (\is_null($this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}
$app = laravel_or_fail($this->app);

$migrator = new MigrateProcessor($this, $this->resolveLaravelMigrationsOptions($database));
$migrator->up();

array_unshift($this->cachedTestMigratorProcessors, $migrator);

$this->resetApplicationArtisanCommands($this->app);
$this->resetApplicationArtisanCommands($app);
}

/**
Expand Down
13 changes: 4 additions & 9 deletions src/Concerns/InteractsWithTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
use Orchestra\Testbench\Contracts\Attributes\BeforeAll as BeforeAllContract;
use Orchestra\Testbench\Contracts\Attributes\BeforeEach as BeforeEachContract;
use Orchestra\Testbench\Contracts\Attributes\Resolvable as ResolvableContract;
use Orchestra\Testbench\Exceptions\ApplicationNotAvailableException;
use Orchestra\Testbench\PHPUnit\AttributeParser;

use function Orchestra\Testbench\laravel_or_fail;

/**
* @internal
*
Expand Down Expand Up @@ -124,10 +125,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__);
}
$app = laravel_or_fail($this->app);

$this->resolvePhpUnitAttributes()
->flatten()
Expand All @@ -144,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__);
}
$app = laravel_or_fail($this->app);

$this->resolvePhpUnitAttributes()
->flatten()
Expand Down
9 changes: 3 additions & 6 deletions src/Concerns/WithFactories.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Exception;
use Illuminate\Database\Eloquent\Factory as ModelFactory;
use Orchestra\Testbench\Exceptions\ApplicationNotAvailableException;

use function Orchestra\Testbench\laravel_or_fail;

/**
* @api
Expand All @@ -25,11 +26,7 @@ trait WithFactories
*/
protected function withFactories(string $path)
{
if (\is_null($this->app)) {
throw ApplicationNotAvailableException::make(__METHOD__);
}

return $this->loadFactoriesUsing($this->app, $path);
return $this->loadFactoriesUsing(laravel_or_fail($this->app), $path);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class Application
resolveApplicationConfiguration as protected resolveApplicationConfigurationFromTrait;
}

/**
* The Illuminate application instance.
*
* @var \Illuminate\Foundation\Application|null
*/
protected $app;

/**
* The application base path.
*
Expand Down
30 changes: 30 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,33 @@ function join_paths(?string $basePath, string ...$paths): string

return $basePath.implode('', $paths);
}

/**
* Ensure the provided `$app` return an instance of Laravel application or throw an exception.
*
* @internal
*
* @param \Illuminate\Foundation\Application|null $app
* @param string|null $caller
* @return \Illuminate\Foundation\Application
*
* @throws \Orchestra\Testbench\Exceptions\ApplicationNotAvailableException
*/
function laravel_or_fail($app, ?string $caller = null): Application
{
if ($app instanceof Application) {
return $app;
}

if (\is_null($caller)) {
$caller = transform(debug_backtrace()[1], function ($debug) {
if (isset($debug['class']) && isset($debug['function'])) {
return \sprintf('%s::%s', $debug['class'], $debug['function']);
}

return $debug['function'];
});
}

throw Exceptions\ApplicationNotAvailableException::make($caller ?? debug_backtrace()[1]['function']);
}
13 changes: 13 additions & 0 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Orchestra\Testbench\Tests;

use Illuminate\Foundation\Application;
use Orchestra\Testbench\Exceptions\ApplicationNotAvailableException;
use Orchestra\Testbench\TestCase;
use PHPUnit\Runner\Version;

use function Orchestra\Testbench\laravel_or_fail;
use function Orchestra\Testbench\laravel_version_compare;
use function Orchestra\Testbench\phpunit_version_compare;

Expand All @@ -24,4 +26,15 @@ public function it_can_compare_phpunit_version()
$this->assertSame(0, phpunit_version_compare(Version::id()));
$this->assertTrue(phpunit_version_compare(Version::id(), '=='));
}

/**
* @test
*/
public function it_can_throw_application_not_available_application_when_app_is_not_laravel()
{
$this->expectException(ApplicationNotAvailableException::class);
$this->expectExceptionMessage(\sprintf('Application is not available to run [%s]', __METHOD__));

laravel_or_fail(null);
}
}

0 comments on commit bbe3b5c

Please sign in to comment.