|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Form\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Domain\Entities\User as Entity; |
|
8
|
|
|
use AbterPhp\Admin\Domain\Entities\UserGroup; |
|
|
|
|
|
|
9
|
|
|
use AbterPhp\Admin\Domain\Entities\UserLanguage; |
|
10
|
|
|
use AbterPhp\Admin\Orm\UserGroupRepo; |
|
11
|
|
|
use AbterPhp\Admin\Orm\UserLanguageRepo; |
|
12
|
|
|
use AbterPhp\Framework\I18n\ITranslator; |
|
13
|
|
|
use Opulence\Http\Requests\RequestMethods; |
|
14
|
|
|
use Opulence\Orm\IEntity; |
|
15
|
|
|
use Opulence\Sessions\ISession; |
|
16
|
|
|
use Opulence\Sessions\Session; |
|
17
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
|
|
20
|
|
|
class UserTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var User - System Under Test */ |
|
23
|
|
|
protected User $sut; |
|
24
|
|
|
|
|
25
|
|
|
/** @var ISession|MockObject */ |
|
26
|
|
|
protected $sessionMock; |
|
27
|
|
|
|
|
28
|
|
|
/** @var ITranslator|MockObject */ |
|
29
|
|
|
protected $translatorMock; |
|
30
|
|
|
|
|
31
|
|
|
/** @var UserGroupRepo|MockObject */ |
|
32
|
|
|
protected $userGroupRepoMock; |
|
33
|
|
|
|
|
34
|
|
|
/** @var UserLanguageRepo|MockObject */ |
|
35
|
|
|
protected $userLanguageRepoMock; |
|
36
|
|
|
|
|
37
|
|
|
public function setUp(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$this->sessionMock = $this->createMock(Session::class); |
|
40
|
|
|
$this->sessionMock->expects($this->any())->method('get')->willReturnArgument(0); |
|
41
|
|
|
|
|
42
|
|
|
$this->translatorMock = $this->createMock(ITranslator::class); |
|
43
|
|
|
$this->translatorMock->expects($this->any())->method('translate')->willReturnArgument(0); |
|
44
|
|
|
|
|
45
|
|
|
$this->userGroupRepoMock = $this->createMock(UserGroupRepo::class); |
|
46
|
|
|
|
|
47
|
|
|
$this->userLanguageRepoMock = $this->createMock(UserLanguageRepo::class); |
|
48
|
|
|
|
|
49
|
|
|
$this->sut = new User( |
|
50
|
|
|
$this->sessionMock, |
|
51
|
|
|
$this->translatorMock, |
|
52
|
|
|
$this->userGroupRepoMock, |
|
53
|
|
|
$this->userLanguageRepoMock |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testCreate() |
|
58
|
|
|
{ |
|
59
|
|
|
$action = 'foo'; |
|
60
|
|
|
$method = RequestMethods::POST; |
|
61
|
|
|
$showUrl = 'bar'; |
|
62
|
|
|
$entityId = '96368723-292f-4943-9903-83ad552fc118'; |
|
63
|
|
|
$password = 'baz'; |
|
64
|
|
|
$username = 'zorro79'; |
|
65
|
|
|
$email = '[email protected]'; |
|
66
|
|
|
$canLogin = true; |
|
67
|
|
|
$isGravatarAllowed = true; |
|
68
|
|
|
$allUserGroups = [ |
|
69
|
|
|
new UserGroup('dc2abea5-8021-4228-882a-31b91fe3687a', 'ug-22', 'UG 22'), |
|
70
|
|
|
new UserGroup('75ccb863-ce02-4b2d-b655-af4c92f2dbe6', 'ug-73', 'UG 73'), |
|
71
|
|
|
new UserGroup('aff15988-2170-4b10-9aad-4ed2ea19f73e', 'ug-112', 'UG 112'), |
|
72
|
|
|
new UserGroup('143522ce-5e0e-4abb-8c4b-67d88ba90d9d', 'ug-432', 'UG 432'), |
|
73
|
|
|
]; |
|
74
|
|
|
$userGroups = [ |
|
75
|
|
|
$allUserGroups[0], |
|
76
|
|
|
$allUserGroups[2], |
|
77
|
|
|
]; |
|
78
|
|
|
$allUserLanguages = [ |
|
79
|
|
|
new UserLanguage('5027689a-39da-4810-b0b6-2ae42e387698', 'ul-52', 'UL 52'), |
|
80
|
|
|
new UserLanguage('afb423cc-6272-4ff4-8a62-e28cac6cb1d1', 'ul-77', 'UL 77'), |
|
81
|
|
|
new UserLanguage('38030970-10be-4b6b-9dc6-38d6a74310ca', 'ul-93', 'UL 93'), |
|
82
|
|
|
new UserLanguage('15c3f8ef-d5ff-4210-a5a2-3732f4073a1b', 'ul-94', 'UL 94'), |
|
83
|
|
|
]; |
|
84
|
|
|
$userLanguage = $allUserLanguages[1]; |
|
85
|
|
|
|
|
86
|
|
|
$this->userGroupRepoMock->expects($this->any())->method('getAll')->willReturn($allUserGroups); |
|
87
|
|
|
$this->userLanguageRepoMock->expects($this->any())->method('getAll')->willReturn($allUserLanguages); |
|
88
|
|
|
|
|
89
|
|
|
$entityStub = new Entity( |
|
90
|
|
|
$entityId, |
|
91
|
|
|
$username, |
|
92
|
|
|
$email, |
|
93
|
|
|
$password, |
|
94
|
|
|
$canLogin, |
|
95
|
|
|
$isGravatarAllowed, |
|
96
|
|
|
$userLanguage, |
|
97
|
|
|
$userGroups |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$form = (string)$this->sut->create($action, $method, $showUrl, $entityStub); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertStringContainsString($action, $form); |
|
103
|
|
|
$this->assertStringContainsString($showUrl, $form); |
|
104
|
|
|
$this->assertStringContainsString('CSRF', $form); |
|
105
|
|
|
$this->assertStringContainsString('POST', $form); |
|
106
|
|
|
$this->assertStringContainsString('username', $form); |
|
107
|
|
|
$this->assertStringContainsString('email', $form); |
|
108
|
|
|
$this->assertStringContainsString('password', $form); |
|
109
|
|
|
$this->assertStringContainsString('password_confirmed', $form); |
|
110
|
|
|
$this->assertStringContainsString('raw_password', $form); |
|
111
|
|
|
$this->assertStringContainsString('raw_password_confirmed', $form); |
|
112
|
|
|
$this->assertStringContainsString('can_login', $form); |
|
113
|
|
|
$this->assertStringContainsString('is_gravatar_allowed', $form); |
|
114
|
|
|
$this->assertStringContainsString('user_group_ids', $form); |
|
115
|
|
|
$this->assertStringContainsString('user_language_id', $form); |
|
116
|
|
|
$this->assertStringContainsString('selected', $form); |
|
117
|
|
|
$this->assertStringContainsString('button', $form); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testCreateThrowsExceptionIfWrongEntityIsProvided() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
123
|
|
|
|
|
124
|
|
|
$entityStub = $this->createMock(IEntity::class); |
|
125
|
|
|
|
|
126
|
|
|
$this->sut->create('foo', 'bar', '/baz', $entityStub); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return MockObject|Entity |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function createMockEntity() |
|
133
|
|
|
{ |
|
134
|
|
|
$entityMock = $this->createMock(Entity::class); |
|
135
|
|
|
|
|
136
|
|
|
return $entityMock; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: