<?php

declare(strict_types=1);
namespace Tests\git2;
use git2\git;
use git2\git_buf;

final class EmailTest extends GitTestCase
{

	public function testCreateFromCommit(): void
	{
		$dir = $this->mkdir('create_from_commit');
		git::repository_init($repo, $dir, false);
		git::repository_index($index, $repo);
		git::index_write_tree($treeId, $index);
		git::tree_lookup($tree, $repo, $treeId);
		git::signature_new($sig, 'John Doe', 'john.doe@example.com', 1640263000, 60);
		git::commit_create_v($id, $repo, 'HEAD', $sig, $sig, null, 'Hello world!', $tree);
		git::commit_lookup($commit, $repo, $id);
		$this->assertOK(git::email_create_from_commit($buf, $commit, null));
		$this->assertInstanceOf(git_buf::class, $buf);
		$this->assertStringEqualsFile(__DIR__ . '/fixtures/email.from_commit.mbox', (string) $buf);
	}

	public function testCreateFromDiff(): void
	{
		$dir = $this->mkdir('crete_from_diff');
		touch($dir . '/foo');
		git::repository_init($repo, $dir, false);
		git::repository_index($index, $repo);
		git::index_add_bypath($index, 'foo');
		git::index_write_tree($treeId, $index);
		git::tree_lookup($tree, $repo, $treeId);
		git::signature_new($sig, 'John Doe', 'john.doe@example.com', 1640263000, 60);
		git::commit_create_v($id, $repo, 'HEAD', $sig, $sig, null, 'Hello world!', $tree);
		git::diff_tree_to_index($diff, $repo, $tree, null);
		git::commit_lookup($commit, $repo, $id);
		$this->assertOK(git::email_create_from_diff(
			$buf,
			$diff,
			0,
			1,
			$id,
			git::commit_summary($commit),
			'body',
			$sig,
			null,
		));
		$this->assertStringEqualsFile(__DIR__ . '/fixtures/email.from_diff.mbox', (string) $buf);
	}

}