{"id":18701784,"url":"https://github.com/morrislaptop/popo-factory","last_synced_at":"2025-06-10T10:35:46.707Z","repository":{"id":44962523,"uuid":"332286808","full_name":"morrislaptop/popo-factory","owner":"morrislaptop","description":"Making it easy to mock your POPO's / Value Objects","archived":false,"fork":false,"pushed_at":"2023-11-01T20:11:57.000Z","size":70,"stargazers_count":9,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T03:51:16.763Z","etag":null,"topics":["faker","popo","value"],"latest_commit_sha":null,"homepage":"https://github.com/morrislaptop/popo-factory","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/morrislaptop.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null}},"created_at":"2021-01-23T19:02:45.000Z","updated_at":"2023-10-06T05:30:45.000Z","dependencies_parsed_at":"2022-07-13T14:00:28.194Z","dependency_job_id":"91167dc0-68cd-4f40-a69f-729f09c76b12","html_url":"https://github.com/morrislaptop/popo-factory","commit_stats":{"total_commits":31,"total_committers":5,"mean_commits":6.2,"dds":"0.19354838709677424","last_synced_commit":"94d1352c4e2997612bd3e8298342ec2d3dabfb16"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":"spatie/package-skeleton-php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morrislaptop%2Fpopo-factory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morrislaptop%2Fpopo-factory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morrislaptop%2Fpopo-factory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morrislaptop%2Fpopo-factory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/morrislaptop","download_url":"https://codeload.github.com/morrislaptop/popo-factory/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248540169,"owners_count":21121308,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["faker","popo","value"],"created_at":"2024-11-07T11:42:31.603Z","updated_at":"2025-04-12T08:32:24.023Z","avatar_url":"https://github.com/morrislaptop.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Making it easy to mock your POPO's / Value Objects\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/morrislaptop/popo-factory.svg?style=flat-square)](https://packagist.org/packages/morrislaptop/popo-factory)\n[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/morrislaptop/popo-factory/Tests?label=tests)](https://github.com/morrislaptop/popo-factory/actions?query=workflow%3ATests+branch%3Amaster)\n[![Total Downloads](https://img.shields.io/packagist/dt/morrislaptop/popo-factory.svg?style=flat-square)](https://packagist.org/packages/morrislaptop/popo-factory)\n\nThis package supports mocking POPOs or Value Objects, it inspects your class properties and populates them with random data generated by Faker. You can create class-based mock factories to have a fluent interface to mock POPOs for different states.\n\nThis is a fork of [Data Transfer Object Factory](https://github.com/anteris-dev/data-transfer-object-factory).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require morrislaptop/popo-factory --dev\n```\n\n## Usage\n\n```php\nuse Morrislaptop\\PopoFactory\\PopoFactory;\n\nclass PersonData\n{\n    public function __construct(\n        public $firstName,\n        public string $lastName,\n        public string $email,\n        public string $homeAddress,\n        public ?string $companyName,\n        public string $workAddress,\n        public Carbon $dob,\n        public PersonData $spouse,\n    ) {\n    }\n}\n\nPopoFactory::new(PersonData::class)-\u003emake();\n```\n\nWhich creates an object that looks like this:\n\n```json\n{\n  \"firstName\": \"Jada\",\n  \"lastName\": \"Bechtelar\",\n  \"email\": \"edmond.mcglynn@example.org\",\n  \"homeAddress\": \"640 Feest Landing\\nBernierburgh, PA 99277\",\n  \"companyName\": \"Ratke Inc\",\n  \"workAddress\": \"75107 Konopelski Radial\\nRutherfordport, AK 22994\",\n  \"dob\": \"2021-01-26T11:04:31.393991Z\",\n  \"spouse\": {\n    \"firstName\": \"Leola\",\n    \"lastName\": \"Koss\",\n    \"email\": \"friedrich41@example.org\",\n    \"homeAddress\": \"852 Fabian Mills\\nNorth Ward, NM 54459\",\n    \"companyName\": \"Fahey and Sons\",\n    \"workAddress\": \"671 Creola Prairie Apt. 663\\nNorth Gretchenview, OR 75622-4176\"\n  }\n}\n```\n\nThe `new` method returns an instance of `Morrislaptop\\PopoFactory\\PopoFactory` which provides the following methods.\n\n- `count()` - _Allows you to specify how many POPOs to be generated. They will be returned in an array._\n- `make()` - _Called when you are ready to generate the POPO(s). Returns the generated object(s)._\n- `random()` - _Generates a random number of POPOs_\n- `sequence()` - _Alternates a specific state. (See below)_\n- `state()` - _Manually sets properties based on the array of values passed._\n\nExamples of these methods can be found below.\n\n```php\n// Creates two DTOs in an array\nPopoFactory::new(PersonData::class)-\u003ecount(2)-\u003emake();\n\n// Sets the first name of every person to \"Jim\"\nPopoFactory::new(PersonData::class)\n    -\u003erandom()\n    -\u003estate([\n        'firstName' =\u003e 'Jim',\n    ])\n    -\u003emake();\n\n// Alternates the names of each person between \"Jim\" and \"Susie\"\nPopoFactory::new(PersonData::class)\n    -\u003erandom()\n    -\u003esequence(\n        [ 'firstName' =\u003e 'Jim' ],\n        [ 'firstName' =\u003e 'Susie' ]\n    )\n    -\u003emake();\n\n```\n\n## Creating Class Based Factories\n\nIt's useful to define specific factories for particular objects, which can easily be done by extending the `PopoFactory` class. \n\nMy specifying a typehint for the `make()` method you will also get typehints in your IDE for your mocked object.\n\n```php\n/**\n * @method PersonData make\n */\nclass PersonDataFactory extends PopoFactory\n{\n    public static function factory(): static\n    {\n        return static::new(PersonData::class)-\u003estate([\n            'firstName' =\u003e 'Craig'\n        ]);\n    }\n\n    public function gotNoJob() {\n        return $this-\u003estate([\n            'companyName' =\u003e null,\n        ]);\n    }\n\n    public function worksAtHome() {\n        return $this-\u003estate(function ($attributes) {\n            return [\n                'workAddress' =\u003e $attributes['homeAddress']\n            ];\n        });\n    }\n}\n```\n\nThen using it in tests like so:\n\n```php\n$person = PersonDataFactory::factory()\n            -\u003egotNoJob()\n            -\u003eworksAtHome()\n            -\u003emake();\n```\n\n\n## Extending\n\nYou can easily extend the factory to support other data types. You can do this through the static `registerProvider()` method on the `PropertyFactory` class. This method takes two arguments. The first should be the FQDN of the class you are providing (e.g. `Carbon\\Carbon`) OR the built-in type (e.g. `string`). The second should be a callback that returns the generated value. This callback is passed two properties when called to assist in generating the value. The first is an instance of `Anteris\\FakerMap\\FakerMap` which can be used to help generate fake data. The second is an instance of `ReflectionProperty` which contains information about the property being generated.\n\nFor example, to support Carbon:\n\n```php\n\nuse Morrislaptop\\PopoFactory\\PropertyFactory;\n\nuse Anteris\\FakerMap\\FakerMap;\n\nPropertyFactory::registerProvider('Carbon\\Carbon', fn(FakerMap $fakerMap) =\u003e Carbon::parse(\n    $fakerMap-\u003eclosest('dateTime')-\u003efake()\n));\n\n```\n\n## Plug\n\nWant an easy way to persist your POPOs in Laravel? Check out [laravel-popo-caster](https://github.com/morrislaptop/laravel-popo-caster)\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Craig Morris](https://github.com/morrislaptop)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorrislaptop%2Fpopo-factory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorrislaptop%2Fpopo-factory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorrislaptop%2Fpopo-factory/lists"}