JeffreyWay / Laravel-Test-Helpers

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

Issues working with Models in namespaces. #34

Open godjen99 opened 11 years ago

godjen99 commented 11 years ago

I have deleted my models folder and creating the proper folder that would hold my Eloquent Models (or business logic), along with editing my composer file, I got everything working fine in my Unit tests when I called the models as normal (new \Users\User()).

To give context, I have a "Users" folder, and in that folder I have the models for a User and a Group (the 'users' table has a group_id on the table). I didn't define the relationship explicitly in the User model, but when using your the factory, it recognized the relationship and attempted to create insert a corresponding record on the 'groups' table to my testing DB. However, I ran into some issues...

Way\Tests\ModelNotFoundException:

C:\xampp\htdocs\laravel\vendor\way\laravel-test-helpers\src\Way\Tests\Factory.php:164 C:\xampp\htdocs\laravel\vendor\way\laravel-test-helpers\src\Way\Tests\Factory.php:122 C:\xampp\htdocs\laravel\vendor\way\laravel-test-helpers\src\Way\Tests\Factory.php:108 C:\xampp\htdocs\laravel\vendor\way\laravel-test-helpers\src\Way\Tests\Factory.php:73 C:\xampp\htdocs\laravel\vendor\way\laravel-test-helpers\src\Way\Tests\Factory.php:324 C:\xampp\htdocs\laravel\vendor\way\laravel-test-helpers\src\Way\Tests\Factory.php:223 C:\xampp\htdocs\laravel\vendor\way\laravel-test-helpers\src\Way\Tests\Factory.php:128 C:\xampp\htdocs\laravel\vendor\way\laravel-test-helpers\src\Way\Tests\Factory.php:108 C:\xampp\htdocs\laravel\vendor\way\laravel-test-helpers\src\Way\Tests\Factory.php:73 C:\xampp\htdocs\laravel\app\tests\unit\Users\UserTest.php:217 C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176 C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129

My call to the Factory was like this: Factory::create('\Users\User');

Digging into the Factory, in the "createRelationship" function, the code reads as $namespace = $this->isNamespaced($parent) ? str_replace(substr(strrchr($parent, '\'), 1), '', $parent) : null;

The isNamespaced, check returned true, as it should, however the namespace it returns from the above code is "string(2) "s\"".

Since I was using namespaces, I checked Users, it was miss reading the namespace as "\s", which ends up making the attempted namespaced class "string(7) "s\group"".

Editing the code, I changed str_replace(substr(strrchr($parent, '\'), 1), '', $parent) to substr($parent, 0, strripos($parent, '\') + 1), and everything worked as expected.

Some extra info, I'm on version of PHP Version 5.4.7.

Thanks for the helpers Jeffery, they are greatly appreciated and have saved me a fair amount of time!

JeffreyWay commented 11 years ago

Thanks. I'll look into this tomorrow and push a fix.

godjen99 commented 11 years ago

Thanks Jeffery. Also, I would like to say thanks for all your work on Laravel 4. You've truly expedited my leaning process! Great testing book too!

I look forward to the fix!

godjen99 commented 11 years ago

Also, what about models in different namespaces?

For example if I had my Group model in a folder and auto-loaded and namespaced in "Groups". Similarly if I had my User in it's folder, namespaced in "Users", and had a relationship defined in my User model to the Group model (stating the User model uses the Groups namespace), should it work via the Factory?

I tried this outside of Unit testing (old fashioned browser testing ;)) and it worked for me. But it broke when using the Factory.

Thanks again Jeffery!

Francois-Francois commented 10 years ago

Have the same problem. I'm on it since many hours, but no aha moment for now.