Skip to content

Commit

Permalink
Merge branch '8.x' into 9.x
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Dec 4, 2024
2 parents e90e37d + 768ffc8 commit 9e0dc20
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 40 deletions.
11 changes: 4 additions & 7 deletions src/Concerns/HandlesDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
use Orchestra\Testbench\Attributes\DefineDatabase;
use Orchestra\Testbench\Attributes\RequiresDatabase;
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 @@ -24,9 +25,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);

TestingFeature::run(
testCase: $this,
Expand Down Expand Up @@ -81,9 +80,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 @@ -67,18 +67,17 @@ protected function tearDownInteractsWithMigrations(): void
*/
protected function loadMigrationsFrom(array|string $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 Down Expand Up @@ -109,16 +108,14 @@ protected function loadMigrationsFrom(array|string $paths): void
#[\Deprecated(message: 'Use `loadMigrationsFrom()` instead', since: '9.0.7')]
protected function loadMigrationsWithoutRollbackFrom(array|string $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 @@ -154,9 +151,7 @@ protected function resolvePackageMigrationsOptions(array|string $paths = []): ar
*/
protected function loadLaravelMigrations(array|string $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 @@ -167,7 +162,7 @@ protected function loadLaravelMigrations(array|string $database = []): void

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

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

/**
Expand Down Expand Up @@ -196,16 +191,14 @@ protected function loadLaravelMigrationsWithoutRollback(array|string $database =
*/
protected function runLaravelMigrations(array|string $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;

/**
* @phpstan-import-type TTestingFeature from \Orchestra\Testbench\PHPUnit\AttributeParser
* @phpstan-import-type TAttributes from \Orchestra\Testbench\PHPUnit\AttributeParser
Expand Down Expand Up @@ -137,10 +138,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 @@ -159,10 +157,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
2 changes: 1 addition & 1 deletion src/Concerns/InteractsWithWorkbench.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function getPackageBootstrappersUsingWorkbench($app): ?array
* @internal
*
* @param \Illuminate\Foundation\Application $app
* @return array<int, class-string>|null
* @return array<int, class-string<\Illuminate\Support\ServiceProvider>>|null
*/
protected function getPackageProvidersUsingWorkbench($app): ?array
{
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 @@ -27,11 +28,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 @@ -47,6 +47,13 @@ class Application
resolveApplicationConfiguration as protected resolveApplicationConfigurationFromTrait;
}

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

/**
* List of configurations.
*
Expand Down
31 changes: 31 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,34 @@ 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) {
/** @phpstan-ignore isset.offset */
if (isset($debug['class']) && isset($debug['function'])) {
return \sprintf('%s::%s', $debug['class'], $debug['function']);
}

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

throw Exceptions\ApplicationNotAvailableException::make($caller);
}
11 changes: 11 additions & 0 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Orchestra\Testbench\Tests;

use Illuminate\Foundation\Application;
use Orchestra\Testbench\Exceptions\ApplicationNotAvailableException;
use Orchestra\Testbench\TestCase;
use PHPUnit\Framework\Attributes\Test;
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 @@ -27,4 +29,13 @@ 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 9e0dc20

Please sign in to comment.