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.

Community
  • 1
  • 1
Vincent acent
  • 465
  • 2
  • 7
  • 15

1 Answers1

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.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • 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. – Alister Bulman Feb 18 '17 at 00:55