Skip to content

Commit

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

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

* wip

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

* wip

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

* Apply fixes from StyleCI

* Update CacheMemcachedStoreTest.php

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

* Apply fixes from StyleCI

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
crynobone and StyleCIBot authored Oct 25, 2023
1 parent d900366 commit 38e7183
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 80 deletions.
23 changes: 8 additions & 15 deletions tests/Auth/AuthPasswordBrokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,16 @@ protected function tearDown(): void
public function testIfUserIsNotFoundErrorRedirectIsReturned()
{
$mocks = $this->getMocks();
$broker = $this->getMockBuilder(PasswordBroker::class)
->onlyMethods(['getUser'])
->addMethods(['makeErrorRedirect'])
->setConstructorArgs(array_values($mocks))
->getMock();
$broker->expects($this->once())->method('getUser')->willReturn(null);
$broker = m::mock(PasswordBroker::class, array_values($mocks))->makePartial();
$broker->shouldReceive('getUser')->once()->andReturnNull();

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

public function testIfTokenIsRecentlyCreated()
{
$mocks = $this->getMocks();
$broker = $this->getMockBuilder(PasswordBroker::class)->addMethods(['emailResetLink', 'getUri'])->setConstructorArgs(array_values($mocks))->getMock();
$broker = m::mock(PasswordBroker::class, array_values($mocks))->makePartial();
$mocks['users']->shouldReceive('retrieveByCredentials')->once()->with(['foo'])->andReturn($user = m::mock(CanResetPassword::class));
$mocks['tokens']->shouldReceive('recentlyCreatedToken')->once()->with($user)->andReturn(true);
$user->shouldReceive('sendPasswordResetNotification')->with('token');
Expand Down Expand Up @@ -65,7 +61,7 @@ public function testUserIsRetrievedByCredentials()
public function testBrokerCreatesTokenAndRedirectsWithoutError()
{
$mocks = $this->getMocks();
$broker = $this->getMockBuilder(PasswordBroker::class)->addMethods(['emailResetLink', 'getUri'])->setConstructorArgs(array_values($mocks))->getMock();
$broker = m::mock(PasswordBroker::class, array_values($mocks))->makePartial();
$mocks['users']->shouldReceive('retrieveByCredentials')->once()->with(['foo'])->andReturn($user = m::mock(CanResetPassword::class));
$mocks['tokens']->shouldReceive('recentlyCreatedToken')->once()->with($user)->andReturn(false);
$mocks['tokens']->shouldReceive('create')->once()->with($user)->andReturn('token');
Expand Down Expand Up @@ -99,12 +95,9 @@ public function testRedirectReturnedByRemindWhenRecordDoesntExistInTable()
public function testResetRemovesRecordOnReminderTableAndCallsCallback()
{
unset($_SERVER['__password.reset.test']);
$broker = $this->getMockBuilder(PasswordBroker::class)
->onlyMethods(['validateReset'])
->addMethods(['getPassword', 'getToken'])
->setConstructorArgs(array_values($mocks = $this->getMocks()))
->getMock();
$broker->expects($this->once())->method('validateReset')->willReturn($user = m::mock(CanResetPassword::class));
$mocks = $this->getMocks();
$broker = m::mock(PasswordBroker::class, array_values($mocks))->makePartial()->shouldAllowMockingProtectedMethods();
$broker->shouldReceive('validateReset')->once()->andReturn($user = m::mock(CanResetPassword::class));
$mocks['tokens']->shouldReceive('delete')->once()->with($user);
$callback = function ($user, $password) {
$_SERVER['__password.reset.test'] = compact('user', 'password');
Expand All @@ -125,7 +118,7 @@ public function testExecutesCallbackInsteadOfSendingNotification()
};

$mocks = $this->getMocks();
$broker = $this->getMockBuilder(PasswordBroker::class)->addMethods(['emailResetLink', 'getUri'])->setConstructorArgs(array_values($mocks))->getMock();
$broker = m::mock(PasswordBroker::class, array_values($mocks))->makePartial();
$mocks['users']->shouldReceive('retrieveByCredentials')->once()->with(['foo'])->andReturn($user = m::mock(CanResetPassword::class));
$mocks['tokens']->shouldReceive('recentlyCreatedToken')->once()->with($user)->andReturn(false);
$mocks['tokens']->shouldReceive('create')->once()->with($user)->andReturn('token');
Expand Down
7 changes: 3 additions & 4 deletions tests/Cache/CacheMemcachedStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Memcached;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use stdClass;

/**
* @requires extension memcached
Expand All @@ -23,7 +22,7 @@ protected function tearDown(): void

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

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

public function testMemcacheGetMultiValuesAreReturnedWithCorrectKeys()
{
$memcache = $this->getMockBuilder(stdClass::class)->addMethods(['getMulti', 'getResultCode'])->getMock();
$memcache = $this->getMockBuilder(Memcached::class)->onlyMethods(['getMulti', 'getResultCode'])->getMock();
$memcache->expects($this->once())->method('getMulti')->with(
['foo:foo', 'foo:bar', 'foo:baz']
)->willReturn([
Expand Down
6 changes: 5 additions & 1 deletion tests/Database/DatabaseConcernsBuildsQueriesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class DatabaseConcernsBuildsQueriesTraitTest extends TestCase
{
public function testTapCallbackInstance()
{
$mock = $this->getMockForTrait(BuildsQueries::class);
$mock = new class
{
use BuildsQueries;
};

$mock->tap(function ($builder) use ($mock) {
$this->assertEquals($mock, $builder);
});
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ public function testSelectResultsetsReturnsMultipleRowset()
$statement->expects($this->once())->method('bindValue')->with(1, 'foo', 2);
$statement->expects($this->once())->method('execute');
$statement->expects($this->atLeastOnce())->method('fetchAll')->willReturn(['boom']);
$statement->expects($this->atLeastOnce())->method('nextRowset')->will($this->returnCallback(function () {
$statement->expects($this->atLeastOnce())->method('nextRowset')->willReturnCallback(function () {
static $i = 1;

return ++$i <= 2;
}));
});
$pdo->expects($this->once())->method('prepare')->with('CALL a_procedure(?)')->willReturn($statement);
$mock = $this->getMockConnection(['prepareBindings'], $writePdo);
$mock->setReadPdo($pdo);
Expand Down
76 changes: 56 additions & 20 deletions tests/Foundation/Testing/DatabaseMigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,36 @@

namespace Illuminate\Tests\Foundation\Testing;

use Illuminate\Contracts\Console\Kernel;
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\Foundation\Application as Testbench;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;

use function Orchestra\Testbench\package_path;

class DatabaseMigrationsTest extends TestCase
{
protected $traitObject;

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

$this->traitObject = $this->getMockForAbstractClass(DatabaseMigrationsTestMockClass::class, [], '', true, true, true, [
'artisan',
'beforeApplicationDestroyed',
]);
protected function tearDown(): void
{
$this->traitObject->tearDown();

$kernelObj = m::mock();
$kernelObj->shouldReceive('setArtisan')
->with(null);
if ($container = m::getContainer()) {
$this->addToAssertionCount($container->mockery_getExpectationCount());
}

$this->traitObject->app = [
Kernel::class => $kernelObj,
];
m::close();
}

private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
Expand All @@ -44,8 +47,8 @@ private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
public function testRefreshTestDatabaseDefault()
{
$this->traitObject
->expects($this->once())
->method('artisan')
->shouldReceive('artisan')
->once()
->with('migrate:fresh', [
'--drop-views' => false,
'--drop-types' => false,
Expand All @@ -62,8 +65,8 @@ public function testRefreshTestDatabaseWithDropViewsOption()
$this->traitObject->dropViews = true;

$this->traitObject
->expects($this->once())
->method('artisan')
->shouldReceive('artisan')
->once()
->with('migrate:fresh', [
'--drop-views' => true,
'--drop-types' => false,
Expand All @@ -80,8 +83,8 @@ public function testRefreshTestDatabaseWithDropTypesOption()
$this->traitObject->dropTypes = true;

$this->traitObject
->expects($this->once())
->method('artisan')
->shouldReceive('artisan')
->once()
->with('migrate:fresh', [
'--drop-views' => false,
'--drop-types' => true,
Expand All @@ -97,10 +100,43 @@ public function testRefreshTestDatabaseWithDropTypesOption()
class DatabaseMigrationsTestMockClass
{
use DatabaseMigrations;

public $app;
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')
);
}
}
76 changes: 57 additions & 19 deletions tests/Foundation/Testing/RefreshDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Illuminate\Tests\Foundation\Testing;

use Illuminate\Contracts\Console\Kernel;
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\Foundation\Application as Testbench;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;

use function Orchestra\Testbench\package_path;

class RefreshDatabaseTest extends TestCase
{
protected $traitObject;
Expand All @@ -17,18 +21,19 @@ protected function setUp(): void
{
RefreshDatabaseState::$migrated = false;

$this->traitObject = $this->getMockForAbstractClass(RefreshDatabaseTestMockClass::class, [], '', true, true, true, [
'artisan',
'beginDatabaseTransaction',
]);
$this->traitObject = m::mock(RefreshDatabaseTestMockClass::class)->makePartial();
$this->traitObject->setUp();
}

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

$kernelObj = m::mock();
$kernelObj->shouldReceive('setArtisan')
->with(null);
if ($container = m::getContainer()) {
$this->addToAssertionCount($container->mockery_getExpectationCount());
}

$this->traitObject->app = [
Kernel::class => $kernelObj,
];
m::close();
}

private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
Expand All @@ -44,8 +49,8 @@ private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
public function testRefreshTestDatabaseDefault()
{
$this->traitObject
->expects($this->once())
->method('artisan')
->shouldReceive('artisan')
->once()
->with('migrate:fresh', [
'--drop-views' => false,
'--drop-types' => false,
Expand All @@ -62,8 +67,8 @@ public function testRefreshTestDatabaseWithDropViewsOption()
$this->traitObject->dropViews = true;

$this->traitObject
->expects($this->once())
->method('artisan')
->shouldReceive('artisan')
->once()
->with('migrate:fresh', [
'--drop-views' => true,
'--drop-types' => false,
Expand All @@ -80,8 +85,8 @@ public function testRefreshTestDatabaseWithDropTypesOption()
$this->traitObject->dropTypes = true;

$this->traitObject
->expects($this->once())
->method('artisan')
->shouldReceive('artisan')
->once()
->with('migrate:fresh', [
'--drop-views' => false,
'--drop-types' => true,
Expand All @@ -96,11 +101,44 @@ public function testRefreshTestDatabaseWithDropTypesOption()

class RefreshDatabaseTestMockClass
{
use InteractsWithConsole;
use RefreshDatabase;

public $app;
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')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CanConfigureMigrationCommandsTest extends TestCase

protected function setUp(): void
{
$this->traitObject = $this->getMockForAbstractClass(CanConfigureMigrationCommandsTestMockClass::class);
$this->traitObject = new CanConfigureMigrationCommandsTestMockClass();
}

private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName)
Expand Down
Loading

0 comments on commit 38e7183

Please sign in to comment.