You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.8 KiB

<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-26
*
* Unit tests for Env class
*/
require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php';
require_once dirname(__FILE__) . '/../../app/router/Router.php';
require_once dirname(__FILE__) . '/../../app/FrontController.php';
require_once dirname(__FILE__) . '/../../classes/Env.class.php';
class EnvTest extends PHPUnit_Framework_TestCase
{
/**
* @runInSeparateProcess
*/
public function testGetRequestUri()
{
if(!defined('DEBUG')) {
define('DEBUG', false);
}
$_SERVER['REQUEST_URI'] = '/test/index.php?id=1&test=wet&id_theme=512';
$this->assertEquals('/test/index.php', Env::getRequestUri());
$_SERVER['REQUEST_URI'] = '/tes?t/index.php?id=1&test=wet&id_theme=512';
$this->assertEquals('/test/index.php', Env::getRequestUri());
}
/**
* @runInSeparateProcess
*/
public function testTrimBaseRequestUri()
{
if(!defined('DEBUG')) {
define('DEBUG', false);
}
$class = new ReflectionClass('Env');
$this->started = $class->getProperty('request');
$this->started->setAccessible(true);
$this->started->setValue(null, array());
FrontController::getInstance()->setBaseUrl('/test');
$_SERVER['REQUEST_URI'] = '/test/index.php?id=1&test=wet&id_theme=512';
$this->assertEquals('/index.php', Env::getRequestUri(true));
}
public function testServer()
{
$this->assertEquals($_SERVER, Env::Server());
$this->assertEquals($_SERVER['DOCUMENT_ROOT'], Env::Server('DOCUMENT_ROOT'));
$this->assertNotEmpty(Env::Server());
$this->assertArrayHasKey('DOCUMENT_ROOT', Env::Server());
}
public function testCookie()
{
$this->assertTrue(Env::setCookie('var', 'value', 20));
$_COOKIE['var'] = 'value';
$this->assertEquals(array('var' => 'value'), Env::Cookie());
$this->assertEquals('value', Env::Cookie('var'));
$this->assertEquals('default', Env::Cookie('new', 'default'));
}
public function testPost()
{
$_POST['var'] = 'value';
$this->assertEquals(array('var' => 'value'), Env::Post());
$this->assertEquals('value', Env::Post('var'));
$this->assertEquals('default', Env::Post('new', 'default'));
}
public function testGet()
{
$_GET['var'] = 'value';
$this->assertEquals(array('var' => 'value'), Env::Get());
$this->assertEquals('value', Env::Get('var'));
$this->assertEquals('default', Env::Get('new', 'default'));
}
public function testFiles()
{
$this->assertEquals('default', Env::Files('file.txt', 'default'));
$this->assertEquals(array(), Env::Files());
}
public function testUnsetFiles()
{
unset($_FILES);
$this->assertEquals('default', Env::Files('file.txt', 'default'));
$_FILES['file'] = array('name' => 'files', 'path' => '/');
$this->assertEquals(array('name' => 'files', 'path' => '/'), Env::Files('file', 'default'));
$this->assertEquals('files', Env::Files('file', 'empty', 'name'));
}
public function testParams()
{
Env::setParams(array('name' => 'tony', 'age' => 21));
$this->assertEquals(array('name' => 'tony', 'age' => 21), Env::getParam());
Env::setParams(array('sex' => 'male'));
$this->assertEquals(array('name' => 'tony', 'age' => 21, 'sex' => 'male'), Env::getParam());
$this->assertEquals('tony', Env::getParam('name'));
$this->assertEquals('default', Env::getParam('height', 'default'));
Env::setParam('surname', 'grebnev');
$this->assertEquals('grebnev', Env::getParam('surname'));
}
}