{"id":17236313,"url":"https://github.com/there4/slim-unit-testing-example","last_synced_at":"2025-04-14T02:31:18.897Z","repository":{"id":11009949,"uuid":"13335873","full_name":"there4/slim-unit-testing-example","owner":"there4","description":"Unit Testing Slim - Example PHPUnit route testing and mocking with the Slim Framework dependency injection container.","archived":false,"fork":false,"pushed_at":"2016-08-02T03:05:05.000Z","size":99,"stargazers_count":122,"open_issues_count":3,"forks_count":31,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-03-27T16:40:56.025Z","etag":null,"topics":[],"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/there4.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":"2013-10-04T22:13:41.000Z","updated_at":"2025-01-13T19:35:39.000Z","dependencies_parsed_at":"2022-08-30T02:20:52.311Z","dependency_job_id":null,"html_url":"https://github.com/there4/slim-unit-testing-example","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/there4%2Fslim-unit-testing-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/there4%2Fslim-unit-testing-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/there4%2Fslim-unit-testing-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/there4%2Fslim-unit-testing-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/there4","download_url":"https://codeload.github.com/there4/slim-unit-testing-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248810883,"owners_count":21165195,"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":"2024-10-15T05:35:53.383Z","updated_at":"2025-04-14T02:31:18.542Z","avatar_url":"https://github.com/there4.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Slim Unit Testing Example [![Build Status](https://travis-ci.org/there4/slim-unit-testing-example.png?branch=master)](https://travis-ci.org/there4/slim-unit-testing-example) [![Code Climate](https://codeclimate.com/github/there4/slim-unit-testing-example/badges/gpa.svg)](https://codeclimate.com/github/there4/slim-unit-testing-example)\n================================================================================\n\u003e Integration and unit testing a Slim PHP application (Slim V2.x)\n\n## Slim V2.x\nThe current stable master of this project is for Slim V2.x. Work is in progress\nto bring this to the new Slim 3.\n\nThis is a sample application to show an approach to integration and unit testing\na [Slim][slim] application. To skip to the heart of this, go check out the\n[testing bootstrap][bootstrap]. It sets a mock environment and provides some\nhelper methods for testing Slim routes.\n\n## About\n\n[Slim][slim] is a great PHP framework with a small footprint and everything you\nneed to build fast applications. I've found it particularly well suited to\ndelivering data to [BackboneJS][bb] applications.\n\nHowever, I haven't found a great deal of information about integration and unit\ntesting with Slim, and have developed my own approach. I've refactored and\nintroduced it into this sample application. I hope it will help others on their\npath to using this great framework.\n\nThis application demonstrates some techniques for integration and unit testing.\nWith this approach, you'll be able to test your application without the need of\nCurl, webservers, or anything other than [PHPUnit][phpunit] installed on your\nsystem. This makes it easy to test your entire app in an automated way with\n[TravisCI][tci]. Check out the [.travis.yml][yml] file in this project for an\nexample.\n\n## Example\n\nHere's [a test][version_test] for a very simple endpoint that returns the\nversion from the application config. We're asserting that Slim responded with a\n`200` and that the version matches what we expect.\n\n```php\nclass VersionTest extends LocalWebTestCase {\n    public function testVersion() {\n        $this-\u003eclient-\u003eget('/version');\n        $this-\u003eassertEquals(200, $this-\u003eclient-\u003eresponse-\u003estatus());\n        $this-\u003eassertEquals($this-\u003eapp-\u003econfig('version'), $this-\u003eclient-\u003eresponse-\u003ebody());\n    }\n}\n```\n\n## Installation\n\nClone the repository and then run `composer install` and then `phpunit`. This\napplication assumes that you have `phpunit` installed globally on your system.\nThis application can be run as a functioning website. You can you use the sample\napache config file in the `build/` folder, or use the native php webserver. To\nuse the php webserver, run `php -S localhost:8080 -t public/` from the project\nroot and open your browser to [http://localhost:8080][lh]\n\n## Concepts\n\nThe `public/index.php` file serves as the application entry point. This file\ninitializes a Slim `$app` with production configuration, includes the routes\nfile from `app/app.php` and then runs the app with `$app-\u003erun();`. This allows\nus to keep our application separate from the index, and gives us an opportunity\nto include our `app/app.php` file in a different context.\n\nWhen phpunit runs, it looks for the phpunit.xml file in our root. This file\nspecifies a testing bootstrap file. PHPUnit includes `testing/bootstrap.php`.\nThis file creates an `$app`, just like in `build/index.php`, but it uses\ntesting configuration. The bootstrap keeps a reference to `$app` for the testing\nframework, and then provides several helper methods for `GET`, `POST`, `PUT`,\n`PATCH`, `HEAD`, and `DELETE`.\n\nWith these methods, you can run tests on Slim routes without a webserver. The\ntests run entirely within a mock environment and will be fast and efficient.\n\n## Unit Testing vs. Integration Testing\n\nUnit tests should test an individual part of code. The system under test should\nbe as small as possible. You would unit test an individual method. Integration\ntesting exercises an entire system. Most of this example is about integration\ntesting. We are running tests that work Slim from initial instantiation to the\nfinal delivery of data. With integration tests, we're treating the entire\napplication as a unit, setting up a particular initial environment and then\nexecuting the `run()` command and finally inspecting the results to ensure that\nthey match our expectations.\n\n## Mocking with Slim\n\nSee the [ZenTest][zen_test] for an example of mocking with Slim dependency\ninjection. In this test we mock a Curl wrapper class from [Shuber][shuber]. This\nallows us to substitute responses and exercise the parts of our application that\nwe feel need testing. It also allows us to run these unit tests on systems that\ndon't have the curl extension installed. We're totally isolated from that\ndependency while this running test.\n\nThe [FileStoreTest][file_test] uses a mock for the authentication\nclass. Notice that the file store route doesn't use that class directly, but\ninstead it is used by the application authenticator method. We're using the app\ndependency injection container to swap out the real object for a mock version.\nThis approach allows us to control authentication results from within our test\nharness.\n\nYou can read more about dependency injection in the [SlimDocs on DI][di], and\nmore about mock objects in the [PHPUnit docs][php_mock].\n\n## Site Tooling\n\nI'd like to give a nod to [Pake][pake]. It's a flexible and powerful build tool\nwritten in PHP. If you've got lots of JavaScript, I might recommend\n[Grunt][grunt] or [Gulp][gulp]. However, for APIs and other sites that need a\nbuild system - I highly recommend Pake. It's got enough tools to handle SSH\ndeployments and other sophisticated build steps. In this project, it's used to\nsetup the dev web server and handle some code sniffs. With the\n[Pake CLI][pake-cli] tool you don't have to install it globally. I think\nit's a compelling and overlooked tool. [Go see it][pake]!\n\n## Contributing\n\nOpen an [issue][issues] for questions, comments, or suggestions. Pull requests\nare welcome, please format the code to PSR-2 standards and include an\nexplanation of the benefits.\n\n\n## Contributors\n\n| Author | Commits\n| --- | ---\n| Craig Davis | 63 |\n| Jeremy Kendall | 3 |\n| guillermo-fisher | 1 |\n\n## Changelog\n\n* 0.1.1 Update Readme and remove `echo` and `include` in place of a proper\n  rendering.\n* 0.1.0 Backwards compatibility breaking - Reorder the parameters on the\n  `get()`, `post()` and http testing methods to be in the new order of\n  `$this-\u003e$method($path, $formVars, $optionalHeaders);`. This makes the testing\n  a little more terse, and clears up any confusion with improved parameter\n  names.\n* 0.0.9 Bug fix for [issue 4][issue4], with thanks to origal for his work in\n  solving a problem with get parameters.\n\n\n## Thanks\n\nThanks must be given to [Nicholas Humfrey][njh] for his work in this\n[integration testing harness][njh_test].\n\n[grunt]: http://gruntjs.com/\n[gulp]: http://gulpjs.com/\n[pake]: https://github.com/indeyets/pake\n[pake-cli]: https://github.com/there4/pake-cli\n[bb]: http://backbonejs.org\n[bootstrap]: https://github.com/there4/slim-unit-testing-example/blob/master/tests/bootstrap.php\n[di]: http://docs.slimframework.com/#Dependency-Injection\n[file_test]: https://github.com/there4/slim-unit-testing-example/blob/master/tests/integration/FileStoreTest.php\n[issue4]: https://github.com/there4/slim-unit-testing-example/issues/4\n[issues]: https://github.com/there4/slim-unit-testing-example/issues\n[lh]: http://localhost:8080\n[njh]: https://github.com/njh\n[njh_test]: https://github.com/njh/njh.me/blob/master/test/IntegrationTest.php\n[php_mock]: http://phpunit.de/manual/3.0/en/mock-objects.html\n[phpunit]: http://phpunit.de/manual/current/en/index.html\n[shuber]: https://github.com/shuber/curl\n[si]: http://docs.slimframework.com/#Response\n[slim]: http://www.slimframework.com/\n[tci]: http://travis-ci.org\n[version_test]: https://github.com/there4/slim-unit-testing-example/blob/master/tests/integration/VersionTest.php\n[yml]: https://github.com/there4/slim-unit-testing-example/blob/master/.travis.yml\n[zen_test]: https://github.com/there4/slim-unit-testing-example/blob/master/tests/integration/ZenTest.php\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthere4%2Fslim-unit-testing-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthere4%2Fslim-unit-testing-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthere4%2Fslim-unit-testing-example/lists"}