1

I have written phpunit test. I ran it by using following commands:

C:\Program Files (x86)\Ampps\www\phpunit.dev\vendor\bin>phpunit -c ../../phpunit.xml

But end up having this error:

PHP Fatal error: Class 'Acme\SumFinder' not found in C:\Program Files (x86)\Ampps\www\phpunit.dev\tests\AcmeTests\SumFinderTest.php on line 15

I have tried several solutions from these questions:

but nothing works. What could be the problem and how am I going to solve it? Thanks!

My directory structures:

  • phpunit.dev/src/Acme/SumFinder.php
  • phpunit.dev/tests/AcmeTests/SumFinderTest.php
  • phpunit.dev/composer.json
  • phpunit.dev/phpunit.xml

My composer.json written like this:

{
 "require-dev": {
 "phpunit/phpunit": "3.7.*"
},
 "autoload": {
   "psr-4": {
   "Acme\\": "./src/"
 }
},
 "autoload-dev": {
  "psr-4": {
  "AcmeTests\\": "./tests/"
  }
 }
}

My Acme\SumFinder.php written like this:

<?php 
 namespace Acme;
 class SumFinder {
  private $inputArray;
  function __construct($inputArray = null) { ... }
  function findSum() { ... }
  function compareArrays() { ... }
 }
?>

My AcmeTests\SumFinderTest.php:

<?php 
  namespace AcmeTests;
  use Acme\SumFinder;

  class SumFinderTest extends \PHPUnit_Framework_TestCase
    function testFindSum() { ... }
    function testCompareArrays() { ... }
?>

My phpunit.xml config file:

<?xml version="1.0" encoding="utf-8" ?>
<phpunit colors="true" bootstrap="./vendor/autoload.php">
  <testsuites>
  <testsuite name="First Test">
    <directory>./tests</directory>
  </testsuite>
</testsuites>

I am using windows 10 and AMPPS stack if it could help solve my problem.

6
  • 2
    Try to fun this command in cmd or powershell composer -o dump-autoload Commented Feb 11, 2017 at 2:06
  • It works! Thanks! But I wonder why it works and composer dump-autoload didn't work? @mcstuffins Commented Feb 11, 2017 at 3:08
  • it should have worked. Did you do -o. All it does is optimizes your composer file. I am glad that it works. Most of the time when this issue occurs, it will be because of composer and the way it auto loads. Commented Feb 11, 2017 at 3:10
  • Nope. I did -o after I had seen your comment. Okay. Lesson learned. Thanks! @mcstuffins Commented Feb 11, 2017 at 13:09
  • No problem man! If you have other problems related to composer, go ahead and dump it in the question and @me in the comments. Commented Feb 11, 2017 at 14:35

1 Answer 1

1

As a general answer: If dumping the optimized autoloader solves your problem, and not optimizing it still shows it, you have a typo somewhere in your directories or file names.

Dumping the optimized autoloader will scan all files it finds in the directory mentioned for PSR-4 or PSR-0 autoloading, and write an array with all class names found, and their corresponding file names. If you made a typo in your path, dumping this array will connect the class name to the correct file path, regardless of any typos.

Note that some file systems (mostly Linux) are case sensitive, others not (Windows), and that cases are relevant for PSR-4 and PSR-0, which would result in the autoloading to work on case-insensitive file systems, but not on case-sensitive ones.

The problem with your question is that none of the information you gave contains an OBVIOUS hint that you were doing something wrong. However, you might have re-typed your code and NOT made the error, while your original code still has the typo in the file path. Double check that.

Sign up to request clarification or add additional context in comments.

1 Comment

I've been caught a couple of times while developing on a Windows system with the case of the filenames not being identical to the actual Class-name within the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.