Skip to content

Refactor dependencies  #139

@marioradu05

Description

@marioradu05

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.

Metadata

Metadata

Assignees

Labels

4.xtarget onl;y the version 4.x

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions