{"id":15952151,"url":"https://github.com/keinos/practice_phpunit-test-of-stdin","last_synced_at":"2026-04-30T00:08:12.518Z","repository":{"id":102705381,"uuid":"258930433","full_name":"KEINOS/Practice_PHPUnit-test-of-STDIN","owner":"KEINOS","description":"✅   Sample PHPUnit test of a function that receives a value from \"STDIN\" input.","archived":false,"fork":false,"pushed_at":"2020-04-26T08:14:31.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-09T16:15:19.971Z","etag":null,"topics":["php","phpunit","phpunit-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/KEINOS.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-26T03:29:35.000Z","updated_at":"2020-08-02T11:23:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"fc5bcad8-7bbb-4613-abc7-07e0a92c7978","html_url":"https://github.com/KEINOS/Practice_PHPUnit-test-of-STDIN","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KEINOS%2FPractice_PHPUnit-test-of-STDIN","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KEINOS%2FPractice_PHPUnit-test-of-STDIN/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KEINOS%2FPractice_PHPUnit-test-of-STDIN/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KEINOS%2FPractice_PHPUnit-test-of-STDIN/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KEINOS","download_url":"https://codeload.github.com/KEINOS/Practice_PHPUnit-test-of-STDIN/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247123099,"owners_count":20887259,"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":["php","phpunit","phpunit-tests"],"created_at":"2024-10-07T13:06:53.372Z","updated_at":"2026-04-30T00:08:12.490Z","avatar_url":"https://github.com/KEINOS.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/KEINOS/Practice_PHPUnit-test-of-STDIN.svg?branch=master)](https://travis-ci.org/KEINOS/Practice_PHPUnit-test-of-STDIN)\n\n# PHPUnit Test Sample For STDIN\n\nThis is a sample PHPUnit test to check the functionability of the below function, which receives a value from \"STDIN\" (Standard Input) and returns that value.\n\n```php\nfunction getContentsFromStdin()\n{\n    $contents = \\file_get_contents('php://stdin');\n    if($contents === false){\n        throw new \\RuntimeException('Failed to read contents from STDIN.');\n    }\n\n    return $contents;\n}\n```\n\n## The test\n\nTo test the functionability of `STDIN` using PHPUnit, one way is to create a mock that overrides a `php://` stream as a wrapper and replaces the value from `STDIN`.\n\n```php\nfinal class FunctionGetContentsFromStdinTest extends \\PHPUnit\\Framework\\TestCase\n{\n    public function testRegularInput()\n    {\n        $result_expect = 'World!';\n\n        // Register stream wrapper \"MockPhpStream\" to \"php://\" protocol\n        $existed = in_array('php', \\stream_get_wrappers());\n        if ($existed) {\n            \\stream_wrapper_unregister(\"php\");\n        }\n        \\stream_wrapper_register(\"php\", '\\\\KEINOS\\Tests\\MockPhpStream');\n\n        // Set value to STDIN\n        \\file_put_contents('php://stdin', $result_expect);\n\n        // Get value from function and restore registration\n        $result_actual = \\KEINOS\\Sample\\getContentsFromStdin();\n        \\stream_wrapper_restore(\"php\");\n\n        // Test\n        $this-\u003eassertSame($result_expect, $result_actual);\n    }\n}\n```\n\n## The Wrapper\n\nThe wrapper script to mock the `PHP://` stream.\n\n- [./tests/MockPhpStream.php](./tests/MockPhpStream.php)\n\nThis helpful wrapper was made by [Denis](https://www.blogger.com/profile/06252737045102742909) in [his blog post](http://news-from-the-basement.blogspot.com/2011/07/mocking-phpinput.html).\n\n## How to Test\n\nTo see the tests in action clone this repo and run the test.\n\n### Test it locally\n\n```shellsession\n$ composer install\n...\n$ composer test\n\u003e ./vendor/bin/phpunit --testdox --bootstrap ./vendor/autoload.php tests\nPHPUnit 7.5.20 by Sebastian Bergmann and contributors.\n\nGetContentsFromFunction\n ✔ Regular input\n\nTime: 76 ms, Memory: 4.00 MB\n\nOK (1 test, 1 assertion)\n\n```\n\n### Test it via Docker\n\n```shellsession\n$ docker build -t test:local .\n...\n$ docker run --rm test:local\n\u003e ./vendor/bin/phpunit --testdox --bootstrap ./vendor/autoload.php tests\nPHPUnit 9.1.3 by Sebastian Bergmann and contributors.\n\nGet Contents From Function\n ✔ Regular input\n\nTime: 00:00.030, Memory: 4.00 MB\n\nOK (1 test, 1 assertion)\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeinos%2Fpractice_phpunit-test-of-stdin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkeinos%2Fpractice_phpunit-test-of-stdin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeinos%2Fpractice_phpunit-test-of-stdin/lists"}