Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
698 views
in Technique[技术] by (71.8m points)

php - 'Class not found' when using namespaces in PHPUnit

I'm new to PHPUnit and am having some trouble setting it up to access my PHP files. The directory structure I'm using for my app is this:

./phpunit.xml

./lib/Application/
  -> Dir1/File1.php (namespace = ApplicationDir1)
  -> Dir1/File2.php
  -> Dir2/File1.php (namespace = ApplicationDir2)

./tests/Application/Tests
  -> Test1.php (namespace = ApplicationTests)
  -> Test2.php 

In my PhpUnit.xml, I have:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit verbose="false">
  <testsuites>
      <testsuite name="Application">
          <directory>./tests/Application/Tests</directory>
      </testsuite>
  </testsuites>
  <logging>
       <log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
       <log type="json" target="/tmp/phpunit-logfile.json"/>
  </logging>
  <filter>
        <whitelist>
            <directory suffix=".php">./lib</directory>
        </whitelist>
  </filter>
</phpunit>

And in one of my test files, I open with:

namespace ApplicationTests;

use ApplicationDir1File1;

class MyTest extends File1 {}

But it keeps on saying:

Class 'ApplicationDir1File1' not found

Where am I going wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you installed PHPUnit using Composer then you can use Composers autoloader. The easiest way to do so would be to add:

"autoload":{
    "psr-0":{
        "your-app-directory":""
    }
}

to composer.json


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...