Chapter 3: MessageTest

Hello,

I’m following the book and am now up to Chapter 3 (page 50).

When running the test with PHPUnit I get the following output:




C:\xampplite\htdocs\yii\testdrive\protected\tests>phpunit unit\MessageTest.php

PHPUnit 3.4.15 by Sebastian Bergmann.


Fatal error: Class 'Controller' not found in C:\xampplite\htdocs\yii\testdrive\p

rotected\controllers\MessageController.php on line 4



The contents of my MessageTest.php are as follows:




<?php


Yii::import('application.controllers.MessageController');

class MessageTest extends CTestCase

{

    public function testRepeat()

    {

        $message = new MessageController('messageTest');

        $yell = "helloooo";

        $this->assertEquals($message->repeat($yell), $yell);

    }

}



The error is triggered when invoking “new MessageController(‘messageTest’)” in the code above.

Part of MessageController.php looks like this:




<?php


class MessageController extends Controller

{

	(..)


	public function actionRepeat($var)

	{

		return $var;

	}



Yii version: r2446

PHP version: 5.3.1

PHPUnit version: 3.4.15

Platform: Windows 7

The same thing happens to me in Chapter 5 with class ProjectTest.

Hello,

Are you sure you are using Yii version 1.1.2? Your revision # indicates a later version. The base controller class "Controller" was introduced in the auto-generated code as of verion 1.1.2 (SVN r2086), but there may be slight differences in the auto-generated code in the version you are using

I know the title of the book just states Yii 1.1, but on page 18 as part of the installation, the book indicates it is using specifically Yii 1.1.2 for all examples. ALL of the ideas and concepts covered will translate to other 1.1.x versions, but if using a version other than 1.1.2, there may be subtle differences in the examples provided.

Please ensure you are using Yii version 1.1.2 for the best experience when following along with the specific examples provided in the book.

I am using Yii 1.1.3 and no issues so far and around chapter 9… have you tried importing the controller class in your MessageController.php Just for the sake of test put


Yii::import('application.components.Controller');

at the top of your MessageController.php Hope this helps

I know it’s a newer version than the one used in the book, but auto including the controller class shouldn’t have changed I suppose?

At home I installed everything as well, using the latest Yii revision, but here I don’t have any missing class errors.

At my work environment tomorrow I will try your suggestion, thank you.

Importing isn’t making any difference.

I still get the class not found error.

I can get it working though by adding “require(’../models/Project.php’);”

An unusual thing: when I run the test and receive the error, and empty file named ‘Project.php’ is created in protected/tests/

  • This file is created when code is at: Yiibase.php, line 354. Appearantly it cannot find the project model in any of the first two ifs.

    public static function autoload($className)

    {

        // use include so that the error PHP file may appear

        if(isset(self::$_coreClasses[$className]))

            include(YII_PATH.self::$_coreClasses[$className]);

        else if(isset(self::$_classes[$className]))

            include(self::$_classes[$className]);

        else

        {

            include($className.'.php'); // right after this is executed, the file is created



I am now using exactly the same Yii and TrackStar code at home as at my work environment, could the difference be somewhere else?

Thanks, this helps resolve the issue on chapter 3.

It’s not clear on the manual/procedure where to insert the code…

Thanks again and God speed!!!