PHPUnit Test不使用命名空间

My PHPUnit Test is not working. It throws this error:

PHP Fatal error: Class 'app\lib\calculator' not found in /mnt/c/MAMP/htdocs/test/CalcTest.php on line 7

Here's my Folder structure:

htdocs
  |----app
  |     |---lib
  |          |---calculator.php
  |----test
  |     |---CalcTest.php
  |----vendor
  |     |---autoload.php (and standard folders of composer)
  |
  |----composer.json
  |----composer.lock
  |----index.php
  |----phpunit.xml

I have used namespaces in these php files to get hang of this practice!

calculator.php

<?php 

namespace app\lib;

class Calculator {

     public function add($a, $b) {
          return $a + $b;
     }

     public function render() {
          return "Hello World!";
     }

     public function returnTrue() {
          return true;
     }

     public function returnArray() {
          return ['Hey', 'World', 'This', 'Is', 'Test'];
     }

}

?>

Here i wanted to require the calculator with namespace in a varbiable:

CalcTest.php:

<?php

class CalcTest extends PHPUnit_Framework_TestCase {

     public function testRenderReturnsHelloWorld() {

          $calc = new \app\lib\calculator();

          $expected = "Hello World";

          $this->assertEquals($expected, $calc->render());

     }

}

?>

The configuration of phpunit in phpunit.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<phpunit  bootstrap="./vendor/autoload.php"
          color="true"
          convertErrorsToExceptions="true"
          convertNoticesToExceptions="true"
          convertWarningsToExceptions="true"
          stopOnFailures="false"
          syntaxCheck="false"
     >
     <testsuites>
          <testsuite name="PHPUnit Tuts">
               <directory>./test/</directory>
          </testsuite>
     </testsuites>
</phpunit>

And when i start the server even the index.php template is not working and throws Error-500...

<?php

require_once 'vendor/autoload.php';

$calc = new \app\lib\calculator();

?>

<!DOCTYPE html>
<html lang="en">
<head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <meta http-equiv="X-UA-Compatible" content="ie=edge">
     <title>PHPUnit Tuts</title>
</head>
<body>

     <h1><?php echo $calc->render(); ?></h1>

     <pre><?php var_dump($calc->returnArray()); ?></pre>

     <code><?php echo $calc->add(2,2); ?></code>

</body>
</html>

Please do NOT answer anything with the require or require_once and the like functions. I would like to understand the namespace.

Thank you.

It doesnt look like you have any autoloading setup for your own classes.

This is why phpunit has no idea about your classes or namespaces.

in your composer file, add an autoload block:

 {
     "name": "acme/test",
     "type": "project",
     "autoload": {
         "psr-4": {
             "": "app/"
         }   
     },  

     "require": {}
 }

This will tell composer to include your application folders in its autoload file. Bear in mind also, that you have your namespaces slightly incorrect.

you should have a tree that looks something like this:

├── composer.json
├── src
│   └── Classes
│       └── Test.php
└── vendor
    ├── autoload.php

not the captial letter on the 'Classes' folder, and the src directory. This isnt essential, but it is good practice.

Once youve done that, your PHPunit will then know about your namespaces because composer will have told it. Bear in mind that you need to run composer's 'dump-autoload' command if you change anything or add stuff.