0
votes

I keep getting this error in my command line on phpStorm when I try use phpunit. Can someone explain this to me? I'm new to phpunit.

C:\xampp\php\phpunit.bat

Warning: Missing argument 3 for PHPUnit_TextUI_TestRunner::doRun(), called in C:\xampp\php\pear\PHPUnit\TextUI\Command.php on line 176 and defined in C:\xampp\htdocs\tutorials\php_unit_testing\vendor\phpunit\phpunit\src\TextUI\TestRunner.php on line 148

PHPUnit 5.3-dev by Sebastian Bergmann and contributors.

Fatal error: Class 'controllers\core\web\Pages' not found in C:\xampp\htdocs\tutorials\php_unit_testing\tests\app\controllers\core\web\PagesTest.php on line 6

Process finished with exit code 255 at 22:23:20. Execution time: 186 ms.

Here is a my code from my xml file:

<?xml version='1.0' encoding='UTF-8' ?>

<phpunit bootstrap="./vendor/autoload.php"
     color="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     stopOnFailures="false"
     syntaxCheck="false"
    >
<testsuites>
        <testsuite name="Tutorial Unit Tests">
            <directory>./tests/</directory>
        </testsuite>
</testsuites>
</phpunit>

And this is the code from pages.php:

<?php


 namespace controllers\core\web;

 class Pages{
 public function rendor(){
    return 'Hello World';
 }

 public function return_true(){
    return true;
 }
 public function return_array(){
    return array('Hello', 'world', 'This', 'is', 'an', 'array');
 }
 }
 ?>

And this is the code from PagesTest.php:

<?php
class PagesTest extends PHPUnit_Framework_TestCase
{ 
public function testRenderReturnsHelloWorld(){ 

    $pages = new \controllers\core\web\Pages();

    $expected = 'Hello Word'; 

    $this->assertEquals($expected, $pages->rendor());

}
}
?>
2

2 Answers

1
votes

It doesn't look like you're loading in the library. Typically you have to create an alternate autoload.php file and include that in the phpunit bootsrap value. So it would be something like this:

<phpunit bootstrap="./autoload.php" ...

  #./auotload.php
  $loader = require_once __DIR__ . '/vendor/autoload.php';
  $loader.add('directory_of_code', 'path/to/that/directory');

  return $loader;
0
votes

After a lot of research I found a solution to the missing class. I add require_once 'app/controllers/core/web/pages.php'; to require the page where I had the class stored with the namespace. This worked for me and I hope it works for anyone else that comes across this. In my autoloader file that I made I have this code now

<?php
require_once 'app/controllers/core/web/pages.php';
$loader = require_once __DIR__ . '/vendor/autoload.php';

I never found figured out the part about

Warning: Missing argument 3 for PHPUnit_TextUI_TestRunner::doRun(), called in C:\xampp\php\pear\PHPUnit\TextUI\Command.php on line 176 and defined in C:\xampp\htdocs\tutorials\php_unit_testing\vendor\phpunit\phpunit\src\TextUI\TestRunner.php on line 148

If anyone figures this out please let me know.