Answer the question
In order to leave comments, you need to log in
Test for EncoderFactoryInterface?
test
/** @var EncoderFactoryInterface|MockObject $encoderFactory */
$encoderFactory = $this->getMockBuilder(EncoderFactoryInterface::class)
->setMethods(['getEncoder'])
->disableOriginalConstructor()
->getMock();
$encoderFactory->expects($this->once())
->method('getEncoder');
$this->assertInstanceOf(EncoderFactoryInterface::class, $encoderFactory);
// trigger encoder getEncoder method
public function __construct(
EncoderFactoryInterface $encoderFactory,
....
dump($this->encoderFactory->getEncoder($newUser)); // NULL
$encodedPass = $this->encoderFactory->getEncoder($newUser)->encodePassword(
$newUser->plainPassword()->plainPassword(),
$newUser->salt()->salt()
);
$this->userPasswordEncoder->encodePassword($newUser, $data['password'])
will return null. class EncoderFactoryTest extends TestCase
{
public function testGetEncoderWithMessageDigestEncoder()
{
...
$encoder = $factory
->getEncoder($this
->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface') // тут у меня юзер с этим интерфейсом
->getMock());
$expectedEncoder = new MessageDigestPasswordEncoder('sha512', true, 5);
$this->assertEquals($expectedEncoder->encodePassword('foo', 'moo'), $encoder->encodePassword('foo', 'moo'));
}
Answer the question
In order to leave comments, you need to log in
You need to set the behavior of the getEncoder method with willReturn :
$encoderFactory = $this->createMock(EncoderFactoryInterface::class);
$encoderFactory
->method('getEncoder')
->willReturn($encoder);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question