row = $row ?: array(); } function Id() { return $this->row[\'Id\'] ?? null; } function Password() { return $this->row[\'Password\'] ?? null; } function Username() { return $this->row[\'Username\'] ?? null; } }'); eval($method); // (1) Password unchanged -> the session's user comes back. $_SESSION = array('username' => 'bob', 'passwordHash' => 'h1'); $dbRow = array('Id' => 5, 'Username' => 'bob', 'Password' => 'h1'); $u = userFromSession(); check('session with the matching password gets its user', $u !== null && $u->Id() === 5, true); // (2) Password was reset -> the session ends. $_SESSION = array('username' => 'bob', 'passwordHash' => 'h1'); $dbRow = array('Id' => 5, 'Username' => 'bob', 'Password' => 'h2'); $u = userFromSession(); check('session whose password changed returns null', $u, null); // (3) Old session without passwordHash (or remote auth) -> user still loads. $_SESSION = array('username' => 'bob'); $dbRow = array('Id' => 5, 'Username' => 'bob', 'Password' => 'h2'); $u = userFromSession(); check('session without passwordHash gets its user', $u !== null && $u->Id() === 5, true); // (4) The user row is gone (missing or disabled) -> no user, and no throw. $_SESSION = array('username' => 'bob', 'passwordHash' => 'h1'); $dbRow = false; $threw = false; try { $u = userFromSession(); } catch (Throwable $e) { $threw = true; $u = null; } check('missing user row does not throw', $threw, false); check('missing user row returns no user', $u === null || $u->Id() === null, true); // (5) No session username -> no user. $_SESSION = array(); $dbRow = array('Id' => 5, 'Username' => 'bob', 'Password' => 'h1'); check('session without a username returns null', userFromSession(), null); echo $failures ? "$failures failed\n" : "all passed\n"; exit($failures ? 1 : 0);