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.

122 lines
4.0 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-26
  8. *
  9. * Unit tests for Env class
  10. */
  11. require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php';
  12. require_once dirname(__FILE__) . '/../../app/router/Router.php';
  13. require_once dirname(__FILE__) . '/../../app/FrontController.php';
  14. require_once dirname(__FILE__) . '/../../classes/Env.class.php';
  15. class EnvTest extends PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @runInSeparateProcess
  19. */
  20. public function testGetRequestUri()
  21. {
  22. if(!defined('DEBUG')) {
  23. define('DEBUG', false);
  24. }
  25. $_SERVER['REQUEST_URI'] = '/test/index.php?id=1&test=wet&id_theme=512';
  26. $this->assertSame('/test/index.php', Env::getRequestUri());
  27. $_SERVER['REQUEST_URI'] = '/tes?t/index.php?id=1&test=wet&id_theme=512';
  28. $this->assertSame('/test/index.php', Env::getRequestUri());
  29. }
  30. /**
  31. * @runInSeparateProcess
  32. */
  33. public function testTrimBaseRequestUri()
  34. {
  35. if(!defined('DEBUG')) {
  36. define('DEBUG', false);
  37. }
  38. $class = new ReflectionClass('Env');
  39. $this->started = $class->getProperty('request');
  40. $this->started->setAccessible(true);
  41. $this->started->setValue(null, array());
  42. FrontController::getInstance()->setBaseUrl('/test');
  43. $_SERVER['REQUEST_URI'] = '/test/index.php?id=1&test=wet&id_theme=512';
  44. $this->assertSame('/index.php', Env::getRequestUri(true));
  45. }
  46. public function testServer()
  47. {
  48. $this->assertSame($_SERVER, Env::Server());
  49. $this->assertSame($_SERVER['DOCUMENT_ROOT'], Env::Server('DOCUMENT_ROOT'));
  50. $this->assertNotEmpty(Env::Server());
  51. $this->assertArrayHasKey('DOCUMENT_ROOT', Env::Server());
  52. }
  53. public function testCookie()
  54. {
  55. $this->assertTrue(Env::setCookie('var', 'value', 20));
  56. $_COOKIE['var'] = 'value';
  57. $this->assertSame(array('var' => 'value'), Env::Cookie());
  58. $this->assertSame('value', Env::Cookie('var'));
  59. $this->assertSame('default', Env::Cookie('new', 'default'));
  60. }
  61. public function testPost()
  62. {
  63. $_POST['var'] = 'value';
  64. $this->assertSame(array('var' => 'value'), Env::Post());
  65. $this->assertSame('value', Env::Post('var'));
  66. $this->assertSame('default', Env::Post('new', 'default'));
  67. }
  68. public function testGet()
  69. {
  70. $_GET['var'] = 'value';
  71. $this->assertSame(array('var' => 'value'), Env::Get());
  72. $this->assertSame('value', Env::Get('var'));
  73. $this->assertSame('default', Env::Get('new', 'default'));
  74. }
  75. public function testFiles()
  76. {
  77. $this->assertSame('default', Env::Files('file.txt', 'default'));
  78. $this->assertSame(array(), Env::Files());
  79. }
  80. public function testUnsetFiles()
  81. {
  82. unset($_FILES);
  83. $this->assertSame('default', Env::Files('file.txt', 'default'));
  84. $_FILES['file'] = array('name' => 'files', 'path' => '/');
  85. $this->assertSame(array('name' => 'files', 'path' => '/'), Env::Files('file', 'default'));
  86. $this->assertSame('files', Env::Files('file', 'empty', 'name'));
  87. }
  88. public function testParams()
  89. {
  90. Env::setParams(array('name' => 'tony', 'age' => 21));
  91. $this->assertSame(array('name' => 'tony', 'age' => 21), Env::getParam());
  92. Env::setParams(array('sex' => 'male'));
  93. $this->assertSame(array('name' => 'tony', 'age' => 21, 'sex' => 'male'), Env::getParam());
  94. $this->assertSame('tony', Env::getParam('name'));
  95. $this->assertSame('default', Env::getParam('height', 'default'));
  96. Env::setParam('surname', 'grebnev');
  97. $this->assertSame('grebnev', Env::getParam('surname'));
  98. }
  99. public function tearDown()
  100. {
  101. $env = new ReflectionClass('Env');
  102. $params = $env->getProperty('params');
  103. $params->setAccessible(true);
  104. $params->setValue('Env', array());
  105. }
  106. }