0

I have a small .php file with that one function which I've pasted below. I would like to test that function.

Is there any way of how I can use phpunit? I'm not using composer.json.

<?php public function LoginMe($a, $b) //nick, psw
        {
        require 'includes/mysql_connection.php';
        require 'includes/config.php';
        require 'includes/messages.php';
        require 'backend/LogsSystem.php';

                // Some code

                return false;
        } ?>
  • Yes, you can use Phpunit to write a unit-test for a function. It has been made for that. However it requires you to setup a working version of Phpunit first. Then check if your test-suites bootstrapping process covers all the initialization needed (or use the setup before class method) so that the function is available. When you require or include files be aware of the side-effects. It are these side-effects that often prevent other parts (here: the test-suite or runner) from working properly. It's worth to debug such issues so you can more easily learn about the side-effects and solve them. – hakre Jun 02 '19 at 14:49

1 Answers1

2

For example, if your script is called authentication.php, this can be a hint for test:

<?php
use PHPUnit\Framework\TestCase;
include 'authentication.php';
class authenticationTest extends TestCase
{
    public function testAuthentication()
    {
    //your test here, call LoginMe with expected parameters
    }
}

You may find interesting this too: PHP Testing, for Procedural Code

GabrieleMartini
  • 1,665
  • 2
  • 19
  • 26
  • I have tried it, does not work. Also I am using Linux, but locally doesn't work also. For example linux errors: PHP Fatal error: Class 'PHPUnit\Framework\TestCase' not found in /home/valkas1/public_html/testing.php on line 5 – last of us fan May 20 '19 at 22:40
  • I just dont understand what am I missing – last of us fan May 20 '19 at 22:40