{"id":15014500,"url":"https://github.com/jakzal/phpunit-globals","last_synced_at":"2025-04-04T16:15:53.236Z","repository":{"id":46030953,"uuid":"123451418","full_name":"jakzal/phpunit-globals","owner":"jakzal","description":"Allows to use attributes to define global variables in PHPUnit test cases","archived":false,"fork":false,"pushed_at":"2025-02-24T17:27:47.000Z","size":144,"stargazers_count":40,"open_issues_count":0,"forks_count":10,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-28T15:09:44.331Z","etag":null,"topics":["annotations","attributes","env","env-vars","environment","environment-variables","globals","php","phpunit","phpunit-extension","phpunit-listener","test","tests"],"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/jakzal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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},"funding":{"github":["jakzal"]}},"created_at":"2018-03-01T15:12:47.000Z","updated_at":"2025-02-27T11:16:35.000Z","dependencies_parsed_at":"2023-02-06T08:31:39.399Z","dependency_job_id":"b65de884-c291-4926-96fd-9583f5b70256","html_url":"https://github.com/jakzal/phpunit-globals","commit_stats":{"total_commits":73,"total_committers":9,"mean_commits":8.11111111111111,"dds":"0.20547945205479456","last_synced_commit":"18d599f5d211b8eb26d6fc0172ba52bf8cf90e86"},"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakzal%2Fphpunit-globals","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakzal%2Fphpunit-globals/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakzal%2Fphpunit-globals/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakzal%2Fphpunit-globals/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jakzal","download_url":"https://codeload.github.com/jakzal/phpunit-globals/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247208178,"owners_count":20901570,"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":["annotations","attributes","env","env-vars","environment","environment-variables","globals","php","phpunit","phpunit-extension","phpunit-listener","test","tests"],"created_at":"2024-09-24T19:45:42.423Z","updated_at":"2025-04-04T16:15:53.220Z","avatar_url":"https://github.com/jakzal.png","language":"PHP","funding_links":["https://github.com/sponsors/jakzal"],"categories":[],"sub_categories":[],"readme":"# PHPUnit Globals\n\nAllows to use attributes to define global variables in PHPUnit test cases.\n\n[![Build](https://github.com/jakzal/phpunit-globals/actions/workflows/build.yml/badge.svg)](https://github.com/jakzal/phpunit-globals/actions/workflows/build.yml)\n\nSupported attributes:\n * `#[Env]` for `$_ENV`\n * `#[Server]` for `$_SERVER`\n * `#[Putenv]` for [`putenv()`](http://php.net/putenv)\n\n\u003e Annotations were previously supported up until v3.5.0 (inclusive).\n\u003e Annotation support is complete, so if you plan on using them keep using v3.5 of this package.\n\nGlobal variables are set before each test case is executed,\nand brought to the original state after each test case has finished.\nThe same applies to `putenv()`/`getenv()` calls.\n\n## Installation\n\n### Composer\n\n```bash\ncomposer require --dev zalas/phpunit-globals\n```\n\n### Phar\n\nThe extension is also distributed as a PHAR, which can be downloaded from the most recent\n[Github Release](https://github.com/jakzal/phpunit-globals/releases).\n\nPut the extension in your PHPUnit extensions directory.\nRemember to instruct PHPUnit to load extensions in your `phpunit.xml` using the `extensionsDirectory` attribute:\n\n```xml\n\u003cphpunit xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"file://./vendor/phpunit/phpunit/phpunit.xsd\"\n         extensionsDirectory=\"tools/phpunit.d\"\n\u003e\n\u003c/phpunit\u003e\n```\n\n## Usage\n\nEnable the globals attribute extension in your PHPUnit configuration:\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003cphpunit ...\u003e\n\n    \u003c!-- ... --\u003e\n\n    \u003cextensions\u003e\n        \u003cbootstrap class=\"Zalas\\PHPUnit\\Globals\\AttributeExtension\" /\u003e\n    \u003c/extensions\u003e\n\u003c/phpunit\u003e\n```\n\nMake sure the `AttributeExtension` is **registered before** any other extensions that might depend on global variables.\n\nGlobal variables can now be defined in attributes:\n\n```php\nuse PHPUnit\\Framework\\TestCase;\nuse Zalas\\PHPUnit\\Globals\\Attribute\\Env;\nuse Zalas\\PHPUnit\\Globals\\Attribute\\Server;\nuse Zalas\\PHPUnit\\Globals\\Attribute\\Putenv;\n\n#[Env('FOO', 'bar')]\nclass ExampleTest extends TestCase\n{\n    #[Env('APP_ENV', 'foo')]\n    #[Env('APP_DEBUG', '0')]\n    #[Server('APP_ENV', 'bar')]\n    #[Server('APP_DEBUG', '1')]\n    #[Putenv('APP_HOST', 'localhost')]\n    public function test_global_variables()\n    {\n        $this-\u003eassertSame('bar', $_ENV['FOO']);\n        $this-\u003eassertSame('foo', $_ENV['APP_ENV']);\n        $this-\u003eassertSame('0', $_ENV['APP_DEBUG']);\n        $this-\u003eassertSame('bar', $_SERVER['APP_ENV']);\n        $this-\u003eassertSame('1', $_SERVER['APP_DEBUG']);\n        $this-\u003eassertSame('localhost', \\getenv('APP_HOST'));\n    }\n}\n```\n\nIt's also possible to mark an attribute with _unset_ so it will not be present in any of the global variables:\n\n```php\nuse PHPUnit\\Framework\\TestCase;\n\nclass ExampleTest extends TestCase\n{\n    #[Env('APP_ENV', unset: true)]\n    #[Server('APP_DEBUG', unset: true)]\n    #[Putenv('APP_HOST', unset: true)]\n    public function test_global_variables()\n    {\n        $this-\u003eassertArrayNotHasKey('APP_ENV', $_ENV);\n        $this-\u003eassertArrayNotHasKey('APP_DEBUG', $_SERVER);\n        $this-\u003eassertArrayNotHasKey('APP_HOST', \\getenv());\n    }\n}\n```\n## Updating to PHPUnit 10\n\nWhen updating from a previous version of this extension dedicated to work with PHPUnit 9,\nreplace the extension registration in `phpunit.xml`:\n\n```xml\n    \u003cextensions\u003e\n        \u003cextension class=\"Zalas\\PHPUnit\\Globals\\AttributeExtension\" /\u003e\n    \u003c/extensions\u003e\n```\n\nwith:\n\n```xml\n    \u003cextensions\u003e\n        \u003cbootstrap class=\"Zalas\\PHPUnit\\Globals\\AttributeExtension\" /\u003e\n    \u003c/extensions\u003e\n```\n\n## Contributing\n\nPlease read the [Contributing guide](CONTRIBUTING.md) to learn about contributing to this project.\nPlease note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md).\nBy participating in this project you agree to abide by its terms.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjakzal%2Fphpunit-globals","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjakzal%2Fphpunit-globals","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjakzal%2Fphpunit-globals/lists"}