Skip to content

Commit

Permalink
👌 IMPROVE: VIP_Integrations_Config_Test
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmoodak committed Jun 16, 2024
1 parent a83322d commit c5586c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
4 changes: 0 additions & 4 deletions integrations/vip-integrations-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ private function read_config_files(): array {
foreach ( $file_names as $file_name ) {
$config = $this->get_config_file_content( $file_name );

if ( null === $config ) {
continue;
}

if ( is_array( $config ) && isset( $config['type'] ) ) {
$type = $config['type'];
unset( $config['type'] );
Expand Down
31 changes: 28 additions & 3 deletions tests/integrations/test-vip-integrations-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

// phpcs:disable Squiz.Commenting.ClassComment.Missing, Squiz.Commenting.FunctionComment.Missing, Squiz.Commenting.FunctionComment.MissingParamComment

use Automattic\Test\Constant_Mocker;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionClass;
use WP_UnitTestCase;
Expand Down Expand Up @@ -39,6 +40,30 @@ public function tearDown(): void {
parent::tearDown();
}

public function test__constructor_doest_not_set_config_file_dir_property_if_constant_is_not_defined() {
$integrations_config = new VipIntegrationsConfig();
$config_file_dir_value = get_class_property_as_public( VipIntegrationsConfig::class, 'config_file_dir' )->getValue( $integrations_config );

$this->assertEquals( ABSPATH . 'config/integrations-config', $config_file_dir_value );
$this->assertIsString( $config_file_dir_value );
}

public function test__constructor_set_config_file_dir_property_if_constant_is_defined() {
$config_dir_path = '/custom-path';

if ( Constant_Mocker::defined( 'WPVIP_INTEGRATIONS_CONFIG_DIR' ) ) {
$config_dir_path = constant( 'WPVIP_INTEGRATIONS_CONFIG_DIR' );
} else {
Constant_Mocker::define( 'WPVIP_INTEGRATIONS_CONFIG_DIR', $config_dir_path );
}

$integrations_config = new VipIntegrationsConfig();
$config_file_dir_prop = get_class_property_as_public( VipIntegrationsConfig::class, 'config_file_dir' )->getValue( $integrations_config );

$this->assertIsString( $config_file_dir_prop );
$this->assertEquals( $config_dir_path, $config_file_dir_prop );
}

public function test__get_config_file_names_returns_an_empty_array_if_dir_does_not_exist(): void {
$obj = ( new ReflectionClass( VipIntegrationsConfig::class ) )->newInstanceWithoutConstructor();
$get_config_file_names = get_class_method_as_public( VipIntegrationsConfig::class, 'get_config_file_names' );
Expand All @@ -53,7 +78,7 @@ public function test__get_config_file_content_returns_null_if_file_is_not_readab
$this->assertNull( $get_config_file_content->invoke( $obj, 'file_name' ) );
}

public function test__configs_property_have_no_value_if_vip_config_is_not_of_type_array(): void {
public function test__read_config_files_does_not_set_configs_property_if_vip_config_is_not_of_type_array(): void {
$vip_configs = [ [ 'invalid-config' ] ];

$mock = $this->get_mock_with_configs( $vip_configs );
Expand All @@ -62,7 +87,7 @@ public function test__configs_property_have_no_value_if_vip_config_is_not_of_typ
$this->assertEquals( [], $configs );
}

public function test__configs_property_have_no_value_if_vip_config_does_not_have_type_property(): void {
public function test__read_config_files_does_not_set_configs_property_if_vip_config_does_not_have_type_property(): void {
$vip_configs = [
[ 'env' => [ 'status' => 'enabled' ] ],
];
Expand All @@ -73,7 +98,7 @@ public function test__configs_property_have_no_value_if_vip_config_does_not_have
$this->assertEquals( [], $configs );
}

public function test__configs_property_have_value(): void {
public function test__read_config_files_successfully_read_all_vip_configs(): void {
$vip_configs = [
[
'type' => 'type-one',
Expand Down

0 comments on commit c5586c6

Please sign in to comment.