Cmorales
Cmorales

Reputation: 1007

Test relations in Laravel 4 with FactoryMuff

I am new to TDD, so excuse me if this is a noob question.

I have Users and Projects, related in 2 different ways:

  1. A Project Belongs to a User
  2. Many Users can be authorized to many Projects.

I am following this tutorial http://net.tutsplus.com/tutorials/php/testing-like-a-boss-in-laravel-models/?search_index=16 , so I am using FactoryMuff.

This is my Project class:

 <?php
use LaravelBook\Ardent\Ardent;
class Project extends Ardent
{
    /**
     * Ardent validation rules
     */
    public static $rules = array(
        'name' => 'required',              // Project Title
        'user_id' => 'required|numeric',  // User owner id
    );

    /**
     * Array used by FactoryMuff to create Test objects
     */
    public static $factory = array(
        'name' => 'string',
        'user_id' => 'factory|User', // Will be the id of an existent User.
    );

    /**
     * Belongs to user
     */
    public function owner()
    {
        return $this->belongsTo( 'User', 'user_id');
    }

    /**
     * Many Users can be authorized
     */
    public function authorized()
    {
        return $this->belongsToMany( 'User', 'project_user', 'project_id', 'user_id')->withTimestamps();
    }

}

And this is my User class:

 <?php

use Zizaco\Entrust\HasRole;
use Zizaco\Confide\ConfideUser;

class User extends ConfideUser {

    /**
     * Ardent validation rules
     */
    public static $rules = array(
        'username' => 'required|min:4|unique:users',
        'email' => 'required|email|unique:users',
        'password'              => 'required|between:4,16|confirmed',
        'password_confirmation' => 'required|between:4,16',
    );

    /**
     * Array used by FactoryMuff to create Test objects
     */
    public static $factory = array(
        'username' => 'string',
        'email' => 'email',
        'password' => '12345',
        'password_confirmation' => '12345',
    );

    (more code)

    /**
     * Many Users can be authorized on many Projects
     */
    public function authorized()
    {
        return $this->belongsToMany( 'Project', 'project_user', 'user_id', 'project_id')->withTimestamps();
    }

    /**
     * Users can have many Projects
     */
    public function projects()
    {
        return $this->hasMany( 'Project');
    }

}

And this is my ProjectTest:

 <?php
use Zizaco\FactoryMuff\Facade\FactoryMuff;
use Way\Tests\Factory;
class ProjectTest extends TestCase
{
    use Way\Tests\ModelHelpers;
    /**
     * Test relationship with User
     */
    public function testRelationshipWithUser()
    {
      // Instantiate new Project
      $project = FactoryMuff::create('Project');

      $this->assertEquals($project->user_id, $project->owner->id);
  }

    /**
     * Test relationship with Authorized Users
     */
    public function testRelationshipWithAuthorizedUsers()
    {
      // Instantiate new Project
      $project = FactoryMuff::create('Project');
      $project->save();
      $project->authorized()->attach($project->user_id);

      $this->assertEquals($project->user_id, $project->authorized->first()->id);
  }

}

If I run the tests individually (commenting the other) both pass. However, if I run both, I get this error:

Caused by
PDOException: SQLSTATE[HY000]: General error: 1 table users has no column named password_confirmation

Why is it complaining about that column in the second test and not in the first? :S

Upvotes: 1

Views: 919

Answers (1)

philipbrown
philipbrown

Reputation: 554

You need to set public $autoPurgeRedundantAttributes = true; on your User model.

Ardent (which Confide extends from) will automatically purge the _confirmation fields, but by default it is set to false.

Upvotes: 1

Related Questions