From 75734bec73a3c77fb4198aacea3cc617fa82e788 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 18:54:44 +0300 Subject: [PATCH] fix unit tests --- .../addPluginsToResult.php | 3 +- tests/Unit/inc/functions/rocketCheckKey.php | 37 ++++--------------- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php b/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php index 862e66df4b..a16f8e352a 100644 --- a/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php +++ b/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php @@ -23,8 +23,7 @@ protected function setUp(): void { [ 'plugin_file' => 'wp-rocket/wp-rocket.php', 'api_url' => 'https://wp-rocket.me', - ], - Mockery::mock( InformationAPIClient::class ) + ] ); } diff --git a/tests/Unit/inc/functions/rocketCheckKey.php b/tests/Unit/inc/functions/rocketCheckKey.php index bcb550038f..efc7a39dac 100644 --- a/tests/Unit/inc/functions/rocketCheckKey.php +++ b/tests/Unit/inc/functions/rocketCheckKey.php @@ -45,13 +45,13 @@ public function testShouldReturnArrayWhenSuccessfulValidation() { ->andReturn( 'wp_rocket_settings' ); Functions\expect( 'rocket_valid_key' )->once()->andReturn( false ); Functions\expect( 'rocket_delete_licence_data_file' )->never(); - Functions\expect( 'wp_remote_request' ) + Functions\expect( 'wp_remote_get' ) ->once() - ->with( 'https://wp-rocket.me/valid_key.php', [ 'method' => 'GET', 'timeout' => 30 ] ) + ->with( 'https://wp-rocket.me/valid_key.php', [ 'timeout' => 30 ] ) ->andReturn( [] ); - Functions\expect( 'is_wp_error' )->twice()->andReturn( false ); + Functions\expect( 'is_wp_error' )->once()->andReturn( false ); Functions\expect( 'wp_remote_retrieve_body' ) - ->twice() + ->once() ->with( [] ) ->andReturn( '{"success": true, "data":{"consumer_key":"ABCDEF","consumer_email":"example@example.org","secret_key":"secret"}}' ); Functions\expect( 'get_rocket_option' )->once()->with( 'license' )->andReturn( true ); @@ -80,11 +80,6 @@ public function testShouldReturnArrayWhenSuccessfulValidation() { 'secret_key' => 'secret', ]; - Functions\expect( 'get_transient' ) - ->once() - ->with( 'wp_rocket_license_validation_timeout_active' ) - ->andReturn( false ); - $this->assertSame( $expected, rocket_check_key() ); } @@ -94,7 +89,7 @@ public function testShouldReturnFalseWhenIsWPError() { ->with( 'WP_ROCKET_WEB_VALID' ) ->andReturn( 'https://wp-rocket.me/valid_key.php' ); Functions\when( 'rocket_valid_key' )->justReturn( false ); - Functions\when( 'wp_remote_request' )->alias( function() { + Functions\when( 'wp_remote_get' )->alias( function() { $wp_error = \Mockery::mock( \WP_Error::class )->makePartial(); $wp_error->shouldReceive( 'get_error_messages' ) ->withNoArgs() @@ -109,11 +104,6 @@ public function testShouldReturnFalseWhenIsWPError() { Functions\expect('update_option') ->never(); - Functions\expect( 'get_transient' ) - ->once() - ->with( 'wp_rocket_license_validation_timeout_active' ) - ->andReturn( false ); - $this->assertFalse( rocket_check_key() ); } @@ -123,7 +113,7 @@ public function testShouldReturnFalseWhenEmptyResponse() { ->with( 'WP_ROCKET_WEB_VALID' ) ->andReturn( 'https://wp-rocket.me/valid_key.php' ); Functions\when( 'rocket_valid_key' )->justReturn( false ); - Functions\when( 'wp_remote_request' )->justReturn( [] ); + Functions\when( 'wp_remote_get' )->justReturn( [] ); Functions\when( 'is_wp_error' )->justReturn( false ); Functions\when( 'wp_remote_retrieve_body' )->justReturn( '' ); Functions\when( 'set_transient' )->justReturn( true ); @@ -131,11 +121,6 @@ public function testShouldReturnFalseWhenEmptyResponse() { Functions\expect('update_option') ->never(); - Functions\expect( 'get_transient' ) - ->once() - ->with( 'wp_rocket_license_validation_timeout_active' ) - ->andReturn( false ); - $this->assertFalse( rocket_check_key() ); } @@ -149,7 +134,7 @@ public function testShouldReturnArrayWhenSuccessFalse() { ->with( 'WP_ROCKET_SLUG' ) ->andReturn( 'wp_rocket_settings' ); Functions\when( 'rocket_valid_key' )->justReturn( false ); - Functions\when( 'wp_remote_request' )->justReturn( [] ); + Functions\when( 'wp_remote_get' )->justReturn( [] ); Functions\when( 'is_wp_error' )->justReturn( false ); Functions\when( 'wp_remote_retrieve_body' )->justReturn( '{"success": false, "data":{"consumer_key":"ABCDEF","consumer_email":"example@example.org","reason":"BAD_KEY"}}' ); Functions\when( 'set_transient' )->justReturn( true ); @@ -157,20 +142,12 @@ public function testShouldReturnArrayWhenSuccessFalse() { Functions\expect('update_option') ->never(); - Functions\expect( 'get_transient' ) - ->once() - ->with( 'wp_rocket_license_validation_timeout_active' ) - ->andReturn( false ); - $expected = [ 'consumer_key' => 'ABCDEF', 'consumer_email' => 'example@example.org', 'secret_key' => '', ]; - Functions\expect( 'delete_transient' )->with( 'wp_rocket_license_validation_timeout' )->andReturn(null); - Functions\expect( 'delete_transient' )->with( 'wp_rocket_license_validation_timeout_active' )->andReturn(null); - $this->assertSame( $expected, rocket_check_key() ); } }