{"id":24780442,"url":"https://github.com/webgriffe/doctrine-fixtures-loader","last_synced_at":"2025-03-24T04:40:29.703Z","repository":{"id":9014069,"uuid":"10769366","full_name":"webgriffe/doctrine-fixtures-loader","owner":"webgriffe","description":"Base loader for your project's object fixtures, with all their dependencies, using Doctrine and closures.","archived":false,"fork":false,"pushed_at":"2013-11-28T16:46:54.000Z","size":136,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-01-29T10:35:49.908Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/webgriffe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-06-18T17:38:41.000Z","updated_at":"2013-11-28T16:47:00.000Z","dependencies_parsed_at":"2022-07-09T21:46:15.707Z","dependency_job_id":null,"html_url":"https://github.com/webgriffe/doctrine-fixtures-loader","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/webgriffe%2Fdoctrine-fixtures-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webgriffe%2Fdoctrine-fixtures-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webgriffe%2Fdoctrine-fixtures-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webgriffe%2Fdoctrine-fixtures-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webgriffe","download_url":"https://codeload.github.com/webgriffe/doctrine-fixtures-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245212107,"owners_count":20578438,"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-01-29T10:30:46.499Z","updated_at":"2025-03-24T04:40:29.684Z","avatar_url":"https://github.com/webgriffe.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Doctrine Fixtures Loader\n========================\n\nBase loader for your project's object fixtures, with all their dependencies, using Doctrine and closures.\n\nIntroduction\n------------\n\nTypically in a real project that uses Doctrine, you have tons of entities related with each others that needs to be loaded during functional tests using `doctrine/data-fixtures` library. This base loader helps you to mantain a fixtures loader for your project that can give to the caller an instance of an object with all related dependencies loaded. See the example below.\n\nUsage\n-----\n\nSuppose that your project has the following entities:\n\n* Order\n* Product\n* Customer\n\nSuppose that for every Order you have many Products and one Customer. Suppose that you need a fixture that loads an Order with a specific payment method. Suppose that your application requires that an Order must have all dependencies set. In this case, what you have to do is to instantiate an Order, **with all of its dependencies**, like as follows:\n\n\t\u003c?php\n\t\n\tnamespace MyProject\\Tests\\Fixtures;\n\t\n\tuse Doctrine\\Common\\DataFixtures\\AbstractFixture;\n\tuse Doctrine\\Common\\Persistence\\ObjectManager;\n\t\n\tclass MyFixture extends AbstractFixture\n\t{\n\t\tpublic function load(ObjectManager $manager)\n\t\t{\n\t\t\t$customer = new Customer();\n\t\t\t// set Customer's properties\n\t\t\t\n\t\t\t$product1 = new Product();\n\t\t\t// set Product's properties\n\t\t\t\n\t\t\t$product2 = new Product();\n\t\t\t// set Product's properties\n\t\t\t\n\t\t\t$order = new Order();\n\t\t\t$order-\u003esetProducts(array($product1, $product2));\n\t\t\t$order-\u003esetCustomer($customer);\n\t\t\t// set other Order's properties\n\t\t\t\n\t\t\t$order-\u003esetPaymentMethod('my_specific_payment_method');\n\t\t\t\n\t\t\t$manager-\u003epersist($order);\n\t\t\t$manager-\u003eflush();\n\t\t}\n\t}\n\nAnd that's ok but the code to build an Order with all of its dependencies will be probably repeated in every fixture that needs an Order. This is really uncomfortable, especially in a real project with much more entities and dependencies, because when you need an Order you don't want to worry about all of its dependencies; you just want an Order so you can set specific properties for the specific test case.\n\nThe solution proposed is to maintain a fixture loader (that extends this base loader) specific for your project as the following:\n\n\t\u003c?php\n\t\n\tnamespace MyProject\\Tests\\Fixtures;\n\t\n\tuse Webgriffe\\DoctrineFixturesLoader\\Loader as BaseLoader;\n\tuse MyProject\\Entity\\Order;\n\tuse MyProject\\Entity\\Customer;\n\tuse MyProject\\Entity\\Product;\n\t\n\tclass Loader extends BaseLoader\n\t{\n\t\tpublic function loadCustomer($referenceName = null, $forcePersist = true)\n\t\t{\n\t\t\t$objectLoader = function (\n\t\t\t\tLoader $loader,\n\t\t\t\t$referenceName\n\t\t\t) {\n\t\t\t\t$customer = new Customer();\n\t\t\t\t// set Customer's properties\n\t\t\t\t\n\t\t\t\treturn $customer;\n\t\t\t};\n\t\t\t\n\t\t\t$this-\u003eload($referenceName, $forcePersist, $objectLoader, 'default-customer');\n\t\t}\n\t\t\n\t\tpublic function loadProduct($referenceName = null, $forcePersist = true)\n\t\t{\n\t\t\t$objectLoader = function (\n\t\t\t\tLoader $loader,\n\t\t\t\t$referenceName\n\t\t\t) {\n\t\t\t\t$product = new Product();\n\t\t\t\t// set Product's properties\n\t\t\t\t\n\t\t\t\treturn $product;\n\t\t\t};\n\t\t\t\n\t\t\t$this-\u003eload($referenceName, $forcePersist, $objectLoader, 'default-product');\n\t\t}\n\t\t\n\t\tpublic function loadOrder($referenceName = null, $forcePersist = true)\n\t\t{\n\t\t\t$objectLoader = function (\n\t\t\t\tLoader $loader,\n\t\t\t\t$referenceName\n\t\t\t) {\t\t\n\t\t\t\t$order = new Order();\n\t\t\t\t$order-\u003esetProducts(\n\t\t\t\t\tarray($loader-\u003eloadProduct(), $loader-\u003eloadProduct('another-product'))\n\t\t\t\t);\n\t\t\t\t$order-\u003esetCustomer($loader-\u003eloadCustomer());\n\t\t\t\t// set other Order's properties\n\t\t\t\t\n\t\t\t\treturn $order;\n\t\t\t};\n\t\t\t\n\t\t\t$this-\u003eload($referenceName, $forcePersist, $objectLoader, 'default-order');\n\t\t}\t\t\t\t\n\t}\n\nNote that you can add any loader methods as you want, if needed. For example here we could add a `loadEmptyOrder()` method that creates an Order without Produts. So, with a fixture loader like the above one, the previous `MyFixture` become as follows:\n\n\t\u003c?php\n\t\n\tnamespace MyProject\\Tests\\Fixtures;\n\t\n\tuse Doctrine\\Common\\DataFixtures\\AbstractFixture;\n\tuse Doctrine\\Common\\Persistence\\ObjectManager;\n\tuse MyProject\\Tests\\Fixtures\\Loader;\n\t\n\tclass MyFixture extends AbstractFixture\n\t{\n\t\tpublic function load(ObjectManager $manager)\n\t\t{\n\t\t\t$loader = new Loader($manager, $this-\u003ereferenceRepository);\n\n\t\t\t$order = $loader-\u003eloadOrder();\n\t\t\t$order-\u003esetPaymentMethod('my_specific_payment_method');\n\n\t\t\t$manager-\u003eflush();\n\t\t}\n\t}\n\t\nThat's all.\n\n\nCredits\n-------\n\nThis base loader has been developed by [Webgriffe®](http://www.webgriffe.com). Please, report to us any bug or suggestion by GitHub issues.\n\t","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebgriffe%2Fdoctrine-fixtures-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebgriffe%2Fdoctrine-fixtures-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebgriffe%2Fdoctrine-fixtures-loader/lists"}