ylsideas/laravel-additions

A package of customisations for the Laravel Framework

v0.1 2020-05-04 15:46 UTC

This package is auto-updated.

Last update: 2024-04-10 03:40:05 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

A package of developer tools and tweaks that can be used with Laravel 7.

Installation

You can install the package via composer:

composer require --dev ylsideas/laravel-additions

Why?

I found myself doing common things in projects or just wanting a few of my custom choices in the development product.

Usage

Quick install for Macros and Helper files.

The following command can be used to create new php files for helpers and macros. These files will be added to the composer.json file and the composer dumpautoload command will be ran afterwards.

php artisan configure --macros --helpers

Publish Stubs

This library adds additional stubs over the defaults in Laravel 7 as of 19/4/20. Such additions are notifications and events stubs.

You can set the stubs directory of where to publish the stub using the app.php config.

'stub_path' => resource_path(),

Running php artisan stub:publish will place the stubs in the resources/stubs/ path. There you can edit the stubs that will be used for making new classes.

Testing Trait Hooks

Often you might want to use traits with tests to work with certain aspects. You can now use traits with annotations @afterAppCreated and @beforeAppDestroyed which hooks into the feature tests and calls the methods specified making it easy to switch functionality in and out for tests.

For example, you could create the trait:

trait WithSomething
{
    use \YlsIdeas\LaravelAdditions\Testing\WithApplicationTraitHooks;

    protected $user;

    /**
     * @afterAppCreated
     */
    public function createUser()
    {
        $this->user = factory(User::class)->create();
    }
  
    public function actingByDefault()
    {
        return $this->actingAs($this->user);
    }
}

Then in your test you can apply the trait knowing the @afterAppCreated annotation will be executed providing a new user allowing you to reject some boiler plate.

class SomethingTest extends \Tests\TestCase
{
    use WithSomething;
 
    public function testSomething()
    {
        $this->actingByDefault()
            ->get('/home')
            ->assertOk();
    }
}

In fact a trait that works like this already exists in this set of tools, the WithUserAuthentication trait. Even tests in this package use these annotations to run setups of the test.

Setup command & Testing hooks

You can create a simple function to set up an application. This is useful if you want to be able to run multiple commands and other tasks. There is also an initial flag which can be used to denote the application is being set up for the first time. This command is designed to be used for local development. To make the setup command you should do the following:

php artisan configure --hooks

This will create a LaravelAdditionsServerProvider in your application's Providers folder. You can then customise the setup hooks as well as the before and after test hook which will fire when you use the php artisan test command. The default hooks look like the following:

class LaravelAdditionsServiceProvider extends LaravelAdditionsHooksServiceProvider
{
    public function onSetup(bool $initial, Command $command) {
        $command->call('migrate:fresh', ['--seed' => true]);

        return true;
    }

    public function beforeTesting(InputInterface $input, OutputInterface $output) {
        $output->writeln('Starting...');
    }

    public function afterTesting(bool $passed, InputInterface $input, OutputInterface $output) {
        $output->writeln('Complete!');
    }
}

Test views generated by Mailable/Notification assertions

When testing a Mailable or Notification has been sent, the rendered view can have assertions made via a factory class which will build a callable to detect the email type, render it into HTML and then provide a ViewAssertion instance that you can perform assertions on.

class TestCase {
    public function testMailable()
    {
        Mail::fake();
        $mailable = new MailType();
        $mailable->send();
        Mail::assertSent(MailableType::class, MailViewAssertion::make(function (ViewAssertion $assertion) {
            $assertion->contains('Hello World!');
        }));
    }

    public function testMailable()
    {
        Notification::fake();
        $notification = new ExampleNotification();
        $user->notify($notification);
        Notification::assertSentTo(
            $user,
            ExampleNotification::class,
            MailViewAssertion::make(function (ViewAssertion $assertion) {
                $assertion->contains('Hello World!');
            })
        );
    }

}

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email peter.fox@ylsideas.co instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.