{"id":19518769,"url":"https://github.com/ssi-anik/testbench-lumen","last_synced_at":"2025-07-26T16:41:06.283Z","repository":{"id":43464334,"uuid":"356757595","full_name":"ssi-anik/testbench-lumen","owner":"ssi-anik","description":"[Package] Lumen Testing Helper for Packages Development","archived":false,"fork":false,"pushed_at":"2023-03-26T23:25:36.000Z","size":54,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-19T18:16:48.873Z","etag":null,"topics":["lumen","package","php","phpunit","testbench","testing","testing-tools"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/anik/testbench-lumen","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/ssi-anik.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":"2021-04-11T03:31:51.000Z","updated_at":"2022-06-21T20:11:21.000Z","dependencies_parsed_at":"2022-09-22T14:25:59.343Z","dependency_job_id":null,"html_url":"https://github.com/ssi-anik/testbench-lumen","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssi-anik%2Ftestbench-lumen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssi-anik%2Ftestbench-lumen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssi-anik%2Ftestbench-lumen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssi-anik%2Ftestbench-lumen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ssi-anik","download_url":"https://codeload.github.com/ssi-anik/testbench-lumen/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250953219,"owners_count":21513274,"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":["lumen","package","php","phpunit","testbench","testing","testing-tools"],"created_at":"2024-11-11T00:14:26.970Z","updated_at":"2025-04-26T07:30:53.990Z","avatar_url":"https://github.com/ssi-anik.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"testbench-lumen\n[![codecov](https://codecov.io/gh/ssi-anik/testbench-lumen/branch/main/graph/badge.svg?token=14AJIHUXGK)](https://codecov.io/gh/ssi-anik/testbench-lumen)\n[![Total Downloads](https://poser.pugx.org/anik/testbench-lumen/downloads)](//packagist.org/packages/anik/testbench-lumen)\n[![Latest Stable Version](https://poser.pugx.org/anik/testbench-lumen/v)](//packagist.org/packages/anik/testbench-lumen)\n==============\n\n`anik/testbench-lumen` is a package, highly inspired by\nthe [orchestral/testbench](https://github.com/orchestral/testbench). `orchestral/testbench` that is a tool for testing\nLaravel packages. Whereas the `anik/testbench-lumen` can only be used with [Lumen](https://github.com/laravel/lumen),\nstarting from Lumen `6.x` and afterwards.\n\n## Installation\n\n```shell\ncomposer require --dev anik/testbench-lumen\n```\n\n## Documentation\n\n- The package uses the `phpunit.xml`. Set up your environment variables in the `phpunit.xml` file.\n\n```xml\n\n\u003cphpunit\u003e\n    // ...\n    \u003cphp\u003e\n        \u003cenv name=\"APP_ENV\" value=\"testing\"/\u003e\n        \u003cenv name=\"CACHE_DRIVER\" value=\"array\"/\u003e\n    \u003c/php\u003e\n\u003c/phpunit\u003e\n```\n\n**NOTE**: The package **doesn't** use the `.env` file. You'll have to primarily set all your variables in\nyour `phpunit.xml` file.\n\n- Your testcases should extend the `\\Anik\\Testbench\\TestCase` class.\n- Your testcases will have access to the [Lumen testing APIs](https://lumen.laravel.com/docs/master/testing).\n\n### Bootstrapping\n\nThe package internally boots the Lumen application for the test cases. While bootstrapping, you can add some\nfunctionalities.\n\n- `afterApplicationCreated($callback)` registers the callbacks that will be called after the application is created. If\n  you register the callback after the application is created, it'll be fired immediately. The callback will access to\n  the `Laravel\\Lumen\\Application` instance.\n- `afterApplicationRefreshed($callback)` registers the callbacks that will be called after the application is refreshed.\n  If you register the callback after the application is refreshed, it'll be fired immediately. The callback will have\n  access to the `Laravel\\Lumen\\Application` instance.\n- `beforeApplicationDestroyed($callback)` registers the callback that will be called before the application is getting\n  destroyed. Will have access to the `Laravel\\Lumen\\Application` instance.\n- `afterApplicationDestroyed($callback)` registers the callback that will be called after the application has been\n  destroyed. Will have access to the `Laravel\\Lumen\\Application` instance.\n\n---\n\nThe application does not by default loads the `facade` and `eloquent`. If you need to enable\n\n- Facade, the return `true` from the `withFacade` method. Default: `false`.\n- Eloquent, the return `true` from the `withEloquent` method. Default: `false`.\n\n---\n\nTo load your required service providers, you can return an **array** of providers from the `serviceProviders()` method.\nDefault is `[]`.\n\n```php\n\u003c?php\n\nprotected function serviceProviders(): array\n{\n    return [\n        // AppServiceProvider::class,\n        // FormRequestServiceProvider::class,\n        // AmqpServiceProvider::class,\n    ];\n}\n```\n\n---\n\nTo add your middlewares, you can add both the global and route middlewares.\n\n- To add global middlewares, you have to return an array of middlewares from the `globalMiddlewares` method. The method\n  has access to the `Laravel\\Lumen\\Application` instance.\n\n```php\n\u003c?php\n\nprotected function globalMiddlewares(Application $app): array\n{\n    return [\n        // CorsMiddleware::class,\n        // NewrelicMiddleware::class,\n    ];\n}\n```\n\n- To add route middlewares, you have to return an associative array of middlewares from the `routeMiddlewares`. The\n  method has access to the `Laravel\\Lumen\\Application` instance.\n\n```php\n\u003c?php\n\nprotected function routeMiddlewares(Application $app): array\n{\n    return [\n        // 'auth' =\u003e Authenticate::class,\n        // 'admin' =\u003e AdminMiddleware::class,\n    ];\n}\n```\n\n---\n\nBy default, the application has the access to the`/` endpoint returning the `app()-\u003eversion()` as the response. To\ndefine your routes for the test purpose, you can use the `routes` method. The method has access to\nthe `Laravel\\Lumen\\Routing\\Router` instance. Defining routes in this method is as same as writing methods in\nthe `routes/web.php` or `routes/api.php`\n\n```php\n\u003c?php\n\nprotected function routes(Router $router): void\n{\n    $router-\u003eget('test-route', function () {\n        return response()-\u003ejson([\n            'error' =\u003e false,\n            'message' =\u003e 'Test route is executed'\n        ], 202);\n    });\n}\n```\n\n---\n\nIf you don't want to report an **Exception**, you can use the `dontReportExceptions` method. The defined exceptions will\nnot be reported. Default is `[]`.\n\n```php\n\u003c?php\n\nprotected function dontReportExceptions(): array\n{\n    return [\n        // AuthenticationException::class,\n    ];\n}\n```\n\n---\n\nIf it's required to work with the `$app` instance before the service providers are being registered,\nthen `beforeServiceProviders` is the method to consider. It'll be called for each test methods.\n\n```php\nprotected function beforeServiceProviders(Application $app)\n{\n    $app['config']-\u003eset(['my-package.enabled' =\u003e false]);\n}\n```\n\n### Annotations\n\nThere are three types of annotations considered during the test run. All the annotated tasks are executed synchronously.\nAll the tasks will receive the `\\Laravel\\Lumen\\Application\\Application` instance in their parameter.\n\n- `@pre-service-register` - Annotated tasks will run before the service providers are being registered. Maybe\n  useful for modifying values in config before the service provider gets registered.\n- `@post-service-register` - Annotated tasks will run after the service providers are being registered.\n- `@setup-before` - Annotated tasks will run after the application has boot properly and before running each testcases.\n  If your tests need to perform some sort of actions before running it, **i.e.** changing environment values, binding\n  something to the container, etc. then you can perform those actions with annotations. Only the method level\n  annotations are executed.\n\nSee [Annotation Test class](https://github.com/ssi-anik/testbench-lumen/blob/main/tests/Integration/AnnotationTest.php)\nto get the hang of it.\n\n```php\n\u003c?php\n\nprotected function firstCalled(Application $app)\n{\n    $app-\u003ebind('value-should-be-found', function () {\n        return 'as-is';\n    });\n}\n\nprotected function secondCalled(Application $app)\n{\n    $app-\u003ebind('value-should-be-found', function () {\n        return 'modified';\n    });\n}\n\n/**\n * @setup-before firstCalled\n * @setup-before secondCalled\n */\npublic function testMultipleAnnotations()\n{\n    $this-\u003eassertEquals('modified', $this-\u003eapp-\u003emake('value-should-be-found'));\n}\n\npublic function defineEnvironmentVariables(Application $app)\n{\n    $app['config']-\u003eset(['testbench-lumen.enabled' =\u003e true]);\n}\n\n/**\n * @pre-service-register defineEnvironmentVariables \n */\npublic function testDefineEnvAnnotation()\n{\n    $this-\u003eassertEquals(true, $this-\u003eapp['config']-\u003eget('testbench-lumen.enabled'));\n}\n```\n\n---\n\n### Examples\n\nAll the scenarios are covered with tests. You can use them as examples.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fssi-anik%2Ftestbench-lumen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fssi-anik%2Ftestbench-lumen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fssi-anik%2Ftestbench-lumen/lists"}