atoum / atoum

The modern, simple and intuitive PHP unit testing framework.
http://atoum.org
Other
1.44k stars 147 forks source link

The class is not found whereas it exists #222

Closed ke20 closed 10 years ago

ke20 commented 11 years ago

Hi,

I'm using Silex in my project and I just installed atoum for create my unit tests. I've also installed atoum-bundle for to have the TestController classes and the WebTestCase.

When I execute atoum bin\atoum -d Tests, the console return

> PHP path: C:\Dev\Tools\PHP\php.exe
> PHP version:
=> PHP 5.3.8 (cli) (built: Aug 23 2011 11:50:20)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
    with Xdebug v2.1.3, Copyright (c) 2002-2012, by Derick Rethans
> Tests\App\Controllers\MyController...
[.___________________________________________________________][0/1]
[E___________________________________________________________][1/1]
=> Test duration: 0.00 second.
=> Memory usage: 0.00 Mb.
> Total test duration: 0.00 second.
> Total test memory usage: 0.00 Mb.
> Running duration: 0.38 second.
Failure (1 test, 1/1 method, 0 void method, 0 skipped method, 0 uncompleted method, 0 failure, 1 error, 0 exception)!
> There is 1 error:
=> Tests\App\Controllers\MyController::testIndex():
==> Error E_USER_ERROR in C:\Dev\Tools\Apache-2.2\htdocs\site\Tests\App\Controllers\MyController.php on unknown line, generated by unknown file:
Tested class 'App\Controllers\MyController' does not exist for test class 'Tests\App\Controllers\MyController'

But my class App\Controllers\StaticPagesController exists and my other class Tests\App\Controllers\MyController also.

My project's structure looks like:

- root
  - App
    - Controllers
      - MyController.php
  - Test
    - App
      - Controllers
        - MyController.php

In my real classe have

<?php
namespace App\Controllers;

use Silex\Application;
use Silex\ControllerProviderInterface;

class MyController implements ControllerProviderInterface {
  public function index($app) {
    // code
  }
}

and in my test class have

<?php
namespace Tests\App\Controllers;

use \atoum\AtoumBundle\Test\Controller\ControllerTest;

require_once __DIR__.'/../../vendor/autoload.php'; // in a boostrap file normaly

class MyController extends ControllerTest {
    public function testIndex() {
        // code
    }
}

It's a bug or it's because atoum-bundle doesn't work with Silex?

mageekguy commented 11 years ago

I don't kown if atoumBundle works fine with SIlex (maybe you should open an issue on its repository to have an answer about this point), but i think that you have an autoloading problem. Use a bootstrap file to set the Silex autoloader. Moreover, i think that http://blog.eexit.net/2011/11/php-projet-silex-tdd-pour-le-code-metier.html can help you to set up atoum and Silex correctly.

ke20 commented 11 years ago

Silex works fine, I use a bootstrap file for my Silex application and a little boostrap file for atoum (which contains just a call to the autoload file)

Thanks for the link, but I already saw it. The author use the atoum file phar and I use composer, the configuration is not same... :(

It's maybe the autoloading as you say, I've use this link http://blog.eexit.net/2012/06/php-utiliser-lautoloader-de-composer-avec-atoum.html.

I will continue to investigate and if I find a solution I'll post it

_EDIT_: I've forgotten to precise, I've no errors when I doesn't define the method testIndex() in the test class

mageekguy commented 11 years ago

All my apologize, i have made a mistake : the problem is not the autoload. Have you overloaded the test namespace ? By default, the namespace of test class should contains tests/units, but in your case, it must be only Tests. See http://docs.atoum.org/fr/chapitre4.html#Changer-l-espace-de-nom-par-defaut for more informations.

ke20 commented 11 years ago

Yes, but the class ControllerTest of atoumBundle is already overloaded by the namespace with the correct namespace to replace, and my test class extends the ControllerTest.

namespace atoum\AtoumBundle\Test\Controller;

use atoum\AtoumBundle\Test\Units\WebTestCase;
use mageekguy\atoum;

abstract class ControllerTest extends WebTestCase
{
    public function __construct(atoum\adapter $adapter = null, atoum\annotations\extractor $annotationExtractor = null, atoum\asserter\generator $asserterGenerator = null, atoum\test\assertion\manager $assertionManager = null, \closure $reflectionClassFactory = null)
    {
        parent::__construct($adapter, $annotationExtractor, $asserterGenerator, $assertionManager, $reflectionClassFactory);
        $this->setTestNamespace('Tests');
    }
}

It's very strange, because in the error message the two paths in error are correct....

mageekguy commented 11 years ago

Any feedback ?

ludofleury commented 11 years ago

Im really not sure, I might think about autoload issue too. Is it possible to have the bootstrap you used for atoum ? On Sep 20, 2013 12:07 PM, "Frédéric Hardy" notifications@github.com wrote:

Any feedback ?

— Reply to this email directly or view it on GitHubhttps://github.com/atoum/atoum/issues/222#issuecomment-24800792 .

ke20 commented 10 years ago

I'm sorry I haven't seen your answers.

My Silex bootstrap file is like that:

    <?php

    use Symfony\Component\HttpFoundation\Response;

    $loader = require_once __DIR__.'/../vendor/autoload.php';
    $loader->add('App', dirname(__DIR__));

    $app = new Silex\Application();

    $app = require_once __DIR__.'/config.php';
    $app = require_once __DIR__.'/routes.php';

    // Manage the errors pages
    $app->error(function (\Exception $e, $code) use ($app) {
        if($app['debug'] === TRUE) {
            return;
        }

        switch ($code) {
            case 404:
                $message = 'La page demandée n\'a pas été trouvée.';
                break;
            default:
                $message = 'Oups, une erreur est survenue.';
        }

        return $app['twig']->render('error.html.twig', array(
            'message' => $message
        ));
    });

    $app->boot();
    $app->run();

    return $app;
ludofleury commented 10 years ago

I was talking about the bootstrap used for atoum :) Still failing ?

ke20 commented 10 years ago

Oh I'm sorry, but my bootstrap file is very tiny because there is three lines...

    $loader = require_once __DIR__.'/../vendor/autoload.php';
    $loader->add('App', dirname(__DIR__).'/../App/');
    $loader->add('Tests', dirname(__DIR__));