From 875698d38f823cf6629046c08f8297e528a7232f Mon Sep 17 00:00:00 2001 From: Mehmood Ahmad <31419912+mehmoodak@users.noreply.github.com> Date: Tue, 5 Sep 2023 15:21:20 +0500 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20ADDS:=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- integrations/integrations.php | 1 - tests/integrations/test-integration.php | 19 ++++++++++++++++++ tests/integrations/test-integrations.php | 25 ++++++++++++++++++++++++ tests/integrations/test-parsely.php | 21 ++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/integrations/integrations.php b/integrations/integrations.php index 924cb748b2..8b17b5c909 100644 --- a/integrations/integrations.php +++ b/integrations/integrations.php @@ -115,7 +115,6 @@ public function activate( string $slug, array $options = [] ): void { if ( null === $integration ) { trigger_error( sprintf( 'VIP Integration with slug "%s" is not a registered integration.', esc_html( $slug ) ), E_USER_WARNING ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error - return; } $integration->activate( $options ); diff --git a/tests/integrations/test-integration.php b/tests/integrations/test-integration.php index eca587fb57..3b9c646d96 100644 --- a/tests/integrations/test-integration.php +++ b/tests/integrations/test-integration.php @@ -9,6 +9,7 @@ // phpcs:disable Squiz.Commenting.ClassComment.Missing, Squiz.Commenting.FunctionComment.Missing, Squiz.Commenting.FunctionComment.MissingParamComment +use PHPUnit\Framework\MockObject\MockObject; use WP_UnitTestCase; require_once __DIR__ . '/fake-integration.php'; @@ -36,6 +37,22 @@ public function test__activate_is_setting_up_the_plugins_config(): void { $this->assertEquals( [ 'config_test' ], $integration->get_config() ); } + public function test__calling_activate_when_the_integration_is_already_loaded_does_not_activate_the_integration_again(): void { + $this->expectException( 'PHPUnit_Framework_Error_Warning' ); + $this->expectExceptionMessage( 'Prevented activating of integration with slug "fake" because it is already available via customer code.' ); + /** + * Integration mock. + * + * @var MockObject|FakeIntegration + */ + $integration_mock = $this->getMockBuilder( FakeIntegration::class )->setConstructorArgs( [ 'fake' ] )->setMethods( [ 'is_integration_already_available_via_customer' ] )->getMock(); + $integration_mock->expects( $this->once() )->method( 'is_integration_already_available_via_customer' )->willReturn( true ); + + $integration_mock->activate(); + + $this->assertFalse( $integration_mock->is_active() ); + } + public function test__calling_activate_twice_on_same_integration_does_not_activate_the_plugin_second_time(): void { $this->expectException( 'PHPUnit_Framework_Error_Warning' ); $this->expectExceptionMessage( 'VIP Integration with slug "fake" is already activated.' ); @@ -44,6 +61,8 @@ public function test__calling_activate_twice_on_same_integration_does_not_activa $integration->activate(); $integration->activate(); + + $this->assertFalse( $integration->is_active() ); } public function test__is_active_returns_false_when_integration_is_not_active(): void { diff --git a/tests/integrations/test-integrations.php b/tests/integrations/test-integrations.php index 2da3e65f0e..a10082cea5 100644 --- a/tests/integrations/test-integrations.php +++ b/tests/integrations/test-integrations.php @@ -52,6 +52,31 @@ public function test__integrations_are_activating_based_on_given_vip_config(): v $this->assertEquals( [], $integration_3->get_config() ); } + public function test__configure_for_vip_is_getting_called_when_the_integration_is_activated_via_vip_config(): void { + $config_mock = $this->getMockBuilder( IntegrationVipConfig::class )->disableOriginalConstructor()->setMethods( [ 'is_active_via_vip' ] )->getMock(); + $config_mock->expects( $this->once() )->method( 'is_active_via_vip' )->willReturn( true ); + /** + * Integrations mock. + * + * @var MockObject|Integrations + */ + $integrations_mock = $this->getMockBuilder( Integrations::class )->setMethods( [ 'get_integration_vip_config' ] )->getMock(); + $integrations_mock->expects( $this->once() )->method( 'get_integration_vip_config' )->willReturn( $config_mock ); + /** + * Integration mock. + * + * @var MockObject|FakeIntegration + */ + $integration_mock = $this->getMockBuilder( FakeIntegration::class )->setConstructorArgs( [ 'fake' ] )->setMethods( [ 'configure_for_vip' ] )->getMock(); + $integration_mock->expects( $this->once() )->method( 'configure_for_vip' ); + + $integrations_mock->register( $integration_mock ); + $integrations_mock->activate_platform_integrations(); + + $this->assertTrue( $integration_mock->is_active() ); + $this->assertEquals( [], $integration_mock->get_config() ); + } + public function test__get_integration_vip_config_returns_instance_of_IntegrationVipConfig(): void { $integrations = new Integrations(); diff --git a/tests/integrations/test-parsely.php b/tests/integrations/test-parsely.php index a0f60f564b..8c2a340b85 100644 --- a/tests/integrations/test-parsely.php +++ b/tests/integrations/test-parsely.php @@ -18,6 +18,27 @@ class VIP_Parsely_Integration_Test extends WP_UnitTestCase { private string $slug = 'parsely'; + public function test_is_integration_already_available_via_customer_returns_expected_value(): void { + $parsely_integration = new ParselyIntegration( $this->slug ); + + if ( is_parsely_disabled() ) { + $this->assertFalse( $parsely_integration->is_integration_already_available_via_customer() ); + return; + } + + // Indicates enablement via filter or option. + $this->assertTrue( $parsely_integration->is_integration_already_available_via_customer() ); + } + + public function test__configure_for_vip_is_adding_necessary_hooks_need_for_configuration_on_vip_platform(): void { + $parsely_integration = new ParselyIntegration( $this->slug ); + + $parsely_integration->configure_for_vip(); + + $this->assertEquals( 10, has_filter( 'wp_parsely_credentials', [ $parsely_integration, 'wp_parsely_credentials_callback' ] ) ); + $this->assertEquals( 10, has_filter( 'wp_parsely_managed_options', [ $parsely_integration, 'wp_parsely_managed_options_callback' ] ) ); + } + public function test__load_call_is_defining_the_enabled_constant_if_plugin_is_not_loaded_already(): void { $parsely_integration = new ParselyIntegration( $this->slug );