sven/laravel-collection-testing-helpers

针对 Laravel 集合的便捷测试方法

v1.0.0 2021-04-19 20:47 UTC

This package is auto-updated.

Last update: 2024-08-26 16:36:08 UTC


README

Laravel Collection Testing Helpers

Laravel 集合测试助手

Latest Version on Packagist Total Downloads Software License Build Status StyleCI

此包为您的 Laravel 集合添加了几个便捷方法,以使对它们的断言更流畅和易于阅读。

安装

您需要遵循几个简单的步骤来安装此包。

下载

通过 composer

$ composer require sven/laravel-collection-testing-helpers --dev

或将包添加到您的开发依赖项中 composer.json 并在命令行中运行 composer update 以下载包

{
    "require-dev": {
        "sven/laravel-collection-testing-helpers": "^1.0"
    }
}

用法

要在测试中使用此包,您必须首先调用 \Sven\LaravelCollectionTestingHelpers\Helpers::enable() 方法,然后在该方法中直接对您的集合使用这些方法

<?php

use Sven\LaravelCollectionTestingHelpers\Helpers;
use Illuminate\Foundation\Testing\TestCase;

class ExampleTest extends TestCase
{
    public function test_collections()
    {
        Helpers::enable();
        
        collect(['apple', 'pear', 'banana'])
            ->assertContains('apple')
            ->assertNotContains('orange');
    }

    public function test_callable_filtering()
    {
        Helpers::enable();
        
        collect(['apple', 'pear', 'banana'])
            ->assertContains(fn ($fruit) => $fruit === 'pear')
            ->assertNotContains(fn ($fruit) => $fruit === 'kiwi');
    }

    public function test_keyed_collections()
    {
        Helpers::enable();
        
        collect([['name' => 'apple'], ['name' => 'pear'], ['name' => 'banana']])
            ->assertContains('name', 'apple')
            ->assertNotContains('name', 'grape');
    }
}

致谢

非常感谢 @eduarguz 在已废弃的 sven/laravel-testing-utils 包中对这项工作的贡献。

贡献

所有贡献(拉取请求、问题以及功能请求)均受欢迎。不过,请先阅读 CONTRIBUTING.md。有关所有贡献者,请参阅 贡献者页面

许可

sven/laravel-collection-testing-helpers 使用 MIT 许可证(MIT)授权。有关更多信息,请参阅 许可文件