I am using Symfony with the FOSUserBundle and now I like to test some things like:
- Doctrine lifecycle
- Controller behind firewall
For those tests I need to be a specific user or at least in a user group. How do I mock a user session so that ...
- The lifecycle field like "createdAt" will use the logged in user
- The Controller act like some mocked user is logged in
Example:
class FooTest extends ... {
function setUp() {
$user = $this->getMock('User', ['getId', 'getName']);
$someWhereGlobal->user = $user;
// after this you should be logged in as a mocked user
// all operations should run using this user.
}
}
You can easily do that with LiipFunctionalTestBundle which authorize you lot of shortcut for create Unit Test.
If already you have a form user for create or edit you can use this for your test unit workflow user in your application :
use the makeClient method for logging test
use your form for test your creation
testing "createdAt" with just call findOneBy in repository user like this