https://github.com/keinos/practice_phpunit-test-of-stdin
✅ Sample PHPUnit test of a function that receives a value from "STDIN" input.
https://github.com/keinos/practice_phpunit-test-of-stdin
php phpunit phpunit-tests
Last synced: about 1 month ago
JSON representation
✅ Sample PHPUnit test of a function that receives a value from "STDIN" input.
- Host: GitHub
- URL: https://github.com/keinos/practice_phpunit-test-of-stdin
- Owner: KEINOS
- License: mit
- Created: 2020-04-26T03:29:35.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-04-26T08:14:31.000Z (about 5 years ago)
- Last Synced: 2025-02-09T16:15:19.971Z (3 months ago)
- Topics: php, phpunit, phpunit-tests
- Language: PHP
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/KEINOS/Practice_PHPUnit-test-of-STDIN)
# PHPUnit Test Sample For STDIN
This 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.
```php
function getContentsFromStdin()
{
$contents = \file_get_contents('php://stdin');
if($contents === false){
throw new \RuntimeException('Failed to read contents from STDIN.');
}return $contents;
}
```## The test
To 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`.
```php
final class FunctionGetContentsFromStdinTest extends \PHPUnit\Framework\TestCase
{
public function testRegularInput()
{
$result_expect = 'World!';// Register stream wrapper "MockPhpStream" to "php://" protocol
$existed = in_array('php', \stream_get_wrappers());
if ($existed) {
\stream_wrapper_unregister("php");
}
\stream_wrapper_register("php", '\\KEINOS\Tests\MockPhpStream');// Set value to STDIN
\file_put_contents('php://stdin', $result_expect);// Get value from function and restore registration
$result_actual = \KEINOS\Sample\getContentsFromStdin();
\stream_wrapper_restore("php");// Test
$this->assertSame($result_expect, $result_actual);
}
}
```## The Wrapper
The wrapper script to mock the `PHP://` stream.
- [./tests/MockPhpStream.php](./tests/MockPhpStream.php)
This 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).
## How to Test
To see the tests in action clone this repo and run the test.
### Test it locally
```shellsession
$ composer install
...
$ composer test
> ./vendor/bin/phpunit --testdox --bootstrap ./vendor/autoload.php tests
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.GetContentsFromFunction
✔ Regular inputTime: 76 ms, Memory: 4.00 MB
OK (1 test, 1 assertion)
```
### Test it via Docker
```shellsession
$ docker build -t test:local .
...
$ docker run --rm test:local
> ./vendor/bin/phpunit --testdox --bootstrap ./vendor/autoload.php tests
PHPUnit 9.1.3 by Sebastian Bergmann and contributors.Get Contents From Function
✔ Regular inputTime: 00:00.030, Memory: 4.00 MB
OK (1 test, 1 assertion)
```