0

I have a PhpUnit test class like this:

use PHPUnit\Framework\TestCase;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

final class SheetValidationTest extends TestCase {

    private function isTitleValid(string $title) {

        $worksheetStub = $this->createStub(Worksheet::class);
        $worksheetStub->method('getTitle')->willReturn($title);

        [ // ... act and assertion here ]
    }
}

When I run this test, I get:

Cannot stub or mock class or interface "PhpOffice\PhpSpreadsheet\Worksheet\Worksheet" which does not exist

The Worksheet class is present (in vendor) and Ctrl+Click takes me to that very class.
The actual code where that class is used works fine.

A Google search and SO search did not help me to solve this problem; anybody got an idea?
Should I use the MockBuilder, instead? Thanks, folks!

jasie
  • 2,192
  • 10
  • 39
  • 54
  • 2
    Well .. it's a run time error .. so must be something with the autoloader logic or app config. 1) Try calling `class_exists()` or `interface_exists()` before that line and see what it will return; 2) Maybe you somehow do not autoload those classes when doing PHPUnit tests? 3) If you `Ctrl+Click` on the class in IDE, where it will take you to? What is the full file name? Maybe it's an issue with the wrong letter case? P.S. I assume that the actual code where that class is used can actually run just fine... – LazyOne May 04 '20 at 09:54
  • Sorry @LazyOne, I should have been more precise, I updated my question. Thanks for your effort. 1) returns false 2) that is a good hint, I will check this 3) yes it does, and the letter case is correct and the actual code does work. – jasie May 04 '20 at 10:02
  • 2
    Try rebuilding Composer's autoloadings (`dump-autoload` option) and check those generated files to see if it will have that class there. If OPCache is used, try disabling it for the moment... When doing PHPUnit, use Debug instead of Run, and try to debug just before first calling that class -- see what classes have been loaded already (Composer autoloader in particular, internals should have a list of known classes) and check if that class will be in the list. Too broad .. but no better ideas from me right now. – LazyOne May 04 '20 at 10:19

0 Answers0