{"id":19783139,"url":"https://github.com/edmondscommerce/mock-server","last_synced_at":"2025-04-30T22:31:22.895Z","repository":{"id":56975107,"uuid":"124100562","full_name":"edmondscommerce/mock-server","owner":"edmondscommerce","description":"Mock Web Server and Router - great for testing PHP projects that interact with remote web services and web sites","archived":false,"fork":false,"pushed_at":"2019-07-22T11:21:58.000Z","size":146,"stargazers_count":2,"open_issues_count":6,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-06T05:34:26.158Z","etag":null,"topics":["composer","html","mock-server","php","testing"],"latest_commit_sha":null,"homepage":"","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/edmondscommerce.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":"2018-03-06T15:44:52.000Z","updated_at":"2018-11-08T16:16:39.000Z","dependencies_parsed_at":"2022-08-21T07:40:30.101Z","dependency_job_id":null,"html_url":"https://github.com/edmondscommerce/mock-server","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edmondscommerce%2Fmock-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edmondscommerce%2Fmock-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edmondscommerce%2Fmock-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edmondscommerce%2Fmock-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edmondscommerce","download_url":"https://codeload.github.com/edmondscommerce/mock-server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251791579,"owners_count":21644421,"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":["composer","html","mock-server","php","testing"],"created_at":"2024-11-12T06:07:24.421Z","updated_at":"2025-04-30T22:31:22.554Z","avatar_url":"https://github.com/edmondscommerce.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mock Web Server and Router \n## By [Edmonds Commerce](https://www.edmondscommerce.co.uk)\n\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/a791bb0914a243749b3c9918c70af2da)](https://www.codacy.com/app/edmondscommerce/mock-server?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=edmondscommerce/mock-server\u0026amp;utm_campaign=Badge_Grade) \n[![Build Status](https://travis-ci.org/edmondscommerce/mock-server.svg?branch=master)](https://travis-ci.org/edmondscommerce/mock-server)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/edmondscommerce/mock-server/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/edmondscommerce/mock-server/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/edmondscommerce/mock-server/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/edmondscommerce/mock-server/?branch=master)\n\n## Installation\n\n### PHP 7.0\n\nInstall via composer\n\n`composer require edmondscommerce/mock-server:~1 --dev`\n\n### PHP 7.1+\n\nInstall via composer\n\n`composer require edmondscommerce/mock-server:~2 --dev`\n\n\n## Usage\n\nTo start the web server, you need to instantiate the `\\EdmondsCommerce\\MockServer\\MockServer` and call `startServer`\n\n```php\n\u003c?php\n$mockServer=\\EdmondsCommerce\\MockServer\\Factory::getMockServer();\n$mockServer-\u003estartServer();\n```\n\n### Configuration\n\nWhen using the Factory, the configuration for the MockServer is pulled from `MockServerConfig` which in turn checks for values in the `$_SERVER` superglobal - generally populated with any thing that has been exported from your environment. \n\nThe default values are fairly sensible though. Based upon the project root, the default configuration expects you to have a [`tests`](./tests) folder which in turn contains a [`MockServer`](./tests/MockServer) folder. Inside the MockServer folder we expect a `router.php` file and a `htdocs` folder which contains static assets to be served directly.\n \n**Note -** this is exactly as it has been configured in this library.\n\n### Router\n\nAn example of a basic router set up can be found in [router.php](./tests/MockServer/router.php)\n\nThe router file should contain an instance of the `\\EdmondsCommerce\\MockServer\\StaticRouter` which is a wrapper around\nSymfony's router class. The file should load the Composer autoloader and create the static router before registering routes for different URIs.\n\nThe router supports static file routes, callback routes and text routes.\n\n**Note -** there are helpful includes [routerTop.php](./src/include/routerTop.php) and [routerBottom.php](./src/include/routerBottom.php) which handle some basic boilerplate for you.\n\n#### Template\n\nHere is a template router for you to start with:\n\n```php\n\u003c?php declare(strict_types=1);\nrequire __DIR__.'/../../vendor/edmondscommerce/mock-server/src/include/routerTop.php';\n\n$router = \\EdmondsCommerce\\MockServer\\Factory::getStaticRouter();\n\n//Add your routes here\n\n/**\n * IMPORTANT - you have to `return` the required routerBottom\n */\nreturn require __DIR__.'/../../vendor/edmondscommerce/mock-server/src/include/routerBottom.php';\n```\n\n### Starting the server manually\n\nTo start the server manually, you can simply use [start-mock-server](./bin/start-mock-server) which will start a backgrounded mock server. If you want it in the foreground, simply pass \"foreground\" as an argument\n\n```bash\n./bin/start-mock-server foreground\n```\n\nIf you want the server to listen on a specific IP address, you can do this by exporting a config variable:\n\n```bash\nexport MockServer_Ip=\"0.0.0.0\"\n```\n\n\n## Router Types\n\n### Static Files (css/js/html/etc...)\n\nStatic files that are located in the htdocs folder will be served without any further configuration\n\nFor the full list of support file types, see: [StaticRouter::STATIC_EXTENSIONS_SUPPORTED](./src/StaticRouter.php#L27)\n\n### Callback\n\nThe callback router sets a closure which will be passed the request object and must return a response object.\n\nSee [this test](./tests/StaticRouterTest.php#L88) for an exmaple of a callback.\n\n### File Download\n\nThe download router will return a file as a download. Internally it sets a callback that then returns a BinaryFileResponse object\n\n### Static Text\n\nSecond param of `addRoute($uri, $response)` is the text that will be returned after visiting specified uri.\n\nSee [this test](./tests/StaticRouterTest.php#L40) for an example of text route.\n\n### Static \n\nSecond param of `addStaticRuote($uri, $response)` is the the file content that will be returned after visiting specified uri.\n\nDo not use this to return files that are in the htdocs folder, it's pointless. This is largely being kept for legacy reasons.\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedmondscommerce%2Fmock-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedmondscommerce%2Fmock-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedmondscommerce%2Fmock-server/lists"}