Skip to content

Commit

Permalink
Adding tests for Oauth Provider authenticaion
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewatts committed Jul 14, 2022
1 parent 8d4f411 commit 3b75930
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 207 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
},
"require-dev": {
"pestphp/pest": "^1.21",
"maximebf/debugbar": "^1.18"
"maximebf/debugbar": "^1.18",
"pestphp/pest-plugin-mock": "^1.0"
}
}
198 changes: 197 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions db/seeds/UserSeeder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php


use Phinx\Seed\AbstractSeed;

class UserSeeder extends AbstractSeed
Expand Down
9 changes: 0 additions & 9 deletions src/Support/Collection.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Support/functions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php declare(strict_types=1);

use Noesis\Support\Collection;
use Noesis\Support\User;
use Noesis\Support\Session;

Expand All @@ -25,10 +24,3 @@ function env(string $key, mixed $default = null)
? $default : $_ENV[$key];
}
}

if (!function_exists('collect')) {
function collect($items)
{
return new Collection($items);
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Auth/Adapter/GithubAuthenticationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

use Illuminate\Database\Eloquent\Collection;
use Laminas\Authentication\Result;
use Mockery\Mock;
use Noesis\Auth\Adapter\GithubAuthentication;

test('GithubAuthentication::validate', function () {
/** @var GithubAuthentication|Mock $githubAuthentication */
$githubAuthentication = mock(GithubAuthentication::class)->makePartial();
$githubAuthentication->shouldReceive( 'getUsersWhere')->andReturn(new Collection(['testuser']));

$githubAuthentication->setEmail('[email protected]');
expect($githubAuthentication->authenticate() instanceof Result)->toBeTrue();
expect($githubAuthentication->authenticate()->isValid())->toBeTrue();
});
16 changes: 16 additions & 0 deletions tests/Unit/Auth/Adapter/LinkedinAuthenticationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

use Illuminate\Database\Eloquent\Collection;
use Laminas\Authentication\Result;
use Mockery\Mock;
use Noesis\Auth\Adapter\LinkedinAuthentication;

test('LinkedinAuthentication::validate', function () {
/** @var LinkedinAuthentication|Mock $linkedinAuthentication */
$linkedinAuthentication = mock(LinkedinAuthentication::class)->makePartial();
$linkedinAuthentication->shouldReceive( 'getUsersWhere')->andReturn(new Collection(['testuser']));

$linkedinAuthentication->setEmail('[email protected]');
expect($linkedinAuthentication->authenticate() instanceof Result)->toBeTrue();
expect($linkedinAuthentication->authenticate()->isValid())->toBeTrue();
});
16 changes: 16 additions & 0 deletions tests/Unit/Auth/Adapter/TwitterAuthenticationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

use Illuminate\Database\Eloquent\Collection;
use Laminas\Authentication\Result;
use Mockery\Mock;
use Noesis\Auth\Adapter\TwitterAuthentication;

test('TwitterAuthentication::validate', function () {
/** @var TwitterAuthentication|Mock $twitterAuth */
$twitterAuth = mock(TwitterAuthentication::class)->makePartial();
$twitterAuth->shouldReceive( 'getUsersWhere')->andReturn(new Collection(['testuser']));

$twitterAuth->setUsername('testuser');
expect($twitterAuth->authenticate() instanceof Result)->toBeTrue();
expect($twitterAuth->authenticate()->isValid())->toBeTrue();
});
5 changes: 0 additions & 5 deletions tests/Unit/ExampleTest.php

This file was deleted.

19 changes: 19 additions & 0 deletions tests/Unit/Support/SessionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

use Noesis\Support\Session;
use Psr\Http\Message\ServerRequestInterface;
use SlimSession\Helper;

test('Session calls Request::getAttribute with string "session"', function () {
$request = mock(ServerRequestInterface::class)
->expect(
getAttribute: function ($attribute) {
// We only return a Helper instance if 'session' is passed
return ($attribute === 'session') ? new Helper : false;
}
);

$session = Session::from($request);

expect($session instanceof Helper)->toBeTrue();
});
Loading

0 comments on commit 3b75930

Please sign in to comment.