symfony + phpunit – how to test UserFactory with PasswordEncoder dependencies

Hello i have problem with my test.
I’m trying to test UserFactory which is creating UserObject by UserDto data.
I dont know how to test it because factory need PasswordEncoder in dependencies.

use AppEntityUser;
use AppServiceFactoryUserFactory;
use PHPUnitFrameworkMockObjectMockObject as MockObject;
use PHPUnitFrameworkTestCase;

class UserFactoryTest extends TestCase
{
    /**
     * @covers UserFactory
     */
    public function testShouldCreateUserObjectFromUserDto()
    {
        //Given
        /**
         * @var UserDto | MockObject
         */
        $userDto = $this->getMockBuilder(UserDto::class);

        //When
        $userFactory = new UserFactory(/* PASSWORD ENCODER */);
        $user = $userFactory->create($userDto);

        //Then
        $this->assertInstanceOf(User::class, $user);
    }
}
namespace AppServiceFactory;

use AppEntityUser;
use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;

class UserFactory
{
    /**
     * @var UserPasswordEncoderInterface
     */
    private $encoder;

    public function __construct(UserPasswordEncoderInterface $encoder)
    {
        $this->encoder = $encoder;
    }

    public function create(UserDto $dto)
    {
        $user = new User();
        $user->setPassword($this->encoder->encodePassword($dto->plainPassword));
        /**
         * CODE..
         */
        return $user;
    }
}

Is this right? how can i test this factory which have dependencies?
i cant user __construct in my TestClass

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *