Skip to content

Add initial unit tests and ci pipeline #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI

on:
push:
branches:
- "master"
pull_request:
types: [opened, synchronize, reopened]

jobs:
phpunit:
name: Tests PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php: [7.2, 7.3, 7.4]
laravel: ["7.*"]

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 1

- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip
coverage: none

- name: Install dependencies
run: |
composer require "illuminate/support:${{ matrix.laravel }}" --prefer-dist --no-interaction

- name: Run Testsuite
run: ./vendor/bin/phpunit

1 change: 1 addition & 0 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ $finder = PhpCsFixer\Finder::create()
->in([
__DIR__.'/src',
__DIR__.'/stubs',
__DIR__.'/tests',
]);

return PhpCsFixer\Config::create()
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"require-dev": {
"fzaninotto/faker": "~1.9.1",
"mockery/mockery": "^1.3.1",
"phpunit/phpunit": "^9.0",
"phpunit/phpunit": "^8.5|^9.0",
"orchestra/testbench": "^5.0",
"friendsofphp/php-cs-fixer": "^2.16"
},
Expand All @@ -40,6 +40,11 @@
"Oneofftech\\Identities\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
Expand Down
15 changes: 15 additions & 0 deletions src/Providers/IdentitiesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Str;
use Illuminate\Support\ServiceProvider;
use Oneofftech\Identities\IdentitiesManager;
use Oneofftech\Identities\Encryption\Encrypter;
use Oneofftech\Identities\View\Components\IdentityLink;
use Oneofftech\Identities\Console\Commands\ScaffoldAuthenticationControllers;
Expand Down Expand Up @@ -43,6 +44,20 @@ public function register()

return new Encrypter($key, $app_config['cipher']);
});

$this->app->singleton(IdentitiesManager::class, function ($app) {
return new IdentitiesManager($app);
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [IdentitiesManager::class];
}

// /**
Expand Down
8 changes: 4 additions & 4 deletions src/View/Components/IdentityLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public function __construct($provider, $action = 'login', $label = null, $parame
public function render()
{
return <<<'blade'
<a href="{{ route('oneofftech::' . $action . '.provider', array_merge($parameters, ['provider' => $provider])) }}" {{ $attributes }}>
{{ __($label, ['provider' => $provider]) }}
</a>
blade;
<a href="{{ route('oneofftech::' . $action . '.provider', array_merge($parameters, ['provider' => $provider])) }}" {{ $attributes }}>
{{ __($label, ['provider' => $provider]) }}
</a>
blade;
}
}
78 changes: 78 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Tests;

use ReflectionFunction;
use Illuminate\Events\Dispatcher;
use Laravel\Socialite\SocialiteServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Oneofftech\Identities\Providers\IdentitiesServiceProvider;

abstract class TestCase extends BaseTestCase
{

/**
* Setup the test environment.
*/
public function setUp(): void
{
parent::setUp();

// Your code here
}

/**
* Define environment setup.
*
* - Sqlite in memory database
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'testing');
$app['config']->set('database.connections.testing', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
$app['config']->set('services.gitlab', [
'client_id' => 'aaa',
'client_secret' => 'bbb',
'redirect' => null,
'instance_uri' => 'https://gitlab.com/'
]);
}

/**
* Loads the service provider during the tests
*/
protected function getPackageProviders($app)
{
return [
SocialiteServiceProvider::class,
\SocialiteProviders\Manager\ServiceProvider::class,
IdentitiesServiceProvider::class
];
}

public function assertListenerIsAttachedToEvent($listener, $event)
{
$dispatcher = app(Dispatcher::class);

foreach ($dispatcher->getListeners(is_object($event) ? get_class($event) : $event) as $listenerClosure) {
$reflection = new ReflectionFunction($listenerClosure);
$listenerClass = $reflection->getStaticVariables()['listener'];

if ($listenerClass === $listener) {
$this->assertTrue(true);

return;
}
}

$this->assertTrue(false, sprintf('Event %s does not have the %s listener attached to it', $event, $listener));
}
}
76 changes: 76 additions & 0 deletions tests/Unit/IdentityLinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;
use Oneofftech\Identities\Facades\Identity;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use InvalidArgumentException;
use Illuminate\Support\Facades\View;
use Illuminate\View\Component;
use Oneofftech\Identities\View\Components\IdentityLink;

class IdentityLinkTest extends TestCase
{
use DatabaseMigrations;

public function setUp(): void
{
parent::setUp();

Identity::routes();
}

public function test_unsupported_action_throws()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Specified action [dance] is not supported.");

new IdentityLink('gitlab', 'dance');
}

public function test_component_uses_default_label()
{
$component = new IdentityLink('gitlab', 'register');

$this->assertEquals('gitlab', $component->provider);
$this->assertEquals('register', $component->action);
$this->assertEquals('Register via :Provider', $component->label);
$this->assertEquals([], $component->parameters);
}

public function test_component_uses_custom_label()
{
$component = new IdentityLink('gitlab', 'register', 'My label');

$this->assertEquals('gitlab', $component->provider);
$this->assertEquals('register', $component->action);
$this->assertEquals('My label', $component->label);
$this->assertEquals([], $component->parameters);
}

public function test_login_link_rendered()
{
$component = new IdentityLink('gitlab');

$view = $this->render($component);

$this->assertStringContainsString('http://localhost/login-via/gitlab', $view);
$this->assertStringContainsString('Log in via Gitlab', $view);
}

public function test_register_link_rendered()
{
$component = new IdentityLink('gitlab', 'register', 'My label');

$view = $this->render($component);

$this->assertStringContainsString('http://localhost/register-via/gitlab', $view);
$this->assertStringContainsString('My label', $view);
}

private function render(Component $component)
{
return view($component->resolveView(), $component->data())->render();
}
}
50 changes: 50 additions & 0 deletions tests/Unit/IdentityServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;
use Laravel\Socialite\Two\GitlabProvider;
use Oneofftech\Identities\Facades\Identity;
use Oneofftech\Identities\IdentitiesManager;
use SocialiteProviders\Manager\SocialiteWasCalled;
use SocialiteProviders\GitLab\GitLabExtendSocialite;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class IdentityServiceProviderTest extends TestCase
{
use DatabaseMigrations;

public function test_it_can_instantiate_the_gitlab_driver()
{
$factory = $this->app->make(IdentitiesManager::class);

$provider = $factory->driver('gitlab');

$this->assertInstanceOf(GitlabProvider::class, $provider);
}

public function test_routes_are_registered()
{
Identity::routes();

/**
* @var \Illuminate\Routing\Router
*/
$router = tap($this->app->make('router'), function ($r) {
// refresh the route name cache
$r->getRoutes()->refreshNameLookups();
});

$this->assertTrue($router->has('oneofftech::login.provider'));
$this->assertTrue($router->has('oneofftech::login.callback'));
$this->assertTrue($router->has('oneofftech::register.provider'));
$this->assertTrue($router->has('oneofftech::register.callback'));
}

public function test_events_are_registered()
{
Identity::events();

$this->assertListenerIsAttachedToEvent(GitLabExtendSocialite::class, SocialiteWasCalled::class);
}
}