<?php
/*
 * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

class RealURL2_URLTest extends sly_StatelessTest {
	public function testGetters() {
		$date = date('r');
		$url  = new _WV24_URL(array('created' => $date, 'namespace' => 'mynamespace', 'ident' => 'foo', 'article_id' => 4, 'clang' => 5, 'path' => '|1|2|', 'url' => 'http://hello.world/'));

		$this->assertEquals('mynamespace',         $url->getNamespace());
		$this->assertEquals('foo',                 $url->getIdent());
		$this->assertEquals(strtotime($date),      $url->getCreated());
		$this->assertEquals(4,                     $url->getArticleID());
		$this->assertEquals(5,                     $url->getClang());
		$this->assertEquals('|1|2|',               $url->getPath());
		$this->assertEquals(array(),               $url->getParams());
		$this->assertEquals('http://hello.world/', $url->getRawURL());
		$this->assertEquals('http://hello.world/', $url->getURL(array(), '', true));
		$this->assertEquals('http://hello.world/', $url->getURL('', '', true));
	}

	/**
	 * @dataProvider  paramDecodingProvider
	 */
	public function testParamDecoding($params, $expected) {
		$prototype = array(
			'created'    => date('r'),
			'namespace'  => 'mynamespace',
			'ident'      => 'foo',
			'article_id' => 4,
			'clang'      => 5,
			'path'       => '|1|2|',
			'url'        => 'http://hello.world/',
			'params'     => $params
		);

		$url = new _WV24_URL($prototype);
		$this->assertEquals($expected, $url->getParams());
	}

	public function paramDecodingProvider() {
		return array(
			array('',                      array()),
			array(null,                    array()),
			array(array(),                 array()),
			array(array(1),                array(1)),
			array(array('key' => 'value'), array('key' => 'value')),
			array('[]',                    array()),
			array('[1,2]',                 array(1, 2)),
			array('{"foo":"bar"}',         array('foo' => 'bar'))
		);
	}
}
