<?php
/**
 * Test function vipgoci_repo_meta_api_data_match().
 *
 * @package Automattic/vip-go-ci
 */

declare(strict_types=1);

namespace Vipgoci\Tests\Integration;

use PHPUnit\Framework\TestCase;

/**
 * Class that implements the testing.
 *
 * @runTestsInSeparateProcesses
 * @preserveGlobalState disabled
 */
final class RepoMetaApiRepoMetaApiDataMatchTest extends TestCase {
	/**
	 * Options array.
	 *
	 * @var $options
	 */
	private array $options = array();

	/**
	 * Options array for meta API.
	 *
	 * @var $options_meta_api_secrets
	 */
	private array $options_meta_api_secrets = array(
		'repo-meta-api-base-url'	=> null,
		'repo-meta-api-user-id'		=> null,
		'repo-meta-api-access-token'	=> null,

		'repo-name'			=> null,
		'repo-owner'			=> null,

		'support-level'			=> null,
		'support-level-field-name'	=> null,
	);

	protected function setUp(): void {
		require_once __DIR__ . '/IncludesForTests.php';

		vipgoci_unittests_get_config_values(
			'repo-meta-api-secrets',
			$this->options_meta_api_secrets,
			true
		);

		$this->options = $this->options_meta_api_secrets;

		$this->options['data_match0'] = array(
		);

		$this->options['data_match1'] = array(
			2 => array(
				'__invalid_field' => array(
					'__somethinginvalid'
				),
			),

			3 => array(
				'invalid_field_761a' => array(
					'invalid_value',
				)
			)
		);

		$this->options['data_match2'] = array(
			2 => array(
				$this->options['support-level-field-name'] => array(
					$this->options['support-level']
				),
			),

			3 => array(
				'invalid_field_761a' => array(
					'invalid_value',
				)
			)
		);

		$this->options['branches-ignore'] = array();

		// This can be an empty string, set to empty if null.
		if ( null === $this->options['repo-meta-api-user-id'] ) {
			$this->options['repo-meta-api-user-id'] = '';
		}

		// This can be an empty string, set to empty if null.
		if ( null === $this->options['repo-meta-api-access-token'] ) {
			$this->options['repo-meta-api-access-token'] = '';
		}
	}

	protected function tearDown(): void {
		unset( $this->options );
		unset( $this->options_meta_api_secrets );
	}

	/**
	 * @covers ::vipgoci_repo_meta_api_data_match
	 */
	public function test_repo_meta_api_data_match1() {
		$options_test = vipgoci_unittests_options_test(
			$this->options,
			array( 'repo-meta-api-user-id', 'repo-meta-api-access-token' ),
			$this
		);

		if ( -1 === $options_test ) {
			return;
		}

		$option_key_no = null;

		vipgoci_unittests_output_suppress();

		$repo_meta_api_data_match = vipgoci_repo_meta_api_data_match(
			$this->options,
			'',
			$option_key_no
		);

		vipgoci_unittests_output_unsuppress();

		$this->assertSame(
			false,
			$repo_meta_api_data_match
		);

		$this->assertSame(
			null,
			$option_key_no
		);
	}

	/**
	 * @covers ::vipgoci_repo_meta_api_data_match
	 */
	public function test_repo_meta_api_data_match2() {
		$options_test = vipgoci_unittests_options_test(
			$this->options,
			array( 'repo-meta-api-user-id', 'repo-meta-api-access-token' ),
			$this
		);

		if ( -1 === $options_test ) {
			return;
		}

		$option_key_no = null;

		vipgoci_unittests_output_suppress();

		$repo_meta_api_data_match = vipgoci_repo_meta_api_data_match(
			$this->options,
			'data_match0',
			$option_key_no
		);

		vipgoci_unittests_output_unsuppress();

		$this->assertSame(
			false,
			$repo_meta_api_data_match
		);

		$this->assertSame(
			null,
			$option_key_no
		);
	}

	/**
	 * @covers ::vipgoci_repo_meta_api_data_match
	 */
	public function test_repo_meta_api_data_match3() {
		$options_test = vipgoci_unittests_options_test(
			$this->options,
			array( 'repo-meta-api-user-id', 'repo-meta-api-access-token' ),
			$this
		);

		if ( -1 === $options_test ) {
			return;
		}

		$option_key_no = null;

		vipgoci_unittests_output_suppress();

		$repo_meta_api_data_match = vipgoci_repo_meta_api_data_match(
			$this->options,
			'data_match1',
			$option_key_no
		);

		vipgoci_unittests_output_unsuppress();

		$this->assertSame(
			false,
			$repo_meta_api_data_match
		);
	
		$this->assertSame(
			null,
			$option_key_no
		);
	}

	/**
	 * @covers ::vipgoci_repo_meta_api_data_match
	 */
	public function test_repo_meta_api_data_match4() {
		$options_test = vipgoci_unittests_options_test(
			$this->options,
			array( 'repo-meta-api-user-id', 'repo-meta-api-access-token' ),
			$this
		);

		if ( -1 === $options_test ) {
			return;
		}

		$option_key_no = null;

		vipgoci_unittests_output_suppress();

		$repo_meta_api_data_match = vipgoci_repo_meta_api_data_match(
			$this->options,
			'data_match2',
			$option_key_no
		);

		vipgoci_unittests_output_unsuppress();

		$this->assertSame(
			true,
			$repo_meta_api_data_match
		);

		$this->assertSame(
			2,
			$option_key_no
		);
	}
}
