Larafeat is a simplified modification of the Lucidarch. It allows you to build and serve features in your Laravel application.
You can install the package via composer:
composer require novadaemon/larafeatThis package supports Laravel 9 and Laravel 10.
Create a new feature using the command make:feature
php artisan make:feature MyAwesomeFeatureYou can write the name without the "Feature" prefix, the command prepend it to the class and file names.
php artisan make:feature MyAwesomeThe above command create the file MyAwesomeFeature class in the directory app/Features.
<?php
namespace App\Features;
use Illuminate\Http\Request;
use Novadaemon\Larafeat\Feature;
class MyAwsomeFeature extends Feature
{
public function handle(Request $request)
{
$order = $request->input('order');
//...
}
}Also, the command generates the file MyAwesomeFeatureTest in the tests directory.
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Features\MyAwsomeFeature;
class MyAwsomeFeatureTest extends TestCase
{
public function test_my_awsome_feature()
{
$this->markTestIncomplete();
}
}The make:fature command accept the --pest option. If this option is present, the file create for test will be a Pest file.
<?php
namespace Tests\Feature;
test('example', function () {
$response = $this->get('/');
$response->assertStatus(200);
});To start serves features, you must extends your controller from Larafeat controller.
<?php
namespace App\Controllers;
use Novadaemon\Larafeat\Controllers\Controller;
use App\Features\MyAwesomeFeature;
class MyController extends Controller
{
public function get()
{
return $this->serve(MyAwesomeFeature::class);
}
}You can define class properties in the Feature constructor, and then, you can pass then in the arguments parameter of the serve method.
<?php
namespace App\Features;
use Illuminate\Http\Request;
use Novadaemon\Larafeat\Feature;
class MyAwsomeFeature extends Feature
{
public function __construct(private string $name)
{
}
public function handle(Request $request)
{
$name = $this->name;
//...
}
}<?php
namespace App\Controllers;
use Novadaemon\Larafeat\Controllers\Controller;
use App\Features\MyAwesomeFeature;
class MyController extends Controller
{
public function get()
{
return $this->serve(MyAwesomeFeature::class, ['name' => 'Jesús']);
}
}Inside the feature yo can disptach Laravel Jobs using the run method.
<?php
namespace App\Features;
use App\Jobs\GreetingJob;
use Illuminate\Http\Request;
use Novadaemon\Larafeat\Feature;
class MyAwsomeFeature extends Feature
{
public function __construct(private string $name)
{
}
public function handle(Request $request)
{
return $this->run(new GrettingJob($this->name));
}
}Contributing is pretty chill and is highly appreciated! Just send a PR and/or create an issue!
The MIT License (MIT). Please see License File for more information.
