<?php
namespace Redefine\Tests\RestApi\Consumer;

use Redefine\RestApi\Consumer;

class RequestTest extends \PHPUnit_Framework_TestCase
{
    public function testInstantiation()
    {
        $request = new Consumer\Request();

        $this->assertInstanceOf('\Psr\Http\Message\RequestInterface', $request);
    }

    public function testAuthentication()
    {
        $request = new Consumer\Request();
        $authentication = null;
        $actual = $request->withAuthentication($authentication);

        $this->assertEquals($request, $actual);

        $key = 'foo';
        $value = 'bar';
        $authentication = $this->getMock('\Redefine\RestApi\Consumer\HeaderInterface');
        $authentication->method('getName')
            ->willReturn($key);
        $authentication->method('getValue')
            ->willReturn($value);
        $request = $request->withAuthentication($authentication);

        $this->assertArrayHasKey($key, $request->getHeaders());
        $this->assertEquals($authentication->getValue(), $request->getHeaderLine($key));
    }

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testAuthenticationError()
    {
        $request = new Consumer\Request();
        $authentication = new \stdClass();
        $request->withAuthentication($authentication);
    }


    public function testBasicAuth()
    {
        $request = new Consumer\Request();

        $username = 'user';
        $password = 'password';
        $authentication = new Consumer\Authentication\BasicAuth($username, $password);
        $key = $authentication->getName();

        $request = $request->withBasicAuth($username, $password);
        $this->assertArrayHasKey($key, $request->getHeaders());
        $this->assertEquals($authentication->getValue(), $request->getHeaderLine($key));
    }

    public function testRequestFormat()
    {
        $request = new Consumer\Request();
        $key = 'Content-Type';

        $request = $request->withRequestFormat('json');
        $this->assertArrayHasKey($key, $request->getHeaders());
        $this->assertEquals('application/json', $request->getHeaderLine($key));

        $request = $request->withRequestFormat('application/json');
        $this->assertArrayHasKey($key, $request->getHeaders());
        $this->assertEquals('application/json', $request->getHeaderLine($key));

        $request = $request->withRequestFormat('xml');
        $this->assertArrayHasKey($key, $request->getHeaders());
        $this->assertEquals('application/xml', $request->getHeaderLine($key));

        $request = $request->withRequestFormat('application/xml');
        $this->assertArrayHasKey($key, $request->getHeaders());
        $this->assertEquals('application/xml', $request->getHeaderLine($key));

        $request = $request->withRequestFormat('text/xml');
        $this->assertArrayHasKey($key, $request->getHeaders());
        $this->assertEquals('text/xml', $request->getHeaderLine($key));
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function testRequestFormatInvalidFormat()
    {
        $request = new Consumer\Request();

        $request = $request->withRequestFormat('something');
    }

    public function testResponseFormat()
    {
        $request = new Consumer\Request();
        $key = 'Accept';

        $request = $request->withResponseFormat('json');
        $this->assertArrayHasKey($key, $request->getHeaders());
        $this->assertEquals('application/json', $request->getHeaderLine($key));

        $request = $request->withResponseFormat('application/json');
        $this->assertArrayHasKey($key, $request->getHeaders());
        $this->assertEquals('application/json', $request->getHeaderLine($key));

        $request = $request->withResponseFormat('xml');
        $this->assertArrayHasKey($key, $request->getHeaders());
        $this->assertEquals('application/xml', $request->getHeaderLine($key));

        $request = $request->withResponseFormat('application/xml');
        $this->assertArrayHasKey($key, $request->getHeaders());
        $this->assertEquals('application/xml', $request->getHeaderLine($key));

        $request = $request->withResponseFormat('text/xml');
        $this->assertArrayHasKey($key, $request->getHeaders());
        $this->assertEquals('text/xml', $request->getHeaderLine($key));
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function testResponseFormatInvalidFormat()
    {
        $request = new Consumer\Request();

        $request = $request->withResponseFormat('something');
    }
}
