2

I know this question has been asked a hundred times but I am new to phpUnit and I don't know how to use the previous answers to help my case

This is my folder structure

    /src/MyDate.php
    /src/Autoload.php

    /tests/MyDateTest.php

    /vendor/phpunit/phpunit

Autoload files has this in it

<?php

  spl_autoload_register(
    function($class) {
      static $classes = null;
      if ($classes === null) {
        $classes = array(
          'mydate' => '/MyDate.php'
        );
      }
      $cn = strtolower($class);
      if (isset($classes[$cn])) {
        require __DIR__ . $classes[$cn];
      }
    }  
  );

And MyDateTest.php has this

<?php

class MyDateTest extends PHPUnit_Framework_TestCase
{
|
|
test be here

When I run

phpunit --bootstrap= src/autoload.php tests/MyDateTest.php

I get this error

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in C:\wamp64\www\datechallange\tests\MyDateTest.php on line 3

I tried every solution in similar answers. Tried requiring the file with the class but then other classes are missing. I tried modifying the autoload file to include the phpunit library but either I am doing something wrong or that is not the way. I tried to modify the xml file so I don't need to use --bootstrap but I have the same error.

I guess the key is to modify that autoload.php file, but I am not sure how.

Thanks

2 Answers 2

4

You appear to have one, or both of two problems.

  • Not using Composer, This will also provide a vendor/autoload.php file, that will automatically pull in any files that have been included via the composer.json file.
  • The latest version of PHPunit (v>6.0) no longer uses the long class names. the still-supported v5+ does though.

The recommended mechanism to install PHPunit 5.7 is:

composer require --dev phpunit/phpunit:^5.7

Alternatively, it can be downloaded as a single phpunit.phar that can be run much as any other PHP script.

Sign up to request clarification or add additional context in comments.

1 Comment

After a recent upgrade, it was bullet #2 that helped me. sed your test suite out and replace PHPUnit_Framework_TestCase with \phpunit\Framework\TestCase
0

You should try this in your test file.

use PHPUnit\Framework\TestCase;

class MyDateTest extends TestCase
{
}

This is the latest correct way of writing.

Comments

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.