Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues working with Models in namespaces. #34

Open
godjen99 opened this issue Aug 21, 2013 · 4 comments
Open

Issues working with Models in namespaces. #34

godjen99 opened this issue Aug 21, 2013 · 4 comments

Comments

@godjen99
Copy link

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
Copy link
Owner

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

@godjen99
Copy link
Author

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
Copy link
Author

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!

@rikless
Copy link

rikless commented Jan 13, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants