ylsideas/laravel-additions

Laravel 框架的自定义扩展包

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

This package is auto-updated.

Last update: 2024-09-10 04:40:45 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

一个可用于 Laravel 7 的开发者工具和调整工具包。

安装

您可以通过 composer 安装此包。

composer require --dev ylsideas/laravel-additions

为什么?

我在项目中发现自己在做一些常见的事情,或者只是想在开发产品中使用一些自定义选择。

使用

快速安装宏和辅助文件。

以下命令可以用于创建新的 PHP 文件用于辅助和宏。这些文件将被添加到 composer.json 文件中,之后将运行 composer dumpautoload 命令。

php artisan configure --macros --helpers

发布占位符

该库在 2020 年 4 月 19 日之前为 Laravel 7 添加了额外的占位符,覆盖了默认值。这些添加包括通知和事件占位符。

您可以使用 app.php 配置设置要发布占位符的目录。

'stub_path' => resource_path(),

运行 php artisan stub:publish 将占位符放在 resources/stubs/ 路径中。在那里您可以编辑将用于创建新类的占位符。

测试特性挂钩

您可能经常想使用特性与测试一起工作以处理某些方面。现在您可以使用带有注解 @afterAppCreated@beforeAppDestroyed 的特性,这些注解将钩入功能测试并调用指定的方法,这使得在测试中轻松切换功能。

例如,您可以创建特性

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);
    }
}

然后在您的测试中应用该特性,知道 @afterAppCreated 注解将被执行,提供一个新用户,让您可以拒绝一些样板。

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

实际上,这个工具集中已经有一个这样的特性,即 WithUserAuthentication 特性。此包中的测试也使用这些注解来运行测试设置。

设置命令 & 测试挂钩

您可以创建一个简单的函数来设置应用程序。如果您想能够运行多个命令和其他任务,这很有用。还有一个初始标志,可以用来表示应用程序正在第一次设置。此命令旨在用于本地开发。要创建设置命令,您应该执行以下操作

php artisan configure --hooks

这将创建一个 LaravelAdditionsServerProvider 在您的应用程序的 Providers 文件夹中。然后您可以自定义设置挂钩以及在使用 php artisan test 命令时触发的测试之前和之后挂钩。默认挂钩如下所示

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!');
    }
}

测试由 Mailable/Notification 断言生成的视图

当测试 Mailable 或 Notification 是否已发送时,通过工厂类可以创建断言,该工厂类将构建一个可调用对象以检测电子邮件类型,将其渲染为 HTML,并提供一个 ViewAssertion 实例,您可以对其执行断言。

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!');
            })
        );
    }

}

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全

如果您发现任何安全问题,请通过电子邮件 peter.fox@ylsideas.co 而不是使用问题跟踪器。

致谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件

Laravel 包模板

本包使用 Laravel 包模板 生成。