Test Failed
Branch master (fdda4e)
by ANTHONIUS
03:39
created

InteractsWithUser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 66
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserRepository() 0 3 1
A iHaveLoggedInAsUser() 0 13 1
A iDontHaveUser() 0 9 2
A getUserEntityManager() 0 5 1
A iHaveUser() 0 14 2
1
<?php
2
3
/*
4
 * This file is part of the EOffice project.
5
 *
6
 * (c) Anthonius Munthi <https://itstoni.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace EOffice\User\Testing;
15
16
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
17
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Response;
18
use EOffice\Contracts\User\Model\UserInterface;
19
use EOffice\Testing\Concerns\InteractsWithORM;
20
use EOffice\User\Model\User;
21
use EOffice\User\Repository\UserRepository;
22
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
23
24
trait InteractsWithUser
25
{
26
    use InteractsWithORM;
27
28
    /**
29
     * @param string $username
30
     * @param string $password
31
     *
32
     * @return Client|KernelBrowser
33
     */
34
    protected function iHaveLoggedInAsUser(string $username, string $password)
35
    {
36
        /** @var Response $response */
37
        $response = static::createClient()->request('POST', '/login', ['json' => [
38
            'username' => $username,
39
            'password' => $password,
40
        ]]);
41
42
        /** @var \stdClass $data */
43
        $data  = json_decode($response->getContent());
44
        $token = (string) $data->token;
45
46
        return static::createClient([], ['headers' => ['authorization' => 'Bearer '.$token]]);
47
    }
48
49
    protected function iHaveUser(string $username = 'test', string $email='[email protected]', string $password = 'test'): UserInterface
50
    {
51
        $repo = $this->getUserRepository();
52
        $user = $repo->findOneBy(['username' => $username]);
53
        $em   = $this->getUserEntityManager();
54
        if (null === $user) {
55
            $user = new User($username, $email, $password);
56
            $em->persist($user);
57
            $em->flush();
58
        }
59
60
        $em->refresh($user);
61
62
        return $user;
63
    }
64
65
    protected function iDontHaveUser(string $username): void
66
    {
67
        /** @var UserRepository $repo */
68
        $repo = $this->getUserRepository();
69
        $user = $repo->loadUserByIdentifier($username);
70
        $em   = $this->getUserEntityManager();
71
        if (null !== $user) {
72
            $em->remove($user);
73
            $em->flush();
74
        }
75
    }
76
77
    /**
78
     * @return \Doctrine\ORM\EntityRepository|\Doctrine\Persistence\ObjectRepository
79
     */
80
    protected function getUserRepository()
81
    {
82
        return $this->getEntityManager()->getRepository(UserInterface::class);
83
    }
84
85
    public function getUserEntityManager()
86
    {
87
        $model = (string) $this->getContainer()->getParameter('eoffice.user.models.user');
0 ignored issues
show
Bug introduced by
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        $model = (string) $this->/** @scrutinizer ignore-call */ getContainer()->getParameter('eoffice.user.models.user');
Loading history...
88
89
        return $this->getEntityManager($model);
90
    }
91
}
92