dotkernel / api

DotKernel's PSR-7 REST style API built around the Mezzio API skeleton.
https://docs.dotkernel.org/api-documentation/
MIT License
35 stars 5 forks source link

Refactor dependencies #139

Closed MarioRadu closed 1 year ago

MarioRadu commented 2 years 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 :

$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.

MarioRadu commented 1 year ago

Refactoring of dependencies were added in https://github.com/dotkernel/api/pull/150 (4.0 branch).