<?php declare(strict_types=1);

require_once __DIR__ . '/../../src/process/QrbProcessStatus.php';

require_once __DIR__ . '/../../src/storage/QrbStorage.php';
require_once __DIR__ . '/../../src/storage/QrbStorageFactory.php';

use PHPUnit\Framework\TestCase;

final class QrbProcessStatusTest extends TestCase
{
    private QrbStorage $qrbStorage;
    private string     $expectedNewFile;
    
    protected function setUp() : void
    {
        $this->qrbStorage = new QrbFileStorage();
        
        $this->expectedNewFile = QrbProcessStatus::getStatusFullFileName();
    }
    
    protected function tearDown() : void
    {
        $this->qrbStorage->deleteData();
        error_log("STATUS-TEST: Data deleted");
    }

    public function testGetStatusPath() : void
    {
        $this->assertEquals(QrbFiles::fixPath(__DIR__ . "/../../data/"), 
                            QrbProcessStatus::getStatusPath());
    }
    
    public function testGetStatusFullFileName() : void
    {
        $this->assertEquals(QrbFiles::fixPath(__DIR__ . "/../../data/qr-bills.status"), 
                            QrbProcessStatus::getStatusFullFileName());
    }
    
    public function testConstructor() : void
    {
        // create "old" status file
        mkdir(QrbProcessStatus::getStatusPath());
        $statusFile = fopen($this->expectedNewFile, "a");
        fwrite($statusFile, "testConstructor()");
        fclose($statusFile);
        $this->assertTrue(file_exists($this->expectedNewFile));
    
        $status = new QrbProcessStatus($this->qrbStorage);
        $this->assertTrue(file_exists($this->expectedNewFile));

        // test if "old" status file has been deleted and the new file has length 0
        clearstatcache();
        $this->assertEquals(0, filesize($this->expectedNewFile));

        $status = NULL;    // calls destructor
    }

    public function testDestructor() : void
    {
        $status = new QrbProcessStatus($this->qrbStorage);
        $status->write("testDestructor()");

        $status = NULL;    // calls destructor
        
        // test if status file contains EOF string
        clearstatcache();
        $statusFile = fopen($this->expectedNewFile, "r");
        $contents = fread($statusFile, filesize($this->expectedNewFile));
        $pos = stripos($contents, QrbProcessStatus::STATUS_EOF_MARKER);
        $this->assertNotFalse($pos);
        $this->assertTrue($pos > 0, "pos is {$pos}");
    }

    public function testWrite() : void
    {
        $status = new QrbProcessStatus($this->qrbStorage);
        $status->write("testWrite()");    // write 11 bytes
        // write() will append "<br />\n" to the message, 
        // so additional 7 bytes => total 18 bytes (*NIX writes 1 byte, DOS writes 2 bytes for '\n')

        // test if status file has at least 18 bytes
        clearstatcache();
        $size = filesize($this->expectedNewFile);
        $this->assertTrue($size >= 18, "size is {$size}");

        $status = NULL;    // calls destructor
    }
}

?>
