1

I know how to test classes and functions but I was wondering how to test a file and pass parameters via the console to this file.

For example I have index.php which needs 1 integer num via the console using fgets(STDIN). Can I make a PHPUnit file and test index.php ?

1 Answer 1

1

Unit Tested can be pretty much any code on the planet. In your case - pipe input data to script with echo bash command. (Of course depends on OS how to pass data through command shell) :

use PHPUnit\Framework\TestCase;

class ConsoleAppTest extends TestCase
{
    public function testIndexFile()
    {
       $out = shell_exec("echo 123 | php index.php");
       // check standard output or some DB modifications if script mangles DB
       $is_ok = verify_results($out);
       $this->assertSame($is_ok, true);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to do that using PHPUnit test framework?
adapted to PHPUnit. Just think about how to code function verify_results() - it strictly depends on your business logic what your script index.php does when invoked. BTW, personally I use SimpleTest because it has less complexities,- starting from Installation , of course probably less features, but SimpleTest is good enough for me.
Thanks for the info but I asked about PHPUnit and what I meant was if it has something which helps with testing files. So for example a function in a class that I could use. However if such thing doesnt exist I will accept your answer!
I doubt if there is any specific methods for file testing as for anything else which highly depends on business logic. Even data input in php script can be done in many different ways - fgets(),file_get_contents(),from database, executing other file, auto-generated and etc. Even file 'index.php' can be auto-generated itself by some conditions. How on earth Unit Framework will know that ? Too specific framework features == small spread of framework between developers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.