Issues (3627)

bundles/UserBundle/Tests/Model/UserModelTest.php (5 issues)

1
<?php
2
3
/*
4
 * @copyright   2020 Mautic, Inc. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://mautic.com
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\UserBundle\Tests\Model;
13
14
use Doctrine\ORM\EntityManager;
15
use Mautic\EmailBundle\Helper\MailHelper;
16
use Mautic\UserBundle\Entity\User;
17
use Mautic\UserBundle\Entity\UserToken;
18
use Mautic\UserBundle\Model\UserModel;
19
use Mautic\UserBundle\Model\UserToken\UserTokenService;
20
use Mautic\UserBundle\Model\UserToken\UserTokenServiceInterface;
21
use Monolog\Logger;
22
use PHPUnit\Framework\TestCase;
23
use Symfony\Bundle\FrameworkBundle\Routing\Router;
24
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
25
use Symfony\Component\Translation\TranslatorInterface;
26
27
class UserModelTest extends TestCase
28
{
29
    /**
30
     * @var UserModel
31
     */
32
    private $userModel;
33
34
    /**
35
     * @var MailHelper
36
     */
37
    private $mailHelper;
38
39
    /**
40
     * @var EntityManager
41
     */
42
    private $entityManager;
43
44
    /**
45
     * @var Router
46
     */
47
    private $router;
48
49
    /**
50
     * @var TranslatorInterface
51
     */
52
    private $translator;
53
54
    /**
55
     * @var User
56
     */
57
    private $user;
58
59
    /**
60
     * @var UserToken
61
     */
62
    private $userToken;
63
64
    /**
65
     * @var UserTokenService
66
     */
67
    private $userTokenService;
68
69
    /**
70
     * @var Logger
71
     */
72
    private $logger;
73
74
    public function setUp(): void
75
    {
76
        $this->mailHelper       = $this->createMock(MailHelper::class);
77
        $this->userTokenService = $this->createMock(UserTokenServiceInterface::class);
78
        $this->entityManager    = $this->createMock(EntityManager::class);
79
        $this->user             = $this->createMock(User::class);
80
        $this->router           = $this->createMock(Router::class);
81
        $this->translator       = $this->createMock(TranslatorInterface::class);
82
        $this->userToken        = $this->createMock(UserToken::class);
83
        $this->logger           = $this->createMock(Logger::class);
84
85
        $this->userModel = new UserModel($this->mailHelper, $this->userTokenService);
86
        $this->userModel->setEntityManager($this->entityManager);
87
        $this->userModel->setRouter($this->router);
88
        $this->userModel->setTranslator($this->translator);
89
        $this->userModel->setLogger($this->logger);
90
    }
91
92
    public function testThatItSendsResetPasswordEmailAndRouterGetsCalledWithCorrectParamters(): void
93
    {
94
        $this->userTokenService->expects($this->once())
0 ignored issues
show
The method expects() does not exist on Mautic\UserBundle\Model\UserToken\UserTokenService. ( Ignorable by Annotation )

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

94
        $this->userTokenService->/** @scrutinizer ignore-call */ 
95
                                 expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
            ->method('generateSecret')
96
            ->willReturn($this->userToken);
97
98
        $this->mailHelper->expects($this->once())
0 ignored issues
show
The method expects() does not exist on Mautic\EmailBundle\Helper\MailHelper. ( Ignorable by Annotation )

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

98
        $this->mailHelper->/** @scrutinizer ignore-call */ 
99
                           expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
            ->method('getMailer')
100
            ->willReturn($this->mailHelper);
101
102
        $this->mailHelper->expects($this->once())
103
            ->method('send');
104
105
        $this->userTokenService->expects($this->once())
106
            ->method('generateSecret')
107
            ->willReturn($this->userToken);
108
109
        $this->router->expects($this->once())
0 ignored issues
show
The method expects() does not exist on Symfony\Bundle\FrameworkBundle\Routing\Router. ( Ignorable by Annotation )

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

109
        $this->router->/** @scrutinizer ignore-call */ 
110
                       expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
            ->method('generate')
111
            ->with('mautic_user_passwordresetconfirm', ['token' => null], UrlGeneratorInterface::ABSOLUTE_URL);
112
113
        $this->userModel->sendResetEmail($this->user);
114
    }
115
116
    public function testThatDatabaseErrorThrowsRuntimeExceptionAndItIsLoggedWhenWeTryToSaveTokenToTheDatabaseWhenWeSendResetPasswordEmail(): void
117
    {
118
        $errorMessage = 'Some error message';
119
120
        $this->expectException(\RuntimeException::class);
121
122
        $this->entityManager->expects($this->once())
0 ignored issues
show
The method expects() does not exist on Doctrine\ORM\EntityManager. ( Ignorable by Annotation )

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

122
        $this->entityManager->/** @scrutinizer ignore-call */ 
123
                              expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
            ->method('flush')
124
            ->willThrowException(new \Exception($errorMessage));
125
126
        $this->logger->expects($this->once())
0 ignored issues
show
The method expects() does not exist on Monolog\Logger. ( Ignorable by Annotation )

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

126
        $this->logger->/** @scrutinizer ignore-call */ 
127
                       expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
            ->method('addError')
128
            ->with($errorMessage);
129
130
        $this->userModel->sendResetEmail($this->user);
131
    }
132
}
133