From bb6f68df2bd5ad1f552bdaa000c9223aeaf5eb15 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 25 Oct 2023 11:37:16 +0800 Subject: [PATCH 01/12] Test Improvements Signed-off-by: Mior Muhammad Zaki --- .../Testing/DatabaseMigrationsTest.php | 47 ++++++++++----- .../Testing/RefreshDatabaseTest.php | 60 ++++++++++++------- .../CanConfigureMigrationCommandsTest.php | 2 +- 3 files changed, 72 insertions(+), 37 deletions(-) diff --git a/tests/Foundation/Testing/DatabaseMigrationsTest.php b/tests/Foundation/Testing/DatabaseMigrationsTest.php index 133a7f5fdd06..ee717b0d7951 100644 --- a/tests/Foundation/Testing/DatabaseMigrationsTest.php +++ b/tests/Foundation/Testing/DatabaseMigrationsTest.php @@ -3,12 +3,16 @@ 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\Foundation\Application as Testbench; use PHPUnit\Framework\TestCase; use ReflectionMethod; +use function Orchestra\Testbench\package_path; + class DatabaseMigrationsTest extends TestCase { protected $traitObject; @@ -17,18 +21,18 @@ protected function setUp(): void { RefreshDatabaseState::$migrated = false; - $this->traitObject = $this->getMockForAbstractClass(DatabaseMigrationsTestMockClass::class, [], '', true, true, true, [ - 'artisan', - 'beforeApplicationDestroyed', - ]); + $this->traitObject = m::mock(DatabaseMigrationsTestMockClass::class)->makePartial(); + + $app = Testbench::create( + basePath: package_path('vendor/orchestra/testbench-core/laravel') + ); - $kernelObj = m::mock(); - $kernelObj->shouldReceive('setArtisan') - ->with(null); + $this->traitObject->app = $app; + } - $this->traitObject->app = [ - Kernel::class => $kernelObj, - ]; + protected function tearDown(): void + { + m::close(); } private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName) @@ -44,8 +48,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, @@ -62,8 +66,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, @@ -80,8 +84,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, @@ -97,10 +101,21 @@ public function testRefreshTestDatabaseWithDropTypesOption() class DatabaseMigrationsTestMockClass { use DatabaseMigrations; + use InteractsWithConsole; public $app; public $dropViews = false; public $dropTypes = false; + + public function __construct() + { + $this->withoutMockingConsoleOutput(); + } + + public function beforeApplicationDestroyed() + { + // + } } diff --git a/tests/Foundation/Testing/RefreshDatabaseTest.php b/tests/Foundation/Testing/RefreshDatabaseTest.php index a5acc8275c85..aad92cc517d7 100644 --- a/tests/Foundation/Testing/RefreshDatabaseTest.php +++ b/tests/Foundation/Testing/RefreshDatabaseTest.php @@ -2,13 +2,16 @@ 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\Foundation\Application as Testbench; use PHPUnit\Framework\TestCase; use ReflectionMethod; +use function Orchestra\Testbench\package_path; + class RefreshDatabaseTest extends TestCase { protected $traitObject; @@ -17,18 +20,18 @@ 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(); + + $app = Testbench::create( + basePath: package_path('vendor/orchestra/testbench-core/laravel') + ); - $kernelObj = m::mock(); - $kernelObj->shouldReceive('setArtisan') - ->with(null); + $this->traitObject->app = $app; + } - $this->traitObject->app = [ - Kernel::class => $kernelObj, - ]; + protected function tearDown(): void + { + m::close(); } private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName) @@ -44,13 +47,15 @@ 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, '--seed' => false, - ]); + ])->andReturnUsing(function () { + $this->addToAssertionCount(1); + }); $refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase'); @@ -62,13 +67,15 @@ 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, '--seed' => false, - ]); + ])->andReturnUsing(function () { + $this->addToAssertionCount(1); + }); $refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase'); @@ -80,13 +87,15 @@ 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, '--seed' => false, - ]); + ])->andReturnUsing(function () { + $this->addToAssertionCount(1); + }); $refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase'); @@ -96,6 +105,7 @@ public function testRefreshTestDatabaseWithDropTypesOption() class RefreshDatabaseTestMockClass { + use InteractsWithConsole; use RefreshDatabase; public $app; @@ -103,4 +113,14 @@ class RefreshDatabaseTestMockClass public $dropViews = false; public $dropTypes = false; + + public function __construct() + { + $this->withoutMockingConsoleOutput(); + } + + public function beforeApplicationDestroyed() + { + // + } } diff --git a/tests/Foundation/Testing/Traits/CanConfigureMigrationCommandsTest.php b/tests/Foundation/Testing/Traits/CanConfigureMigrationCommandsTest.php index 4a5a49696f1e..58f54a633931 100644 --- a/tests/Foundation/Testing/Traits/CanConfigureMigrationCommandsTest.php +++ b/tests/Foundation/Testing/Traits/CanConfigureMigrationCommandsTest.php @@ -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) From 6327823f2b24c7ff4a33207f1019a0c02b823f8e Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 25 Oct 2023 11:59:48 +0800 Subject: [PATCH 02/12] wip Signed-off-by: Mior Muhammad Zaki --- .../Testing/DatabaseMigrationsTest.php | 38 +++++++++----- .../Testing/RefreshDatabaseTest.php | 50 +++++++++++-------- 2 files changed, 54 insertions(+), 34 deletions(-) diff --git a/tests/Foundation/Testing/DatabaseMigrationsTest.php b/tests/Foundation/Testing/DatabaseMigrationsTest.php index ee717b0d7951..ba592f941d4d 100644 --- a/tests/Foundation/Testing/DatabaseMigrationsTest.php +++ b/tests/Foundation/Testing/DatabaseMigrationsTest.php @@ -6,7 +6,9 @@ use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\RefreshDatabaseState; +use Illuminate\Support\Facades\Facade; use Mockery as m; +use Orchestra\Testbench\Concerns\Testing; use Orchestra\Testbench\Foundation\Application as Testbench; use PHPUnit\Framework\TestCase; use ReflectionMethod; @@ -22,17 +24,12 @@ protected function setUp(): void RefreshDatabaseState::$migrated = false; $this->traitObject = m::mock(DatabaseMigrationsTestMockClass::class)->makePartial(); - - $app = Testbench::create( - basePath: package_path('vendor/orchestra/testbench-core/laravel') - ); - - $this->traitObject->app = $app; + $this->traitObject->prepare(); } protected function tearDown(): void { - m::close(); + unset($this->traitObject); } private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName) @@ -102,20 +99,37 @@ class DatabaseMigrationsTestMockClass { use DatabaseMigrations; use InteractsWithConsole; - - public $app; + use Testing; public $dropViews = false; public $dropTypes = false; - public function __construct() + public function prepare() { + $this->app = $this->refreshApplication(); $this->withoutMockingConsoleOutput(); } - public function beforeApplicationDestroyed() + public function __destruct() + { + $this->tearDownTheTestEnvironment(); + } + + 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') + ); } } diff --git a/tests/Foundation/Testing/RefreshDatabaseTest.php b/tests/Foundation/Testing/RefreshDatabaseTest.php index aad92cc517d7..197642563be5 100644 --- a/tests/Foundation/Testing/RefreshDatabaseTest.php +++ b/tests/Foundation/Testing/RefreshDatabaseTest.php @@ -6,10 +6,10 @@ 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 @@ -21,17 +21,12 @@ protected function setUp(): void RefreshDatabaseState::$migrated = false; $this->traitObject = m::mock(RefreshDatabaseTestMockClass::class)->makePartial(); - - $app = Testbench::create( - basePath: package_path('vendor/orchestra/testbench-core/laravel') - ); - - $this->traitObject->app = $app; + $this->traitObject->prepare(); } protected function tearDown(): void { - m::close(); + unset($this->traitObject); } private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName) @@ -53,9 +48,7 @@ public function testRefreshTestDatabaseDefault() '--drop-views' => false, '--drop-types' => false, '--seed' => false, - ])->andReturnUsing(function () { - $this->addToAssertionCount(1); - }); + ]); $refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase'); @@ -73,9 +66,7 @@ public function testRefreshTestDatabaseWithDropViewsOption() '--drop-views' => true, '--drop-types' => false, '--seed' => false, - ])->andReturnUsing(function () { - $this->addToAssertionCount(1); - }); + ]); $refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase'); @@ -93,9 +84,7 @@ public function testRefreshTestDatabaseWithDropTypesOption() '--drop-views' => false, '--drop-types' => true, '--seed' => false, - ])->andReturnUsing(function () { - $this->addToAssertionCount(1); - }); + ]); $refreshTestDatabaseReflection = $this->__reflectAndSetupAccessibleForProtectedTraitMethod('refreshTestDatabase'); @@ -107,20 +96,37 @@ class RefreshDatabaseTestMockClass { use InteractsWithConsole; use RefreshDatabase; - - public $app; + use Testing; public $dropViews = false; public $dropTypes = false; - public function __construct() + public function prepare() { + $this->app = $this->refreshApplication(); $this->withoutMockingConsoleOutput(); } - public function beforeApplicationDestroyed() + public function __destruct() + { + $this->tearDownTheTestEnvironment(); + } + + 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') + ); } } From 8cb3ff73a1eea4ec8161a90a00d5f981eaf7ee55 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 25 Oct 2023 04:00:07 +0000 Subject: [PATCH 03/12] Apply fixes from StyleCI --- tests/Foundation/Testing/DatabaseMigrationsTest.php | 2 -- tests/Foundation/Testing/RefreshDatabaseTest.php | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Foundation/Testing/DatabaseMigrationsTest.php b/tests/Foundation/Testing/DatabaseMigrationsTest.php index ba592f941d4d..ccb65ab9af27 100644 --- a/tests/Foundation/Testing/DatabaseMigrationsTest.php +++ b/tests/Foundation/Testing/DatabaseMigrationsTest.php @@ -2,11 +2,9 @@ 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 Illuminate\Support\Facades\Facade; use Mockery as m; use Orchestra\Testbench\Concerns\Testing; use Orchestra\Testbench\Foundation\Application as Testbench; diff --git a/tests/Foundation/Testing/RefreshDatabaseTest.php b/tests/Foundation/Testing/RefreshDatabaseTest.php index 197642563be5..7296d6c34355 100644 --- a/tests/Foundation/Testing/RefreshDatabaseTest.php +++ b/tests/Foundation/Testing/RefreshDatabaseTest.php @@ -10,6 +10,7 @@ use Orchestra\Testbench\Foundation\Application as Testbench; use PHPUnit\Framework\TestCase; use ReflectionMethod; + use function Orchestra\Testbench\package_path; class RefreshDatabaseTest extends TestCase From 7c73792a3f1d2e441201bc9b983400ecd9797bf7 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 25 Oct 2023 13:18:21 +0800 Subject: [PATCH 04/12] wip Signed-off-by: Mior Muhammad Zaki --- .../Testing/DatabaseMigrationsTest.php | 23 +++++++++++++------ .../Testing/RefreshDatabaseTest.php | 21 +++++++++++++---- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/tests/Foundation/Testing/DatabaseMigrationsTest.php b/tests/Foundation/Testing/DatabaseMigrationsTest.php index ccb65ab9af27..0d14f7909c22 100644 --- a/tests/Foundation/Testing/DatabaseMigrationsTest.php +++ b/tests/Foundation/Testing/DatabaseMigrationsTest.php @@ -19,15 +19,19 @@ class DatabaseMigrationsTest extends TestCase protected function setUp(): void { - RefreshDatabaseState::$migrated = false; - $this->traitObject = m::mock(DatabaseMigrationsTestMockClass::class)->makePartial(); - $this->traitObject->prepare(); + $this->traitObject->setUp(); } protected function tearDown(): void { - unset($this->traitObject); + $this->traitObject->tearDown(); + + if ($container = m::getContainer()) { + $this->addToAssertionCount($container->mockery_getExpectationCount()); + } + + m::close(); } private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName) @@ -103,15 +107,20 @@ class DatabaseMigrationsTestMockClass public $dropTypes = false; - public function prepare() + public function setUp() { + RefreshDatabaseState::$migrated = false; + $this->app = $this->refreshApplication(); $this->withoutMockingConsoleOutput(); } - public function __destruct() + public function tearDown() { - $this->tearDownTheTestEnvironment(); + RefreshDatabaseState::$migrated = false; + + $this->callBeforeApplicationDestroyedCallbacks(); + $this->app?->flush(); } protected function setUpTraits() diff --git a/tests/Foundation/Testing/RefreshDatabaseTest.php b/tests/Foundation/Testing/RefreshDatabaseTest.php index 7296d6c34355..5a15a5d4958e 100644 --- a/tests/Foundation/Testing/RefreshDatabaseTest.php +++ b/tests/Foundation/Testing/RefreshDatabaseTest.php @@ -22,12 +22,18 @@ protected function setUp(): void RefreshDatabaseState::$migrated = false; $this->traitObject = m::mock(RefreshDatabaseTestMockClass::class)->makePartial(); - $this->traitObject->prepare(); + $this->traitObject->setUp(); } protected function tearDown(): void { - unset($this->traitObject); + $this->traitObject->tearDown(); + + if ($container = m::getContainer()) { + $this->addToAssertionCount($container->mockery_getExpectationCount()); + } + + m::close(); } private function __reflectAndSetupAccessibleForProtectedTraitMethod($methodName) @@ -103,15 +109,20 @@ class RefreshDatabaseTestMockClass public $dropTypes = false; - public function prepare() + public function setUp() { + RefreshDatabaseState::$migrated = false; + $this->app = $this->refreshApplication(); $this->withoutMockingConsoleOutput(); } - public function __destruct() + public function tearDown() { - $this->tearDownTheTestEnvironment(); + RefreshDatabaseState::$migrated = false; + + $this->callBeforeApplicationDestroyedCallbacks(); + $this->app?->flush(); } protected function setUpTraits() From d6d531990c4578c0937024ec619a143899b12c1a Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 25 Oct 2023 13:20:38 +0800 Subject: [PATCH 05/12] wip Signed-off-by: Mior Muhammad Zaki --- tests/Auth/AuthPasswordBrokerTest.php | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/tests/Auth/AuthPasswordBrokerTest.php b/tests/Auth/AuthPasswordBrokerTest.php index f89971b4c7cf..f37b5e82dc40 100755 --- a/tests/Auth/AuthPasswordBrokerTest.php +++ b/tests/Auth/AuthPasswordBrokerTest.php @@ -22,12 +22,8 @@ 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'])); } @@ -35,7 +31,7 @@ public function testIfUserIsNotFoundErrorRedirectIsReturned() 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'); @@ -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'); @@ -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'); @@ -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'); From 5e285da214320bd0ed21f1617bb84d50752c8b29 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 25 Oct 2023 13:30:44 +0800 Subject: [PATCH 06/12] wip Signed-off-by: Mior Muhammad Zaki --- tests/Cache/CacheMemcachedStoreTest.php | 20 ++++++++-------- ...DatabaseConcernsBuildsQueriesTraitTest.php | 5 +++- tests/Database/DatabaseConnectionTest.php | 4 ++-- tests/Queue/QueueDatabaseQueueUnitTest.php | 4 ++-- tests/Queue/QueueSqsJobTest.php | 23 ++++++------------- 5 files changed, 25 insertions(+), 31 deletions(-) diff --git a/tests/Cache/CacheMemcachedStoreTest.php b/tests/Cache/CacheMemcachedStoreTest.php index 367a8ca1b655..980b4b550a93 100755 --- a/tests/Cache/CacheMemcachedStoreTest.php +++ b/tests/Cache/CacheMemcachedStoreTest.php @@ -23,31 +23,31 @@ protected function tearDown(): void public function testGetReturnsNullWhenNotFound() { - $memcache = $this->getMockBuilder(stdClass::class)->addMethods(['get', 'getResultCode'])->getMock(); - $memcache->expects($this->once())->method('get')->with($this->equalTo('foo:bar'))->willReturn(null); - $memcache->expects($this->once())->method('getResultCode')->willReturn(1); + $memcache = m::mock(Memcached::class)->makePartial(); + $memcache->shouldReceive('get')->once()->with('foo:bar')->andReturn(null); + $memcache->shouldReceive('getResultCode')->once()->andReturn(1); $store = new MemcachedStore($memcache, 'foo'); $this->assertNull($store->get('bar')); } public function testMemcacheValueIsReturned() { - $memcache = $this->getMockBuilder(stdClass::class)->addMethods(['get', 'getResultCode'])->getMock(); - $memcache->expects($this->once())->method('get')->willReturn('bar'); - $memcache->expects($this->once())->method('getResultCode')->willReturn(0); + $memcache = m::mock(Memcached::class)->makePartial(); + $memcache->shouldReceive('get')->once()->andReturn('bar'); + $memcache->shouldReceive('getResultCode')->once()->andReturn(0); $store = new MemcachedStore($memcache); $this->assertSame('bar', $store->get('foo')); } public function testMemcacheGetMultiValuesAreReturnedWithCorrectKeys() { - $memcache = $this->getMockBuilder(stdClass::class)->addMethods(['getMulti', 'getResultCode'])->getMock(); - $memcache->expects($this->once())->method('getMulti')->with( + $memcache = m::mock(Memcached::class)->makePartial(); + $memcache->shouldReceive('getMulti')->once()->with( ['foo:foo', 'foo:bar', 'foo:baz'] - )->willReturn([ + )->andReturn([ 'fizz', 'buzz', 'norf', ]); - $memcache->expects($this->once())->method('getResultCode')->willReturn(0); + $memcache->shouldReceive('getResultCode')->once()->andReturn(0); $store = new MemcachedStore($memcache, 'foo'); $this->assertEquals([ 'foo' => 'fizz', diff --git a/tests/Database/DatabaseConcernsBuildsQueriesTraitTest.php b/tests/Database/DatabaseConcernsBuildsQueriesTraitTest.php index 55d03551a542..03c3152d6efe 100644 --- a/tests/Database/DatabaseConcernsBuildsQueriesTraitTest.php +++ b/tests/Database/DatabaseConcernsBuildsQueriesTraitTest.php @@ -9,7 +9,10 @@ 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); }); diff --git a/tests/Database/DatabaseConnectionTest.php b/tests/Database/DatabaseConnectionTest.php index 9ad3819a0057..b91db617a9b5 100755 --- a/tests/Database/DatabaseConnectionTest.php +++ b/tests/Database/DatabaseConnectionTest.php @@ -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); diff --git a/tests/Queue/QueueDatabaseQueueUnitTest.php b/tests/Queue/QueueDatabaseQueueUnitTest.php index e4bfe062c018..17087db4e592 100644 --- a/tests/Queue/QueueDatabaseQueueUnitTest.php +++ b/tests/Queue/QueueDatabaseQueueUnitTest.php @@ -99,7 +99,7 @@ public function testFailureToCreatePayloadFromObject() $job = new stdClass; $job->invalid = "\xc3\x28"; - $queue = $this->getMockForAbstractClass(Queue::class); + $queue = m::mock(Queue::class)->makePartial(); $class = new ReflectionClass(Queue::class); $createPayload = $class->getMethod('createPayload'); @@ -113,7 +113,7 @@ public function testFailureToCreatePayloadFromArray() { $this->expectException('InvalidArgumentException'); - $queue = $this->getMockForAbstractClass(Queue::class); + $queue = m::mock(Queue::class)->makePartial(); $class = new ReflectionClass(Queue::class); $createPayload = $class->getMethod('createPayload'); diff --git a/tests/Queue/QueueSqsJobTest.php b/tests/Queue/QueueSqsJobTest.php index bebe16b8209a..83706d769585 100644 --- a/tests/Queue/QueueSqsJobTest.php +++ b/tests/Queue/QueueSqsJobTest.php @@ -45,10 +45,7 @@ protected function setUp(): void $this->queueUrl = $this->baseUrl.'/'.$this->account.'/'.$this->queueName; // Get a mock of the SqsClient - $this->mockedSqsClient = $this->getMockBuilder(SqsClient::class) - ->addMethods(['deleteMessage']) - ->disableOriginalConstructor() - ->getMock(); + $this->mockedSqsClient = m::mock(SqsClient::class)->makePartial(); // Use Mockery to mock the IoC Container $this->mockedContainer = m::mock(Container::class); @@ -83,27 +80,21 @@ public function testFireProperlyCallsTheJobHandler() public function testDeleteRemovesTheJobFromSqs() { - $this->mockedSqsClient = $this->getMockBuilder(SqsClient::class) - ->addMethods(['deleteMessage']) - ->disableOriginalConstructor() - ->getMock(); - $queue = $this->getMockBuilder(SqsQueue::class)->onlyMethods(['getQueue'])->setConstructorArgs([$this->mockedSqsClient, $this->queueName, $this->account])->getMock(); + $this->mockedSqsClient = m::mock(SqsClient::class)->makePartial(); + $queue = m::mock(SqsQueue::class, [$this->mockedSqsClient, $this->queueName, $this->account])->makePartial(); $queue->setContainer($this->mockedContainer); $job = $this->getJob(); - $job->getSqs()->expects($this->once())->method('deleteMessage')->with(['QueueUrl' => $this->queueUrl, 'ReceiptHandle' => $this->mockedReceiptHandle]); + $job->getSqs()->shouldReceive('deleteMessage')->once()->with(['QueueUrl' => $this->queueUrl, 'ReceiptHandle' => $this->mockedReceiptHandle]); $job->delete(); } public function testReleaseProperlyReleasesTheJobOntoSqs() { - $this->mockedSqsClient = $this->getMockBuilder(SqsClient::class) - ->addMethods(['changeMessageVisibility']) - ->disableOriginalConstructor() - ->getMock(); - $queue = $this->getMockBuilder(SqsQueue::class)->onlyMethods(['getQueue'])->setConstructorArgs([$this->mockedSqsClient, $this->queueName, $this->account])->getMock(); + $this->mockedSqsClient = m::mock(SqsClient::class)->makePartial(); + $queue = m::mock(SqsQueue::class, [$this->mockedSqsClient, $this->queueName, $this->account])->makePartial(); $queue->setContainer($this->mockedContainer); $job = $this->getJob(); - $job->getSqs()->expects($this->once())->method('changeMessageVisibility')->with(['QueueUrl' => $this->queueUrl, 'ReceiptHandle' => $this->mockedReceiptHandle, 'VisibilityTimeout' => $this->releaseDelay]); + $job->getSqs()->shouldReceive('changeMessageVisibility')->once()->with(['QueueUrl' => $this->queueUrl, 'ReceiptHandle' => $this->mockedReceiptHandle, 'VisibilityTimeout' => $this->releaseDelay]); $job->release($this->releaseDelay); $this->assertTrue($job->isReleased()); } From a4eb8e5faad02a55bf1c40b7063925599070932b Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 25 Oct 2023 05:30:58 +0000 Subject: [PATCH 07/12] Apply fixes from StyleCI --- tests/Cache/CacheMemcachedStoreTest.php | 1 - tests/Database/DatabaseConcernsBuildsQueriesTraitTest.php | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Cache/CacheMemcachedStoreTest.php b/tests/Cache/CacheMemcachedStoreTest.php index 980b4b550a93..283c6d2a403c 100755 --- a/tests/Cache/CacheMemcachedStoreTest.php +++ b/tests/Cache/CacheMemcachedStoreTest.php @@ -7,7 +7,6 @@ use Memcached; use Mockery as m; use PHPUnit\Framework\TestCase; -use stdClass; /** * @requires extension memcached diff --git a/tests/Database/DatabaseConcernsBuildsQueriesTraitTest.php b/tests/Database/DatabaseConcernsBuildsQueriesTraitTest.php index 03c3152d6efe..730b91855ff1 100644 --- a/tests/Database/DatabaseConcernsBuildsQueriesTraitTest.php +++ b/tests/Database/DatabaseConcernsBuildsQueriesTraitTest.php @@ -9,7 +9,8 @@ class DatabaseConcernsBuildsQueriesTraitTest extends TestCase { public function testTapCallbackInstance() { - $mock = new class { + $mock = new class + { use BuildsQueries; }; From f627e590a9208c7ee61fc11e5eea75a4c0f7ad34 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 25 Oct 2023 13:49:23 +0800 Subject: [PATCH 08/12] Update CacheMemcachedStoreTest.php --- tests/Cache/CacheMemcachedStoreTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Cache/CacheMemcachedStoreTest.php b/tests/Cache/CacheMemcachedStoreTest.php index 283c6d2a403c..7561bdaeac94 100755 --- a/tests/Cache/CacheMemcachedStoreTest.php +++ b/tests/Cache/CacheMemcachedStoreTest.php @@ -7,6 +7,7 @@ use Memcached; use Mockery as m; use PHPUnit\Framework\TestCase; +use stdClass; /** * @requires extension memcached @@ -22,7 +23,7 @@ protected function tearDown(): void public function testGetReturnsNullWhenNotFound() { - $memcache = m::mock(Memcached::class)->makePartial(); + $memcache = m::mock(stdClass::class)->makePartial(); $memcache->shouldReceive('get')->once()->with('foo:bar')->andReturn(null); $memcache->shouldReceive('getResultCode')->once()->andReturn(1); $store = new MemcachedStore($memcache, 'foo'); @@ -31,7 +32,7 @@ public function testGetReturnsNullWhenNotFound() public function testMemcacheValueIsReturned() { - $memcache = m::mock(Memcached::class)->makePartial(); + $memcache = m::mock(stdClass::class)->makePartial(); $memcache->shouldReceive('get')->once()->andReturn('bar'); $memcache->shouldReceive('getResultCode')->once()->andReturn(0); $store = new MemcachedStore($memcache); @@ -40,7 +41,7 @@ public function testMemcacheValueIsReturned() public function testMemcacheGetMultiValuesAreReturnedWithCorrectKeys() { - $memcache = m::mock(Memcached::class)->makePartial(); + $memcache = m::mock(stdClass::class)->makePartial(); $memcache->shouldReceive('getMulti')->once()->with( ['foo:foo', 'foo:bar', 'foo:baz'] )->andReturn([ From bd566fbce656c9aacbeea38a7ee680d8b6eb2671 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 25 Oct 2023 14:05:16 +0800 Subject: [PATCH 09/12] wip Signed-off-by: Mior Muhammad Zaki --- tests/Cache/CacheMemcachedStoreTest.php | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/tests/Cache/CacheMemcachedStoreTest.php b/tests/Cache/CacheMemcachedStoreTest.php index 7561bdaeac94..867bda791b03 100755 --- a/tests/Cache/CacheMemcachedStoreTest.php +++ b/tests/Cache/CacheMemcachedStoreTest.php @@ -9,9 +9,6 @@ use PHPUnit\Framework\TestCase; use stdClass; -/** - * @requires extension memcached - */ class CacheMemcachedStoreTest extends TestCase { protected function tearDown(): void @@ -23,31 +20,31 @@ protected function tearDown(): void public function testGetReturnsNullWhenNotFound() { - $memcache = m::mock(stdClass::class)->makePartial(); - $memcache->shouldReceive('get')->once()->with('foo:bar')->andReturn(null); - $memcache->shouldReceive('getResultCode')->once()->andReturn(1); + $memcache = $this->getMockBuilder(stdClass::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'); $this->assertNull($store->get('bar')); } public function testMemcacheValueIsReturned() { - $memcache = m::mock(stdClass::class)->makePartial(); - $memcache->shouldReceive('get')->once()->andReturn('bar'); - $memcache->shouldReceive('getResultCode')->once()->andReturn(0); + $memcache = $this->getMockBuilder(stdClass::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); $this->assertSame('bar', $store->get('foo')); } public function testMemcacheGetMultiValuesAreReturnedWithCorrectKeys() { - $memcache = m::mock(stdClass::class)->makePartial(); - $memcache->shouldReceive('getMulti')->once()->with( + $memcache = $this->getMockBuilder(stdClass::class)->onlyMethods(['getMulti', 'getResultCode'])->getMock(); + $memcache->expects($this->once())->method('getMulti')->with( ['foo:foo', 'foo:bar', 'foo:baz'] - )->andReturn([ + )->willReturn([ 'fizz', 'buzz', 'norf', ]); - $memcache->shouldReceive('getResultCode')->once()->andReturn(0); + $memcache->expects($this->once())->method('getResultCode')->willReturn(0); $store = new MemcachedStore($memcache, 'foo'); $this->assertEquals([ 'foo' => 'fizz', From 11f246ed3b953b1eba5e2610cc64a5babb5b4352 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 25 Oct 2023 14:08:56 +0800 Subject: [PATCH 10/12] wip Signed-off-by: Mior Muhammad Zaki --- tests/Cache/CacheMemcachedStoreTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Cache/CacheMemcachedStoreTest.php b/tests/Cache/CacheMemcachedStoreTest.php index 867bda791b03..4b3d4954f7d9 100755 --- a/tests/Cache/CacheMemcachedStoreTest.php +++ b/tests/Cache/CacheMemcachedStoreTest.php @@ -9,6 +9,9 @@ use PHPUnit\Framework\TestCase; use stdClass; +/** + * @requires extension memcached + */ class CacheMemcachedStoreTest extends TestCase { protected function tearDown(): void From 5a0bd0500596742cb8d4363fde809111ec52490f Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 25 Oct 2023 14:11:13 +0800 Subject: [PATCH 11/12] wip Signed-off-by: Mior Muhammad Zaki --- tests/Cache/CacheMemcachedStoreTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Cache/CacheMemcachedStoreTest.php b/tests/Cache/CacheMemcachedStoreTest.php index 4b3d4954f7d9..ddf8f80f0f7c 100755 --- a/tests/Cache/CacheMemcachedStoreTest.php +++ b/tests/Cache/CacheMemcachedStoreTest.php @@ -23,7 +23,7 @@ protected function tearDown(): void public function testGetReturnsNullWhenNotFound() { - $memcache = $this->getMockBuilder(stdClass::class)->onlyMethods(['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'); @@ -32,7 +32,7 @@ public function testGetReturnsNullWhenNotFound() public function testMemcacheValueIsReturned() { - $memcache = $this->getMockBuilder(stdClass::class)->onlyMethods(['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); @@ -41,7 +41,7 @@ public function testMemcacheValueIsReturned() public function testMemcacheGetMultiValuesAreReturnedWithCorrectKeys() { - $memcache = $this->getMockBuilder(stdClass::class)->onlyMethods(['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([ From 0cd904057b58aea5558dc4efa3e57e377e209b7d Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 25 Oct 2023 06:11:25 +0000 Subject: [PATCH 12/12] Apply fixes from StyleCI --- tests/Cache/CacheMemcachedStoreTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Cache/CacheMemcachedStoreTest.php b/tests/Cache/CacheMemcachedStoreTest.php index ddf8f80f0f7c..1f9bf5ae8569 100755 --- a/tests/Cache/CacheMemcachedStoreTest.php +++ b/tests/Cache/CacheMemcachedStoreTest.php @@ -7,7 +7,6 @@ use Memcached; use Mockery as m; use PHPUnit\Framework\TestCase; -use stdClass; /** * @requires extension memcached