johnykvsky/dummygenerator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

johnykvsky/dummygenerator

Composer 安装命令:

composer require johnykvsky/dummygenerator

包简介

Dummy / fake data generator for modern PHP

README 文档

README

logo

Your Random Data Source

Software License Build Status Coverage Status

Installation

composer require johnykvsky/dummygenerator --dev

About

DummyGenerator is dummy/fake data generator for PHP. It's a fork of Faker, heavily rewritten at core, but overall is same easy to use. In example:

$generator = DummyGenerator::create(); // all extensions are loaded
echo $generator->firstName();

Documentation

Full documentation is available in docs/index.md

But why...?

Faker died for our because of being hard to maintain as mentioned in sunsetting-faker. Faker 2.0 seems to be dead because of "death by committee" kind of stuff.

I needed simple dummy data generator for PHP 8.3, with modern architecture in mind. This is how DummyGenerator came to life.

Changes in compare to Faker

  • required PHP >= 8.3
  • PHPStan level 8 friendly
  • PHPUnit tests for core and extensions (yep, some just check for not empty, but hey, it's random data)
  • all mt_rand / array_rand replaced with \Random\Randomizer
  • no static methods, only one magic method (__call() in generator)
  • interfaces and dependency injection for everything (all core implementations can be replaced with different ones)
  • implementations can be changed on the fly with withDefinition()
  • language providers removed from core, that makes generator ~9.5Mb smaller
  • changed Uuid, it supports v4 only, use uuid4()
  • removed database providers (core is only for dummy data generation)
  • removed HmlLorem
  • removed File::filePath() since it was interacting with system, not only generating dummy data
  • regexify has been removed from core as it is not used any more, it's available in dummyproviders if needed
  • added Enum, to get random values from PHP enums
  • added String, to generate random string from given pool (as text() is not that good for short lengths)
  • added support for SystemClock, PSR-20 implementation of Clock, used in date/time generation
  • added AnyDateTime, as alternative/replacement for DateTime extension (see docs for more info)
  • some extensions have updated properties, i.e. list of available currencies

Worth noticing:

  • DateTime extension now also supports DateTimeInterface for methods params (not only strings)
  • boolean() supports also float values from range 0 to 1, i.e. ->boolean(0.001) for 0.1% chance

This package also fixes following problems with FakerPHP:

  • __destruct() messing up with seed()
  • bug with unique()->optional() causing massive memory usage
  • not allowing combination of valid and unique (more about chaining in strategies)
  • Factroy::create() sharing state with other instances
  • and other various items, mostly fixed by with switching to \Random\Randomizer and making proper use of it

But most of all: this is written from scratch, no looking back at old Fake architecture. Core is just an organizer (knows nothing about extensions or clock), depends on Container, which holds everything:

  • Strategy (Unique, Valid, Chance...)
  • Extensions (Person, Address, Internet...)
  • Calculators (Iban, Ean...)
  • Randomizer (\Random\Randomizer, Xoshiro256StarStar engine for seed...`)
  • Clock (PSR-20)
  • Replacer

And all that can be replaced with your own implementation. Check overview for more info.

Languages

One of main points of DummyGenerator is to keep core language agnostic. This is why all languages has been removed from core.

However, core use general English language for generating data.

Person extension provides only ~15 names than can be used as first name, last name, part of email etc. If you want more, check dummyproviders to get full providers for en_US,en_GB and pl_PL. I have created them to show how to make them / convert from old Faker, to allow anyone to work on other languages.

Keep in mind:

  • core will stay language agnostic with some small samples (i.e. mentioned names) in English
  • I have no current plans to support any language
  • I have no current plans to work on extending/improving existing language providers.
  • if someone like to make a PR to improve/extend one of mentioned languages - I will gladly look at it.
  • I will not accept PRs with other languages - but I will gladly link in this readme to repositories with them.

Why PHP >= 8.3

Because of introduced in PHP 8.3:

  • Randomizer::getFloat()
  • Randomizer::getBytesFromString()
  • and not so important but nice: typed class constants

What is this fake / dummy data

When writing tests or populating test database you need to came up with various data, like first name, last name, some dates, maybe description, location coordinates and so on. When you deal with multi-language site and want to have it also multilanguage - you need to came up with every language names or address format.

All of that can be done by hand, but it's much easier to do $generator->firstName() and just don't care about what name it will be. Load provider and don't care about given locale names or phone formats.

Another use case - imagine you have description with 100 chars limit and want to test if it properly gives error when more is passed - instead of copying some text you can just use $generator->text(150) to get ~150 characters long text.

Last but not least - it make sure your tests will get random data on each run, not every single time same value. If your code is good and tests correct - then it should be no problem. If tests start failing from time to time - then what you think, where is the problem:

  • with code
  • with tests
  • with random data, it should not be random

I leave answer to you. And yes, there might be cases when data should not be random, but usually it's not that case ;)

Other stuff

There are two Randomizer implementations available:

  • default Randomizer
  • additional XoshiroRandomizer, which supports seed - to be used in tests

There is script\ExtensionsDocs.php that can be used to generate list of available extensions and their methods (look at generate-spec.php)

Since PHPUnit is still missing --repeat, in repository phpunit-repeat you can find Linux shell script for running tests multiple times.

TODO

  • cleanup tests

johnykvsky/dummygenerator 适用场景与选型建议

johnykvsky/dummygenerator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.32k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2024 年 12 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「generator」 「faker」 「Dummy」 「Fake data」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 johnykvsky/dummygenerator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 johnykvsky/dummygenerator 我们能提供哪些服务?
定制开发 / 二次开发

基于 johnykvsky/dummygenerator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14.32k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 10
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-17