How to do PHPUni tests in CLI script?

Published 2 months ago by gidaban79

I know subject is not with Laravel, but maybe someone can expain me how to do PHPUnit test?

I have already script in PHP i know it works, but how to write test for it ?

it's simple script to generate multiplication table. Thing is script generates and display data.

Best Answer (As Selected By gidaban79)
ChristophHarms

Thing is script generates and display data.

That makes it hard to test. You could use expectOutputString() to test against console output. Or you could use shell_exec() in your test and inspect the returned string. But that would be neither elegant nor expedient.

What you should do is refactor your script and separate logic from presentation. Write classes that only generate the data and other classes that only display it. That way, you can instantiate those classes in your tests and test their methods. Neat and lean. :)

ChristophHarms

Thing is script generates and display data.

That makes it hard to test. You could use expectOutputString() to test against console output. Or you could use shell_exec() in your test and inspect the returned string. But that would be neither elegant nor expedient.

What you should do is refactor your script and separate logic from presentation. Write classes that only generate the data and other classes that only display it. That way, you can instantiate those classes in your tests and test their methods. Neat and lean. :)

gidaban79

Thanks :)

Please sign in or create an account to participate in this conversation.