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.

120 lines
3.9 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. Config::set('DEBUG', false);
  23. $_SERVER['REQUEST_URI'] = '/test/index.php?id=1&test=wet&id_theme=512';
  24. $this->assertSame('/test/index.php', Env::getRequestUri());
  25. $_SERVER['REQUEST_URI'] = '/tes?t/index.php?id=1&test=wet&id_theme=512';
  26. $this->assertSame('/test/index.php', Env::getRequestUri());
  27. }
  28. /**
  29. * @runInSeparateProcess
  30. */
  31. public function testTrimBaseRequestUri()
  32. {
  33. Config::set('DEBUG', false);
  34. $class = new ReflectionClass('Env');
  35. $this->started = $class->getProperty('request');
  36. $this->started->setAccessible(true);
  37. $this->started->setValue(null, array());
  38. FrontController::getInstance()->setBaseUrl('/test');
  39. $_SERVER['REQUEST_URI'] = '/test/index.php?id=1&test=wet&id_theme=512';
  40. $this->assertSame('/index.php', Env::getRequestUri(true));
  41. }
  42. public function testServer()
  43. {
  44. $this->assertSame($_SERVER, Env::Server());
  45. $this->assertSame($_SERVER['DOCUMENT_ROOT'], Env::Server('DOCUMENT_ROOT'));
  46. $this->assertNotEmpty(Env::Server());
  47. $this->assertArrayHasKey('DOCUMENT_ROOT', Env::Server());
  48. }
  49. public function testCookie()
  50. {
  51. $this->assertTrue(Env::setCookie('var', 'value', 20));
  52. $_COOKIE['var'] = 'value';
  53. $this->assertSame(array('var' => 'value'), Env::Cookie());
  54. $this->assertSame('value', Env::Cookie('var'));
  55. $this->assertSame('default', Env::Cookie('new', 'default'));
  56. }
  57. public function testPost()
  58. {
  59. $_POST['var'] = 'value';
  60. $this->assertSame(array('var' => 'value'), Env::Post());
  61. $this->assertSame('value', Env::Post('var'));
  62. $this->assertSame('default', Env::Post('new', 'default'));
  63. }
  64. public function testGet()
  65. {
  66. $_GET['var'] = 'value';
  67. $this->assertSame(array('var' => 'value'), Env::Get());
  68. $this->assertSame('value', Env::Get('var'));
  69. $this->assertSame('default', Env::Get('new', 'default'));
  70. }
  71. public function testFiles()
  72. {
  73. $this->assertSame('default', Env::Files('file.txt', 'default'));
  74. $this->assertSame(array(), Env::Files());
  75. }
  76. public function testUnsetFiles()
  77. {
  78. unset($_FILES);
  79. $this->assertSame('default', Env::Files('file.txt', 'default'));
  80. $_FILES['file'] = array('name' => 'files', 'path' => '/');
  81. $this->assertSame(array('name' => 'files', 'path' => '/'), Env::Files('file', 'default'));
  82. $this->assertSame('files', Env::Files('file', 'empty', 'name'));
  83. }
  84. public function testParams()
  85. {
  86. Env::setParams(array('name' => 'tony', 'age' => 21));
  87. $this->assertSame(array('name' => 'tony', 'age' => 21), Env::getParam());
  88. Env::setParams(array('sex' => 'male'));
  89. $this->assertSame(array('name' => 'tony', 'age' => 21, 'sex' => 'male'), Env::getParam());
  90. $this->assertSame('tony', Env::getParam('name'));
  91. $this->assertSame('default', Env::getParam('height', 'default'));
  92. Env::setParam('surname', 'grebnev');
  93. $this->assertSame('grebnev', Env::getParam('surname'));
  94. }
  95. public function tearDown()
  96. {
  97. $env = new ReflectionClass('Env');
  98. $params = $env->getProperty('params');
  99. $params->setAccessible(true);
  100. $params->setValue('Env', array());
  101. }
  102. }