Unit testing - writing a test for e.g. actionUpdate

Hey guys,

I had to modify the automatically generated functions a bit (as usual I guess) and more changes follow, so i wanted to write some tests for all the modified funtions. But in some parts my PHP knowledge just isn’t enough. Maybe one of you could help.

In this example I want to write a test for the actionUpdate method for a model called "entry" this entry has many translations (which contains the content of the entry). In the entry table you can just find columns for relations, dates etc.

The actionUpdate looks as follows:


public function actionUpdate($id)

    {

        $model = $this->loadModel($id);

        $translation = $model->translations[0];


        $this->performAjaxValidation($model);

        $this->performAjaxValidation($translation);


        if (isset($_POST['Entry'], $_POST['Translation'])) {

            $model->attributes = $_POST['Entry'];

            $translation->attributes = $_POST['Translation'];

            // validate $model and $translation

            $valid = $model->validate();

            $valid = $translation->validate() && $valid;


            if ($valid) {

                // use false parameter to disable validation

                $model->save(false);


                $translation->save(false);


                if ($model->save() || $translation->save())

                    $this->redirect(array('view', 'id' => $model->entry_id));

            }

        }


        $this->render('update', array(

            'model' => $model,

            'translation' => $translation,

        ));

    }



The point where I don’t know how to do it is more or like in the beginning. How can I create the 2 objects (entry and translation) and later put them in the actionUpdate method?

… I’m relatively new with PHP and mostly Yii … so, sorry if it might be a stupid question ;-).

Cheers,

Mayjestic

anyone could give me some advice here please? Would be awesome.

Controller actions are not to be tested with unit tests since these depend on environment. You should use functional testing with selenium for these.