PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/Session/SaveHandler/MongoDBTest.php

https://github.com/telkins/zf2
PHP | 156 lines | 80 code | 28 blank | 48 comment | 2 complexity | 6b69985ff0fe2c2528295a3079100f50 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Session\SaveHandler;
  10. use Mongo;
  11. use Zend\Session\SaveHandler\MongoDB;
  12. use Zend\Session\SaveHandler\MongoDBOptions;
  13. /**
  14. * @group Zend_Session
  15. */
  16. class MongoDBTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @var Mongo|MongoClient
  20. */
  21. protected $mongo;
  22. /**
  23. * MongoCollection instance
  24. *
  25. * @var MongoCollection
  26. */
  27. protected $mongoCollection;
  28. /**
  29. * @var MongoDBOptions
  30. */
  31. protected $options;
  32. /**
  33. * Setup performed prior to each test method
  34. *
  35. * @return void
  36. */
  37. public function setUp()
  38. {
  39. if (!extension_loaded('mongo')) {
  40. $this->markTestSkipped('Zend\Session\SaveHandler\MongoDB tests are not enabled due to missing Mongo extension');
  41. }
  42. $this->options = new MongoDBOptions(array(
  43. 'database' => 'zf2_tests',
  44. 'collection' => 'sessions',
  45. ));
  46. $mongoClass = (version_compare(phpversion('mongo'), '1.3.0', '<')) ? '\Mongo' : '\MongoClient';
  47. $this->mongo = new $mongoClass();
  48. $this->mongoCollection = $this->mongo->selectCollection($this->options->getDatabase(), $this->options->getCollection());
  49. }
  50. /**
  51. * Tear-down operations performed after each test method
  52. *
  53. * @return void
  54. */
  55. public function tearDown()
  56. {
  57. if ($this->mongoCollection) {
  58. $this->mongoCollection->drop();
  59. }
  60. }
  61. public function testReadWrite()
  62. {
  63. $saveHandler = new MongoDB($this->mongo, $this->options);
  64. $this->assertTrue($saveHandler->open('savepath', 'sessionname'));
  65. $id = '242';
  66. $data = array('foo' => 'bar', 'bar' => array('foo' => 'bar'));
  67. $this->assertTrue($saveHandler->write($id, serialize($data)));
  68. $this->assertEquals($data, unserialize($saveHandler->read($id)));
  69. $data = array('foo' => array(1, 2, 3));
  70. $this->assertTrue($saveHandler->write($id, serialize($data)));
  71. $this->assertEquals($data, unserialize($saveHandler->read($id)));
  72. }
  73. public function testReadDestroysExpiredSession()
  74. {
  75. /* Note: due to the session save handler's open() method reading the
  76. * "session.gc_maxlifetime" INI value directly, it's necessary to set
  77. * that to simulate natural session expiration.
  78. */
  79. $oldMaxlifetime = ini_get('session.gc_maxlifetime');
  80. ini_set('session.gc_maxlifetime', 0);
  81. $saveHandler = new MongoDB($this->mongo, $this->options);
  82. $this->assertTrue($saveHandler->open('savepath', 'sessionname'));
  83. $id = '242';
  84. $data = array('foo' => 'bar');
  85. $this->assertNull($this->mongoCollection->findOne(array('_id' => $id)));
  86. $this->assertTrue($saveHandler->write($id, serialize($data)));
  87. $this->assertNotNull($this->mongoCollection->findOne(array('_id' => $id)));
  88. $this->assertEquals('', $saveHandler->read($id));
  89. $this->assertNull($this->mongoCollection->findOne(array('_id' => $id)));
  90. ini_set('session.gc_maxlifetime', $oldMaxlifetime);
  91. }
  92. public function testGarbageCollection()
  93. {
  94. $saveHandler = new MongoDB($this->mongo, $this->options);
  95. $this->assertTrue($saveHandler->open('savepath', 'sessionname'));
  96. $data = array('foo' => 'bar');
  97. $this->assertTrue($saveHandler->write(123, serialize($data)));
  98. $this->assertTrue($saveHandler->write(456, serialize($data)));
  99. $this->assertEquals(2, $this->mongoCollection->count());
  100. $saveHandler->gc(5);
  101. $this->assertEquals(2, $this->mongoCollection->count());
  102. /* Note: MongoDate uses micro-second precision, so even a maximum
  103. * lifetime of zero would not match records that were just inserted.
  104. * Use a negative number instead.
  105. */
  106. $saveHandler->gc(-1);
  107. $this->assertEquals(0, $this->mongoCollection->count());
  108. }
  109. /**
  110. * @expectedException MongoCursorException
  111. */
  112. public function testWriteExceptionEdgeCaseForChangedSessionName()
  113. {
  114. $saveHandler = new MongoDB($this->mongo, $this->options);
  115. $this->assertTrue($saveHandler->open('savepath', 'sessionname'));
  116. $id = '242';
  117. $data = array('foo' => 'bar');
  118. /* Note: a MongoCursorException will be thrown if a record with this ID
  119. * already exists with a different session name, since the upsert query
  120. * cannot insert a new document with the same ID and new session name.
  121. * This should only happen if ID's are not unique or if the session name
  122. * is altered mid-process.
  123. */
  124. $saveHandler->write($id, serialize($data));
  125. $saveHandler->open('savepath', 'sessionname_changed');
  126. $saveHandler->write($id, serialize($data));
  127. }
  128. }