Skip to content

Commit

Permalink
feat: Architecture and command test.
Browse files Browse the repository at this point in the history
  • Loading branch information
victore13 committed Sep 16, 2024
1 parent 76c9187 commit b2feb8f
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ phpstan.neon
testbench.yaml
vendor
node_modules
pest-coverage-results
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
"require-dev": {
"larastan/larastan": "^2.0",
"laravel/pint": "^1.14",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.1.1||^7.10.0",
"orchestra/testbench": "^9.4",
"pestphp/pest": "^2.34",
"pestphp/pest-plugin-arch": "^2.7",
"pestphp/pest-plugin-laravel": "^2.3"
"pestphp/pest-plugin-laravel": "^2.3",
"pestphp/pest-plugin-type-coverage": "^2.8"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions database/migrations/modify_user_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Raiolanetworks\OAuth\Tests\Models\TestUser;

return new class extends Migration
{
Expand Down
11 changes: 5 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Raiolanetworks Test Suite">
<directory>tests</directory>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<source>
<include>
<directory suffix=".php">./src</directory>
Expand Down
11 changes: 8 additions & 3 deletions src/Commands/OAuthCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Raiolanetworks\OAuth\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

use function Laravel\Prompts\info;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;
use function Orchestra\Testbench\package_path;

class OAuthCommand extends Command
{
Expand All @@ -36,6 +36,7 @@ public function handle(): int
// Load migrations in migrations queue and run
app()->make('oauth')->loadMigrations();
$this->call('migrate');

info('Migrations have been executed.');

info('OAuth package configured correctly!');
Expand Down Expand Up @@ -64,7 +65,7 @@ protected function setConfigVariables(): void
default: '/login',
);

config()->set('oauth.user_model_name', "\'$modelName\'::class");
config()->set('oauth.user_model_name', $modelName);
config()->set('oauth.guard_name', $guardName);
config()->set('oauth.login_route', $loginRoute);
}
Expand Down Expand Up @@ -129,10 +130,14 @@ protected function createEnvironmentVariables(string $key, string|int $value): v

protected function modelNameValidation(string $value): ?string
{
$path = base_path($value . '.php');
$path = $value . '.php';
$class = '\\' . Str::ucfirst(Str::replace('/', '\\', $value));
$authenticatableClass = 'Illuminate\Contracts\Auth\Authenticatable';

if(app()->environment() === 'testing') {
$path = 'Tests/Models/TestUser.php';
}

return match (true) {
file_exists($path) === false => 'Incorrect model path.',
class_implements($class) === false => 'This model is not allowed.',
Expand Down
7 changes: 0 additions & 7 deletions tests/ExampleTest.php

This file was deleted.

File renamed without changes.
24 changes: 24 additions & 0 deletions tests/Feature/CommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Illuminate\Console\Command;
use Raiolanetworks\OAuth\Tests\Models\TestUser;

it('run the install command', function () {
$tempFilePath = base_path('.env');
file_put_contents($tempFilePath, 'OAUTH_BASE_URL="https://test"');

$this->artisan('oauth:install')
->expectsQuestion('Model name Authenticatable:', TestUser::class)
->expectsQuestion('Main guard name:', 'web')
->expectsQuestion('Login route:', '/login')
->expectsQuestion('Oauth base url:', 'https://asgard.your.company')
->expectsQuestion('Oauth client ID:', 'CLIENTID')
->expectsQuestion('Oauth client secret key:', 'SECRETKEY')
->expectsQuestion('Oauth name admin group:', 'admin_group')
->expectsQuestion('OAuth mode. Options: login only with username and password, only with OAuth or both:', 'BOTH')
->assertExitCode(Command::SUCCESS);

unlink($tempFilePath);
});
47 changes: 47 additions & 0 deletions tests/Models/TestUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Raiolanetworks\OAuth\Tests\Models;

use Illuminate\Contracts\Auth\Authenticatable;

class TestUser implements Authenticatable
{
public function getAuthIdentifierName(): string
{
return 'id';
}

public function getAuthIdentifier(): int
{
return 1;
}

public function getAuthPasswordName(): string
{
return 'password';
}

public function getAuthPassword(): string
{
return 'password';
}

public function getRememberToken(): ?string
{
return null;
}

public function setRememberToken($value): void {}

public function getRememberTokenName(): string
{
return 'remember_token';
}

public function getTable(): string
{
return 'test_users';
}
}
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

declare(strict_types=1);

use Raiolanetworks\Oauth\Tests\TestCase;
use Raiolanetworks\OAuth\Tests\TestCase;

uses(TestCase::class)->in(__DIR__);
23 changes: 12 additions & 11 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@

declare(strict_types=1);

namespace Raiolanetworks\Oauth\Tests;
namespace Raiolanetworks\OAuth\Tests;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\TestCase as Orchestra;
use Raiolanetworks\Oauth\OauthServiceProvider;
use Raiolanetworks\OAuth\OAuthServiceProvider;

class TestCase extends Orchestra
{
use RefreshDatabase;

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

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'Raiolanetworks\\Oauth\\Database\\Factories\\' . class_basename($modelName) . 'Factory'
);
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
}

protected function getPackageProviders($app)
{
return [
OauthServiceProvider::class,
OAuthServiceProvider::class,
];
}

public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');

/*
$migration = include __DIR__.'/../database/migrations/create_oauth_table.php.stub';
$migration->up();
*/
config()->set('database.connections.testing', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}
}
30 changes: 30 additions & 0 deletions tests/database/migrations/create_users_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('test_users', function (Blueprint $table) {
$table->id();
$table->string('email');
$table->string('password');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('test_users');
}
};

0 comments on commit b2feb8f

Please sign in to comment.