{"id":30671498,"url":"https://github.com/golossus/php-test-fixture-loading","last_synced_at":"2025-09-01T03:16:34.564Z","repository":{"id":54963301,"uuid":"329352917","full_name":"golossus/php-test-fixture-loading","owner":"golossus","description":"php-test-fixture-loading is a package which tries to ease in the process of loading testing fixtures to have a better maintainability and code reuse.","archived":false,"fork":false,"pushed_at":"2021-01-19T10:40:13.000Z","size":21,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-18T22:18:39.231Z","etag":null,"topics":["data-fixtures","php","symfony","testing"],"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/golossus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-13T15:37:46.000Z","updated_at":"2021-01-25T15:04:25.000Z","dependencies_parsed_at":"2022-08-14T07:31:16.384Z","dependency_job_id":null,"html_url":"https://github.com/golossus/php-test-fixture-loading","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/golossus/php-test-fixture-loading","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/golossus%2Fphp-test-fixture-loading","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/golossus%2Fphp-test-fixture-loading/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/golossus%2Fphp-test-fixture-loading/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/golossus%2Fphp-test-fixture-loading/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/golossus","download_url":"https://codeload.github.com/golossus/php-test-fixture-loading/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/golossus%2Fphp-test-fixture-loading/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273068857,"owners_count":25039911,"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","status":"online","status_checked_at":"2025-09-01T02:00:09.058Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["data-fixtures","php","symfony","testing"],"created_at":"2025-09-01T03:16:30.060Z","updated_at":"2025-09-01T03:16:34.554Z","avatar_url":"https://github.com/golossus.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.com/golossus/php-test-fixture-loading.svg?branch=main)](https://travis-ci.com/golossus/php-test-fixture-loading)\n[![codecov](https://codecov.io/gh/golossus/php-test-fixture-loading/branch/main/graph/badge.svg?token=8VWM3FM5SU)](https://codecov.io/gh/golossus/php-test-fixture-loading)\n[![Version](https://poser.pugx.org/golossus/php-test-fixture-loading/version)](//packagist.org/packages/golossus/php-test-fixture-loading)\n[![Total Downloads](https://poser.pugx.org/golossus/php-test-fixture-loading/downloads)](//packagist.org/packages/golossus/php-test-fixture-loading)\n[![License](https://poser.pugx.org/golossus/php-test-fixture-loading/license)](//packagist.org/packages/golossus/php-test-fixture-loading)\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://www.golossus.com\" target=\"_blank\"\u003e\n        \u003cimg height=\"100\" src=\"https://avatars2.githubusercontent.com/u/58183018\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n[php-test-fixture-loading][1] is a package which tries to ease in the process of loading testing fixtures to have a\nbetter maintainability and code reuse. With this package, (data) fixtures can be created as simple classes that can be\njoined to compose more complex testing scenarios.\n\nInstallation\n------------\n\n```shell\ncomposer require --dev golossus/php-test-fixture-loading \n```\n\nUsage\n-----\n\n## Configuration\n\nUse the trait [FixtureLoaderTrait][2] on a base test class or `TestCase`. Those will typically inherit from a `PHPUnit`\ntest class, like the _Symfony_ `WebTestCase` or similar.\n\nAdditionally, this trait has an abstract method `buildFixture` which should be implemented as well. This method is\nresponsible to adapt the way data fixtures are instantiated, because different projects might follow different\napproaches. This is specially true when a Dependency Injection container is needed to build a fixture.\n\nAs an example take the following `TestCase` class which uses a _Symfony_ 4 `WebTestCase`. Take a look at the trait and\nthe method:\n\n```php\n\u003c?php declare(strict_types = 1);\n\nnamespace App\\Tests\\Rest;\n\nuse Golossus\\TestFixtureLoading\\Fixture;\nuse Golossus\\TestFixtureLoading\\FixtureLoaderTrait;\nuse Symfony\\Bundle\\FrameworkBundle\\Test\\WebTestCase;\n\nclass FunctionalTestCase extends WebTestCase\n{\n    use FixtureLoaderTrait;\n\n    protected function buildFixture(string $namespace): Fixture\n    {\n        return self::$container-\u003eget($namespace);\n    }\n\n}\n```\n\nYour case might be more complex (or maybe simpler), so implement this method to cover your needs.\n\n\u003e For _Symfony_ applications like in the example above don't forget to declare the fixtures folder as services, or\n\u003e you won't be able to build the fixtures.\n\n\u003e Although the example uses the testing container to build the data fixtures, so in practice you're allowed to build\n\u003e private services (which is the default behaviour), actually it's possible that you need to make the fixtures public.\n\u003e Look at the following example:\n\n```yaml\n# in cofig/services_test.yaml \nservices:\n  _defaults:\n    autowire: true\n    autoconfigure: true\n\n  App\\Tests\\DataFixtures\\:\n    resource: '../tests/DataFixtures/*'\n    public: true\n```\n\n### Creating a fixture class\n\nTo create a fixture is so easy as creating a new class which implement the [Fixture][3] interface. This interface only\nprovides two methods: `load` and `depends`. The first one defines the logic to create the data and provides\na [FixtureRepository][4] parameter to store a reference on memory of any object created (and maybe stored in the\ndatabase) which can be retrieved afterwards on the test function. The second method is useful to define which other data\nfixtures should be loaded before the current one.\n\n\u003e All dependencies of a loaded fixture will be also loaded even if they are not specified directly in the list of\n\u003e fixtures to load.\n\n\u003e If any fixture declares a dependency graph which produces a cycle, will throw an exception during the loading phase.\n\n\u003e Even if some dependency is used more than once it will actually load just one time.\n\nAs an example of fixture:\n\n```php\n\u003c?php declare(strict_types = 1);\n\nnamespace Tests\\Fixtures;\n\nuse Golossus\\TestFixtureLoading\\AbstractFixture;\nuse Golossus\\TestFixtureLoading\\FixtureRepository;\n...\n\nclass AdminUserFixture extends AbstractFixture\n{\n    ...\n    const ADMIN_USER = 'admin-user';\n\n    public function load(FixtureRepository $fixtureRepository): void\n    {\n        $company = $fixtureRepository-\u003eget(CompanyFixture::COMPANY);\n\n        $user = $this-\u003ecreateAdminUserForCompany($company);\n\n        $fixtureRepository-\u003eset(self::ADMIN_USER, $user);\n    }\n\n    public function depends(): array\n    {\n        return [\n            CompanyFixture::class,\n        ];\n    }\n}\n```\n\n### Loading fixtures\n\nOnce the configuration above has been completed, the usage is quite straightforward, just use the method `loadFixtures`\nprovided in the previous trait to load any desired fixture. This method returns a special fixture repository which is\nused during loading to store object references on demand.\n\n```php\npublic function testWhateverYouLike()\n{\n    // First load the required data fixtures\n    $fixtures = $this-\u003eloadFixtures([\n        SomeDataFixture::class,\n        AnotherDataFixture::class,\n    ]);\n    \n    // You can get a data fixture object by key\n    $dummy = $fixtures-\u003eget('some-key'); \n    \n    // The rest should be a normal test\n}\n```\n\nCommunity\n---------\n\n* Join our [Slack][5] to meet the community and get support.\n* Follow us on [GitHub][6].\n* Read our [Code of Conduct][7].\n\nContributing\n------------\n\nThis is an Open Source project. The Golossus team wants to enable it to be community-driven and open\nto [contributors][8]. Take a look at [contributing documentation][9].\n\nSecurity Issues\n---------------\n\nIf you discover a security vulnerability, please follow our [disclosure procedure][10].\n\nAbout Us\n--------\n\nThis package development is led by the Golossus Team [Leaders][11] and supported by [contributors][8].\n\n[1]: https://github.com/golossus/php-lazy-proxy-loading\n\n[2]: ./lib/FixtureLoaderTrait.php\n\n[3]: ./lib/Fixture.php\n\n[4]: ./lib/FixtureRepository.php\n\n[5]: https://join.slack.com/t/golossus/shared_invite/zt-db4brnes-M8q1Lw2ouFT5X~gQg69NQQ\n\n[6]: https://github.com/golossus\n\n[7]: ./CODE_OF_CONDUCT.md\n\n[8]: ./CONTRIBUTORS.md\n\n[9]: ./CONTRIBUTING.md\n\n[10]: ./CONTRIBUTING.md#reporting-a-security-issue\n\n[11]: ./CONTRIBUTING.md#leaders\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgolossus%2Fphp-test-fixture-loading","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgolossus%2Fphp-test-fixture-loading","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgolossus%2Fphp-test-fixture-loading/lists"}