I'm new with unit testing and I'm trying to write a test for this plugin...
/**
* @param AccountManagement $subject
* @param CustomerInterface $customer
* @param null $password
* @param string $redirectUrl
* @return array
*/
public function beforeCreateAccount(
AccountManagement $subject,
CustomerInterface $customer,
$password = null,
$redirectUrl = ''
) {
$customer->setFirstname(str_replace(' ', '', $customer->getFirstname()));
return [$customer, $password, $redirectUrl];
}
Ant this is my approach to writing the test...
public function setUp(): void
{
$this->createMock(AccountManagement::class);
$this->accountManagementPluginClass = new AccountManagementPlugin();
}
public function testBeforeCreateAccount()
{
$customerMok = $this->createMock(Customer::class);
$customerMok->expects()->method('getFirstName')->willReturn('Diego Maradona');
// I tried this instead too
// $customerMok->setFirstName('Diego Maradona')
$arr = $this->accountManagementPluginClass->beforeCreateAccount(
$this->createMock(AccountManagement::class),
$customerMok
);
$this->assertEquals('DiegoMaradona', $arr[0]->getFirstName());
}
I know that the problem is that the mock objects have no logic. The only return is the mock value. So which is the right way to test a method who:
- receive an object as a parameter (Ex: $customer)
- modify that object (Ex: $customer->setFirstName())
- return the object modified (Ex: $customer->getFirstName())