<?php

namespace Omnipay\SecurePay\Message;

use Omnipay\Tests\TestCase;

class SecureXMLAuthorizeRequestTest extends TestCase
{
    public function setUp()
    {
        $this->request = new SecureXMLAuthorizeRequest($this->getHttpClient(), $this->getHttpRequest());

        $this->request->initialize(
            array(
                'merchantId' => 'ABC0030',
                'transactionPassword' => 'abc123',
                'amount' => '12.00',
                'transactionId' => '1234',
                'card' => array(
                    'number' => '4444333322221111',
                    'expiryMonth' => '10',
                    'expiryYear' => '2020',
                    'cvv' => '123',
                ),
            )
        );
    }

    public function testSendSuccess()
    {
        $this->setMockHttpResponse('SecureXMLAuthorizeRequestSuccess.txt');
        $response = $this->request->send();
        $data = $response->getData();

        $this->assertInstanceOf('Omnipay\\SecurePay\\Message\\SecureXMLResponse', $response);

        $this->assertTrue($response->isSuccessful());
        $this->assertFalse($response->isRedirect());
        $this->assertSame('009729', $response->getTransactionReference());
        $this->assertSame('00', $response->getCode());
        $this->assertSame('Approved', $response->getMessage());
        $this->assertSame('10', (string) $data->Payment->TxnList->Txn->txnType);
    }

    public function testSendFailure()
    {
        $this->setMockHttpResponse('SecureXMLAuthorizeRequestFailure.txt');
        $response = $this->request->send();

        $this->assertInstanceOf('Omnipay\\SecurePay\\Message\\SecureXMLResponse', $response);

        $this->assertFalse($response->isSuccessful());
        $this->assertFalse($response->isRedirect());
        $this->assertSame('510', $response->getCode());
        $this->assertSame('Unable To Connect To Server', $response->getMessage());
    }

    public function testInsufficientFundsFailure()
    {
        $this->setMockHttpResponse('SecureXMLAuthorizeRequestInsufficientFundsFailure.txt');
        $response = $this->request->send();

        $this->assertInstanceOf('Omnipay\\SecurePay\\Message\\SecureXMLResponse', $response);

        $this->assertFalse($response->isSuccessful());
        $this->assertFalse($response->isRedirect());
        $this->assertNull($response->getTransactionReference());
        $this->assertSame('51', $response->getCode());
        $this->assertSame('Insufficient Funds', $response->getMessage());
    }

    public function testInvalidMerchantFailure()
    {
        $this->setMockHttpResponse('SecureXMLAuthorizeRequestInvalidMerchantFailure.txt');
        $response = $this->request->send();

        $this->assertInstanceOf('Omnipay\\SecurePay\\Message\\SecureXMLResponse', $response);

        $this->assertFalse($response->isSuccessful());
        $this->assertFalse($response->isRedirect());
        $this->assertNull($response->getTransactionReference());
        $this->assertSame('504', $response->getCode());
        $this->assertSame('Invalid merchant ABC0030', $response->getMessage());
    }

    public function testInvalidMerchantIDFailure()
    {
        $this->setMockHttpResponse('SecureXMLAuthorizeRequestInvalidMerchantIDFailure.txt');
        $response = $this->request->send();

        $this->assertInstanceOf('Omnipay\\SecurePay\\Message\\SecureXMLResponse', $response);

        $this->assertFalse($response->isSuccessful());
        $this->assertFalse($response->isRedirect());
        $this->assertNull($response->getTransactionReference());
        $this->assertSame('504', $response->getCode());
        $this->assertSame('Invalid merchant ID', $response->getMessage());
    }

    public function testSetMessageId()
    {
        $this->request->setMessageId('message_identifier_here');
        $this->assertSame('message_identifier_here', $this->request->getMessageId());
    }

    public function testAutogeneratedMessageId()
    {
        $this->assertNotNull($this->request->getMessageId());
        $this->assertSame(30, strlen($this->request->getMessageId()));
    }
}
