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.

110 lines
3.2 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-11-15
  8. *
  9. * Unit tests for SessionModel class
  10. */
  11. require_once dirname(__FILE__) . '/../../Registry.php';
  12. require_once dirname(__FILE__) . '/../../Config.php';
  13. require_once dirname(__FILE__) . '/../../classes/Env.class.php';
  14. require_once dirname(__FILE__) . '/../../model/Db.php';
  15. require_once dirname(__FILE__) . '/../../model/DbDriver.php';
  16. require_once dirname(__FILE__) . '/../model/MyDbDriver.php';
  17. require_once dirname(__FILE__) . '/../../model/Model.php';
  18. require_once dirname(__FILE__) . '/../../model/SqlModel.php';
  19. require_once dirname(__FILE__) . '/../../session/Session.model.php';
  20. class SessionModelTest extends PHPUnit_Framework_TestCase
  21. {
  22. protected $model;
  23. public function setUp()
  24. {
  25. $conf = array('default' => array('driver' => 'MockDbDriver', 'hostname' => 'somehost', 'database' => 'db', 'username' => 'test', 'password' => '1234'));
  26. if (!class_exists('MockDbDriver')) {
  27. $this->getMockForAbstractClass('MyDbDriver', array($conf), 'MockDbDriver', false);
  28. }
  29. if (!class_exists('MockDbExpr')) {
  30. $this->getMock('DbExpr', array(), array(), 'MockDbExpr', false);
  31. }
  32. Config::set('Db', $conf);
  33. set_new_overload(array($this, 'newCallback'));
  34. }
  35. public function testOpen()
  36. {
  37. $this->model = new SessionModel();
  38. $this->assertTrue($this->model->open('path', 'name'));
  39. }
  40. public function testClose()
  41. {
  42. $this->model = new SessionModel();
  43. $this->assertTrue($this->model->close());
  44. }
  45. public function testRead()
  46. {
  47. $this->model = new SessionModel();
  48. $this->assertEquals('data', $this->model->read(1));
  49. }
  50. public function testWrite()
  51. {
  52. $this->model = new SessionModel();
  53. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  54. $this->assertEmpty(Env::Server('HTTP_X_FORWARDED_FOR'));
  55. $this->assertTrue($this->model->write(2, 'user|.s:20;id=2;id=2'));
  56. }
  57. public function testDestroy()
  58. {
  59. $this->model = new SessionModel();
  60. $this->assertTrue($this->model->destroy(2));
  61. }
  62. public function testGc()
  63. {
  64. $this->model = new SessionModel();
  65. $this->assertTrue($this->model->gc(2000));
  66. }
  67. public function testDestroyByUserId()
  68. {
  69. $this->model = new SessionModel();
  70. $this->assertEquals('session', $this->model->destroyByUserId(12));
  71. }
  72. public function tearDown()
  73. {
  74. Config::getInstance()->offsetUnset('Db');
  75. $config = new ReflectionClass('Db');
  76. $registry = $config->getProperty('connections');
  77. $registry->setAccessible(true);
  78. $registry->setValue('Db', array());
  79. unset_new_overload();
  80. }
  81. protected function newCallback($className)
  82. {
  83. switch ($className) {
  84. case 'DbExpr':
  85. return 'MockDbExpr';
  86. case 'MockDbDriver':
  87. return 'MockDbDriver';
  88. case 'CacheKey':
  89. return 'MockCacheKey';
  90. default:
  91. return $className;
  92. }
  93. }
  94. }