{"id":15066750,"url":"https://github.com/shapintv/datagen","last_synced_at":"2025-04-10T13:53:25.908Z","repository":{"id":56546818,"uuid":"111332153","full_name":"shapintv/datagen","owner":"shapintv","description":"Deal with DB \u0026 fixtures","archived":false,"fork":false,"pushed_at":"2020-12-09T09:51:57.000Z","size":106,"stargazers_count":2,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T12:39:20.114Z","etag":null,"topics":["database","db","dbal","fixtures","stripe","symfony"],"latest_commit_sha":null,"homepage":null,"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/shapintv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-19T21:08:31.000Z","updated_at":"2020-12-09T09:51:59.000Z","dependencies_parsed_at":"2022-08-15T20:40:43.833Z","dependency_job_id":null,"html_url":"https://github.com/shapintv/datagen","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shapintv%2Fdatagen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shapintv%2Fdatagen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shapintv%2Fdatagen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shapintv%2Fdatagen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shapintv","download_url":"https://codeload.github.com/shapintv/datagen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248229070,"owners_count":21068819,"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":["database","db","dbal","fixtures","stripe","symfony"],"created_at":"2024-09-25T01:11:31.755Z","updated_at":"2025-04-10T13:53:25.886Z","avatar_url":"https://github.com/shapintv.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Datagen\n\n[![Latest Version](https://img.shields.io/github/release/shapintv/datagen.svg?style=flat-square)](https://github.com/shapintv/datagen/releases)\n[![Build Status](https://img.shields.io/travis/shapintv/datagen.svg?style=flat-square)](https://travis-ci.com/shapintv/datagen)\n[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/shapintv/datagen.svg?style=flat-square)](https://scrutinizer-ci.com/g/shapintv/datagen)\n[![Quality Score](https://img.shields.io/scrutinizer/g/shapintv/datagen.svg?style=flat-square)](https://scrutinizer-ci.com/g/shapintv/datagen)\n[![Total Downloads](https://img.shields.io/packagist/dt/shapin/datagen.svg?style=flat-square)](https://packagist.org/packages/shapin/datagen)\n\nDatagen is a PHP library to deal with fixtures loading.\n\n## Installation\n\nThe recommended way to install Datagen is through\n[Composer](http://getcomposer.org/). Require the `shapin/datagen` package:\n\n    $ composer require shapin/datagen\n\n## Usage\n\nThe main entrypoint is the `Shapin\\Datagen\\Datagen` class and its `load` method.\n\n```php\nuse Doctrine\\DBAL\\DriverManager;\nuse Shapin\\Stripe\\StripeClient;\nuse Symfony\\Component\\HttpClient\\HttpClient;\n\nuse Shapin\\Datagen\\Datagen;\nuse Shapin\\Datagen\\DBAL\\Processor as DBALProcessor;\nuse Shapin\\Datagen\\Loader;\nuse Shapin\\Datagen\\Stripe\\Processor as StripeProcessor;\nuse Shapin\\Datagen\\ReferenceManager;\n\n// Create a Loader for your fixtures\n$loader = new Loader();\n$loader-\u003eaddFixture(new MyAwesomeFixture(), ['group1', 'group2']);\n$loader-\u003eaddFixture(new AnotherAwesomeFixture()); // Groups are faculative\n\n$referenceManager = new ReferenceManager();\n\n$connectionParams = [\n    'dbname' =\u003e 'testDB',\n    'user' =\u003e 'user',\n    'password' =\u003e 'pass',\n    'host' =\u003e 'localhost',\n    'driver' =\u003e 'pdo_mysql',\n];\n$connection = DriverManager::getConnection($connectionParams);\n\n$httpClient = HttpClient::create([\n    'base_uri' =\u003e 'http://127.0.0.1:12111/v1/',\n    'auth_bearer' =\u003e 'api_key',\n    'headers' =\u003e [\n        'Content-Type' =\u003e 'application/json',\n    ],\n]);\n$stripeClient = new StripeClient($httpClient);\n\n// Create your processors (see next section for more information regarding supported fixtures)\n$processors = [\n    new DBALProcessor($connection, $referenceManager),\n    new StripeProcessor($stripeClient, $referenceManager),\n];\n\n// Create a Datagen\n$datagen = new Datagen($loader, $processors);\n\n// Load everything!\n$datagen-\u003eload();\n```\n\n### Symfony\n\nThis library contains a bundle in order to integrate seamlessly with Symfony. For now, there isn't any symfony recipe so you'll need to manually register the bundle into your Kernel:\n\n```php\n    public function registerBundles()\n    {\n        $contents = require $this-\u003egetProjectDir().'/config/bundles.php';\n        foreach ($contents as $class =\u003e $envs) {\n            if ($envs[$this-\u003eenvironment] ?? $envs['all'] ?? false) {\n                yield new $class();\n            }\n        }\n\n        // No symfony recipe (yet) for Datagen\n        if (in_array($this-\u003eenvironment, ['dev', 'test'])) {\n            yield new \\Shapin\\Datagen\\Bridge\\Symfony\\Bundle\\ShapinDatagenBundle();\n        }\n    }\n```\n\nOnce registered, you'll have access to new commands in order to play with datagen.\n\n## Creating fixtures\n\n### DBAL\n\nIn order to create a table and hydrate it, extends the `Table` base class:\n\n```php\n\u003c?php\n\nuse Shapin\\Datagen\\DBAL\\Table;\nuse Doctrine\\DBAL\\Schema\\Schema;\nuse Ramsey\\Uuid\\Uuid;\n\nclass Category extends Table\n{\n    protected static $tableName = 'category';\n    protected static $order = 15;\n\n    /**\n     * {@inheritdoc}\n     */\n    public function addTableToSchema(Schema $schema)\n    {\n        $table = $schema-\u003ecreateTable(self::$tableName);\n        $table-\u003eaddColumn('id', 'uuid');\n        $table-\u003eaddColumn('name', 'string');\n        $table-\u003eaddColumn('description', 'text', ['notnull' =\u003e false]);\n\n        $table-\u003esetPrimaryKey(['id']);\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function getRows(): iterable\n    {\n        yield 'category_1' =\u003e [\n            'id' =\u003e Uuid::uuid4(),\n            'name' =\u003e 'Category 1',\n            'description' =\u003e 'My awesome first category',\n        ];\n\n        yield 'another_category' =\u003e [\n            'id' =\u003e Uuid::uuid4(),\n            'name' =\u003e 'Another category',\n        ];\n    }\n}\n```\n\n### Stripe\n\nIn order to create fixtures for Stripe, you'll need to use [shapintv/stripe](https://github.com/shapintv/stripe) library.\n\nFor example, if you want to create a product:\n\n```php\n\u003c?php\n\nuse Shapin\\Datagen\\Stripe\\Fixture;\n\nclass StripeProduct extends Fixture\n{\n    protected static $order = 30;\n\n    /**\n     * {@inheritdoc}\n     */\n    public function getObjectName(): string\n    {\n        return 'product';\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function getObjects(): iterable\n    {\n        yield 'my_product' =\u003e [\n            'id' =\u003e 'my_product',\n            'name' =\u003e 'This is my product!',\n            'type' =\u003e 'product',\n        ];\n    }\n}\n```\n\n## And so much more!\n\n### Groups\n\nWhen adding your fixtures to the Loader, you can use the second argument to specify some groups.\nWhen launching the `Datagen::load` method, you can specify which groups you want to include and/or to exclude.\n\n```php\n// Load everything\n$datagen-\u003eload();\n// Load only fixtures from a given group\n$datagen-\u003eload(['group1']);\n// Do not load fixtures from a given group\n$datagen-\u003eload([], ['group2']);\n// Load all fixtures from group 1, ignoring group2. If a fixture is in both group, it will be ignored.\n$datagen-\u003eload(['group1'], ['group2']);\n```\n\n### References\n\nYou can (and should!) name all your fixtures in order to be allowed to use them elsewhere.\n\n```php\n\u003c?php\n\nuse Doctrine\\DBAL\\Schema\\Schema;\nuse Shapin\\Datagen\\DBAL\\Table;\nuse Ramsey\\Uuid\\Uuid;\n\nclass Category extends Table\n{\n    protected static $tableName = 'category';\n    protected static $order = 15;\n\n    /**\n     * {@inheritdoc}\n     */\n    public function addTableToSchema(Schema $schema)\n    {\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function getRows(): iterable\n    {\n        // This fixture is not named and won't be referencable.\n        yield [\n            'id' =\u003e Uuid::uuid4(),\n            'name' =\u003e 'Dead category',\n        ];\n\n        // The name of this fixture is \"category_1\". We'll use it later!\n        yield 'category_1' =\u003e [\n            'id' =\u003e Uuid::uuid4(),\n            'name' =\u003e 'Category 1',\n            'description' =\u003e 'My awesome first category',\n        ];\n    }\n}\n\nclass Subcategory extends Table\n{\n    protected static $tableName = 'subcategory';\n    protected static $order = 25;\n\n    /**\n     * {@inheritdoc}\n     */\n    public function addTableToSchema(Schema $schema)\n    {\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function getRows(): iterable\n    {\n        yield 'subcategory_1' =\u003e [\n            'id' =\u003e Uuid::uuid4(),\n            'category_id' =\u003e 'REF:category.category_1.id', // Here is our reference!\n            'name' =\u003e 'Subcategory 1',\n        ];\n    }\n}\n```\n\n## License\n\nDatagen is released under the MIT License. See the bundled LICENSE file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshapintv%2Fdatagen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshapintv%2Fdatagen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshapintv%2Fdatagen/lists"}