marty/mcfly
Composer 安装命令:
composer require marty/mcfly
包简介
Marty McFly - Back to the Future to write your Symfony fixtures
README 文档
README
Marty MacFly - Back to the Future to write your Symfony fixtures Marty MacFly allows you to quickly and easily create fixtures to simplify development and testing for Symfony.
Main features
Installing
PHP 8.0+ and Composer are required.
composer req --dev Marty/McFly
You need to create a fixture file per entity and extend Marty\McFly\Fixture instead of Doctrine\Bundle\FixturesBundle\Fixture.
<?php // src/DataFixtures/CompanyFixtures.php namespace App\DataFixtures; use Doctrine\Persistence\ObjectManager; use Marty\McFly\Fixture; class CompanyFixtures extends Fixture { public function load(ObjectManager $manager): void { // $product = new Product(); // $manager->persist($product); $manager->flush(); } }
By adding Marty\McFly\Fixture, you must adhere to the Marty\McFly\Interface\CreateInterface interface and add a generate() function.
⚠️ The
create()function was renamed togenerate()between v1 and v2 to avoid breaking changes. Thecreate()function is still compatible but it is recommended to migrate togenerate()to benefit from the new features.
<?php // src/DataFixtures/CompanyFixtures.php namespace App\DataFixtures; use Doctrine\Persistence\ObjectManager; use Marty\McFly\Fixture; class CompanyFixtures extends Fixture { public function load(ObjectManager $manager): void { // $this->generate(); $manager->flush(); } public function generate(?array $properties = null, array|string|null $references = null): object { // TODO: Implement generate() method. throw new \RuntimeException("The generate() method is not implemented."); } }
Configure your template (generate() is a factory, work with Reflection without setters or _construct)
<?php // src/DataFixtures/CompanyFixtures.php namespace App\DataFixtures; use App\Entity\Company; use Doctrine\Persistence\ObjectManager; use Marty\McFly\Fixture; class CompanyFixtures extends Fixture { public function load(ObjectManager $manager): void { // Create a random Company, persist it (for database), and automatically add a reference to use it in other fixtures. $this->generate(); // Create 10 randoms Companies to "Paris" (Since 2.0) $this->generateMany(10, [ 'city' => 'Paris', 'postalCode' => '75000' ]); // Finally, flush to the database $manager->flush(); } public function generate(?array $properties = null, array|string|null $references = null): object { return $this->createAndSave(Company::class, $properties, [ 'name' => self::$faker->company(), 'address' => self::$faker->address(), 'city' => self::$faker->city(), 'postalCode' => self::$faker->postcode(), ], $references ); } }
Configure your template : The alternative style (using setter if you have) :
<?php // src/DataFixtures/CompanyFixtures.php namespace App\DataFixtures; use App\Entity\Company; use Doctrine\Persistence\ObjectManager; use Marty\McFly\Fixture; class CompanyFixtures extends Fixture { public function load(ObjectManager $manager): void { // Create a random Company, persist it (for database), and automatically add a reference to use it in other fixtures. $this->generate(); // Alternative : Create 10 randoms Companies to "Paris" with setter for ($i=0 ; $i<10 ; $i++) { $this->generate() ->setCity('Paris') ->setPostalCode('75000'); } // Finally, flush to the database $manager->flush(); } public function generate(?array $properties = null, array|string|null $references = null): object { $company = (new Company()) ->setName(self::getFaker()->company()) ->setAddress(self::getFaker()->address()) ->setCity(self::getFaker()->city()) ->setPostalCode(self::getFaker()->postcode()); $this->save($company, $references); return $company; } }
Usage
Create a random entity
<?php // ... class CompanyFixtures extends Fixture { public function load(ObjectManager $manager): void { // Create a random Company $this->generate(); // Finally, flush to the database $manager->flush(); } // ... generate() definition }
Creating an entity by setting only the necessary properties (recommandation style).
<?php // ... class CompanyFixtures extends Fixture { public function load(ObjectManager $manager): void { // Create a random Company to "Paris" $this->generate([ 'city' => 'Paris', 'postalCode' => '75000', ]); // Finally, flush to the database $manager->flush(); } // ... generate() definition }
Creating an entity by setting only the necessary properties (alternative style).
⚠️ With this syntax, the entity is create and change after.
<?php // ... class CompanyFixtures extends Fixture { public function load(ObjectManager $manager): void { // Create a random Company to "Paris" $this->generate() ->setCity('Paris') ->setPostalCode('75000'); // Finally, flush to the database $manager->flush(); } // ... generate() definition }
Create multiple entities while customizing certain properties.
<?php // ... class CompanyFixtures extends Fixture { public function load(ObjectManager $manager): void { // Create 10 random Company to "Paris" $this->generateMany(10, [ 'city' => 'Paris', 'postalCode' => '75000', ]); // Finally, flush to the database $manager->flush(); } // ... generate() definition }
Create multiple entities while customizing certain properties with random values for each element.
<?php // ... class CompanyFixtures extends Fixture { public function load(ObjectManager $manager): void { // Create 10 random Company to "Paris" $this->generateMany(10, function() { return [ 'city' => 'Paris', 'postalCode' => self::$faker->randomElement(['75000', '75100', '75200']), ]; }); // Finally, flush to the database $manager->flush(); } // ... generate() definition }
Create multiple entities while customizing certain properties (alternative.
<?php // ... class CompanyFixtures extends Fixture { public function load(ObjectManager $manager): void { // Create 10 random Company to "Paris" for ($i=0 ; $i<10 ; $i++) { $this->generate() ->setCity('Hill Valley') ; } // Finally, flush to the database $manager->flush(); } // ... generate() definition }
Dependencies
<?php namespace App\DataFixtures; use Doctrine\Common\DataFixtures\DependentFixtureInterface; use Doctrine\Persistence\ObjectManager; use Marty\McFly\Fixture; use App\Entity\Company; class UserFixture extends Fixture implements DependentFixtureInterface { public function load(ObjectManager $manager): void { // Use this function to loop through all companies and create users for them. /** @var array<Company> $companies */ $companies = $this->getReferencesByClass(Company::class); // Need a random company? Use this method. /** @var Company $company */ $company = $this->getRandomReferenceByClass(Company::class); } public function create(string|array $references = null): User { // You can also use it in the template to add a default random company. /** @var Company $company */ $company = $this->getRandomReferenceByClass(Company::class); // .. } public function getDependencies(): array { return [ CompanyFixture::class, ]; } }
References, counter, enum an random values
<?php // -- class InvoiceFixture extends Fixture implements DependentFixtureInterface { // -- public function generate(?array $properties = null, array|string|null $references = null): Invoice { $invoice = $this->createAndSave(Invoice::class, $properties, [ 'createdAt' => new DateTimeImmutable(), 'number' => InvoiceFixture::count(), // the current number, auto-incrementation on save() 'user' => $this->getRandomReferenceByClass(User::class), // a random User 'status' => self::randomValue(Status::cases()), // randomly a value of Status Enum, 'confirmed' => self::randomValue([True, False]), // randomly True or False 'comment' => self::randomValue([null, self::getFaker()->sentence()]), // Add random sentence or randomly NULL 'product' ], $references ); // Add RandomProduct for ($i=0 ; $i<10 ; $i++) { /** @var Product $product */ $randomProduct = $this->getRandomReferenceByClass(Product::class); $invoice->addProduct($randomProduct); } return $invoice; } // -- }
Credits
- Arnaud Lemercier is based on Wixiweb.
License
Marty MacFly is licensed under The MIT License (MIT).
marty/mcfly 适用场景与选型建议
marty/mcfly 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 261 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 08 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「symfony」 「faker」 「data」 「test」 「Fixture」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 marty/mcfly 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 marty/mcfly 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 marty/mcfly 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
The bundle for easy using json-rpc api on your project
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Symfony bundle to manage fixtures with Alice and Faker.
Additional plugin for fakerphp/faker that allows you to generate a random animal
统计信息
- 总下载量: 261
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-18