<?php declare(strict_types=1);

require_once __DIR__ . '/../../src/data/QrbCreditorInformation.php';

use PHPUnit\Framework\TestCase;

final class QrbCreditorInformationTest extends TestCase
{

    public function testValidateHappyDayBankIbanMissingCheckIban() : void
    {
        $creditorInformationContents = array();
        
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH9300762011623852957";
        $creditorInformationContents[QrbCreditorInformation::KEY_BESR_ID]       = "123456";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        QrbCreditorInformation::validate($creditorInformation);
        
        // dummy test for testdox (risky)
        $this->assertTrue(array_key_exists(QrbCreditorInformation::KEY_CRED_INFO, $creditorInformation));
    }
    
    public function testValidateHappyDayBankIbanWithCheckIban() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "false";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH9300762011623852957";
        $creditorInformationContents[QrbCreditorInformation::KEY_BESR_ID]       = "123456";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        QrbCreditorInformation::validate($creditorInformation);
        
        // dummy test for testdox (risky)
        $this->assertTrue(array_key_exists(QrbCreditorInformation::KEY_CRED_INFO, $creditorInformation));
    }

    public function testValidateHappyDayQrIban() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "true";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH4431999123000889012";
        $creditorInformationContents[QrbCreditorInformation::KEY_BESR_ID]       = "123456";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        QrbCreditorInformation::validate($creditorInformation);
        
        // dummy test for testdox (risky)
        $this->assertTrue(array_key_exists(QrbCreditorInformation::KEY_CRED_INFO, $creditorInformation));
    }
    
    public function testValidateMissingSurroundingKey() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "true";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH9300762011623852957";
        
        $this->expectException(QrbException::class);
        QrbCreditorInformation::validate($creditorInformationContents);
    }

    public function testValidateMissingName() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "true";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH9300762011623852957";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $this->expectException(QrbException::class);
        QrbCreditorInformation::validate($creditorInformation);
    }

    public function testValidateMissingAddrLine1() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "true";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH9300762011623852957";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $this->expectException(QrbException::class);
        QrbCreditorInformation::validate($creditorInformation);
    }

    public function testValidateMissingAddrLine2() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "true";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH9300762011623852957";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $this->expectException(QrbException::class);
        QrbCreditorInformation::validate($creditorInformation);
    }

    public function testValidateMissingCountry() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "true";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH9300762011623852957";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $this->expectException(QrbException::class);
        QrbCreditorInformation::validate($creditorInformation);
    }

    public function testValidateMissingIban() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "true";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $this->expectException(QrbException::class);
        QrbCreditorInformation::validate($creditorInformation);
    }

    public function testValidateIbanInvalidCountry() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "true";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "DE9300762011623852957";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $this->expectException(QrbException::class);
        QrbCreditorInformation::validate($creditorInformation);
    }

    public function testValidateIbanQrIdNotMumeric() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "true";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH93A0762011623852957";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $this->expectException(QrbException::class);
        QrbCreditorInformation::validate($creditorInformation);
    }

    public function testValidateNotQrIban() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "true";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH9300762011623852957";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $this->expectException(QrbException::class);
        QrbCreditorInformation::validate($creditorInformation);
    }

    public function testValidateBesrNotNumeric() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "false";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH9300762011623852957";
        $creditorInformationContents[QrbCreditorInformation::KEY_BESR_ID]       = "ABCD";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $this->expectException(QrbException::class);
        QrbCreditorInformation::validate($creditorInformation);
    }

    public function testValidateBesrTooShort() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "false";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH9300762011623852957";
        $creditorInformationContents[QrbCreditorInformation::KEY_BESR_ID]       = "12";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $this->expectException(QrbException::class);
        QrbCreditorInformation::validate($creditorInformation);
    }

    public function testValidateBesrTooLong() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "false";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH9300762011623852957";
        $creditorInformationContents[QrbCreditorInformation::KEY_BESR_ID]       = "123456789";
        
        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $this->expectException(QrbException::class);
        QrbCreditorInformation::validate($creditorInformation);
    }
    
    public function testGettersBankIbanWithBesrAndToString() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "false";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH9300762011623852957";
        $creditorInformationContents[QrbCreditorInformation::KEY_BESR_ID]       = "123456";

        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $credInfo = new QrbCreditorInformation($creditorInformation);
        $this->assertEquals("Hans Muster",      $credInfo->getName());
        $this->assertEquals("Musterstrasse 99", $credInfo->getAddressLine1());
        $this->assertEquals("9999 Musterstadt", $credInfo->getAddressLine2());
        $this->assertEquals("CH",               $credInfo->getCountry());
        $this->assertFalse(                     $credInfo->isQrIban());
        $this->assertEquals("123456",           $credInfo->getBesrId());
        
        echo $credInfo . "\n";
    }
    
    public function testGettersBankIbanWithoutBesr() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "false";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH9300762011623852957";

        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $credInfo = new QrbCreditorInformation($creditorInformation);
        $this->assertEquals("Hans Muster",           $credInfo->getName());
        $this->assertEquals("Musterstrasse 99",      $credInfo->getAddressLine1());
        $this->assertEquals("9999 Musterstadt",      $credInfo->getAddressLine2());
        $this->assertEquals("CH",                    $credInfo->getCountry());
        $this->assertFalse(                          $credInfo->isQrIban());
        $this->assertEquals("CH9300762011623852957", $credInfo->getIban());
        $this->assertNull(                           $credInfo->getBesrId());
    }
    
    public function testGettersQrIbanWithBesr() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "true";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH4431999123000889012";
        $creditorInformationContents[QrbCreditorInformation::KEY_BESR_ID]       = "123456";

        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $credInfo = new QrbCreditorInformation($creditorInformation);
        $this->assertEquals("Hans Muster",           $credInfo->getName());
        $this->assertEquals("Musterstrasse 99",      $credInfo->getAddressLine1());
        $this->assertEquals("9999 Musterstadt",      $credInfo->getAddressLine2());
        $this->assertEquals("CH",                    $credInfo->getCountry());
        $this->assertTrue(                           $credInfo->isQrIban());
        $this->assertEquals("CH4431999123000889012", $credInfo->getIban());
        $this->assertEquals("123456",                $credInfo->getBesrId());
    }
    
    public function testGettersQrIbanWithoutBesr() : void
    {
        $creditorInformationContents = array();
        $creditorInformationContents[QrbCreditorInformation::KEY_NAME]          = "Hans Muster";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_1]   = "Musterstrasse 99";
        $creditorInformationContents[QrbCreditorInformation::KEY_ADDR_LINE_2]   = "9999 Musterstadt";
        $creditorInformationContents[QrbCreditorInformation::KEY_COUNTRY]       = "CH";
        $creditorInformationContents[QrbCreditorInformation::KEY_CHECK_QR_IBAN] = "true";
        $creditorInformationContents[QrbCreditorInformation::KEY_IBAN]          = "CH4431999123000889012";

        $creditorInformation = array();
        $creditorInformation[QrbCreditorInformation::KEY_CRED_INFO] = $creditorInformationContents;
        $credInfo = new QrbCreditorInformation($creditorInformation);
        $this->assertEquals("Hans Muster",           $credInfo->getName());
        $this->assertEquals("Musterstrasse 99",      $credInfo->getAddressLine1());
        $this->assertEquals("9999 Musterstadt",      $credInfo->getAddressLine2());
        $this->assertEquals("CH",                    $credInfo->getCountry());
        $this->assertTrue(                           $credInfo->isQrIban());
        $this->assertEquals("CH4431999123000889012", $credInfo->getIban());
        $this->assertNull(                           $credInfo->getBesrId());
    }
}

?>
