<?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_DispatcherTest extends RealURL2_BaseTest {
	/**
	 * @dataProvider provider
	 */
	public function testDispatching($expected, $url, array $get = array()) {
		self::rebuildAll();

		$_SERVER['REQUEST_URI'] = $url;
		$_GET                   = $get;

		$storage    = WV24_Util::getStorage();
		$resolver   = new _WV24_Resolver($storage, sly_Core::cache());
		$dispatcher = new _WV24_Dispatcher($resolver);
		$result     = $dispatcher->dispatch(true);

		if ($expected === null) {
			$this->assertNull($result);
			return;
		}

		$this->assertInstanceof('sly_Model_Article', $result);
		$this->assertEquals($expected, $result->getId().'_'.$result->getClang());
	}

	public function provider() {
		return array(
			// vanilla cases
			array('98_5', '/'),
			array('98_8', '/en/'),
			array('49_8', '/en/Computer+virus/'),
			array('90_5', '/ackermannfunktion.html'),

			// remove params
			array('90_5', '/ackermannfunktion.html?params'),
			array('90_5', '/ackermannfunktion.html?params&more'),
			array('90_5', '/ackermannfunktion.html&broken'),

			// replace index files automatically
			array('98_5', '/index.html'),
			array('98_5', '/index.php'),

			// fix missing slash
			array('98_5', ''),
			array('98_8', '/en'),

			// don't attempt to fix article URLs
			array(null, '/ackermannX.html')
		);
	}
}
