Issues (3627)

bundles/UserBundle/Tests/Model/UserModelTest.php (8 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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Mautic...lper\MailHelper::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Mautic\EmailBundle\Helper\MailHelper of property $mailHelper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
77
        $this->userTokenService = $this->createMock(UserTokenServiceInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Mautic...erviceInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Mautic\UserBundle\Model\UserToken\UserTokenService of property $userTokenService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
78
        $this->entityManager    = $this->createMock(EntityManager::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Doctri...M\EntityManager::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Doctrine\ORM\EntityManager of property $entityManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79
        $this->user             = $this->createMock(User::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Mautic...dle\Entity\User::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Mautic\UserBundle\Entity\User of property $user.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
        $this->router           = $this->createMock(Router::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Symfon...\Routing\Router::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Bundle\FrameworkBundle\Routing\Router of property $router.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81
        $this->translator       = $this->createMock(TranslatorInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Symfon...slatorInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\Translation\TranslatorInterface of property $translator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
82
        $this->userToken        = $this->createMock(UserToken::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Mautic...ntity\UserToken::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Mautic\UserBundle\Entity\UserToken of property $userToken.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
83
        $this->logger           = $this->createMock(Logger::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Monolog\Logger::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Monolog\Logger of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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())
95
            ->method('generateSecret')
96
            ->willReturn($this->userToken);
97
98
        $this->mailHelper->expects($this->once())
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())
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())
123
            ->method('flush')
124
            ->willThrowException(new \Exception($errorMessage));
125
126
        $this->logger->expects($this->once())
127
            ->method('addError')
128
            ->with($errorMessage);
129
130
        $this->userModel->sendResetEmail($this->user);
131
    }
132
}
133