In my service method I am mocking I have part where I am persisting entity from user table like:
$user = $this->tokenStorage->getToken()->getUser();
$userFirstName = 'Unknown user';
$new = new MyEntity();
if ($user != 'anon.') {
$new->setUserId($user);
$userFirstName = $user->getFirstName();
}
I am writing that part in a separate test method where I want to return sepcific User object, meaning I want to test it..
public function testUser()
{
$tokenStorage = $this->createMock(TokenStorageInterface::class);
$userRepo = $this->createMock(UserRepository::class);
$username = ['username' => 'test@test.com']; // this will find the entry in the DB
$testUserObject = new User();
$userRepo->expects($this->any())
->method('findOneBY')
->with($username)
->will($this->returnValue($testUserObject));
$user = $tokenStorage->getToken()->getUser();
$this->assertEquals($testUserObject, $user);
}
but I get:
Error: Call to a member function getUser() on null
and I think this is the user in my test db..
Or maybe this is not a proper way to test this case? Can someone help I am new at writing tests..
Another question is, can I use returned value of this test method and use it's user object for testing whole method where I persist this user object?