Skip to content

Commit 0ff1229

Browse files
[10.x] Test Improvements (#48962)
* Test Improvements Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Apply fixes from StyleCI * Test Improvements Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 2703f3e commit 0ff1229

File tree

11 files changed

+100
-238
lines changed

11 files changed

+100
-238
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
"league/flysystem-sftp-v3": "^3.0",
106106
"mockery/mockery": "^1.5.1",
107107
"nyholm/psr7": "^1.2",
108-
"orchestra/testbench-core": "^8.12",
108+
"orchestra/testbench-core": "^8.15.1",
109109
"pda/pheanstalk": "^4.0",
110110
"phpstan/phpstan": "^1.4.7",
111111
"phpunit/phpunit": "^10.0.7",

tests/Foundation/Testing/DatabaseMigrationsTest.php

Lines changed: 43 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,141 +2,106 @@
22

33
namespace Illuminate\Tests\Foundation\Testing;
44

5+
use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
6+
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
57
use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;
68
use Illuminate\Foundation\Testing\DatabaseMigrations;
79
use Illuminate\Foundation\Testing\RefreshDatabaseState;
810
use Mockery as m;
9-
use Orchestra\Testbench\Concerns\Testing;
11+
use Orchestra\Testbench\Concerns\ApplicationTestingHooks;
1012
use Orchestra\Testbench\Foundation\Application as Testbench;
1113
use PHPUnit\Framework\TestCase;
12-
use ReflectionMethod;
1314

1415
use function Orchestra\Testbench\package_path;
1516

1617
class DatabaseMigrationsTest extends TestCase
1718
{
18-
protected $traitObject;
19+
use ApplicationTestingHooks;
20+
use DatabaseMigrations;
21+
use InteractsWithConsole;
22+
23+
public $dropViews = false;
24+
25+
public $dropTypes = false;
1926

2027
protected function setUp(): void
2128
{
22-
$this->traitObject = m::mock(DatabaseMigrationsTestMockClass::class)->makePartial();
23-
$this->traitObject->setUp();
29+
RefreshDatabaseState::$migrated = false;
30+
31+
$this->afterApplicationCreated(function () {
32+
$this->app['config']->set([
33+
'database.default' => 'testing',
34+
'database.connections.testing' => [
35+
'driver' => 'sqlite',
36+
'database' => ':memory:',
37+
],
38+
]);
39+
});
40+
41+
$this->setUpTheApplicationTestingHooks();
42+
$this->withoutMockingConsoleOutput();
2443
}
2544

2645
protected function tearDown(): void
2746
{
28-
$this->traitObject->tearDown();
29-
30-
if ($container = m::getContainer()) {
31-
$this->addToAssertionCount($container->mockery_getExpectationCount());
32-
}
47+
$this->tearDownTheApplicationTestingHooks();
3348

34-
m::close();
49+
RefreshDatabaseState::$migrated = false;
3550
}
3651

37-
private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
52+
protected function refreshApplication()
3853
{
39-
$migrateFreshUsingReflection = new ReflectionMethod(
40-
get_class($this->traitObject),
41-
$methodName
54+
$this->app = Testbench::create(
55+
basePath: package_path('vendor/orchestra/testbench-core/laravel'),
4256
);
43-
44-
return $migrateFreshUsingReflection;
4557
}
4658

4759
public function testRefreshTestDatabaseDefault()
4860
{
49-
$this->traitObject
50-
->shouldReceive('artisan')
61+
$this->app->instance(ConsoleKernelContract::class, $kernel = m::spy(ConsoleKernel::class));
62+
63+
$kernel->shouldReceive('call')
5164
->once()
5265
->with('migrate:fresh', [
5366
'--drop-views' => false,
5467
'--drop-types' => false,
5568
'--seed' => false,
5669
]);
5770

58-
$refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('runDatabaseMigrations');
59-
60-
$refreshTestDatabaseReflection->invoke($this->traitObject);
71+
$this->runDatabaseMigrations();
6172
}
6273

6374
public function testRefreshTestDatabaseWithDropViewsOption()
6475
{
65-
$this->traitObject->dropViews = true;
76+
$this->dropViews = true;
6677

67-
$this->traitObject
68-
->shouldReceive('artisan')
78+
$this->app->instance(ConsoleKernelContract::class, $kernel = m::spy(ConsoleKernel::class));
79+
80+
$kernel->shouldReceive('call')
6981
->once()
7082
->with('migrate:fresh', [
7183
'--drop-views' => true,
7284
'--drop-types' => false,
7385
'--seed' => false,
7486
]);
7587

76-
$refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('runDatabaseMigrations');
77-
78-
$refreshTestDatabaseReflection->invoke($this->traitObject);
88+
$this->runDatabaseMigrations();
7989
}
8090

8191
public function testRefreshTestDatabaseWithDropTypesOption()
8292
{
83-
$this->traitObject->dropTypes = true;
93+
$this->dropTypes = true;
94+
95+
$this->app->instance(ConsoleKernelContract::class, $kernel = m::spy(ConsoleKernel::class));
8496

85-
$this->traitObject
86-
->shouldReceive('artisan')
97+
$kernel->shouldReceive('call')
8798
->once()
8899
->with('migrate:fresh', [
89100
'--drop-views' => false,
90101
'--drop-types' => true,
91102
'--seed' => false,
92103
]);
93104

94-
$refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('runDatabaseMigrations');
95-
96-
$refreshTestDatabaseReflection->invoke($this->traitObject);
97-
}
98-
}
99-
100-
class DatabaseMigrationsTestMockClass
101-
{
102-
use DatabaseMigrations;
103-
use InteractsWithConsole;
104-
use Testing;
105-
106-
public $dropViews = false;
107-
108-
public $dropTypes = false;
109-
110-
public function setUp()
111-
{
112-
RefreshDatabaseState::$migrated = false;
113-
114-
$this->app = $this->refreshApplication();
115-
$this->withoutMockingConsoleOutput();
116-
}
117-
118-
public function tearDown()
119-
{
120-
RefreshDatabaseState::$migrated = false;
121-
122-
$this->callBeforeApplicationDestroyedCallbacks();
123-
$this->app?->flush();
124-
}
125-
126-
protected function setUpTraits()
127-
{
128-
return [];
129-
}
130-
131-
protected function setUpTheTestEnvironmentTraitToBeIgnored(string $use): bool
132-
{
133-
return true;
134-
}
135-
136-
protected function refreshApplication()
137-
{
138-
return Testbench::create(
139-
basePath: package_path('vendor/orchestra/testbench-core/laravel')
140-
);
105+
$this->runDatabaseMigrations();
141106
}
142107
}

tests/Foundation/Testing/RefreshDatabaseTest.php

Lines changed: 31 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,143 +2,96 @@
22

33
namespace Illuminate\Tests\Foundation\Testing;
44

5+
use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
6+
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
57
use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;
68
use Illuminate\Foundation\Testing\RefreshDatabase;
79
use Illuminate\Foundation\Testing\RefreshDatabaseState;
810
use Mockery as m;
9-
use Orchestra\Testbench\Concerns\Testing;
11+
use Orchestra\Testbench\Concerns\ApplicationTestingHooks;
1012
use Orchestra\Testbench\Foundation\Application as Testbench;
1113
use PHPUnit\Framework\TestCase;
12-
use ReflectionMethod;
1314

1415
use function Orchestra\Testbench\package_path;
1516

1617
class RefreshDatabaseTest extends TestCase
1718
{
18-
protected $traitObject;
19+
use ApplicationTestingHooks;
20+
use InteractsWithConsole;
21+
use RefreshDatabase;
22+
23+
public $dropViews = false;
24+
25+
public $dropTypes = false;
1926

2027
protected function setUp(): void
2128
{
2229
RefreshDatabaseState::$migrated = false;
2330

24-
$this->traitObject = m::mock(RefreshDatabaseTestMockClass::class)->makePartial();
25-
$this->traitObject->setUp();
31+
$this->setUpTheApplicationTestingHooks();
32+
$this->withoutMockingConsoleOutput();
2633
}
2734

2835
protected function tearDown(): void
2936
{
30-
$this->traitObject->tearDown();
31-
32-
if ($container = m::getContainer()) {
33-
$this->addToAssertionCount($container->mockery_getExpectationCount());
34-
}
37+
$this->tearDownTheApplicationTestingHooks();
3538

36-
m::close();
39+
RefreshDatabaseState::$migrated = false;
3740
}
3841

39-
private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
42+
protected function refreshApplication()
4043
{
41-
$migrateFreshUsingReflection = new ReflectionMethod(
42-
get_class($this->traitObject),
43-
$methodName
44+
$this->app = Testbench::create(
45+
basePath: package_path('vendor/orchestra/testbench-core/laravel'),
4446
);
45-
46-
return $migrateFreshUsingReflection;
4747
}
4848

4949
public function testRefreshTestDatabaseDefault()
5050
{
51-
$this->traitObject
52-
->shouldReceive('artisan')
51+
$this->app->instance(ConsoleKernelContract::class, $kernel = m::spy(ConsoleKernel::class));
52+
53+
$kernel->shouldReceive('call')
5354
->once()
5455
->with('migrate:fresh', [
5556
'--drop-views' => false,
5657
'--drop-types' => false,
5758
'--seed' => false,
5859
]);
5960

60-
$refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase');
61-
62-
$refreshTestDatabaseReflection->invoke($this->traitObject);
61+
$this->refreshTestDatabase();
6362
}
6463

6564
public function testRefreshTestDatabaseWithDropViewsOption()
6665
{
67-
$this->traitObject->dropViews = true;
66+
$this->dropViews = true;
6867

69-
$this->traitObject
70-
->shouldReceive('artisan')
68+
$this->app->instance(ConsoleKernelContract::class, $kernel = m::spy(ConsoleKernel::class));
69+
70+
$kernel->shouldReceive('call')
7171
->once()
7272
->with('migrate:fresh', [
7373
'--drop-views' => true,
7474
'--drop-types' => false,
7575
'--seed' => false,
7676
]);
7777

78-
$refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase');
79-
80-
$refreshTestDatabaseReflection->invoke($this->traitObject);
78+
$this->refreshTestDatabase();
8179
}
8280

8381
public function testRefreshTestDatabaseWithDropTypesOption()
8482
{
85-
$this->traitObject->dropTypes = true;
83+
$this->dropTypes = true;
84+
85+
$this->app->instance(ConsoleKernelContract::class, $kernel = m::spy(ConsoleKernel::class));
8686

87-
$this->traitObject
88-
->shouldReceive('artisan')
87+
$kernel->shouldReceive('call')
8988
->once()
9089
->with('migrate:fresh', [
9190
'--drop-views' => false,
9291
'--drop-types' => true,
9392
'--seed' => false,
9493
]);
9594

96-
$refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase');
97-
98-
$refreshTestDatabaseReflection->invoke($this->traitObject);
99-
}
100-
}
101-
102-
class RefreshDatabaseTestMockClass
103-
{
104-
use InteractsWithConsole;
105-
use RefreshDatabase;
106-
use Testing;
107-
108-
public $dropViews = false;
109-
110-
public $dropTypes = false;
111-
112-
public function setUp()
113-
{
114-
RefreshDatabaseState::$migrated = false;
115-
116-
$this->app = $this->refreshApplication();
117-
$this->withoutMockingConsoleOutput();
118-
}
119-
120-
public function tearDown()
121-
{
122-
RefreshDatabaseState::$migrated = false;
123-
124-
$this->callBeforeApplicationDestroyedCallbacks();
125-
$this->app?->flush();
126-
}
127-
128-
protected function setUpTraits()
129-
{
130-
return [];
131-
}
132-
133-
protected function setUpTheTestEnvironmentTraitToBeIgnored(string $use): bool
134-
{
135-
return true;
136-
}
137-
138-
public function refreshApplication()
139-
{
140-
return Testbench::create(
141-
basePath: package_path('vendor/orchestra/testbench-core/laravel')
142-
);
95+
$this->refreshTestDatabase();
14396
}
14497
}

tests/Integration/Database/ConfigureCustomDoctrineTypeTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class ConfigureCustomDoctrineTypeTest extends DatabaseTestCase
1414
{
1515
protected function defineEnvironment($app)
1616
{
17+
parent::defineEnvironment($app);
18+
1719
$app['config']['database.connections.sqlite.database'] = ':memory:';
1820
$app['config']['database.dbal.types'] = [
1921
'bit' => MySQLBitType::class,

tests/Integration/Database/DBAL/TimestampTypeTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class TimestampTypeTest extends DatabaseTestCase
1111
{
1212
protected function defineEnvironment($app)
1313
{
14+
parent::defineEnvironment($app);
15+
1416
$app['config']['database.dbal.types'] = [
1517
'timestamp' => TimestampType::class,
1618
];

0 commit comments

Comments
 (0)