JeffreyWay / Laravel-Test-Helpers

Easier testing in Laravel.
https://packagist.org/packages/way/laravel-test-helpers
217 stars 40 forks source link

Unable to generate attributes if the model has a non-standard table name #23

Open ingro opened 11 years ago

ingro commented 11 years ago

I was trying to use this library and found a (possible?) bug.

For various reason I don't have standard table's name and so I used the $table property inside each model to connect it to the right table.

So when I tried to use: Factory::attributesFor('Pages'); I only got an empty array, and the same with the other available methods.

Further investigation pointed me to the parseTableName method on the Factory class:

protected function parseTableName($class)
{
        return $this->isNamespaced($class)
            ? str_plural(substr(strrchr($class, '\\'), 1))
            : str_plural($class);
}

I'm actually using a workaround, replacing the return value using the getTable method of Eloquent's model, but I don't know if this is the right way to fix it...

LimeBlast commented 11 years ago

I'm experiencing a similar issue to this (i.e. running Factory::attributesFor('User') returns nothing but an empty array) - although I am using the standard table names for the models.

LimeBlast commented 11 years ago

UPDATE: Having had a bit of a play myself, I've also found that the issue lies in the parseTableName() function, although my issue was fixed by throwing a strtolower() into the equation:

protected function parseTableName($class)
{
    return strtolower($this->isNamespaced($class)
        ? str_plural(substr(strrchr($class, '\\'), 1))
        : str_plural($class));
}

This, however, obviously doesn't fix @ingro's issue, which I can only imagine being fixed by getting the table name from the model itself - although I'm not sure quite how to go about doing such a thing.

jamime commented 11 years ago

Pull request #27 should fix both of these issues :smile:.

Factory::attributesFor('Pages'); and Factory::attributesFor('pages'); will now use the pages table unless a public $table variable is declared in the model.

LimeBlast commented 11 years ago

Good stuff, looking forward to it being accepted into the repo :+1:

ekrembk commented 11 years ago

I'm experiencing the same problem, too. I create some tables and models in Turkish and parseTableName does not work.

BTW I don't think getTable() is a good solution here. What if the model isn't extending Eloquent? We can't use getTable() then.