Pavlin Petkov
Pavlin Petkov

Reputation: 1142

Test php file with console input with PHPUnit?

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 ?

Upvotes: 1

Views: 726

Answers (1)

Agnius Vasiliauskas
Agnius Vasiliauskas

Reputation: 11267

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);
    }
}

Upvotes: 1

Related Questions