diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 67522e6..f5c018f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -21,7 +21,7 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} - extensions: json, zip, dom, curl, libxml, mbstring + extensions: json, zip, dom, curl, libxml, mbstring, PDO_PGSQL tools: composer:v2 coverage: xdebug diff --git a/alchemy.yml b/alchemy.yml index aa85f3c..8f6f49a 100644 --- a/alchemy.yml +++ b/alchemy.yml @@ -25,7 +25,7 @@ actions: - macos-latest - windows-latest php: - extensions: json, zip, dom, curl, libxml, mbstring + extensions: json, zip, dom, curl, libxml, mbstring, PDO_PGSQL versions: - '8.3' - '8.2' diff --git a/tests/login.test.php b/tests/login.test.php index ddd7d9b..954826a 100644 --- a/tests/login.test.php +++ b/tests/login.test.php @@ -21,26 +21,38 @@ dbInstance()->delete('users')->execute(); }); -test('user can login', function (array $testUser) { +test('user can login', function () { $auth = authInstance(); + $testUser = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $success = $auth->login($testUser); expect($success)->toBeTrue(); expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); expect($auth->user()->username)->toBe($testUser['username']); -})->with('test-user'); +}); -test('login generates tokens on success', function (array $testUser) { +test('login generates tokens on success', function () { $auth = authInstance(); + $testUser = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $success = $auth->login($testUser); expect($success)->toBeTrue(); expect($auth->data())->not()->toBeNull(); expect($auth->data()->accessToken)->toBeString(); expect($auth->data()->refreshToken)->toBeString(); -})->with('test-user'); +}); test('login fails with incorrect password', function () { $auth = authInstance(); diff --git a/tests/register.test.php b/tests/register.test.php index 8457031..59b712a 100644 --- a/tests/register.test.php +++ b/tests/register.test.php @@ -9,9 +9,15 @@ dbInstance()->delete('users')->execute(); }); -test('user can register an account', function (array $userData) { +test('user can register an account', function () { $auth = authInstance(); + $userData = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $success = $auth->register($userData); if (!$success) { @@ -21,11 +27,17 @@ expect($success)->toBeTrue(); expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); expect($auth->user()->username)->toBe($userData['username']); -})->with('test-user'); +}); -test('user can login after registering', function (array $userData) { +test('user can login after registering', function () { $auth = authInstance(); + $userData = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $registerSuccess = $auth->register($userData); expect($registerSuccess)->toBeTrue(); @@ -35,11 +47,17 @@ expect($loginSuccess)->toBeTrue(); expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); expect($auth->user()->username)->toBe($userData['username']); -})->with('test-user'); +}); -test('user can only sign up once', function (array $userData) { +test('user can only sign up once', function () { $auth = authInstance(); + $userData = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $auth->config([ 'unique' => ['email', 'username'] ]); @@ -56,11 +74,17 @@ 'email' => 'email already exists', 'username' => 'username already exists', ]); -})->with('test-user'); +}); -test('register passwords are encrypted', function (array $userData) { +test('register passwords are encrypted', function () { $auth = authInstance(); + $userData = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $auth->config([ 'hidden' => [] ]); @@ -70,4 +94,4 @@ expect($registerSuccess)->toBeTrue(); expect($auth->user()->password)->not()->toBe($userData['password']); expect(password_verify($userData['password'], $auth->user()->password))->toBeTrue(); -})->with('test-user'); +}); diff --git a/tests/session.test.php b/tests/session.test.php index 3f699eb..2fb5022 100644 --- a/tests/session.test.php +++ b/tests/session.test.php @@ -16,10 +16,16 @@ dbInstance()->delete('users')->execute(); }); -test('register should create a new session when session => true', function (array $userData) { +test('register should create a new session when session => true', function () { $auth = authInstance(); $auth->config(['session' => true]); + $userData = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $success = $auth->register($userData); expect($success)->toBeTrue(); @@ -28,9 +34,9 @@ expect(session_status())->toBe(PHP_SESSION_ACTIVE); expect($_SESSION['auth']['user']['username'] ?? null)->toBe($userData['username']); -})->with('test-user'); +}); -test('register should not create a new session when session => false', function (array $userData) { +test('register should not create a new session when session => false', function () { $auth = authInstance(); $auth->config(['session' => false]); @@ -47,12 +53,18 @@ expect(session_status())->toBe(PHP_SESSION_NONE); expect($_SESSION['auth']['user']['username'] ?? null)->toBeNull(); -})->with('test-user'); +}); -test('login should create session when session => true', function (array $userData) { +test('login should create session when session => true', function () { $auth = authInstance(); $auth->config(['session' => true]); + $userData = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $success = $auth->login($userData); expect($success)->toBeTrue(); @@ -61,12 +73,18 @@ expect(session_status())->toBe(PHP_SESSION_ACTIVE); expect($_SESSION['auth']['user']['username'] ?? null)->toBe($userData['username']); -})->with('test-user'); +}); -test('session should create auth.ttl when session.lifetime is not 0', function (array $userData) { +test('session should create auth.ttl when session.lifetime is not 0', function () { $auth = authInstance(); $auth->config(['session' => true, 'session.lifetime' => 2]); + $userData = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $timeBeforeLogin = time(); $success = $auth->login($userData); @@ -79,12 +97,18 @@ expect($_SESSION['auth']['user']['username'] ?? null)->toBe($userData['username']); expect($_SESSION['auth']['ttl'])->toBeGreaterThan($timeBeforeLogin); -})->with('test-user'); +}); -test('session should not create auth.ttl when session.lifetime is 0', function (array $userData) { +test('session should not create auth.ttl when session.lifetime is 0', function () { $auth = authInstance(); $auth->config(['session' => true, 'session.lifetime' => 0]); + $userData = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $success = $auth->login($userData); expect($success)->toBeTrue(); @@ -95,12 +119,18 @@ expect($_SESSION['auth']['user']['username'] ?? null)->toBe($userData['username']); expect($_SESSION['auth']['ttl'] ?? null)->toBeNull(); -})->with('test-user'); +}); -test('session should expire after session.lifetime', function (array $userData) { +test('session should expire after session.lifetime', function () { $auth = authInstance(); $auth->config(['session' => true, 'session.lifetime' => 2]); + $userData = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $success = $auth->login($userData); expect($success)->toBeTrue(); @@ -114,9 +144,9 @@ expect($auth->id())->toBeNull(); expect($auth->user())->toBeNull(); -})->with('test-user'); +}); -test('login should regenerate session id when session => true and session is already active', function (array $userData) { +test('login should regenerate session id when session => true and session is already active', function () { $auth = authInstance(); $auth->config(['session' => true]); @@ -124,6 +154,12 @@ $sessionId = session_id(); + $userData = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $success = $auth->login($userData); $newSessionId = session_id(); @@ -136,12 +172,18 @@ expect($_SESSION['auth']['user']['username'] ?? null)->toBe($userData['username']); expect($newSessionId)->not()->toBe($sessionId); -})->with('test-user'); +}); -test('logout should remove auth info from session when session => true', function (array $userData) { +test('logout should remove auth info from session when session => true', function () { $auth = authInstance(); $auth->config(['session' => true]); + $userData = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $success = $auth->login($userData); expect($success)->toBeTrue(); @@ -152,4 +194,4 @@ expect($auth->user())->toBeNull(); expect($_SESSION['auth']['user']['username'] ?? null)->toBeNull(); -})->with('test-user'); +}); diff --git a/tests/table.test.php b/tests/table.test.php index 35e7e05..ff235cd 100644 --- a/tests/table.test.php +++ b/tests/table.test.php @@ -11,37 +11,49 @@ dbInstance()->delete('myusers')->execute(); }); -test('register should save user in user defined table', function (array $testUser) { +test('register should save user in user defined table', function () { $auth = authInstance(); $auth->config(['session' => false, 'db.table' => 'myusers']); - $success = $auth->register($testUser); + $success = $auth->register([ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]); if (!$success) { $this->fail(json_encode($auth->errors())); } expect($auth->user()->username)->toBe('test-user'); -})->with('test-user'); +}); -test('login should work with user defined table', function (array $testUser) { +test('login should work with user defined table', function () { $auth = authInstance(); $auth->config(['session' => false, 'db.table' => 'myusers']); - $success = $auth->login($testUser); + $success = $auth->login([ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]); if (!$success) { $this->fail(json_encode($auth->errors())); } expect($auth->user()->username)->toBe('test-user'); -})->with('test-user'); +}); -test('update should work with user defined table', function (array $testUser) { +test('update should work with user defined table', function () { $auth = authInstance(); $auth->config(['session' => true, 'db.table' => 'myusers', 'session.lifetime' => '1 day']); - $success = $auth->login($testUser); + $success = $auth->login([ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]); if (!$success) { $this->fail(json_encode($auth->errors())); @@ -58,7 +70,7 @@ expect($response['user']['username'])->toBe('test-user55'); expect($response['user']['email'])->toBe('test-user55@example.com'); -})->with('test-user')->skip(); +})->skip(); test('user table can use uuid as id', function () { createUsersTable('uuid_users', true); diff --git a/tests/user.test.php b/tests/user.test.php index 71eab7c..4086518 100644 --- a/tests/user.test.php +++ b/tests/user.test.php @@ -22,10 +22,16 @@ dbInstance()->delete('users')->execute(); }); -test('auth user is instance of Leaf\Auth\User', function (array $testUser) { +test('auth user is instance of Leaf\Auth\User', function () { $auth = authInstance(); $auth->config(['db.table' => 'users']); + $testUser = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $success = $auth->login($testUser); if (!$success) { @@ -35,12 +41,18 @@ expect($success)->toBeTrue(); expect($auth->user())->toBeInstanceOf(\Leaf\Auth\User::class); expect($auth->user()->username)->toBe($testUser['username']); -})->with('test-user'); +}); -test('logout can use logout callback to run custom action', function (array $testUser) { +test('logout can use logout callback to run custom action', function () { $auth = authInstance(); $auth->config(['db.table' => 'users']); + $testUser = [ + 'username' => 'test-user', + 'email' => 'test-user@example.com', + 'password' => 'password' + ]; + $success = $auth->login($testUser); if (!$success) { @@ -56,4 +68,4 @@ }); expect($auth->user())->toBeNull(); -})->with('test-user'); +});