The class under test is in app \ web:

<?php declare(strict_types = 1); namespace app\web; class Application { private $config = []; public function __construct(array $config = null) { if ($config !== null) { $this->checkConfig($config); $this->config = $config; } } public function configure(array $config) { $this->checkConfig($config); $this->config = $config; } public function checkConfigure() { return !empty($this->config); } public function run() { if (empty($this->config)) { return false; } else { return true; } } public function notFound() { throw new \InvalidArgumentException(); } public function sayHello() { echo 'Hello World!'; } private function checkConfig($config) { if (!is_array($config)) { throw new \Exception("Config must be an array!"); } } } 

Class with tests in tests \ web:

 <?php namespace tests\web; use app\web\Application; class ApplicationTest extends \PHPUnit_Framework_TestCase { public function testRunApplication(){ $app = new Application(['param'=>'value']); $this->assertTrue($app->run()); $wrongApp = new Application([]); $this->assertFalse($wrongApp->run()); $this->assertEquals(true, $app->run()); $this->markTestIncomplete('Incomplete'); } } 

Actually the problem is that when you run phpunit an error occurs:

Fatal error: Class 'app \ web \ Application' not found in C: \ wamp64 \ www \ unit_test_learning \ tests \ web \ ApplicationTest.php on line 11

I did not find a solution on the network. Please tell me what I'm doing wrong.

* used composer if important

  • and from where and with what settings does the test run? - Dmitry Kozlov
  • Correct autoloader. In the autoloader, you need to add a directory - where the namespace app begins. That is, if there is a psr4 - which is most likely with the $loader->setPsr4('app\\', __DIR__ . '/path/to/app'); - Goncharov Alexander
  • @GoncharovAlexander did not help, the error remained. (Added to vendor / composer / autoload_real.php - ToshiDono

1 answer 1

They helped me in another service. The error was in the loader. Corrected on:

  "autoload": { "psr-4": { "app\\web\\": "app/web/" , "\\":"" } } 

Shoveled a bunch of articles and documentation, but did not understand how to form the autoloader in the composer.

I would be very grateful if someone helps to understand this.