Skip to content

Commit

Permalink
Add basic standalone test environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian Schur committed Mar 15, 2024
1 parent cb768e6 commit eaeac43
Show file tree
Hide file tree
Showing 36 changed files with 1,769 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/environment/app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Nodus\Packages\LivewireCore\Tests\environment\app\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller;
use Nodus\Packages\LivewireCore\SupportsLivewire;
use Nodus\Packages\LivewireCore\Tests\environment\app\Providers\AppServiceProvider;

class HomeController extends Controller
{
use SupportsLivewire, AuthorizesRequests, ValidatesRequests;

use AuthorizesRequests, ValidatesRequests;

public function overview()
{
$views = [];
foreach (AppServiceProvider::getComponents() as $component) {
$views[] = get_class($component);
}
return view('overview', ['views' => $views]);
}

public function show(string $view)
{
return $this->livewire($view)->layout('base')->section('content');
}
}
44 changes: 44 additions & 0 deletions tests/environment/app/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];

/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
67 changes: 67 additions & 0 deletions tests/environment/app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Nodus\Packages\LivewireCore\Tests\environment\app\Providers;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Livewire\Livewire;

class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}

/**
* Bootstrap any application services.
*/
public function boot(): void
{
foreach (self::getComponents() as $component) {
$parts = [];
$ref = new \ReflectionClass($component);
foreach (explode('\\', $ref->getNamespaceName()) as $part) {
$parts[] = Str::kebab($part);
}
$parts[] = Str::kebab($ref->getShortName());
$alias = implode('.', $parts);
Livewire::component($alias, $component);
}
}

public static function getComponents()
{
$components = [];

if (!App::runningInConsole()) {
foreach (File::files('../../../../../../tests/Data') as $report) {
if (!str_ends_with($report->getFilename(), '.php')) {
continue;
}

try {
$className = str_replace('.php', '', $report->getFilename());
if (Str::contains($report->getRealPath(), 'livewire-forms')) {
$namespace = 'Nodus\Packages\LivewireForms\Tests\Data\\' . $className;
} else {
$namespace = 'Nodus\Packages\LivewireDatatables\Tests\Data\\' . $className;
}
/** @var Report $classInstance */
$classInstance = new $namespace();
} catch (Throwable) {
continue;
}

$components[] = $classInstance;
}
}

return $components;
}
}
19 changes: 19 additions & 0 deletions tests/environment/app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Nodus\Packages\LivewireCore\Tests\environment\app\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
use Nodus\Packages\LivewireCore\Tests\environment\app\Http\Controllers\HomeController;

class RouteServiceProvider extends ServiceProvider
{
public function boot(): void
{

$this->routes(function () {
Route::get('/',[HomeController::class,'overview'])->name('overview');
Route::get('/{view}',[HomeController::class,'show'])->name('show');
});
}
}
67 changes: 67 additions & 0 deletions tests/environment/artisan
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env php
<?php

define('LARAVEL_START', microtime(true));

if (str_contains(getcwd(), 'livewire-core')) {
/**
* Ausführung im Core
*/
$appPath = realpath(getcwd() . '/tests/environment/');
$vendorPath = realpath(getcwd() . '/vendor');
} else {
/**
* Ausführung im Projekt
*/
$appPath = realpath(getcwd() . '/vendor/nodus-it/livewire-core/tests/environment/');
$vendorPath = realpath(getcwd() . '/vendor/');
}

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any of our classes manually. It's great to relax.
|
*/
require getcwd() . '/vendor/autoload.php';


$app = require_once __DIR__ . '/bootstrap/app.php';

/*
|--------------------------------------------------------------------------
| Run The Artisan Application
|--------------------------------------------------------------------------
|
| When we run the console application, the current CLI command will be
| executed in this console and the response sent back to a terminal
| or another output device for the developers. Here goes nothing!
|
*/

$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);

$status = $kernel->handle(
$input = new Symfony\Component\Console\Input\ArgvInput,
new Symfony\Component\Console\Output\ConsoleOutput
);

/*
|--------------------------------------------------------------------------
| Shutdown The Application
|--------------------------------------------------------------------------
|
| Once Artisan has finished running, we will fire off the shutdown events
| so that any final work may be done by the application before we shut
| down the process. This is the last thing to happen to the request.
|
*/

$kernel->terminate($input, $status);

exit($status);
10 changes: 10 additions & 0 deletions tests/environment/bootstrap/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

$app = new Illuminate\Foundation\Application($appPath);

$app->singleton(Illuminate\Contracts\Http\Kernel::class, \Illuminate\Foundation\Http\Kernel::class);
$app->singleton(Illuminate\Contracts\Console\Kernel::class, \Illuminate\Foundation\Console\Kernel::class);
$app->singleton(Illuminate\Contracts\Debug\ExceptionHandler::class, \Illuminate\Foundation\Exceptions\Handler::class);


return $app;
2 changes: 2 additions & 0 deletions tests/environment/bootstrap/cache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
8 changes: 8 additions & 0 deletions tests/environment/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{

"autoload": {
"psr-4": {
"Nodus\\Packages\\LivewireCore\\Tests\\": "app"
}
}
}
Loading

0 comments on commit eaeac43

Please sign in to comment.