Skip to content

Commit

Permalink
Merge pull request #4 from illegalstudio/testing-first-step
Browse files Browse the repository at this point in the history
Testing the package
  • Loading branch information
nahime0 authored Apr 25, 2023
2 parents ed4fb02 + e699f8f commit 1f9a907
Show file tree
Hide file tree
Showing 42 changed files with 2,107 additions and 159 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Test

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
package-tests:

runs-on: ubuntu-latest

steps:
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '8.1'
- uses: actions/checkout@v3
- name: Install Dependencies
run: composer update && composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist && composer dump-autoload
- name: Execute tests (Unit and Feature tests) via PEST
run: vendor/bin/pest
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
composer.lock
node_modules/
vendor/
composer.lock
18 changes: 10 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@
"require-dev": {
"fakerphp/faker": "^1.9.1",
"laravel/pint": "^1.0",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^6.4",
"pestphp/pest": "^1.22",
"pestphp/pest-plugin-laravel": "^1.4",
"spatie/laravel-ignition": "^1.0",
"phpunit/phpunit": "^9.6",
"pestphp/pest-plugin-mock": "^1.0",
"orchestra/testbench": "^7.22"
"mockery/mockery": "^1.5",
"nunomaduro/collision": "^7.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
"spatie/laravel-ignition": "^2.0",
"phpunit/phpunit": "^10.0",
"pestphp/pest-plugin-mock": "^2.0",
"orchestra/testbench": "^8.0",
"spatie/laravel-ray": "^1.32",
"nunomaduro/larastan": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
18 changes: 18 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
includes:
- ./vendor/nunomaduro/larastan/extension.neon

parameters:

paths:
- src/

# Level 9 is the highest level
level: 5

# ignoreErrors:
# - '#PHPDoc tag @var#'
#
# excludePaths:
# - ./*/*/FileToBeExcluded.php
#
# checkMissingIterableValueType: false
19 changes: 19 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<coverage/>
<php>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
<env name="DB_CONNECTION" value="testing"/>
</php>
<source>
<include>
<directory>./functions</directory>
<directory>./src</directory>
</include>
</source>
</phpunit>
22 changes: 22 additions & 0 deletions phpunit.xml.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./functions</directory>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<php>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
<env name="DB_CONNECTION" value="testing"/>
</php>
</phpunit>
6 changes: 4 additions & 2 deletions src/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ class Authenticator
*
* @param string $name The name of the auth
*/
public function __construct(private readonly string $name)
public function __construct(private readonly string $name, Collection $parameters = null)
{
$this->parameters = Collection::make([
$parameters = $parameters ?? new Collection();

$this->parameters = $parameters->merge([
'enabled' => true,
'registration_enabled' => true,
'forgot_password_enabled' => true,
Expand Down
46 changes: 38 additions & 8 deletions src/Registrators/Registrator.php → src/Builder.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

namespace Illegal\InsideAuth\Registrators;
namespace Illegal\InsideAuth;

use Exception;
use Illegal\InsideAuth\Authenticator;
use Illegal\InsideAuth\Contracts\RegistratorInterface;
use Illegal\InsideAuth\Contracts\AbstractRegistrator;
use Illegal\InsideAuth\Registrators\MiddlewareRegistrator;
use Illegal\InsideAuth\Registrators\RouteRegistrator;
use Illegal\InsideAuth\Registrators\SecurityRegistrator;
use Illuminate\Foundation\Application;

/**
* This class is the main class of the InsideAuth package.
Expand All @@ -17,7 +20,7 @@
* @see SecurityRegistrator
*
*/
final class Registrator
final class Builder
{
/**
* This variable carries the authenticator for the current request.
Expand All @@ -41,6 +44,13 @@ final class Registrator
*/
private array $authenticators = [];

/**
* @param Application $app The Laravel application instance
*/
public function __construct(private readonly Application $app)
{
}

/**
* Boot the components of the Auth set
* @throws Exception
Expand All @@ -63,10 +73,30 @@ public function boot(string $name = 'auth'): Authenticator
$authenticator = new Authenticator($name);

collect($this->registrators)
->map(function ($registrator, $category) use ($authenticator, $name) {
return new $registrator($authenticator, $category);
})->map(function (RegistratorInterface $registrator) {
$registrator->boot();
->map(function ($registrator) use ($authenticator) {

/**
* Build the registrator.
*
* @var AbstractRegistrator $registrator
*/
$registrator = $this->app->make($registrator);
$registrator->withAuthName($authenticator->name());

/**
* Merge all parameters from the registrator into the authenticator
*/
$authenticator->merge(
$registrator->collectAndMergeParameters()
);

return $registrator;
})->map(function (AbstractRegistrator $registrator) use ($authenticator) {

/**
* Boot the registrator
*/
$registrator->boot($authenticator->parameters);
});

/**
Expand Down
52 changes: 52 additions & 0 deletions src/Contracts/AbstractRegistrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Illegal\InsideAuth\Contracts;

use Illegal\InsideAuth\Authenticator;
use Illuminate\Config\Repository;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;

abstract class AbstractRegistrator
{
/**
* @var string The prefix to be used for the parameters
*/
protected string $prefix = "";

/**
* @var string The authName that the registrators should use.
*/
protected string $authName = "auth";

public function __construct(protected readonly Repository $config, protected readonly Router $router)
{
}

/**
* Magic getter for parameters
*/
public abstract function __get(string $key);

/**
* Setter for the authName
*/
public function withAuthName($authName): static
{
$this->authName = $authName;

return $this;
}

/**
* This function will collect and merge all parameters inside the provided Authenticator
* @see Authenticator::merge()
*/
public abstract function collectAndMergeParameters(): Collection;

/**
* Set of actions to be performed when booting the auth.
* @param Collection $allParameters All the parameters gathered by the authentication system
*/
public abstract function boot(Collection $allParameters): void;
}
25 changes: 0 additions & 25 deletions src/Contracts/RegistratorInterface.php

This file was deleted.

21 changes: 21 additions & 0 deletions src/Factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Illegal\InsideAuth\Factories;

use Illegal\InsideAuth\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

class UserFactory extends Factory
{
protected $model = User::class;

public function definition(): array
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => bcrypt('password'),
];
}
}
4 changes: 2 additions & 2 deletions src/Http/Middleware/InjectIntoApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace Illegal\InsideAuth\Http\Middleware;

use Closure;
use Illegal\InsideAuth\Builder;
use Illegal\InsideAuth\InsideAuth;
use Illegal\InsideAuth\Registrators\Registrator;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* This class will inject the current authenticator into:
* - The request
* - The current property of the Registrator / InsideAuth facade
* @see Registrator::current()
* @see Builder::current()
* @see InsideAuth::current()
*/
class InjectIntoApplication
Expand Down
5 changes: 2 additions & 3 deletions src/InsideAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Illegal\InsideAuth;

use Illegal\InsideAuth\Registrators\Registrator;
use Illuminate\Support\Facades\Facade;

/**
* @mixin Registrator
* @mixin Builder
*/
class InsideAuth extends Facade
{
public static function getFacadeAccessor(): string
{
return Registrator::class;
return Builder::class;
}
}
9 changes: 9 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illegal\InsideAuth\Models;

use Illegal\InsideAuth\Events\UserDeleted;
use Illegal\InsideAuth\Factories\UserFactory;
use Illegal\InsideAuth\InsideAuth;
use Illegal\LaravelUtils\Contracts\HasPrefix;
use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification;
Expand Down Expand Up @@ -125,4 +126,12 @@ public function sendEmailVerificationNotification(): void

$this->notify($notification);
}

/**
* Create a new factory instance for the model.
*/
protected static function newFactory(): UserFactory
{
return UserFactory::new();
}
}
Loading

0 comments on commit 1f9a907

Please sign in to comment.