2

I created a small project with a composer.json and have this set up:

"autoload": {
    "psr-4": {
        "MyApp\\": "src/"
    }
 },
 "autoload-dev": {
    "psr-4": {
       "MyApp\\Tests\\": "tests/"
    }
 },

Folder Structure

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         cacheResultFile=".phpunit.cache/test-results"
         executionOrder="depends,defects"
         forceCoversAnnotation="true"
         beStrictAboutCoversAnnotation="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutTodoAnnotatedTests="true"
         convertDeprecationsToExceptions="true"
         failOnRisky="true"
         failOnWarning="true"
         verbose="true">
    <testsuites>
        <testsuite name="default">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <coverage cacheDirectory=".phpunit.cache/code-coverage"
              processUncoveredFiles="true">
        <include>
            <directory suffix=".php">src</directory>
        </include>
    </coverage>
</phpunit>

ClientCrawler.php

namespace MyApp;

class ClientCrawler
{

}

ClientCrawlerTest.php

namespace MyApp\Tests;

use MyApp\ClientCrawler;
use PHPUnit\Framework\TestCase;

class ClientCrawlerTest extends TestCase
{
    public function testCheckClient()
    {
        $this->assertTrue(true);

        $crawler = new ClientCrawler();
    }
}

Test Command (PhpStorm)

C:\xampp\php\php.exe D:\phpunit\phpunit.phar --no-configuration --filter MyApp\\Tests\\ClientCrawlerTest --test-suffix ClientCrawlerTest.php C:\Users\MYNAME\PhpstormProjects\CodingPractice\tests --teamcity --cache-result-file=C:\Users\MYNAME\PhpstormProjects\CodingPractice\.phpunit.result.cache

Result

Error: Class "MyApp\ClientCrawler" not found

Things I tried

  • composer dump-autoload
  • Deleted vendor and composer install (with another dump-autoload)
  • Deleted the .phpunit.result.cache
  • Restarted PhpStorm

Where am I wrong?

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
PrimuS
  • 2,505
  • 6
  • 33
  • 66
  • 1
    What does your `phpunit.xml` look like? You have to tell php unit to use `vendor/autoload.php`: https://stackoverflow.com/questions/15710410/autoloading-classes-in-phpunit-using-composer-and-autoload-php – Dylan Reimerink Nov 23 '21 at 20:21
  • Updated the question with the `phpunit.xml` – PrimuS Nov 23 '21 at 20:23
  • 3
    Only now noticed, your command includes `--no-configuration` which means phpunit will ignore your phpunit.xml. So remove that flag and use the config, or add `--bootstrap vendor/autoload.php` to the command to specify the bootstrap file without the config file – Dylan Reimerink Nov 23 '21 at 20:34
  • @PrimuS to add to the previous comment, share an image of your `Test Frameworks` configuration on `PHPStorm` – matiaslauriti Nov 24 '21 at 01:21
  • Have you tried using less configuration parameters when calling PHPUnit? Also, does `vendor/autoload.php` contain any hints about your application's namespace? – Nico Haase Nov 24 '21 at 06:18

0 Answers0