Completed
Push — symfony5 ( 074008...b11eb2 )
by
unknown
267:19 queued 255:17
created

EmailProviderTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 7
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\MVC\Symfony\Security\Tests\User;
10
11
use eZ\Publish\API\Repository\PermissionResolver;
12
use eZ\Publish\API\Repository\Values\User\User as APIUser;
13
use eZ\Publish\API\Repository\UserService;
14
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
15
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
16
use eZ\Publish\Core\MVC\Symfony\Security\User\EmailProvider;
17
use eZ\Publish\Core\MVC\Symfony\Security\UserInterface;
18
use eZ\Publish\Core\Repository\Values\Content\Content;
19
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
20
use eZ\Publish\Core\Repository\Values\User\User;
21
use eZ\Publish\Core\MVC\Symfony\Security\User as MVCUser;
22
use eZ\Publish\Core\Repository\Values\User\UserReference;
23
use PHPUnit\Framework\TestCase;
24
use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface;
25
26 View Code Duplication
class EmailProviderTest extends TestCase
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
{
28
    /** @var \eZ\Publish\API\Repository\UserService|\PHPUnit\Framework\MockObject\MockObject */
29
    private $userService;
30
31
    /** @var \eZ\Publish\API\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject */
32
    private $permissionResolver;
33
34
    /** @var \eZ\Publish\Core\MVC\Symfony\Security\User\EmailProvider */
35
    private $userProvider;
36
37
    protected function setUp(): void
38
    {
39
        parent::setUp();
40
        $this->userService = $this->createMock(UserService::class);
41
        $this->permissionResolver = $this->createMock(PermissionResolver::class);
42
        $this->userProvider = new EmailProvider($this->userService, $this->permissionResolver);
43
    }
44
45
    public function testLoadUserByUsernameAlreadyUserObject()
46
    {
47
        $user = $this->createMock(UserInterface::class);
48
        $this->assertSame($user, $this->userProvider->loadUserByUsername($user));
49
    }
50
51
    public function testLoadUserByUsernameUserNotFound()
52
    {
53
        $this->expectException(\Symfony\Component\Security\Core\Exception\UsernameNotFoundException::class);
54
55
        $username = '[email protected]';
56
        $this->userService
57
            ->expects($this->once())
58
            ->method('loadUserByEmail')
59
            ->with($username)
60
            ->will($this->throwException(new NotFoundException('user', $username)));
61
        $this->userProvider->loadUserByUsername($username);
62
    }
63
64
    public function testLoadUserByUsername()
65
    {
66
        $username = '[email protected]';
67
        $apiUser = $this->createMock(APIUser::class);
68
69
        $this->userService
70
            ->expects($this->once())
71
            ->method('loadUserByEmail')
72
            ->with($username)
73
            ->will($this->returnValue($apiUser));
74
75
        $user = $this->userProvider->loadUserByUsername($username);
76
        $this->assertInstanceOf(UserInterface::class, $user);
77
        $this->assertSame($apiUser, $user->getAPIUser());
78
        $this->assertSame(['ROLE_USER'], $user->getRoles());
79
    }
80
81
    public function testRefreshUserNotSupported()
82
    {
83
        $this->expectException(\Symfony\Component\Security\Core\Exception\UnsupportedUserException::class);
84
85
        $user = $this->createMock(SymfonyUserInterface::class);
86
        $this->userProvider->refreshUser($user);
87
    }
88
89
    public function testRefreshUser()
90
    {
91
        $userId = 123;
92
        $apiUser = new User(
93
            [
94
                'content' => new Content(
95
                    [
96
                        'versionInfo' => new VersionInfo(
97
                            ['contentInfo' => new ContentInfo(['id' => $userId])]
98
                        ),
99
                    ]
100
                ),
101
            ]
102
        );
103
        $refreshedAPIUser = clone $apiUser;
104
        $user = $this->createMock(UserInterface::class);
105
        $user
106
            ->expects($this->once())
107
            ->method('getAPIUser')
108
            ->will($this->returnValue($apiUser));
109
        $user
110
            ->expects($this->once())
111
            ->method('setAPIUser')
112
            ->with($refreshedAPIUser);
113
114
        $this->userService
115
            ->expects($this->once())
116
            ->method('loadUser')
117
            ->with($userId)
118
            ->will($this->returnValue($refreshedAPIUser));
119
120
        $this->permissionResolver
121
            ->expects($this->once())
122
            ->method('setCurrentUserReference')
123
            ->with(new UserReference($apiUser->getUserId()));
124
125
        $this->assertSame($user, $this->userProvider->refreshUser($user));
126
    }
127
128
    public function testRefreshUserNotFound()
129
    {
130
        $this->expectException(\Symfony\Component\Security\Core\Exception\UsernameNotFoundException::class);
131
132
        $userId = 123;
133
        $apiUser = new User(
134
            [
135
                'content' => new Content(
136
                    [
137
                        'versionInfo' => new VersionInfo(
138
                            ['contentInfo' => new ContentInfo(['id' => $userId])]
139
                        ),
140
                    ]
141
                ),
142
            ]
143
        );
144
        $user = $this->createMock(UserInterface::class);
145
        $user
146
            ->expects($this->once())
147
            ->method('getAPIUser')
148
            ->will($this->returnValue($apiUser));
149
150
        $this->userService
151
            ->expects($this->once())
152
            ->method('loadUser')
153
            ->with($userId)
154
            ->will($this->throwException(new NotFoundException('user', 'foo')));
155
156
        $this->userProvider->refreshUser($user);
157
    }
158
159
    /**
160
     * @dataProvider supportsClassProvider
161
     */
162
    public function testSupportsClass($class, $supports)
163
    {
164
        $this->assertSame($supports, $this->userProvider->supportsClass($class));
165
    }
166
167
    public function supportsClassProvider()
168
    {
169
        return [
170
            [SymfonyUserInterface::class, false],
171
            [MVCUser::class, true],
172
            [get_class($this->createMock(MVCUser::class)), true],
173
        ];
174
    }
175
176
    public function testLoadUserByAPIUser()
177
    {
178
        $apiUser = $this->createMock(APIUser::class);
179
180
        $user = $this->userProvider->loadUserByAPIUser($apiUser);
181
182
        $this->assertInstanceOf(MVCUser::class, $user);
183
        $this->assertSame($apiUser, $user->getAPIUser());
184
        $this->assertSame(['ROLE_USER'], $user->getRoles());
185
    }
186
}
187