0

I have

Error: Class 'app\Main' not found

And I don`t know how to fix it?

I use PHPSTORM and phpunit latest version.

Structure of my directories

Main.php

<?php
namespace app;

class Main
{
    public function Calc()
    {
        return 1;
    }
}

MainTest.php

<?php

namespace app;

use PHPUnit\Framework\TestCase;

class MainTest extends TestCase
{
    public function testCalc()
    {
        $a = new Main();
    }
}

phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit verbose="true">
    <testsuites>
        <testsuite name="My Test Suite">
            <directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">tests</directory>
           <!-- <file phpVersion="5.3.0" phpVersionOperator=">=">/path/to/MyTest.php</file>-->
        </testsuite>
    </testsuites>
</phpunit>

composer.json

{
  "autoload": {
    "classmap": [
      "src/"
    ]
  },
  "require": {
    "phpunit/phpunit": "7.0.1"
  }
}

2 Answers2

0

You need to tell the autoloader where to find the php file for a class. That's done by following the PSR-0 standard.

Add this to your composer.json:

{
   ...
     "autoload": {
      "psr-0": { "": "tests/app/" }
   }
}

Now the autoloader can find your classes you need to let PHPunit know there is a file to execute before running the tests: a bootstrap file. However, it's nicer to use a PHPunit configuration file:

  <!-- /phpunit.xml.dist -->
  <?xml version="1.0" encoding="utf-8" ?>
  <phpunit bootstrap="./vendor/autoload.php">

    <testsuites>
        <testsuite name="The project's test suite">
            <directory>./tests/app</directory>
        </testsuite>
    </testsuites>

</phpunit>
0

I think that I solved it. Maybe it was conflict between "app" namespace in app directory and test/app . Its working now. I change namespace in app to "framework" and add it to the composer.json

   "autoload": {
        "psr-4": {
            "framework\\": "app"
        }
    }