| 1 | <?php |
| 2 | |
| 3 | namespace Liuch\DmarcSrg; |
| 4 | |
| 5 | use Liuch\DmarcSrg\Exception\LogicException; |
| 6 | |
| 7 | class SessionTest extends \PHPUnit\Framework\TestCase |
| 8 | { |
| 9 | private $sess = null; |
| 10 | |
| 11 | public function setUp(): void |
| 12 | { |
| 13 | $_SERVER['REQUEST_URI'] = '/'; |
| 14 | \session_start(); |
| 15 | \session_unset(); |
| 16 | \session_destroy(); |
| 17 | \session_write_close(); |
| 18 | $this->sess = new Session(); |
| 19 | } |
| 20 | |
| 21 | public function tearDown(): void |
| 22 | { |
| 23 | $this->sess->destroy(); |
| 24 | } |
| 25 | |
| 26 | public function testGetDataWhenSessionDoesNotExist(): void |
| 27 | { |
| 28 | $this->assertSame([], $this->sess->getData()); |
| 29 | } |
| 30 | |
| 31 | public function testSetAndGetDataWhenSessionDoesNotExist(): void |
| 32 | { |
| 33 | $this->sess->setData([ 'user' => 1 ]); |
| 34 | $this->assertSame([ 'user' => 1 ], $this->sess->getData()); |
| 35 | } |
| 36 | |
| 37 | public function testSetAndGetDataWhenSessionExistsButNotStarted(): void |
| 38 | { |
| 39 | $this->sess->setData([]); |
| 40 | $this->sess->commit(); |
| 41 | $this->sess->setData([ 'user' => 1 ]); |
| 42 | $this->assertSame([ 'user' => 1 ], $this->sess->getData()); |
| 43 | } |
| 44 | |
| 45 | public function testSetAndGetDataWhenSessionStarted(): void |
| 46 | { |
| 47 | $this->sess->getData(); |
| 48 | $this->sess->setData([ 'user' => 1 ]); |
| 49 | $this->assertSame([ 'user' => 1 ], $this->sess->getData()); |
| 50 | } |
| 51 | |
| 52 | public function testCommitWhenSessionIsNotStarted(): void |
| 53 | { |
| 54 | $this->sess->setData([ 'user' => 1 ]); |
| 55 | $this->sess->commit(); |
| 56 | $this->sess->commit(); |
| 57 | $this->assertSame([ 'user' => 1 ], $this->sess->getData()); |
| 58 | } |
| 59 | |
| 60 | public function testCommitWhenSessionIsStarted(): void |
| 61 | { |
| 62 | $this->sess->setData([ 'user' => 1 ]); |
| 63 | $this->sess->commit(); |
| 64 | $this->assertSame([ 'user' => 1 ], $this->sess->getData()); |
| 65 | } |
| 66 | |
| 67 | public function testCloseWhenSessionIsNotStarted(): void |
| 68 | { |
| 69 | $this->sess->setData([ 'user' => 1 ]); |
| 70 | $this->sess->commit(); |
| 71 | $this->sess->close(); |
| 72 | $this->assertSame([ 'user' => 1 ], $this->sess->getData()); |
| 73 | } |
| 74 | |
| 75 | public function testCloseWhenSessionIsStarted(): void |
| 76 | { |
| 77 | $this->sess->setData([ 'user' => 1 ]); |
| 78 | $this->sess->commit(); |
| 79 | $this->sess->setData([ 'user' => 2 ]); |
| 80 | $this->sess->close(); |
| 81 | $this->assertSame([ 'user' => 1 ], $this->sess->getData()); |
| 82 | } |
| 83 | |
| 84 | public function testDestroyWhenSessionIsNotStarted(): void |
| 85 | { |
| 86 | $this->sess->setData([ 'user' => 1 ]); |
| 87 | $id = \session_id(); |
| 88 | $this->sess->commit(); |
| 89 | $this->sess->destroy(); |
| 90 | $this->assertSame([], $this->sess->getData()); |
| 91 | $this->assertNotSame($id, \session_id()); |
| 92 | } |
| 93 | |
| 94 | public function testDestroyWhenSessionIsStarted(): void |
| 95 | { |
| 96 | $this->sess->setData([ 'user' => 1 ]); |
| 97 | $id = \session_id(); |
| 98 | $this->sess->commit(); |
| 99 | $this->sess->getData(); |
| 100 | $this->sess->destroy(); |
| 101 | $this->assertSame([], $this->sess->getData()); |
| 102 | $this->assertNotSame($id, \session_id()); |
| 103 | } |
| 104 | |
| 105 | public function testStrictModeActivated(): void |
| 106 | { |
| 107 | $fake_id = 'myfakesessionid00000000000000000'; |
| 108 | \session_id($fake_id); |
| 109 | $this->sess->getData(); |
| 110 | $this->assertNotSame($fake_id, \session_id()); |
| 111 | } |
| 112 | } |