Skip to content

Commit 38e7183

Browse files
[10.x] Test Improvements (#48815)
* Test Improvements 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> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Apply fixes from StyleCI * Update CacheMemcachedStoreTest.php * wip 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 --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent d900366 commit 38e7183

9 files changed

+141
-80
lines changed

tests/Auth/AuthPasswordBrokerTest.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,16 @@ protected function tearDown(): void
2222
public function testIfUserIsNotFoundErrorRedirectIsReturned()
2323
{
2424
$mocks = $this->getMocks();
25-
$broker = $this->getMockBuilder(PasswordBroker::class)
26-
->onlyMethods(['getUser'])
27-
->addMethods(['makeErrorRedirect'])
28-
->setConstructorArgs(array_values($mocks))
29-
->getMock();
30-
$broker->expects($this->once())->method('getUser')->willReturn(null);
25+
$broker = m::mock(PasswordBroker::class, array_values($mocks))->makePartial();
26+
$broker->shouldReceive('getUser')->once()->andReturnNull();
3127

3228
$this->assertSame(PasswordBrokerContract::INVALID_USER, $broker->sendResetLink(['credentials']));
3329
}
3430

3531
public function testIfTokenIsRecentlyCreated()
3632
{
3733
$mocks = $this->getMocks();
38-
$broker = $this->getMockBuilder(PasswordBroker::class)->addMethods(['emailResetLink', 'getUri'])->setConstructorArgs(array_values($mocks))->getMock();
34+
$broker = m::mock(PasswordBroker::class, array_values($mocks))->makePartial();
3935
$mocks['users']->shouldReceive('retrieveByCredentials')->once()->with(['foo'])->andReturn($user = m::mock(CanResetPassword::class));
4036
$mocks['tokens']->shouldReceive('recentlyCreatedToken')->once()->with($user)->andReturn(true);
4137
$user->shouldReceive('sendPasswordResetNotification')->with('token');
@@ -65,7 +61,7 @@ public function testUserIsRetrievedByCredentials()
6561
public function testBrokerCreatesTokenAndRedirectsWithoutError()
6662
{
6763
$mocks = $this->getMocks();
68-
$broker = $this->getMockBuilder(PasswordBroker::class)->addMethods(['emailResetLink', 'getUri'])->setConstructorArgs(array_values($mocks))->getMock();
64+
$broker = m::mock(PasswordBroker::class, array_values($mocks))->makePartial();
6965
$mocks['users']->shouldReceive('retrieveByCredentials')->once()->with(['foo'])->andReturn($user = m::mock(CanResetPassword::class));
7066
$mocks['tokens']->shouldReceive('recentlyCreatedToken')->once()->with($user)->andReturn(false);
7167
$mocks['tokens']->shouldReceive('create')->once()->with($user)->andReturn('token');
@@ -99,12 +95,9 @@ public function testRedirectReturnedByRemindWhenRecordDoesntExistInTable()
9995
public function testResetRemovesRecordOnReminderTableAndCallsCallback()
10096
{
10197
unset($_SERVER['__password.reset.test']);
102-
$broker = $this->getMockBuilder(PasswordBroker::class)
103-
->onlyMethods(['validateReset'])
104-
->addMethods(['getPassword', 'getToken'])
105-
->setConstructorArgs(array_values($mocks = $this->getMocks()))
106-
->getMock();
107-
$broker->expects($this->once())->method('validateReset')->willReturn($user = m::mock(CanResetPassword::class));
98+
$mocks = $this->getMocks();
99+
$broker = m::mock(PasswordBroker::class, array_values($mocks))->makePartial()->shouldAllowMockingProtectedMethods();
100+
$broker->shouldReceive('validateReset')->once()->andReturn($user = m::mock(CanResetPassword::class));
108101
$mocks['tokens']->shouldReceive('delete')->once()->with($user);
109102
$callback = function ($user, $password) {
110103
$_SERVER['__password.reset.test'] = compact('user', 'password');
@@ -125,7 +118,7 @@ public function testExecutesCallbackInsteadOfSendingNotification()
125118
};
126119

127120
$mocks = $this->getMocks();
128-
$broker = $this->getMockBuilder(PasswordBroker::class)->addMethods(['emailResetLink', 'getUri'])->setConstructorArgs(array_values($mocks))->getMock();
121+
$broker = m::mock(PasswordBroker::class, array_values($mocks))->makePartial();
129122
$mocks['users']->shouldReceive('retrieveByCredentials')->once()->with(['foo'])->andReturn($user = m::mock(CanResetPassword::class));
130123
$mocks['tokens']->shouldReceive('recentlyCreatedToken')->once()->with($user)->andReturn(false);
131124
$mocks['tokens']->shouldReceive('create')->once()->with($user)->andReturn('token');

tests/Cache/CacheMemcachedStoreTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Memcached;
88
use Mockery as m;
99
use PHPUnit\Framework\TestCase;
10-
use stdClass;
1110

1211
/**
1312
* @requires extension memcached
@@ -23,7 +22,7 @@ protected function tearDown(): void
2322

2423
public function testGetReturnsNullWhenNotFound()
2524
{
26-
$memcache = $this->getMockBuilder(stdClass::class)->addMethods(['get', 'getResultCode'])->getMock();
25+
$memcache = $this->getMockBuilder(Memcached::class)->onlyMethods(['get', 'getResultCode'])->getMock();
2726
$memcache->expects($this->once())->method('get')->with($this->equalTo('foo:bar'))->willReturn(null);
2827
$memcache->expects($this->once())->method('getResultCode')->willReturn(1);
2928
$store = new MemcachedStore($memcache, 'foo');
@@ -32,7 +31,7 @@ public function testGetReturnsNullWhenNotFound()
3231

3332
public function testMemcacheValueIsReturned()
3433
{
35-
$memcache = $this->getMockBuilder(stdClass::class)->addMethods(['get', 'getResultCode'])->getMock();
34+
$memcache = $this->getMockBuilder(Memcached::class)->onlyMethods(['get', 'getResultCode'])->getMock();
3635
$memcache->expects($this->once())->method('get')->willReturn('bar');
3736
$memcache->expects($this->once())->method('getResultCode')->willReturn(0);
3837
$store = new MemcachedStore($memcache);
@@ -41,7 +40,7 @@ public function testMemcacheValueIsReturned()
4140

4241
public function testMemcacheGetMultiValuesAreReturnedWithCorrectKeys()
4342
{
44-
$memcache = $this->getMockBuilder(stdClass::class)->addMethods(['getMulti', 'getResultCode'])->getMock();
43+
$memcache = $this->getMockBuilder(Memcached::class)->onlyMethods(['getMulti', 'getResultCode'])->getMock();
4544
$memcache->expects($this->once())->method('getMulti')->with(
4645
['foo:foo', 'foo:bar', 'foo:baz']
4746
)->willReturn([

tests/Database/DatabaseConcernsBuildsQueriesTraitTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ class DatabaseConcernsBuildsQueriesTraitTest extends TestCase
99
{
1010
public function testTapCallbackInstance()
1111
{
12-
$mock = $this->getMockForTrait(BuildsQueries::class);
12+
$mock = new class
13+
{
14+
use BuildsQueries;
15+
};
16+
1317
$mock->tap(function ($builder) use ($mock) {
1418
$this->assertEquals($mock, $builder);
1519
});

tests/Database/DatabaseConnectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ public function testSelectResultsetsReturnsMultipleRowset()
117117
$statement->expects($this->once())->method('bindValue')->with(1, 'foo', 2);
118118
$statement->expects($this->once())->method('execute');
119119
$statement->expects($this->atLeastOnce())->method('fetchAll')->willReturn(['boom']);
120-
$statement->expects($this->atLeastOnce())->method('nextRowset')->will($this->returnCallback(function () {
120+
$statement->expects($this->atLeastOnce())->method('nextRowset')->willReturnCallback(function () {
121121
static $i = 1;
122122

123123
return ++$i <= 2;
124-
}));
124+
});
125125
$pdo->expects($this->once())->method('prepare')->with('CALL a_procedure(?)')->willReturn($statement);
126126
$mock = $this->getMockConnection(['prepareBindings'], $writePdo);
127127
$mock->setReadPdo($pdo);

tests/Foundation/Testing/DatabaseMigrationsTest.php

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,36 @@
22

33
namespace Illuminate\Tests\Foundation\Testing;
44

5-
use Illuminate\Contracts\Console\Kernel;
5+
use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;
66
use Illuminate\Foundation\Testing\DatabaseMigrations;
77
use Illuminate\Foundation\Testing\RefreshDatabaseState;
88
use Mockery as m;
9+
use Orchestra\Testbench\Concerns\Testing;
10+
use Orchestra\Testbench\Foundation\Application as Testbench;
911
use PHPUnit\Framework\TestCase;
1012
use ReflectionMethod;
1113

14+
use function Orchestra\Testbench\package_path;
15+
1216
class DatabaseMigrationsTest extends TestCase
1317
{
1418
protected $traitObject;
1519

1620
protected function setUp(): void
1721
{
18-
RefreshDatabaseState::$migrated = false;
22+
$this->traitObject = m::mock(DatabaseMigrationsTestMockClass::class)->makePartial();
23+
$this->traitObject->setUp();
24+
}
1925

20-
$this->traitObject = $this->getMockForAbstractClass(DatabaseMigrationsTestMockClass::class, [], '', true, true, true, [
21-
'artisan',
22-
'beforeApplicationDestroyed',
23-
]);
26+
protected function tearDown(): void
27+
{
28+
$this->traitObject->tearDown();
2429

25-
$kernelObj = m::mock();
26-
$kernelObj->shouldReceive('setArtisan')
27-
->with(null);
30+
if ($container = m::getContainer()) {
31+
$this->addToAssertionCount($container->mockery_getExpectationCount());
32+
}
2833

29-
$this->traitObject->app = [
30-
Kernel::class => $kernelObj,
31-
];
34+
m::close();
3235
}
3336

3437
private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
@@ -44,8 +47,8 @@ private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
4447
public function testRefreshTestDatabaseDefault()
4548
{
4649
$this->traitObject
47-
->expects($this->once())
48-
->method('artisan')
50+
->shouldReceive('artisan')
51+
->once()
4952
->with('migrate:fresh', [
5053
'--drop-views' => false,
5154
'--drop-types' => false,
@@ -62,8 +65,8 @@ public function testRefreshTestDatabaseWithDropViewsOption()
6265
$this->traitObject->dropViews = true;
6366

6467
$this->traitObject
65-
->expects($this->once())
66-
->method('artisan')
68+
->shouldReceive('artisan')
69+
->once()
6770
->with('migrate:fresh', [
6871
'--drop-views' => true,
6972
'--drop-types' => false,
@@ -80,8 +83,8 @@ public function testRefreshTestDatabaseWithDropTypesOption()
8083
$this->traitObject->dropTypes = true;
8184

8285
$this->traitObject
83-
->expects($this->once())
84-
->method('artisan')
86+
->shouldReceive('artisan')
87+
->once()
8588
->with('migrate:fresh', [
8689
'--drop-views' => false,
8790
'--drop-types' => true,
@@ -97,10 +100,43 @@ public function testRefreshTestDatabaseWithDropTypesOption()
97100
class DatabaseMigrationsTestMockClass
98101
{
99102
use DatabaseMigrations;
100-
101-
public $app;
103+
use InteractsWithConsole;
104+
use Testing;
102105

103106
public $dropViews = false;
104107

105108
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+
);
141+
}
106142
}

tests/Foundation/Testing/RefreshDatabaseTest.php

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
namespace Illuminate\Tests\Foundation\Testing;
44

5-
use Illuminate\Contracts\Console\Kernel;
5+
use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
77
use Illuminate\Foundation\Testing\RefreshDatabaseState;
88
use Mockery as m;
9+
use Orchestra\Testbench\Concerns\Testing;
10+
use Orchestra\Testbench\Foundation\Application as Testbench;
911
use PHPUnit\Framework\TestCase;
1012
use ReflectionMethod;
1113

14+
use function Orchestra\Testbench\package_path;
15+
1216
class RefreshDatabaseTest extends TestCase
1317
{
1418
protected $traitObject;
@@ -17,18 +21,19 @@ protected function setUp(): void
1721
{
1822
RefreshDatabaseState::$migrated = false;
1923

20-
$this->traitObject = $this->getMockForAbstractClass(RefreshDatabaseTestMockClass::class, [], '', true, true, true, [
21-
'artisan',
22-
'beginDatabaseTransaction',
23-
]);
24+
$this->traitObject = m::mock(RefreshDatabaseTestMockClass::class)->makePartial();
25+
$this->traitObject->setUp();
26+
}
27+
28+
protected function tearDown(): void
29+
{
30+
$this->traitObject->tearDown();
2431

25-
$kernelObj = m::mock();
26-
$kernelObj->shouldReceive('setArtisan')
27-
->with(null);
32+
if ($container = m::getContainer()) {
33+
$this->addToAssertionCount($container->mockery_getExpectationCount());
34+
}
2835

29-
$this->traitObject->app = [
30-
Kernel::class => $kernelObj,
31-
];
36+
m::close();
3237
}
3338

3439
private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
@@ -44,8 +49,8 @@ private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
4449
public function testRefreshTestDatabaseDefault()
4550
{
4651
$this->traitObject
47-
->expects($this->once())
48-
->method('artisan')
52+
->shouldReceive('artisan')
53+
->once()
4954
->with('migrate:fresh', [
5055
'--drop-views' => false,
5156
'--drop-types' => false,
@@ -62,8 +67,8 @@ public function testRefreshTestDatabaseWithDropViewsOption()
6267
$this->traitObject->dropViews = true;
6368

6469
$this->traitObject
65-
->expects($this->once())
66-
->method('artisan')
70+
->shouldReceive('artisan')
71+
->once()
6772
->with('migrate:fresh', [
6873
'--drop-views' => true,
6974
'--drop-types' => false,
@@ -80,8 +85,8 @@ public function testRefreshTestDatabaseWithDropTypesOption()
8085
$this->traitObject->dropTypes = true;
8186

8287
$this->traitObject
83-
->expects($this->once())
84-
->method('artisan')
88+
->shouldReceive('artisan')
89+
->once()
8590
->with('migrate:fresh', [
8691
'--drop-views' => false,
8792
'--drop-types' => true,
@@ -96,11 +101,44 @@ public function testRefreshTestDatabaseWithDropTypesOption()
96101

97102
class RefreshDatabaseTestMockClass
98103
{
104+
use InteractsWithConsole;
99105
use RefreshDatabase;
100-
101-
public $app;
106+
use Testing;
102107

103108
public $dropViews = false;
104109

105110
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+
);
143+
}
106144
}

tests/Foundation/Testing/Traits/CanConfigureMigrationCommandsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class CanConfigureMigrationCommandsTest extends TestCase
1212

1313
protected function setUp(): void
1414
{
15-
$this->traitObject = $this->getMockForAbstractClass(CanConfigureMigrationCommandsTestMockClass::class);
15+
$this->traitObject = new CanConfigureMigrationCommandsTestMockClass();
1616
}
1717

1818
private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)

0 commit comments

Comments
 (0)