<?php
/**
 * Test function vipgoci_ap_nonfunctional_changes().
 *
 * @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 ApNonfunctionalChangesTest extends TestCase {
	var $options_git = array(
		'repo-owner'			=> null,
		'repo-name'			=> null,
		'git-path'			=> null,
		'github-repo-url'		=> null,
	);

	/**
	 * Variable for options.
	 *
	 * @var $options
	 */
	private array $options = array();

	var $options_auto_approvals_nonfunc = array(
		'commit-test-ap-nonfunctionalchanges-1b' => null,
		'commit-test-ap-nonfunctionalchanges-2'  => null,
	);

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

		vipgoci_unittests_get_config_values(
			'git',
			$this->options_git
		);

		vipgoci_unittests_get_config_values(
			'auto-approvals',
			$this->options_auto_approvals_nonfunc
		);

		$this->options = array_merge(
			$this->options_git,
			$this->options_auto_approvals_nonfunc
		);

		$this->options[ 'github-token' ] =
			vipgoci_unittests_get_config_value(
				'git-secrets',
				'github-token',
				true // Fetch from secrets file
			);

		if ( empty( $this->options['github-token'] ) ) {
			$this->options['github-token'] = '';
		}

		$this->options['token'] =
			$this->options['github-token'];

		unset( $this->options['github-token'] );

		$this->options['local-git-repo'] = false;

		$this->options['autoapprove'] = true;

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

		$this->options['skip-draft-prs'] = false;

		$this->options['autoapprove-php-nonfunctional-changes-file-extensions'] = array( 'php' );
	}

	protected function tearDown(): void {
		if ( false !== $this->options['local-git-repo'] ) {
			vipgoci_unittests_remove_git_repo(
				$this->options['local-git-repo']
			);
		}

		unset( $this->options );
		unset( $this->options_git );
		unset( $this->options_auto_approvals_nonfunc );
	}

	/**
	 * Test pull request in which multiple files are altered but
	 * only two are auto-approvable.
	 *
	 * @covers ::vipgoci_ap_nonfunctional_changes
	 */
	public function testNonFunctionalChanges1() {
		$auto_approved_files_arr = array();

		$this->options['commit'] =
			$this->options['commit-test-ap-nonfunctionalchanges-1b'];

		$this->options['local-git-repo'] =
			vipgoci_unittests_setup_git_repo(
				$this->options
			);

		vipgoci_unittests_output_suppress();

		vipgoci_ap_nonfunctional_changes(
			$this->options,
			$auto_approved_files_arr
		);

		vipgoci_unittests_output_unsuppress();

		$this->assertSame(
			array(
				'file1.php' => 'autoapprove-nonfunctional-changes',
				'file2.php' => 'autoapprove-nonfunctional-changes',

				/*
				 * - file3.php is not approved, has changed functionally
				 * - file100.txt is not approvable by this function
				 * - file101.png, same.
				 */
			),
			$auto_approved_files_arr
		);
	}

	/**
	 * Test pull request in which on file is removed
	 * and another one is altered.
	 *
	 * @covers ::vipgoci_ap_nonfunctional_changes
	 */
	public function testNonFunctionalChanges2() {
		$auto_approved_files_arr = array();

		$this->options['commit'] =
			$this->options['commit-test-ap-nonfunctionalchanges-2'];

		$this->options['local-git-repo'] =
			vipgoci_unittests_setup_git_repo(
				$this->options
			);

		vipgoci_unittests_output_suppress();

		vipgoci_ap_nonfunctional_changes(
			$this->options,
			$auto_approved_files_arr
		);

		vipgoci_unittests_output_unsuppress();

		$this->assertSame(
			array(
				'file1.php' => 'autoapprove-nonfunctional-changes',

				/*
				 * - file2.php is not approved, has been removed.
				 */
			),
			$auto_approved_files_arr
		);
	}
}
