<?php declare(strict_types=1);
require_once __DIR__ . '/../../../src/storage/file/QrbProtocolFile.php';

use PHPUnit\Framework\TestCase;

final class QrbProtocolFileTest extends TestCase
{
    private const TEST_OUTPUT_PATH = "../../../ProtocolFileTest";
    private const PROTOCOL_FILE_NAME = "ProtocolFileTest.csv";
    
    private $path;
    private $expectedFile;

    protected function setUp() : void
    {
        $this->path = self::TEST_OUTPUT_PATH;
        $this->expectedFile = $this->path . "/" . self::PROTOCOL_FILE_NAME;
        mkdir($this->path, 0777, true);
    }
    
    protected function tearDown() : void
    {
        rmdir($this->path);
    }
    
    public function testWriteEmptyProtocol()
    {
        $protocol = new QrbProtocolFile();
        $this->assertFalse($protocol->write($this->expectedFile, array()));
        $this->assertFalse(file_exists($this->expectedFile));
    }
    
    public function testWriteNonEmptyProtocol()
    {
        $protocol = new QrbProtocolFile();
        $entry = new QrbProtocolEntry($this->createRecipient(1));
        $entries = [ 1 => $entry ];
        $this->assertTrue($protocol->write($this->expectedFile, $entries));
        $this->assertTrue(file_exists($this->expectedFile));
        unlink($this->expectedFile);
    }

    private function createRecipient(int $index) : QrbRecipient
    {
        $line = array();
        array_push($line, "en");
        $headerNames = array();
        $headerNames[QrbRecipient::HDR_LANGUAGE] = 0;
        $supportedLanguages = array();
        array_push($supportedLanguages, "en");
        return new QrbRecipient($index, $line, $headerNames, false, false, false, false, $supportedLanguages);
    }
}

?>
