|
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'); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
return $this->getEntityManager($model); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|