vishal
vishal

Reputation: 4083

Entity Class not found PHPUnit Test

Below are the paths where files are located,

src\TW\Talk\Entity\Talk.php
src\Tests\Talk\Entity\TalkTest.php
src\phpunit.xml.dist

In TalkTest.php, I have included PHPUnit and the entity Talk.

require_once 'TW/Talk/Entity/Talk.php';
require('PHPUnit/Autoload.php');

Class TalkTest extends PHPUnit_Framework_TestCase
{
    ...
}

In phpunit.xml.dist file, I have,

<phpunit>
  <testsuites>
    <testsuite name="TW">
      <file>Tests/Talk/Entity/TalkTest.php</file>
    </testsuite>
  </testsuites>
</phpunit>

I am running phpunit command from src directory, I am getting error that Fatel Error: Class 'Tests\TW\Talk\Enity\Talk' not found.

For reference, I am referring to php-object-freezer-master which has similar structure.

Any idea why the TalkTest is not able to find Talk class ?

phpunit command is trying to find Talk entity in Tests folder.

Upvotes: 1

Views: 3410

Answers (2)

vishal
vishal

Reputation: 4083

Changing phpunit.xml.dist to

<phpunit bootstrap="loader.php">
  <testsuites>
    <testsuite name="TW_Talk">
      <directory>Tests</directory>
    </testsuite>
  </testsuites>
</phpunit>

and loader file as,

<?php

function tw_test_autoloader($class) {
    if(file_exists(__DIR__."\\" . $class . ".php"))
        require_once(__DIR__."\\" . $class . ".php");
}

spl_autoload_register('tw_test_autoloader');

Worked for me.

But still if I replace directory tag to file

<file>Tests\TW\Talk\Entity\TalkTest.php</file>

It does not work.

Upvotes: 1

Maciej Sz
Maciej Sz

Reputation: 12035

Check your include_path:

echo get_include_path();

It should contain the directory to which your TW/Talk/Entity/Talk.php is relative. If it is not there, then you must add it either to php.ini or to PHPUnit's bootstrap.

You can easily test if PHP can find your file using your include path with this:

var_dump( stream_resolve_include_path('TW/Talk/Entity/Talk.php') );

Upvotes: 0

Related Questions