Closed MarioRadu closed 1 year ago
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 :
$entityManager
$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.
Refactoring of dependencies were added in https://github.com/dotkernel/api/pull/150 (4.0 branch).
In order to be able to write unit tests, we need to refactor the current dependencies implementations.
For example :
The injected dependency
$entityManager
will generate new dependencies :This results into writing unit tests almost impossible.
Dependencies should not create new dependencies and all dependencies should be injected.