<?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_RuntimeCacheTest extends RealURL2_BaseTest {
	public function testSettings() {
		$cache = new _WV24_RuntimeCache();
		$cache->prewarmSettings(5);
		$cache->prewarmSettings(8);

		$this->assertEquals('//cli',    $cache->getSetting('default_fragment', 5));
		$this->assertEquals('//cli/en', $cache->getSetting('default_fragment', 8));
		$this->assertEquals(true,       $cache->getSetting('cleanup', 5));
		$this->assertEquals(false,      $cache->getSetting('cleanup', 8));
		$this->assertEquals(true,       $cache->getSetting('lowercase', 5));
		$this->assertEquals(false,      $cache->getSetting('lowercase', 8));

		$this->assertEquals('//cli',    $cache->getDefaultFragment(5));
		$this->assertEquals('//cli/en', $cache->getDefaultFragment(8));
	}

	public function testAutoPrewarm() {
		$cache = new _WV24_RuntimeCache();

		$this->assertEquals('//cli',    $cache->getSetting('default_fragment', 5));
		$this->assertEquals('//cli/en', $cache->getSetting('default_fragment', 8));
	}

	public function testHasUrl() {
		$cache = new _WV24_RuntimeCache();
		$this->assertFalse($cache->hasUrl(1, 5));

		$cache->addUrl(1, 5, 'http://example.com/');
		$this->assertTrue($cache->hasUrl(1, 5));
		$this->assertEquals('http://example.com/', $cache->getUrl(1, 5));

		$cache->forgetUrl(1, 5);
		$this->assertFalse($cache->hasUrl(1, 5));
	}

	public function testGetArticleData() {
		$cache = new _WV24_RuntimeCache();
		$cache->prewarmArticleData(5, array(1));

		$data = $cache->getArticleData(1, 5);
		$this->assertEquals(array(
			'id'       => 1,
			'clang'    => 5,
			'name'     => 'Entwicklung',
			're_id'    => 48,
			'is_cat'   => true,
			'path'     => array(48),
			'fragment' => '',
			'skipped'  => false,
			'url'      => null
		), $data);

		$this->assertEquals(1, $cache->getArticleData(1, 5, 'id'));
		$this->assertEquals('Entwicklung', $cache->getArticleData(1, 5, 'name'));
		$this->assertNull($cache->getArticleData(1, 5, 'url'));
		$this->assertFalse($cache->getArticleData(1, 5, 'skipped'));
	}

	public function testGetPathInfo() {
		$cache = new _WV24_RuntimeCache();
		$cache->prewarmArticleData(5);

		$data = $cache->getPathInfo(array(1, 2, 90), 5);
		$this->assertCount(3, $data);
		$this->assertEquals(array(
			1 => array(
				'id'       => 1,
				'clang'    => 5,
				'name'     => 'Entwicklung',
				're_id'    => 48,
				'is_cat'   => true,
				'path'     => array(48),
				'fragment' => '',
				'skipped'  => false,
				'url'      => null
			),
			2 => array(
				'id'       => 2,
				'clang'    => 5,
				'name'     => 'Hardware',
				're_id'    => 48,
				'is_cat'   => true,
				'path'     => array(48),
				'fragment' => '',
				'skipped'  => false,
				'url'      => null
			),
			90 => array(
				'id'       => 90,
				'clang'    => 5,
				'name'     => 'Ackermannfunktion',
				're_id'    => 0,
				'is_cat'   => false,
				'path'     => array(),
				'fragment' => '',
				'skipped'  => false,
				'url'      => null
			)
		), $data);
	}

	public function testInvalidStuff() {
		$cache = new _WV24_RuntimeCache();

		try {
			$cache->addUrl(1000, 5, 'http://example.com/');
			$this->fail('Should not have been able to set the URL of an unknown article.');
		}
		catch (Exception $e) { }

		try {
			$cache->addUrl(1, 12, 'http://example.com/');
			$this->fail('Should not have been able to set the URL of an unknown article.');
		}
		catch (Exception $e) { }

		try {
			$cache->getSetting('foo');
			$this->fail('Should not have been able retrieve unknown setting "foo".');
		}
		catch (Exception $e) { }

		try {
			$cache = new _WV24_RuntimeCache();
			$cache->prewarmArticleData(5, array(1));
			$cache->getArticleData(2, 5, 'path');

			$this->fail('Should not have been able retrieve data for non-cached article.');
		}
		catch (Exception $e) { }

		try {
			$cache = new _WV24_RuntimeCache();
			$cache->prewarmArticleData(5, array(1));
			$cache->getArticleData(1, 5, 'foo');

			$this->fail('Should not have been able retrieve data for unknown key.');
		}
		catch (Exception $e) { }

		// don't warn about "incomplete" test
		$this->assertTrue(true);
	}
}
