123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace yiiunit\extensions\mongodb;
- use yii\mongodb\Session;
- use Yii;
- class SessionTest extends TestCase
- {
- /**
- * @var string test session collection name.
- */
- protected static $sessionCollection = '_test_session';
- protected function tearDown()
- {
- $this->dropCollection(static::$sessionCollection);
- parent::tearDown();
- }
- /**
- * Creates test session instance.
- * @return Session session instance.
- */
- protected function createSession()
- {
- return Yii::createObject([
- 'class' => Session::className(),
- 'db' => $this->getConnection(),
- 'sessionCollection' => static::$sessionCollection,
- ]);
- }
- // Tests:
- public function testWriteSession()
- {
- $session = $this->createSession();
- $id = uniqid();
- $data = [
- 'name' => 'value'
- ];
- $dataSerialized = serialize($data);
- $this->assertTrue($session->writeSession($id, $dataSerialized), 'Unable to write session!');
- $collection = $session->db->getCollection($session->sessionCollection);
- $rows = $this->findAll($collection);
- $this->assertCount(1, $rows, 'No session record!');
- $row = array_shift($rows);
- $this->assertEquals($id, $row['id'], 'Wrong session id!');
- $this->assertEquals($dataSerialized, $row['data'], 'Wrong session data!');
- $this->assertTrue($row['expire'] > time(), 'Wrong session expire!');
- $newData = [
- 'name' => 'new value'
- ];
- $newDataSerialized = serialize($newData);
- $this->assertTrue($session->writeSession($id, $newDataSerialized), 'Unable to update session!');
- $rows = $this->findAll($collection);
- $this->assertCount(1, $rows, 'Wrong session records after update!');
- $newRow = array_shift($rows);
- $this->assertEquals($id, $newRow['id'], 'Wrong session id after update!');
- $this->assertEquals($newDataSerialized, $newRow['data'], 'Wrong session data after update!');
- $this->assertTrue($newRow['expire'] >= $row['expire'], 'Wrong session expire after update!');
- }
- /**
- * @depends testWriteSession
- */
- public function testDestroySession()
- {
- $session = $this->createSession();
- $id = uniqid();
- $data = [
- 'name' => 'value'
- ];
- $dataSerialized = serialize($data);
- $session->writeSession($id, $dataSerialized);
- $this->assertTrue($session->destroySession($id), 'Unable to destroy session!');
- $collection = $session->db->getCollection($session->sessionCollection);
- $rows = $this->findAll($collection);
- $this->assertEmpty($rows, 'Session record not deleted!');
- }
- /**
- * @depends testWriteSession
- */
- public function testReadSession()
- {
- $session = $this->createSession();
- $id = uniqid();
- $data = [
- 'name' => 'value'
- ];
- $dataSerialized = serialize($data);
- $session->writeSession($id, $dataSerialized);
- $sessionData = $session->readSession($id);
- $this->assertEquals($dataSerialized, $sessionData, 'Unable to read session!');
- $collection = $session->db->getCollection($session->sessionCollection);
- list($row) = $this->findAll($collection);
- $newRow = $row;
- $newRow['expire'] = time() - 1;
- unset($newRow['_id']);
- $collection->update(['_id' => $row['_id']], $newRow);
- $sessionData = $session->readSession($id);
- $this->assertEquals('', $sessionData, 'Expired session read!');
- }
- public function testGcSession()
- {
- $session = $this->createSession();
- $collection = $session->db->getCollection($session->sessionCollection);
- $collection->batchInsert([
- [
- 'id' => uniqid(),
- 'expire' => time() + 10,
- 'data' => 'actual',
- ],
- [
- 'id' => uniqid(),
- 'expire' => time() - 10,
- 'data' => 'expired',
- ],
- ]);
- $this->assertTrue($session->gcSession(10), 'Unable to collection garbage session!');
- $rows = $this->findAll($collection);
- $this->assertCount(1, $rows, 'Wrong records count!');
- }
- /**
- * @depends testWriteSession
- */
- public function testWriteCustomField()
- {
- $session = $this->createSession();
- $session->writeCallback = function ($session) {
- return [
- 'user_id' => 15
- ];
- };
- $session->writeSession('test', 'session data');
- $rows = $this->findAll($session->db->getCollection($session->sessionCollection));
- $this->assertEquals('session data', $rows[0]['data']);
- $this->assertEquals(15, $rows[0]['user_id']);
- }
- }
|