承接 soyhuce/array-factory 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

soyhuce/array-factory

Composer 安装命令:

composer require soyhuce/array-factory

包简介

This is my package array-factory

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status GitHub PHPStan Action Status Total Downloads

When testing your application, you may need to write the same data structures (lists, collections, Etc.) to populate models or something else. These often duplicated in multiple test files.

A bit like Laravel's Factory classes, ArrayFactory permits to generate and reuse custom data structures and usable among your tests.

Installation

You can install the package via composer:

composer require --dev soyhuce/array-factory

Usage

Create a factory

use Soyhuce\ArrayFactory;

$factory = new ArrayFactory(['foo' => 'bar']); // from an array
$factory = ArrayFactory::new(['foo' => 'bar']); // from static method new
$factory = ArrayFactory::new(fn () => ['foo' => 'bar']); // with a callable
$factory = ArrayFactory::new(fn () => ['foo' => faker()->word() ]); // using \Faker\Generator generation

Use it

$factory->createOne();
// ['foo' => 'bar']
$factory->createMany(['foo' => 'qux'], ['foo' => 'bar'])
// [['foo' => 'qux'], ['foo' => 'bar']]
$factory->count(2)->asCollection();
// Illuminate\Support\Collection([ ['foo' => 'bar'], ['foo' => 'bar'] ])

Add states

You may need to define states and use it in a specific context.

$factory = new ArrayFactory(
    definition: ['foo' => 'bar'],
    states: [
        'qux' => ['foo' => 'qux'],
    ]
);

$factory->createOne();
// ['foo' => 'bar']
$factory->state('qux')->createOne();
// ['foo' => 'qux']

Add custom Collection

You can transform the array into a custom Collection.

$factory = new ArrayFactory(
    collection: FooCollection::class,
);

$foo = FooFactory::new()->asCollection();

You can also transform the array into a custom Data object by defining the Spatie\LaravelData\Data class.

Add custom \Spatie\LaravelData\Data

$factory = new ArrayFactory(
    data: FooData::class,
);

$factory = FooFactory::new()->asData();

It allows get many results as collection of datas

$datas = $factory->manyAsDataCollection(['id' => 2], ['id' => 3]);
// Collection([FooData(id: 2), FooData(id: 3)])

Create an extended class

class FooFactory extends ArrayFactory
{    
    protected string $collection = FooCollection::class;
    
    protected string $data = FooData::class;
    
    public function definition(): array
    {
        return [
            'id' => 1,
            'activated' => true,
            'message' => faker()->sentence(),
            'value' => 0,
        ];
    }

    public function id(int $id): static
    {
        return $this->state(['id' => $id]);
    }

    public function activated(bool $activated = true): static
    {
        return $this->state(['activated' => $activated]);
    }
}

Then, you can simply use it in your tests.

    $foo1 = FooFactory::new()->createOne(); 
    // ['id' => 1, 'activated' => true, 'value' => 0]
    $foo1 = FooFactory::new()->createOne(['value' => 1]]);
     // ['id' => 1, 'activated' => true, 'value' => 1]
    $foo2 = FooFactory::new()->id(2)->createOne();
     // ['id' => 2, 'activated' => true, 'value' => 0]
    $foo3 = FooFactory::new()->activated(false)->createOne();
     // ['id' => 1, 'activated' => false, 'value' => 0]

Testing

Runs tests.

composer test

Runs all pre-commit checks.

composer all

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

统计信息

  • 总下载量: 9.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固