PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/http-foundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 251 lines | 173 code | 54 blank | 24 comment | 0 complexity | b0fa620f6a3d098a88bd47bc886d704a MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;
  11. use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;
  12. /**
  13. * @author Markus Bachmann <markus.bachmann@bachi.biz>
  14. * @requires extension mongo
  15. * @group time-sensitive
  16. */
  17. class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $mongo;
  23. private $storage;
  24. public $options;
  25. protected function setUp()
  26. {
  27. parent::setUp();
  28. $mongoClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? 'Mongo' : 'MongoClient';
  29. $this->mongo = $this->getMockBuilder($mongoClass)
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $this->options = array(
  33. 'id_field' => '_id',
  34. 'data_field' => 'data',
  35. 'time_field' => 'time',
  36. 'expiry_field' => 'expires_at',
  37. 'database' => 'sf2-test',
  38. 'collection' => 'session-test',
  39. );
  40. $this->storage = new MongoDbSessionHandler($this->mongo, $this->options);
  41. }
  42. /**
  43. * @expectedException \InvalidArgumentException
  44. */
  45. public function testConstructorShouldThrowExceptionForInvalidMongo()
  46. {
  47. new MongoDbSessionHandler(new \stdClass(), $this->options);
  48. }
  49. /**
  50. * @expectedException \InvalidArgumentException
  51. */
  52. public function testConstructorShouldThrowExceptionForMissingOptions()
  53. {
  54. new MongoDbSessionHandler($this->mongo, array());
  55. }
  56. public function testOpenMethodAlwaysReturnTrue()
  57. {
  58. $this->assertTrue($this->storage->open('test', 'test'), 'The "open" method should always return true');
  59. }
  60. public function testCloseMethodAlwaysReturnTrue()
  61. {
  62. $this->assertTrue($this->storage->close(), 'The "close" method should always return true');
  63. }
  64. public function testRead()
  65. {
  66. $collection = $this->createMongoCollectionMock();
  67. $this->mongo->expects($this->once())
  68. ->method('selectCollection')
  69. ->with($this->options['database'], $this->options['collection'])
  70. ->will($this->returnValue($collection));
  71. $that = $this;
  72. // defining the timeout before the actual method call
  73. // allows to test for "greater than" values in the $criteria
  74. $testTimeout = time() + 1;
  75. $collection->expects($this->once())
  76. ->method('findOne')
  77. ->will($this->returnCallback(function ($criteria) use ($that, $testTimeout) {
  78. $that->assertArrayHasKey($that->options['id_field'], $criteria);
  79. $that->assertEquals($criteria[$that->options['id_field']], 'foo');
  80. $that->assertArrayHasKey($that->options['expiry_field'], $criteria);
  81. $that->assertArrayHasKey('$gte', $criteria[$that->options['expiry_field']]);
  82. $that->assertInstanceOf('MongoDate', $criteria[$that->options['expiry_field']]['$gte']);
  83. $that->assertGreaterThanOrEqual($criteria[$that->options['expiry_field']]['$gte']->sec, $testTimeout);
  84. return array(
  85. $that->options['id_field'] => 'foo',
  86. $that->options['data_field'] => new \MongoBinData('bar', \MongoBinData::BYTE_ARRAY),
  87. $that->options['id_field'] => new \MongoDate(),
  88. );
  89. }));
  90. $this->assertEquals('bar', $this->storage->read('foo'));
  91. }
  92. public function testWrite()
  93. {
  94. $collection = $this->createMongoCollectionMock();
  95. $this->mongo->expects($this->once())
  96. ->method('selectCollection')
  97. ->with($this->options['database'], $this->options['collection'])
  98. ->will($this->returnValue($collection));
  99. $that = $this;
  100. $data = array();
  101. $collection->expects($this->once())
  102. ->method('update')
  103. ->will($this->returnCallback(function ($criteria, $updateData, $options) use ($that, &$data) {
  104. $that->assertEquals(array($that->options['id_field'] => 'foo'), $criteria);
  105. $that->assertEquals(array('upsert' => true, 'multiple' => false), $options);
  106. $data = $updateData['$set'];
  107. }));
  108. $expectedExpiry = time() + (int) ini_get('session.gc_maxlifetime');
  109. $this->assertTrue($this->storage->write('foo', 'bar'));
  110. $this->assertEquals('bar', $data[$this->options['data_field']]->bin);
  111. $that->assertInstanceOf('MongoDate', $data[$this->options['time_field']]);
  112. $this->assertInstanceOf('MongoDate', $data[$this->options['expiry_field']]);
  113. $this->assertGreaterThanOrEqual($expectedExpiry, $data[$this->options['expiry_field']]->sec);
  114. }
  115. public function testWriteWhenUsingExpiresField()
  116. {
  117. $this->options = array(
  118. 'id_field' => '_id',
  119. 'data_field' => 'data',
  120. 'time_field' => 'time',
  121. 'database' => 'sf2-test',
  122. 'collection' => 'session-test',
  123. 'expiry_field' => 'expiresAt',
  124. );
  125. $this->storage = new MongoDbSessionHandler($this->mongo, $this->options);
  126. $collection = $this->createMongoCollectionMock();
  127. $this->mongo->expects($this->once())
  128. ->method('selectCollection')
  129. ->with($this->options['database'], $this->options['collection'])
  130. ->will($this->returnValue($collection));
  131. $that = $this;
  132. $data = array();
  133. $collection->expects($this->once())
  134. ->method('update')
  135. ->will($this->returnCallback(function ($criteria, $updateData, $options) use ($that, &$data) {
  136. $that->assertEquals(array($that->options['id_field'] => 'foo'), $criteria);
  137. $that->assertEquals(array('upsert' => true, 'multiple' => false), $options);
  138. $data = $updateData['$set'];
  139. }));
  140. $this->assertTrue($this->storage->write('foo', 'bar'));
  141. $this->assertEquals('bar', $data[$this->options['data_field']]->bin);
  142. $that->assertInstanceOf('MongoDate', $data[$this->options['time_field']]);
  143. $that->assertInstanceOf('MongoDate', $data[$this->options['expiry_field']]);
  144. }
  145. public function testReplaceSessionData()
  146. {
  147. $collection = $this->createMongoCollectionMock();
  148. $this->mongo->expects($this->once())
  149. ->method('selectCollection')
  150. ->with($this->options['database'], $this->options['collection'])
  151. ->will($this->returnValue($collection));
  152. $data = array();
  153. $collection->expects($this->exactly(2))
  154. ->method('update')
  155. ->will($this->returnCallback(function ($criteria, $updateData, $options) use (&$data) {
  156. $data = $updateData;
  157. }));
  158. $this->storage->write('foo', 'bar');
  159. $this->storage->write('foo', 'foobar');
  160. $this->assertEquals('foobar', $data['$set'][$this->options['data_field']]->bin);
  161. }
  162. public function testDestroy()
  163. {
  164. $collection = $this->createMongoCollectionMock();
  165. $this->mongo->expects($this->once())
  166. ->method('selectCollection')
  167. ->with($this->options['database'], $this->options['collection'])
  168. ->will($this->returnValue($collection));
  169. $collection->expects($this->once())
  170. ->method('remove')
  171. ->with(array($this->options['id_field'] => 'foo'));
  172. $this->assertTrue($this->storage->destroy('foo'));
  173. }
  174. public function testGc()
  175. {
  176. $collection = $this->createMongoCollectionMock();
  177. $this->mongo->expects($this->once())
  178. ->method('selectCollection')
  179. ->with($this->options['database'], $this->options['collection'])
  180. ->will($this->returnValue($collection));
  181. $that = $this;
  182. $collection->expects($this->once())
  183. ->method('remove')
  184. ->will($this->returnCallback(function ($criteria) use ($that) {
  185. $that->assertInstanceOf('MongoDate', $criteria[$that->options['expiry_field']]['$lt']);
  186. $that->assertGreaterThanOrEqual(time() - 1, $criteria[$that->options['expiry_field']]['$lt']->sec);
  187. }));
  188. $this->assertTrue($this->storage->gc(1));
  189. }
  190. private function createMongoCollectionMock()
  191. {
  192. $collection = $this->getMockBuilder('MongoCollection')
  193. ->disableOriginalConstructor()
  194. ->getMock();
  195. return $collection;
  196. }
  197. }