{"id":24962380,"url":"https://github.com/directorytree/dummy","last_synced_at":"2025-08-14T08:49:05.016Z","repository":{"id":268303153,"uuid":"902508525","full_name":"DirectoryTree/Dummy","owner":"DirectoryTree","description":"Generate PHP class instances populated with dummy data","archived":false,"fork":false,"pushed_at":"2024-12-28T22:19:10.000Z","size":183,"stargazers_count":28,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-21T03:06:18.843Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/DirectoryTree.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-12T17:48:15.000Z","updated_at":"2025-02-13T19:03:26.000Z","dependencies_parsed_at":"2024-12-15T23:18:07.315Z","dependency_job_id":"1070774e-a85d-4aee-86bb-7b0f00549813","html_url":"https://github.com/DirectoryTree/Dummy","commit_stats":null,"previous_names":["directorytree/dummy"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DirectoryTree%2FDummy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DirectoryTree%2FDummy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DirectoryTree%2FDummy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DirectoryTree%2FDummy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DirectoryTree","download_url":"https://codeload.github.com/DirectoryTree/Dummy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248305785,"owners_count":21081561,"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":[],"created_at":"2025-02-03T09:00:01.636Z","updated_at":"2025-08-14T08:49:04.985Z","avatar_url":"https://github.com/DirectoryTree.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://github.com/DirectoryTree/Dummy/blob/master/art/logo.svg\" width=\"250\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\nGenerate PHP class instances populated with fake dummy data using \u003ca href=\"https://github.com/FakerPHP/Faker\" target=\"_blank\"\u003eFaker\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://github.com/directorytree/dummy/actions\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/github/actions/workflow/status/directorytree/dummy/run-tests.yml?branch=master\u0026style=flat-square\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/directorytree/dummy\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/packagist/v/directorytree/dummy.svg?style=flat-square\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/directorytree/dummy\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/packagist/dt/directorytree/dummy.svg?style=flat-square\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/directorytree/dummy\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/packagist/l/directorytree/dummy.svg?style=flat-square\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n---\n\n## Index\n\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Introduction](#introduction)\n- [Setup](#setup)\n    - [HasFactory Trait](#hasfactory-trait)\n    - [Class Factory](#class-factory)\n- [Usage](#usage)\n    - [Factory States](#factory-states)\n    - [Factory Callbacks](#factory-callbacks)\n    - [Factory Sequences](#factory-sequences)\n    - [Factory Collections](#factory-collections)\n\n## Requirements\n\n- PHP \u003e= 8.0\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require directorytree/dummy --dev\n```\n\n## Introduction\n\nConsider you have a class representing a restaurant reservation:\n\n```php\nnamespace App\\Data;\n\nclass Reservation\n{\n    public function __construct(\n        public string $name,\n        public string $email,\n        public DateTime $date,\n    ) {}\n}\n```\n\nTo make dummy instances of this class during testing, you have to manually populate it with dummy data.\n\nThis can quickly get out of hand as your class grows, and you may find yourself writing the same dummy data generation code over and over again.\n\nDummy provides you with a simple way to generate dummy instances of your classes using a simple API:\n\n```php\n// Generate one instance:\n$reservation = Reservation::factory()-\u003emake();\n\n// Generate multiple instances:\n$collection = Reservation::factory()-\u003ecount(5)-\u003emake();\n```\n\n## Setup\n\nDummy provides you two different ways to generate classes with dummy data.\n\n### HasFactory Trait\n\nThe `HasFactory` trait is applied directly to the class you would like to generate dummy instances of.\n\nTo use the `HasFactory` trait, you must implement the `toFactoryInstance` and `getFactoryDefinition` methods:\n\n```php\nnamespace App\\Data;\n\nuse DateTime;\nuse Faker\\Generator;\nuse DirectoryTree\\Dummy\\HasFactory;\n\nclass Reservation\n{\n    use HasFactory;\n    \n    /**\n     * Constructor.\n     */\n    public function __construct(\n        public string $name,\n        public string $email,\n        public DateTime $date,\n    ) {}\n    \n    /**\n     * Define the factory's default state.\n     */\n    protected function getFactoryDefinition(Generator $faker): array\n    {\n        return [\n            'name' =\u003e $faker-\u003ename(),\n            'email' =\u003e $faker-\u003eemail(),\n            'datetime' =\u003e $faker-\u003edateTime(),\n        ];\n    }\n    \n    /**\n     * Create a new instance of the class using the factory definition.\n     */\n    protected static function toFactoryInstance(array $attributes): static\n    {\n        return new static(\n            $attributes['name'],\n            $attributes['email'],\n            $attributes['datetime'],\n        );\n    }\n}\n```\n\nOnce implemented, you may call the `Reservation::factory()` method to create a new dummy factory:\n\n```php\n$factory = Reservation::factory();\n```\n\n#### Dynamic State Methods\n\nThe `HasFactory` trait supports defining dynamic state methods. You can define state methods in your class using the format `get{StateName}State` and call them dynamically on the factory:\n\n```php\nnamespace App\\Data;\n\nuse DateTime;\nuse Faker\\Generator;\nuse DirectoryTree\\Dummy\\HasFactory;\n\nclass Reservation\n{\n    use HasFactory;\n\n    public function __construct(\n        public string $name,\n        public string $email,\n        public DateTime $datetime,\n        public string $status = 'pending',\n        public string $type = 'standard',\n    ) {}\n    \n     // Dynamic state methods...\n\n    public static function getConfirmedState(): array\n    {\n        return ['status' =\u003e 'confirmed'];\n    }\n\n    public static function getPremiumState(): array\n    {\n        return [\n            'type' =\u003e 'premium',\n            'status' =\u003e 'confirmed',\n        ];\n    }\n\n    public static function getCancelledState(): array\n    {\n        return ['status' =\u003e 'cancelled'];\n    }\n\n    protected static function toFactoryInstance(array $attributes): self\n    {\n        return new static(\n            $attributes['name'],\n            $attributes['email'],\n            $attributes['datetime'],\n            $attributes['status'] ?? 'pending',\n            $attributes['type'] ?? 'standard',\n        );\n    }\n\n    protected static function getFactoryDefinition(Generator $faker): array\n    {\n        return [\n            'name' =\u003e $faker-\u003ename(),\n            'email' =\u003e $faker-\u003eemail(),\n            'datetime' =\u003e $faker-\u003edateTime(),\n        ];\n    }\n}\n```\n\nYou can then use these state methods dynamically:\n\n```php\n// Create a confirmed reservation\n$confirmed = Reservation::factory()-\u003econfirmed()-\u003emake();\n\n// Create a premium reservation\n$premium = Reservation::factory()-\u003epremium()-\u003emake();\n\n// Chain multiple states\n$premiumCancelled = Reservation::factory()-\u003epremium()-\u003ecancelled()-\u003emake();\n```\n\n### Class Factory\n\nIf you need more control over the dummy data generation process, you may use the `Factory` class.\n\nThe `Factory` class is used to generate dummy instances of a class using a separate factory class definition.\n\nTo use the `Factory` class, you must extend it with your own and override the `definition` and `generate` methods:\n\n```php\nnamespace App\\Factories;\n\nuse App\\Data\\Reservation;\nuse DirectoryTree\\Dummy\\Factory;\n\nclass ReservationFactory extends Factory\n{\n    /**\n     * Define the factory's default state.\n     */\n    protected function definition(): array\n    {\n        return [\n            'name' =\u003e $this-\u003efaker-\u003ename(),\n            'email' =\u003e $this-\u003efaker-\u003eemail(),\n            'datetime' =\u003e $this-\u003efaker-\u003edateTime(),\n        ];\n    }\n    \n    /**\n     * Generate a new instance of the class.\n     */\n    protected function generate(array $attributes): Reservation\n    {\n        return new Reservation(\n            $attributes['name'],\n            $attributes['email'],\n            $attributes['datetime'],\n        );\n    }\n}\n```\n\n## Usage\n\nOnce you've defined a factory, you can generate dummy instances of your class using the `make` method:\n\n```php\n// Using the trait:\n$reservation = Reservation::factory()-\u003emake();\n\n// Using the factory class:\n$reservation = ReservationFactory::new()-\u003emake();\n```\n\nTo add or override attributes in your definition, you may pass an array of attributes to the `make` method:\n\n```php\n$reservation = Reservation::factory()-\u003emake([\n    'name' =\u003e 'John Doe',\n]);\n```\n\nTo generate multiple instances of the class, you may use the `count` method:\n\n\u003e This will return a `Illuminate\\SupportCollection` instance containing the generated classes.\n\n```php\n$collection = Reservation::factory()-\u003ecount(5)-\u003emake();\n```\n\n### Factory States\n\nState manipulation methods allow you to define discrete modifications \nthat can be applied to your dummy factories in any combination.\n\nFor example, your `App\\Factories\\Reservation` factory might contain a `tomorrow`\nstate method that modifies one of its default attribute values:\n\n```php\nclass ReservationFactory extends Factory\n{\n    // ...\n\n    /**\n     * Indicate that the reservation is for tomorrow.\n     */\n    public function tomorrow(): Factory\n    {\n        return $this-\u003estate(function (array $attributes) {\n            return ['datetime' =\u003e new DateTime('tomorrow')];\n        });\n    }\n}\n```\n\n### Factory Callbacks\n\nFactory callbacks are registered using the `afterMaking` method and allow you to perform \nadditional tasks after making or creating a class. You should register these callbacks\nby defining a `configure` method on your factory class. This method will be \nautomatically called when the factory is instantiated:\n\n```php\nclass ReservationFactory extends Factory\n{\n    // ...\n    \n    /**\n     * Configure the dummy factory.\n     */\n    protected function configure(): static\n    {\n        return $this-\u003eafterMaking(function (Reservation $reservation) {\n            // ...\n        });\n    }\n}\n```\n\n### Factory Sequences\n\nSometimes you may wish to alternate the value of a given attribute for each generated \nclass. \n\nYou may accomplish this by defining a state transformation as a `sequence`:\n\n```php\nReservation::factory()\n    -\u003ecount(3)\n     -\u003esequence(\n        ['datetime' =\u003e new Datetime('tomorrow')],\n        ['datetime' =\u003e new Datetime('next week')],\n        ['datetime' =\u003e new Datetime('next month')],\n    )\n    -\u003emake();\n```\n\n### Factory Collections\n\nBy default, when making more than one dummy class, an instance of `Illuminate\\Support\\Collection` will be returned.\n\nIf you need to customize the collection of classes generated by a factory, you may override the `collect` method:\n\n```php\nclass ReservationFactory extends Factory\n{\n    // ...\n    \n    /**\n     * Create a new collection of classes.\n     */\n    public function collect(array $instances = []): ReservationCollection\n    {\n        return new ReservationCollection($instances);\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdirectorytree%2Fdummy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdirectorytree%2Fdummy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdirectorytree%2Fdummy/lists"}