In order to be able to write unit tests, we need to refactor the current dependencies implementations.
For example :
class UserService {
public function __construct(
EntityManager $entityManager,
UserRoleService $userRoleService,
MailService $mailService,
TemplateRendererInterface $templateRenderer,
array $config = []
) {
$this->userRepository = $entityManager->getRepository(User::class);
$this->userAvatarRepository = $entityManager->getRepository(UserAvatar::class);
$this->userDetailRepository = $entityManager->getRepository(UserDetail::class);
$this->userRoleService = $userRoleService;
$this->mailService = $mailService;
$this->templateRenderer = $templateRenderer;
$this->config = $config;
}
}
The injected dependency $entityManager will generate new dependencies :
$this->userRepository = $entityManager->getRepository(User::class);
$this->userAvatarRepository = $entityManager->getRepository(UserAvatar::class);
$this->userDetailRepository = $entityManager->getRepository(UserDetail::class);
This results into writing unit tests almost impossible.
Dependencies should not create new dependencies and all dependencies should be injected.
In order to be able to write unit tests, we need to refactor the current dependencies implementations.
For example :
The injected dependency
$entityManagerwill generate new dependencies :This results into writing unit tests almost impossible.
Dependencies should not create new dependencies and all dependencies should be injected.