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.

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