|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * UserFrosting (http://www.userfrosting.com) |
| 4 | + * |
| 5 | + * @link https://github.com/userfrosting/UserFrosting |
| 6 | + * @copyright Copyright (c) 2019 Alexander Weissman |
| 7 | + * @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License) |
| 8 | + */ |
| 9 | + |
| 10 | +namespace UserFrosting\Sprinkle\Account\Tests\Integration\Session; |
| 11 | + |
| 12 | +use UserFrosting\Session\Session; |
| 13 | +use UserFrosting\Sprinkle\Core\Database\Models\Session as SessionTable; |
| 14 | +use UserFrosting\Sprinkle\Core\Session\DatabaseSessionHandler; |
| 15 | +use UserFrosting\Sprinkle\Core\Tests\TestDatabase; |
| 16 | +use UserFrosting\Sprinkle\Core\Tests\RefreshDatabase; |
| 17 | +use UserFrosting\Sprinkle\Core\Tests\withDatabaseSessionHandler; |
| 18 | +use UserFrosting\Tests\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Integration tests for the session service. |
| 22 | + */ |
| 23 | +class SessionDatabaseHandlerTest extends TestCase |
| 24 | +{ |
| 25 | + use TestDatabase; |
| 26 | + use RefreshDatabase; |
| 27 | + use withDatabaseSessionHandler; |
| 28 | + |
| 29 | + public function setUp() |
| 30 | + { |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + $this->setupTestDatabase(); |
| 34 | + $this->refreshDatabase(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Test session table connection & existance |
| 39 | + */ |
| 40 | + public function testSessionTable() |
| 41 | + { |
| 42 | + $connection = $this->ci->db->connection(); |
| 43 | + $config = $this->ci->config; |
| 44 | + $table = $config['session.database.table']; |
| 45 | + |
| 46 | + // Check connexion is ok and returns what's expected from DatabaseSessionHandler |
| 47 | + $this->assertInstanceOf(\Illuminate\Database\ConnectionInterface::class, $connection); |
| 48 | + $this->assertInstanceOf(\Illuminate\Database\Query\Builder::class, $connection->table($table)); |
| 49 | + |
| 50 | + // Check table exist |
| 51 | + $this->assertTrue($connection->getSchemaBuilder()->hasTable($table)); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @depends testSessionTable |
| 56 | + */ |
| 57 | + public function testSessionWrite() |
| 58 | + { |
| 59 | + $config = $this->ci->config; |
| 60 | + $connection = $this->ci->db->connection(); |
| 61 | + |
| 62 | + // Define random session ID |
| 63 | + $session_id = 'test'.rand(1, 100000); |
| 64 | + |
| 65 | + // Make sure db is empty at first |
| 66 | + $this->assertEquals(0, SessionTable::count()); |
| 67 | + $this->assertNull(SessionTable::find($session_id)); |
| 68 | + |
| 69 | + // Get handler |
| 70 | + $handler = new DatabaseSessionHandler($connection, $config['session.database.table'], $config['session.minutes']); |
| 71 | + |
| 72 | + // Write session |
| 73 | + // https://github.com/laravel/framework/blob/5.4/src/Illuminate/Session/DatabaseSessionHandler.php#L132 |
| 74 | + $this->assertTrue($handler->write($session_id, 'foo')); |
| 75 | + |
| 76 | + // Closing the handler does nothing anyway |
| 77 | + // https://github.com/laravel/framework/blob/5.4/src/Illuminate/Session/DatabaseSessionHandler.php#L78 |
| 78 | + $this->assertTrue($handler->close()); |
| 79 | + |
| 80 | + // Read session |
| 81 | + // https://github.com/laravel/framework/blob/5.4/src/Illuminate/Session/DatabaseSessionHandler.php#L86-L101 |
| 82 | + $this->assertSame('foo', $handler->read($session_id)); |
| 83 | + |
| 84 | + // Check manually that the file has been written |
| 85 | + $this->assertNotEquals(0, SessionTable::count()); |
| 86 | + $this->assertNotNull(SessionTable::find($session_id)); |
| 87 | + $this->assertSame(base64_encode('foo'), SessionTable::find($session_id)->payload); |
| 88 | + |
| 89 | + // Destroy session |
| 90 | + // https://github.com/laravel/framework/blob/5.4/src/Illuminate/Session/DatabaseSessionHandler.php#L256 |
| 91 | + $this->assertTrue($handler->destroy($session_id)); |
| 92 | + |
| 93 | + // Check db to make sure it's gone |
| 94 | + $this->assertEquals(0, SessionTable::count()); |
| 95 | + $this->assertNull(SessionTable::find($session_id)); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Simulate session service with database handler. |
| 100 | + * We can't use the real service as it is created before we can even setup |
| 101 | + * the in-memory database with the basic table we need |
| 102 | + * |
| 103 | + * @depends testSessionWrite |
| 104 | + */ |
| 105 | + public function testUsingSessionDouble() |
| 106 | + { |
| 107 | + $this->ci->session->destroy(); |
| 108 | + |
| 109 | + $config = $this->ci->config; |
| 110 | + $connection = $this->ci->db->connection(); |
| 111 | + $handler = new DatabaseSessionHandler($connection, $config['session.database.table'], $config['session.minutes']); |
| 112 | + $session = new Session($handler, $config['session']); |
| 113 | + |
| 114 | + $this->assertInstanceOf(Session::class, $session); |
| 115 | + $this->assertInstanceOf(DatabaseSessionHandler::class, $session->getHandler()); |
| 116 | + $this->assertSame($handler, $session->getHandler()); |
| 117 | + |
| 118 | + $this->sessionTests($session); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @depends testUsingSessionDouble |
| 123 | + */ |
| 124 | + public function testUsingSessionService() |
| 125 | + { |
| 126 | + // Reset CI Session |
| 127 | + $this->useDatabaseSessionHandler(); |
| 128 | + |
| 129 | + // Make sure config is set |
| 130 | + $this->sessionTests($this->ci->session); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @param Session $session |
| 135 | + */ |
| 136 | + protected function sessionTests(Session $session) |
| 137 | + { |
| 138 | + // Make sure session service have correct instance |
| 139 | + $this->assertInstanceOf(Session::class, $session); |
| 140 | + $this->assertInstanceOf(DatabaseSessionHandler::class, $session->getHandler()); |
| 141 | + |
| 142 | + // Destroy previously defined session |
| 143 | + $session->destroy(); |
| 144 | + |
| 145 | + // Start new one and validate status |
| 146 | + $this->assertSame(PHP_SESSION_NONE, $session->status()); |
| 147 | + $session->start(); |
| 148 | + $this->assertSame(PHP_SESSION_ACTIVE, $session->status()); |
| 149 | + |
| 150 | + // Get id |
| 151 | + $session_id = $session->getId(); |
| 152 | + |
| 153 | + // Set something to the session |
| 154 | + $session->set('foo', 'bar'); |
| 155 | + $this->assertEquals('bar', $session->get('foo')); |
| 156 | + |
| 157 | + // Close session to initiate write |
| 158 | + session_write_close(); |
| 159 | + |
| 160 | + // Make sure db was filled with something |
| 161 | + $this->assertNotEquals(0, SessionTable::count()); |
| 162 | + $this->assertNotNull(SessionTable::find($session_id)); |
| 163 | + } |
| 164 | +} |
0 commit comments