<?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_CacheTest extends sly_StatelessTest {
	public function testSet() {
		$ns = 'test'.uniqid();

		sly_Core::cache()->flush('realurl2', true);
		$this->assertNull(_WV24_Cache::findURL($ns, 'foo'));

		$url = new _WV24_URL(array('created' => date('r'), 'namespace' => $ns, 'ident' => 'foo', 'article_id' => 4, 'clang' => 5, 'path' => '', 'url' => 'http://hello.world/'));
		_WV24_Cache::cacheURL($url);
		$this->assertInstanceOf('_WV24_URL', _WV24_Cache::findURL($ns, 'foo'));

		sly_Core::cache()->flush('realurl2', true);
		$this->assertNull(_WV24_Cache::findURL($ns, 'foo'));
	}

	public function testClear() {
		$ns   = 'test'.uniqid();
		$urlA = new _WV24_URL(array('created' => date('r'), 'namespace' => $ns, 'ident' => 'fooA', 'article_id' => 4, 'clang' => 5, 'path' => '', 'url' => 'http://hello.world/A'));
		$urlB = new _WV24_URL(array('created' => date('r'), 'namespace' => $ns, 'ident' => 'fooB', 'article_id' => 4, 'clang' => 5, 'path' => '', 'url' => 'http://hello.world/B'));
		$urlC = new _WV24_URL(array('created' => date('r'), 'namespace' => $ns, 'ident' => 'fooC', 'article_id' => 4, 'clang' => 5, 'path' => '', 'url' => 'http://hello.world/C'));

		_WV24_Cache::cacheURL($urlA);
		_WV24_Cache::cacheURL($urlB);
		_WV24_Cache::cacheURL($urlC);

		_WV24_Cache::clear($ns);

		$this->assertNull(_WV24_Cache::findURL($ns, 'fooA'));
		$this->assertNull(_WV24_Cache::findURL($ns, 'fooB'));
		$this->assertNull(_WV24_Cache::findURL($ns, 'fooC'));

		_WV24_Cache::cacheURL($urlA);
		_WV24_Cache::cacheURL($urlB);
		_WV24_Cache::cacheURL($urlC);

		_WV24_Cache::clear($ns, 'fooB');

		$this->assertInstanceof('_WV24_URL', _WV24_Cache::findURL($ns, 'fooA'));
		$this->assertNull(_WV24_Cache::findURL($ns, 'fooB'));
		$this->assertInstanceof('_WV24_URL', _WV24_Cache::findURL($ns, 'fooC'));
	}
}
