Skip to content

Commit

Permalink
[10.x] Test Improvements (#48962)
Browse files Browse the repository at this point in the history
* Test Improvements

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

* Apply fixes from StyleCI

* wip

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

* Apply fixes from StyleCI

* Test Improvements

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]>

* Apply fixes from StyleCI

* wip

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

* Apply fixes from StyleCI

* wip

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

* wip

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
crynobone and StyleCIBot authored Nov 10, 2023
1 parent 2703f3e commit 0ff1229
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 238 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"league/flysystem-sftp-v3": "^3.0",
"mockery/mockery": "^1.5.1",
"nyholm/psr7": "^1.2",
"orchestra/testbench-core": "^8.12",
"orchestra/testbench-core": "^8.15.1",
"pda/pheanstalk": "^4.0",
"phpstan/phpstan": "^1.4.7",
"phpunit/phpunit": "^10.0.7",
Expand Down
121 changes: 43 additions & 78 deletions tests/Foundation/Testing/DatabaseMigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,141 +2,106 @@

namespace Illuminate\Tests\Foundation\Testing;

use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\RefreshDatabaseState;
use Mockery as m;
use Orchestra\Testbench\Concerns\Testing;
use Orchestra\Testbench\Concerns\ApplicationTestingHooks;
use Orchestra\Testbench\Foundation\Application as Testbench;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;

use function Orchestra\Testbench\package_path;

class DatabaseMigrationsTest extends TestCase
{
protected $traitObject;
use ApplicationTestingHooks;
use DatabaseMigrations;
use InteractsWithConsole;

public $dropViews = false;

public $dropTypes = false;

protected function setUp(): void
{
$this->traitObject = m::mock(DatabaseMigrationsTestMockClass::class)->makePartial();
$this->traitObject->setUp();
RefreshDatabaseState::$migrated = false;

$this->afterApplicationCreated(function () {
$this->app['config']->set([
'database.default' => 'testing',
'database.connections.testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
],
]);
});

$this->setUpTheApplicationTestingHooks();
$this->withoutMockingConsoleOutput();
}

protected function tearDown(): void
{
$this->traitObject->tearDown();

if ($container = m::getContainer()) {
$this->addToAssertionCount($container->mockery_getExpectationCount());
}
$this->tearDownTheApplicationTestingHooks();

m::close();
RefreshDatabaseState::$migrated = false;
}

private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
protected function refreshApplication()
{
$migrateFreshUsingReflection = new ReflectionMethod(
get_class($this->traitObject),
$methodName
$this->app = Testbench::create(
basePath: package_path('vendor/orchestra/testbench-core/laravel'),
);

return $migrateFreshUsingReflection;
}

public function testRefreshTestDatabaseDefault()
{
$this->traitObject
->shouldReceive('artisan')
$this->app->instance(ConsoleKernelContract::class, $kernel = m::spy(ConsoleKernel::class));

$kernel->shouldReceive('call')
->once()
->with('migrate:fresh', [
'--drop-views' => false,
'--drop-types' => false,
'--seed' => false,
]);

$refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('runDatabaseMigrations');

$refreshTestDatabaseReflection->invoke($this->traitObject);
$this->runDatabaseMigrations();
}

public function testRefreshTestDatabaseWithDropViewsOption()
{
$this->traitObject->dropViews = true;
$this->dropViews = true;

$this->traitObject
->shouldReceive('artisan')
$this->app->instance(ConsoleKernelContract::class, $kernel = m::spy(ConsoleKernel::class));

$kernel->shouldReceive('call')
->once()
->with('migrate:fresh', [
'--drop-views' => true,
'--drop-types' => false,
'--seed' => false,
]);

$refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('runDatabaseMigrations');

$refreshTestDatabaseReflection->invoke($this->traitObject);
$this->runDatabaseMigrations();
}

public function testRefreshTestDatabaseWithDropTypesOption()
{
$this->traitObject->dropTypes = true;
$this->dropTypes = true;

$this->app->instance(ConsoleKernelContract::class, $kernel = m::spy(ConsoleKernel::class));

$this->traitObject
->shouldReceive('artisan')
$kernel->shouldReceive('call')
->once()
->with('migrate:fresh', [
'--drop-views' => false,
'--drop-types' => true,
'--seed' => false,
]);

$refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('runDatabaseMigrations');

$refreshTestDatabaseReflection->invoke($this->traitObject);
}
}

class DatabaseMigrationsTestMockClass
{
use DatabaseMigrations;
use InteractsWithConsole;
use Testing;

public $dropViews = false;

public $dropTypes = false;

public function setUp()
{
RefreshDatabaseState::$migrated = false;

$this->app = $this->refreshApplication();
$this->withoutMockingConsoleOutput();
}

public function tearDown()
{
RefreshDatabaseState::$migrated = false;

$this->callBeforeApplicationDestroyedCallbacks();
$this->app?->flush();
}

protected function setUpTraits()
{
return [];
}

protected function setUpTheTestEnvironmentTraitToBeIgnored(string $use): bool
{
return true;
}

protected function refreshApplication()
{
return Testbench::create(
basePath: package_path('vendor/orchestra/testbench-core/laravel')
);
$this->runDatabaseMigrations();
}
}
109 changes: 31 additions & 78 deletions tests/Foundation/Testing/RefreshDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,143 +2,96 @@

namespace Illuminate\Tests\Foundation\Testing;

use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\RefreshDatabaseState;
use Mockery as m;
use Orchestra\Testbench\Concerns\Testing;
use Orchestra\Testbench\Concerns\ApplicationTestingHooks;
use Orchestra\Testbench\Foundation\Application as Testbench;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;

use function Orchestra\Testbench\package_path;

class RefreshDatabaseTest extends TestCase
{
protected $traitObject;
use ApplicationTestingHooks;
use InteractsWithConsole;
use RefreshDatabase;

public $dropViews = false;

public $dropTypes = false;

protected function setUp(): void
{
RefreshDatabaseState::$migrated = false;

$this->traitObject = m::mock(RefreshDatabaseTestMockClass::class)->makePartial();
$this->traitObject->setUp();
$this->setUpTheApplicationTestingHooks();
$this->withoutMockingConsoleOutput();
}

protected function tearDown(): void
{
$this->traitObject->tearDown();

if ($container = m::getContainer()) {
$this->addToAssertionCount($container->mockery_getExpectationCount());
}
$this->tearDownTheApplicationTestingHooks();

m::close();
RefreshDatabaseState::$migrated = false;
}

private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
protected function refreshApplication()
{
$migrateFreshUsingReflection = new ReflectionMethod(
get_class($this->traitObject),
$methodName
$this->app = Testbench::create(
basePath: package_path('vendor/orchestra/testbench-core/laravel'),
);

return $migrateFreshUsingReflection;
}

public function testRefreshTestDatabaseDefault()
{
$this->traitObject
->shouldReceive('artisan')
$this->app->instance(ConsoleKernelContract::class, $kernel = m::spy(ConsoleKernel::class));

$kernel->shouldReceive('call')
->once()
->with('migrate:fresh', [
'--drop-views' => false,
'--drop-types' => false,
'--seed' => false,
]);

$refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase');

$refreshTestDatabaseReflection->invoke($this->traitObject);
$this->refreshTestDatabase();
}

public function testRefreshTestDatabaseWithDropViewsOption()
{
$this->traitObject->dropViews = true;
$this->dropViews = true;

$this->traitObject
->shouldReceive('artisan')
$this->app->instance(ConsoleKernelContract::class, $kernel = m::spy(ConsoleKernel::class));

$kernel->shouldReceive('call')
->once()
->with('migrate:fresh', [
'--drop-views' => true,
'--drop-types' => false,
'--seed' => false,
]);

$refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase');

$refreshTestDatabaseReflection->invoke($this->traitObject);
$this->refreshTestDatabase();
}

public function testRefreshTestDatabaseWithDropTypesOption()
{
$this->traitObject->dropTypes = true;
$this->dropTypes = true;

$this->app->instance(ConsoleKernelContract::class, $kernel = m::spy(ConsoleKernel::class));

$this->traitObject
->shouldReceive('artisan')
$kernel->shouldReceive('call')
->once()
->with('migrate:fresh', [
'--drop-views' => false,
'--drop-types' => true,
'--seed' => false,
]);

$refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase');

$refreshTestDatabaseReflection->invoke($this->traitObject);
}
}

class RefreshDatabaseTestMockClass
{
use InteractsWithConsole;
use RefreshDatabase;
use Testing;

public $dropViews = false;

public $dropTypes = false;

public function setUp()
{
RefreshDatabaseState::$migrated = false;

$this->app = $this->refreshApplication();
$this->withoutMockingConsoleOutput();
}

public function tearDown()
{
RefreshDatabaseState::$migrated = false;

$this->callBeforeApplicationDestroyedCallbacks();
$this->app?->flush();
}

protected function setUpTraits()
{
return [];
}

protected function setUpTheTestEnvironmentTraitToBeIgnored(string $use): bool
{
return true;
}

public function refreshApplication()
{
return Testbench::create(
basePath: package_path('vendor/orchestra/testbench-core/laravel')
);
$this->refreshTestDatabase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class ConfigureCustomDoctrineTypeTest extends DatabaseTestCase
{
protected function defineEnvironment($app)
{
parent::defineEnvironment($app);

$app['config']['database.connections.sqlite.database'] = ':memory:';
$app['config']['database.dbal.types'] = [
'bit' => MySQLBitType::class,
Expand Down
2 changes: 2 additions & 0 deletions tests/Integration/Database/DBAL/TimestampTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class TimestampTypeTest extends DatabaseTestCase
{
protected function defineEnvironment($app)
{
parent::defineEnvironment($app);

$app['config']['database.dbal.types'] = [
'timestamp' => TimestampType::class,
];
Expand Down
Loading

0 comments on commit 0ff1229

Please sign in to comment.