<?php

use PHPOnCouch\Exceptions;
use PHPOnCouch\Adapter\CouchHttpAdapterCurl;

require_once join(DIRECTORY_SEPARATOR, [dirname(__DIR__), '_config', 'config.php']);

/**
 * Generated by PHPUnit_SkeletonGenerator on 2016-11-01 at 15:39:08.
 * @backupGlobals disabled
 * @backupStaticAttributes disabled
 */
class AbstractCouchHttpAdapterTest extends PHPUnit_Framework_TestCase
{

	protected $adapter;

	/**
	 * Sets up the fixture, for example, opens a network connection.
	 * This method is called before a test is executed.
	 */
	protected function setUp()
	{
		$this->adapter = new CouchHttpAdapterCurl("localhost", []);
	}

	/**
	 * Tears down the fixture, for example, closes a network connection.
	 * This method is called after a test is executed.
	 */
	protected function tearDown()
	{
		$this->adapter = null;
	}

	/**
	 * @covers PHPOnCouch\Adapter\AbstractCouchHttpAdapter::setDsn()
	 * @covers PHPOnCouch\Adapter\AbstractCouchHttpAdapter::getDsn()
	 */
	public function testDsnAccessors()
	{
		$adapter = $this->adapter;
		$this->assertEquals('localhost', $adapter->getDsn());
		$newDsn = '127.0.0.1';
		$adapter->setDsn($newDsn);
		$this->assertEquals($newDsn, $adapter->getDsn());
	}

	/**
	 * @covers PHPOnCouch\Adapter\AbstractCouchHttpAdapter::setSessionCookie()
	 * @covers PHPOnCouch\Adapter\AbstractCouchHttpAdapter::getSessionCookie()
	 * @covers PHPOnCouch\Adapter\AbstractCouchHttpAdapter::hasSessionCookie()
	 */
	public function testSessionAccessors()
	{
		$adapter = $this->adapter;
		$adapter->setSessionCookie("");
		$this->assertEmpty($adapter->getSessionCookie());
		$this->assertFalse($adapter->hasSessionCookie());

		$cookie = "thisisnotreallyacookie";
		$adapter->setSessionCookie($cookie);
		$this->assertEquals($cookie, $adapter->getSessionCookie());
		$this->assertTrue($adapter->hasSessionCookie());
	}

	/**
	 * @covers PHPOnCouch\Adapter\AbstractCouchHttpAdapter::getOptions()
	 * @covers PHPOnCouch\Adapter\AbstractCouchHttpAdapter::setOptions()
	 */
	public function testOptionAccessor()
	{
		$adapter = $this->adapter;

		$val = null;
		$adapter->setOptions($val);
		$this->assertEquals($val, $adapter->getOptions());
	}

	/**
	 * @covers PHPOnCouch\Adapter\AbstractCouchHttpAdapter::dsnPart()
	 */
	public function testDsnParser()
	{
		$opt = ['optionTest'];
		$dsn = 'http://john:pass@localhost.com';
		$adapter = new CouchHttpAdapterCurl($dsn . "/", $opt);

		$dsnPart = new \ReflectionMethod($adapter, 'dsnPart');
		$dsnPart->setAccessible(true);

		//Dsn test
		$fullDsn = $dsnPart->invokeArgs($adapter, []);
		$this->assertEquals($dsn, $fullDsn);

		//Host
		$host = $dsnPart->invokeArgs($adapter, ['host']);
		$this->assertEquals($host, 'localhost.com');

		//User
		$user = $dsnPart->invokeArgs($adapter, ['user']);
		$this->assertEquals($user, 'john');
	}

	/**
	 * @covers PHPOnCouch\Adapter\AbstractCouchHttpAdapter::__construct()
	 */
	public function testConstruct()
	{
		$opt = ['optionTest'];
		$dsn = 'http://john@localhost.com';
		$adapter = new CouchHttpAdapterCurl($dsn . "/", $opt);

		//Should set options
		$this->assertEquals($adapter->getOptions(), $opt);
		$this->assertEquals($adapter->getDsn(), $dsn);

		$dsnPart = new \ReflectionMethod($adapter, 'dsnPart');
		$dsnPart->setAccessible(true);
		$port = $dsnPart->invokeArgs($adapter, ['port']);
		$this->assertEquals($port, 80);
	}

}
