{"id":41977426,"url":"https://github.com/brandon14/unit-test-examples","last_synced_at":"2026-01-25T23:33:18.961Z","repository":{"id":36213491,"uuid":"159921432","full_name":"brandon14/unit-test-examples","owner":"brandon14","description":"Demo repository highlighting writing PHP Unit tests.","archived":false,"fork":false,"pushed_at":"2023-08-07T02:57:58.000Z","size":820,"stargazers_count":2,"open_issues_count":13,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-04-14T06:04:17.325Z","etag":null,"topics":["php","unit-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/brandon14.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-01T07:45:54.000Z","updated_at":"2023-09-19T13:16:37.000Z","dependencies_parsed_at":"2023-02-14T06:25:19.113Z","dependency_job_id":null,"html_url":"https://github.com/brandon14/unit-test-examples","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/brandon14/unit-test-examples","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brandon14%2Funit-test-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brandon14%2Funit-test-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brandon14%2Funit-test-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brandon14%2Funit-test-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brandon14","download_url":"https://codeload.github.com/brandon14/unit-test-examples/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brandon14%2Funit-test-examples/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28761814,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-25T23:06:19.311Z","status":"ssl_error","status_checked_at":"2026-01-25T23:03:50.555Z","response_time":113,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["php","unit-testing"],"created_at":"2026-01-25T23:33:15.839Z","updated_at":"2026-01-25T23:33:18.947Z","avatar_url":"https://github.com/brandon14.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/brandon14/unit-test-examples/actions/workflows/run-tests.yml\"\u003e\u003cimg src=\"https://img.shields.io/github/workflow/status/brandon14/unit-test-examples/run-tests?style=flat-square\u0026maxAge=36000\" alt=\"Build Status\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://codeclimate.com/github/brandon14/unit-test-examples/maintainability\"\u003e\u003cimg src=\"https://img.shields.io/codeclimate/maintainability/brandon14/unit-test-examples.svg?style=flat-square\" alt=\"Code Climate Maintainability\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://codecov.io/gh/brandon14/unit-test-examples\"\u003e\u003cimg src=\"https://img.shields.io/codecov/c/github/brandon14/unit-test-examples.svg?style=flat-square\" alt=\"CodeCov\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://github.com/brandon14/unit-test-examples/blob/master/LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/github/license/brandon14/unit-test-examples.svg?style=flat-square\" alt=\"License\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/brandon14/unit-test-examples/issues\"\u003e\u003cimg src=\"https://img.shields.io/github/issues/brandon14/unit-test-examples.svg?style=flat-square\" alt=\"Issues\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://github.com/brandon14/unit-test-examples/issues?q=is%3Aissue+is%3Aclosed\"\u003e\u003cimg src=\"https://img.shields.io/github/issues-closed/brandon14/unit-test-examples.svg?style=flat-square\" alt=\"Issues Closed\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://github.com/brandon14/unit-test-examples/pulls\"\u003e\u003cimg src=\"https://img.shields.io/github/issues-pr/brandon14/unit-test-examples.svg?style=flat-square\" alt=\"Pull Requests\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://github.com/brandon14/unit-test-examples/pulls?q=is%3Apr+is%3Aclosed\"\u003e\u003cimg src=\"https://img.shields.io/github/issues-pr-closed/brandon14/unit-test-examples.svg?style=flat-square\" alt=\"Pull Requests Closed\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n# Unit Test Examples\n\n## Table Of Contents\n\n1. [Requirements](https://github.com/brandon14/unit-test-examples#requirements)\n2. [Purpose](https://github.com/brandon14/unit-test-examples#purpose)\n3. [Standards](https://github.com/brandon14/unit-test-examples#standards)\n4. [Coverage](https://github.com/brandon14/unit-test-examples#coverage)\n5. [Contributing](https://github.com/brandon14/unit-test-examples#contributing)\n6. [Documentation](https://github.com/brandon14/unit-test-examples#documentation)\n\n## Requirements\n\n| Tech                                 | Version  |\n| ------------------------------------ | -------- |\n| [PHP](https://secure.php.net/)       | \u003e= 7.4.0 |\n| [Composer](https://getcomposer.org/) | *        |\n\n| PHP Extension    | Version  |\n| ---------------- | -------- |\n| ext-pdo          | *        |\n| ext-redis        | \u003e= 5.0.0 |\n| ext-zend-opcache | *        |\n\n\n## Purpose\n\nThis repository serves to demonstrate writing unit test for a PHP library using PHP Unit.\nThese tests are in no way perfect, but I tried to get good coverage while still\nproviding meaningful test. After all you can have 100% coverage and still not really\ntest the full functionality of your application.\n\nThe project is composed of two *\"service\"* classes that make up the functionality\nof this mock library. One is the `LastModified` service and the other is the `StatusService`.\n\nThe `LastModified` service provides a way to register `LastModifiedTimeProvider` classes\nthat can return an `int` time value that represents the time that whatever the provider is\nrepresenting (i.e. Users, files, Products, etc.) was last modified. The service can take\n1 to many providers, and return the most recent modified time. If you could imagine, this\ncould be used as part of a blog website were the providers could be one to iterate over\nthe webserver files to get the last modified time, and another provider to check for the\nmost recent blog post in the database. The service also facilitates getting the last modified\ntime for a single provider or an array of registered providers.\n\nThe `StatusService` functions in a similar way with `StatusServiceProviders` that can\nreturn an array containing the \"status\" of whatever the provider is representing (i.e.\nweb service, database, cache service, etc). The service facilitates getting the status of\na single provider, many, or all providers much like the `LastModified` service. As you\ncould imagine, this could be useful in many ways. If you had a website that hit a database,\nyou could use this service to provide an API to get the status of the database.\n\nWith both services, I provided a couple simple providers to provide an example on how\nto implement the providers. Also as stated above, the test serve to show how to write\nPHP Unit tests and have examples of mocking the filesystem, and through clean architecture\nyou can have classes that need external service such as databases and cache services and\nbe able to write unit tests without breaking outside the boundary of the application\nusing mock objects.\n\nWhen running the tests via `phpunit`, the order of the tests are randomized. The reason for this\nis so that you can identify tests that depend upon other tests due to shared state (i.e. database state).\nIn these examples, there is no shared state amongst tests, but it is still\ngood to randomize unit test order so that none of these issues will creep into the project.\n\n## Standards\n\nThis project adheres to PSR standards where applicable. Also I tried to design this example\nproject using a clean architecture that promotes SOLID principles and seeks to help\nothers in writing cleaner code. I am no expert by any means, but I feel although the\nexample services may be a bit contrived, the implementation of them follow at least good\npractices for the most part.\n\nI also wanted to use at least PHP 7.1 to show off easier input type validation for method\nparameters. By making PHP more strict, not only do we help the compiler out, it also makes\nour code less error prone. Imagine writing a function the takes an array as a parameter with\nno type hint, and 4 years down the road some new developer doesn't read the docs (or they weren't\nthere) and passes a string into it, and :boom:, some code just blew up.\n\n## Coverage\n\nThe latest code coverage information can be found via [Codecov](https://codecov.io/gh/brandon14/unit-test-examples). We\nstrive to maintain 100% coverage as this is an example repo highlighting writing unit tests in PHP.\n\n## Contributing\n\nGot something you wanna add? Found a bug or otherwise bad code? Feel free to submit pull\nrequests to add in new features, fix bugs, or clean things up. Just be sure to follow the\n[Code of Conduct](https://github.com/brandon14/unit-test-examples/blob/master/.github/CODE_OF_CONDUCT.md)\nand [Contributing Guide](https://github.com/brandon14/brandonclothier.me/blob/master/.github/CONTRIBUTING.md),\nand I encourage creating clean and well described pull requests if possible.\n\n## Documentation\n\nDocumentation to this project can be found [here](https://brandon14.github.io/unit-test-examples/). While this isn't a library\nand does not really need documentation per-se, I wanted to add auto-building the documentation as a Github Actions in order\nto explore the possibilities with Github Actions.\n\nThis code is released under the MIT license.\n\nCopyright \u0026copy; 2018-2021 Brandon Clothier\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://forthebadge.com\"\u003e\u003cimg src=\"https://forthebadge.com/images/badges/60-percent-of-the-time-works-every-time.svg\" alt=\"Works 60% Of The Time\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://forthebadge.com\"\u003e\u003cimg src=\"https://forthebadge.com/images/badges/certified-steve-bruhle.svg\" alt=\"Dr. Steve Brule\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://forthebadge.com\"\u003e\u003cimg src=\"https://forthebadge.com/images/badges/contains-technical-debt.svg\" alt=\"Contains Technical Debt\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://forthebadge.com\"\u003e\u003cimg src=\"https://forthebadge.com/images/badges/built-by-neckbeards.svg\" alt=\"Built By Neckbeards\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n---\n\n## Contributors\n\n![GitHub Contributors Image](https://contrib.rocks/image?repo=brandon14/unit-test-examples)\n\n---\n\n## 😂 Here is a random joke that'll make you laugh!\n\n![Jokes Card](https://readme-jokes.vercel.app/api)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrandon14%2Funit-test-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrandon14%2Funit-test-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrandon14%2Funit-test-examples/lists"}