Skip to content

Commit 2317a47

Browse files
authored
Ensure support for PHP 7.2 to 7.4, add initial unit tests (#1)
* Add GitHub action pipeline to run unit tests * Add base unit tests to ensure minimal compatibility with PHP 7.2, 7.3 and 7.4
1 parent 6f676bd commit 2317a47

File tree

8 files changed

+275
-5
lines changed

8 files changed

+275
-5
lines changed

.github/workflows/ci.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- "master"
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
jobs:
11+
phpunit:
12+
name: Tests PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
13+
runs-on: ubuntu-latest
14+
15+
strategy:
16+
fail-fast: true
17+
matrix:
18+
php: [7.2, 7.3, 7.4]
19+
laravel: ["7.*"]
20+
21+
steps:
22+
- uses: actions/checkout@v2
23+
with:
24+
fetch-depth: 1
25+
26+
- name: Cache dependencies
27+
uses: actions/cache@v1
28+
with:
29+
path: ~/.composer/cache/files
30+
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
31+
32+
- name: Setup PHP
33+
uses: shivammathur/setup-php@v2
34+
with:
35+
php-version: ${{ matrix.php }}
36+
extensions: dom, curl, libxml, mbstring, zip
37+
coverage: none
38+
39+
- name: Install dependencies
40+
run: |
41+
composer require "illuminate/support:${{ matrix.laravel }}" --prefer-dist --no-interaction
42+
43+
- name: Run Testsuite
44+
run: ./vendor/bin/phpunit
45+

.php_cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ $finder = PhpCsFixer\Finder::create()
2424
->in([
2525
__DIR__.'/src',
2626
__DIR__.'/stubs',
27+
__DIR__.'/tests',
2728
]);
2829

2930
return PhpCsFixer\Config::create()

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"require-dev": {
3232
"fzaninotto/faker": "~1.9.1",
3333
"mockery/mockery": "^1.3.1",
34-
"phpunit/phpunit": "^9.0",
34+
"phpunit/phpunit": "^8.5|^9.0",
3535
"orchestra/testbench": "^5.0",
3636
"friendsofphp/php-cs-fixer": "^2.16"
3737
},
@@ -40,6 +40,11 @@
4040
"Oneofftech\\Identities\\": "src/"
4141
}
4242
},
43+
"autoload-dev": {
44+
"psr-4": {
45+
"Tests\\": "tests/"
46+
}
47+
},
4348
"extra": {
4449
"laravel": {
4550
"providers": [

src/Providers/IdentitiesServiceProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Str;
66
use Illuminate\Support\ServiceProvider;
7+
use Oneofftech\Identities\IdentitiesManager;
78
use Oneofftech\Identities\Encryption\Encrypter;
89
use Oneofftech\Identities\View\Components\IdentityLink;
910
use Oneofftech\Identities\Console\Commands\ScaffoldAuthenticationControllers;
@@ -43,6 +44,20 @@ public function register()
4344

4445
return new Encrypter($key, $app_config['cipher']);
4546
});
47+
48+
$this->app->singleton(IdentitiesManager::class, function ($app) {
49+
return new IdentitiesManager($app);
50+
});
51+
}
52+
53+
/**
54+
* Get the services provided by the provider.
55+
*
56+
* @return array
57+
*/
58+
public function provides()
59+
{
60+
return [IdentitiesManager::class];
4661
}
4762

4863
// /**

src/View/Components/IdentityLink.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ public function __construct($provider, $action = 'login', $label = null, $parame
7373
public function render()
7474
{
7575
return <<<'blade'
76-
<a href="{{ route('oneofftech::' . $action . '.provider', array_merge($parameters, ['provider' => $provider])) }}" {{ $attributes }}>
77-
{{ __($label, ['provider' => $provider]) }}
78-
</a>
79-
blade;
76+
<a href="{{ route('oneofftech::' . $action . '.provider', array_merge($parameters, ['provider' => $provider])) }}" {{ $attributes }}>
77+
{{ __($label, ['provider' => $provider]) }}
78+
</a>
79+
blade;
8080
}
8181
}

tests/TestCase.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use ReflectionFunction;
6+
use Illuminate\Events\Dispatcher;
7+
use Laravel\Socialite\SocialiteServiceProvider;
8+
use Orchestra\Testbench\TestCase as BaseTestCase;
9+
use Oneofftech\Identities\Providers\IdentitiesServiceProvider;
10+
11+
abstract class TestCase extends BaseTestCase
12+
{
13+
14+
/**
15+
* Setup the test environment.
16+
*/
17+
public function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
// Your code here
22+
}
23+
24+
/**
25+
* Define environment setup.
26+
*
27+
* - Sqlite in memory database
28+
*
29+
* @param \Illuminate\Foundation\Application $app
30+
* @return void
31+
*/
32+
protected function getEnvironmentSetUp($app)
33+
{
34+
// Setup default database to use sqlite :memory:
35+
$app['config']->set('database.default', 'testing');
36+
$app['config']->set('database.connections.testing', [
37+
'driver' => 'sqlite',
38+
'database' => ':memory:',
39+
'prefix' => '',
40+
]);
41+
$app['config']->set('services.gitlab', [
42+
'client_id' => 'aaa',
43+
'client_secret' => 'bbb',
44+
'redirect' => null,
45+
'instance_uri' => 'https://gitlab.com/'
46+
]);
47+
}
48+
49+
/**
50+
* Loads the service provider during the tests
51+
*/
52+
protected function getPackageProviders($app)
53+
{
54+
return [
55+
SocialiteServiceProvider::class,
56+
\SocialiteProviders\Manager\ServiceProvider::class,
57+
IdentitiesServiceProvider::class
58+
];
59+
}
60+
61+
public function assertListenerIsAttachedToEvent($listener, $event)
62+
{
63+
$dispatcher = app(Dispatcher::class);
64+
65+
foreach ($dispatcher->getListeners(is_object($event) ? get_class($event) : $event) as $listenerClosure) {
66+
$reflection = new ReflectionFunction($listenerClosure);
67+
$listenerClass = $reflection->getStaticVariables()['listener'];
68+
69+
if ($listenerClass === $listener) {
70+
$this->assertTrue(true);
71+
72+
return;
73+
}
74+
}
75+
76+
$this->assertTrue(false, sprintf('Event %s does not have the %s listener attached to it', $event, $listener));
77+
}
78+
}

tests/Unit/IdentityLinkTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Tests\TestCase;
6+
use Oneofftech\Identities\Facades\Identity;
7+
use Illuminate\Foundation\Testing\DatabaseMigrations;
8+
use InvalidArgumentException;
9+
use Illuminate\Support\Facades\View;
10+
use Illuminate\View\Component;
11+
use Oneofftech\Identities\View\Components\IdentityLink;
12+
13+
class IdentityLinkTest extends TestCase
14+
{
15+
use DatabaseMigrations;
16+
17+
public function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
Identity::routes();
22+
}
23+
24+
public function test_unsupported_action_throws()
25+
{
26+
$this->expectException(InvalidArgumentException::class);
27+
$this->expectExceptionMessage("Specified action [dance] is not supported.");
28+
29+
new IdentityLink('gitlab', 'dance');
30+
}
31+
32+
public function test_component_uses_default_label()
33+
{
34+
$component = new IdentityLink('gitlab', 'register');
35+
36+
$this->assertEquals('gitlab', $component->provider);
37+
$this->assertEquals('register', $component->action);
38+
$this->assertEquals('Register via :Provider', $component->label);
39+
$this->assertEquals([], $component->parameters);
40+
}
41+
42+
public function test_component_uses_custom_label()
43+
{
44+
$component = new IdentityLink('gitlab', 'register', 'My label');
45+
46+
$this->assertEquals('gitlab', $component->provider);
47+
$this->assertEquals('register', $component->action);
48+
$this->assertEquals('My label', $component->label);
49+
$this->assertEquals([], $component->parameters);
50+
}
51+
52+
public function test_login_link_rendered()
53+
{
54+
$component = new IdentityLink('gitlab');
55+
56+
$view = $this->render($component);
57+
58+
$this->assertStringContainsString('http://localhost/login-via/gitlab', $view);
59+
$this->assertStringContainsString('Log in via Gitlab', $view);
60+
}
61+
62+
public function test_register_link_rendered()
63+
{
64+
$component = new IdentityLink('gitlab', 'register', 'My label');
65+
66+
$view = $this->render($component);
67+
68+
$this->assertStringContainsString('http://localhost/register-via/gitlab', $view);
69+
$this->assertStringContainsString('My label', $view);
70+
}
71+
72+
private function render(Component $component)
73+
{
74+
return view($component->resolveView(), $component->data())->render();
75+
}
76+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Tests\TestCase;
6+
use Laravel\Socialite\Two\GitlabProvider;
7+
use Oneofftech\Identities\Facades\Identity;
8+
use Oneofftech\Identities\IdentitiesManager;
9+
use SocialiteProviders\Manager\SocialiteWasCalled;
10+
use SocialiteProviders\GitLab\GitLabExtendSocialite;
11+
use Illuminate\Foundation\Testing\DatabaseMigrations;
12+
13+
class IdentityServiceProviderTest extends TestCase
14+
{
15+
use DatabaseMigrations;
16+
17+
public function test_it_can_instantiate_the_gitlab_driver()
18+
{
19+
$factory = $this->app->make(IdentitiesManager::class);
20+
21+
$provider = $factory->driver('gitlab');
22+
23+
$this->assertInstanceOf(GitlabProvider::class, $provider);
24+
}
25+
26+
public function test_routes_are_registered()
27+
{
28+
Identity::routes();
29+
30+
/**
31+
* @var \Illuminate\Routing\Router
32+
*/
33+
$router = tap($this->app->make('router'), function ($r) {
34+
// refresh the route name cache
35+
$r->getRoutes()->refreshNameLookups();
36+
});
37+
38+
$this->assertTrue($router->has('oneofftech::login.provider'));
39+
$this->assertTrue($router->has('oneofftech::login.callback'));
40+
$this->assertTrue($router->has('oneofftech::register.provider'));
41+
$this->assertTrue($router->has('oneofftech::register.callback'));
42+
}
43+
44+
public function test_events_are_registered()
45+
{
46+
Identity::events();
47+
48+
$this->assertListenerIsAttachedToEvent(GitLabExtendSocialite::class, SocialiteWasCalled::class);
49+
}
50+
}

0 commit comments

Comments
 (0)