Completed
Push — 1.0 ( a45813...b1efe3 )
by Kamil
42:02 queued 02:34
created

UserDeleteListenerSpec   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 8
dl 0
loc 85
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 5 1
A it_deletes_user_if_it_is_different_than_currently_logged_one() 0 20 1
A it_deletes_user_if_no_user_is_logged_in() 0 20 1
A it_deletes_user_if_there_is_no_token() 0 18 1
A it_does_not_allow_to_delete_currently_logged_user() 0 15 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 spec\Sylius\Bundle\UserBundle\EventListener;
15
16
use PhpSpec\ObjectBehavior;
17
use Prophecy\Argument;
18
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
19
use Sylius\Component\User\Model\UserInterface;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
22
use Symfony\Component\HttpFoundation\Session\SessionInterface;
23
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
24
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
25
26
final class UserDeleteListenerSpec extends ObjectBehavior
27
{
28
    function let(TokenStorageInterface $tokenStorage, SessionInterface $session, FlashBagInterface $flashBag): void
29
    {
30
        $this->beConstructedWith($tokenStorage, $session);
31
        $session->getBag('flashes')->willReturn($flashBag);
32
    }
33
34
    function it_deletes_user_if_it_is_different_than_currently_logged_one(
35
        TokenStorageInterface $tokenStorage,
36
        FlashBagInterface $flashBag,
37
        ResourceControllerEvent $event,
38
        UserInterface $userToBeDeleted,
39
        UserInterface $currentlyLoggedUser,
40
        TokenInterface $tokenInterface
41
    ): void {
42
        $event->getSubject()->willReturn($userToBeDeleted);
43
        $userToBeDeleted->getId()->willReturn(11);
44
45
        $tokenStorage->getToken()->willReturn($tokenInterface);
46
        $currentlyLoggedUser->getId()->willReturn(1);
47
        $tokenInterface->getUser()->willReturn($currentlyLoggedUser);
48
49
        $event->stopPropagation()->shouldNotBeCalled();
50
        $flashBag->add('error', Argument::any())->shouldNotBeCalled();
51
52
        $this->deleteUser($event);
53
    }
54
55
    function it_deletes_user_if_no_user_is_logged_in(
56
        TokenStorageInterface $tokenStorage,
57
        FlashBagInterface $flashBag,
58
        ResourceControllerEvent $event,
59
        UserInterface $userToBeDeleted,
60
        TokenInterface $tokenInterface
61
    ): void {
62
        $event->getSubject()->willReturn($userToBeDeleted);
63
        $userToBeDeleted->getId()->willReturn(11);
64
65
        $tokenStorage->getToken()->willReturn($tokenInterface);
66
        $tokenInterface->getUser()->willReturn(null);
67
68
        $event->stopPropagation()->shouldNotBeCalled();
69
        $event->setErrorCode(Argument::any())->shouldNotBeCalled();
70
        $event->setMessage(Argument::any())->shouldNotBeCalled();
71
        $flashBag->add('error', Argument::any())->shouldNotBeCalled();
72
73
        $this->deleteUser($event);
74
    }
75
76
    function it_deletes_user_if_there_is_no_token(
77
        TokenStorageInterface $tokenStorage,
78
        FlashBagInterface $flashBag,
79
        ResourceControllerEvent $event,
80
        UserInterface $userToBeDeleted
81
    ): void {
82
        $event->getSubject()->willReturn($userToBeDeleted);
83
        $userToBeDeleted->getId()->willReturn(11);
84
85
        $tokenStorage->getToken()->willReturn(null);
86
87
        $event->stopPropagation()->shouldNotBeCalled();
88
        $event->setErrorCode(Argument::any())->shouldNotBeCalled();
89
        $event->setMessage(Argument::any())->shouldNotBeCalled();
90
        $flashBag->add('error', Argument::any())->shouldNotBeCalled();
91
92
        $this->deleteUser($event);
93
    }
94
95
    function it_does_not_allow_to_delete_currently_logged_user(ResourceControllerEvent $event, UserInterface $userToBeDeleted, UserInterface $currentlyLoggedInUser, $tokenStorage, $flashBag, TokenInterface $token): void
96
    {
97
        $event->getSubject()->willReturn($userToBeDeleted);
98
        $userToBeDeleted->getId()->willReturn(1);
99
        $tokenStorage->getToken()->willReturn($token);
100
        $currentlyLoggedInUser->getId()->willReturn(1);
101
        $token->getUser()->willReturn($currentlyLoggedInUser);
102
103
        $event->stopPropagation()->shouldBeCalled();
104
        $event->setErrorCode(Response::HTTP_UNPROCESSABLE_ENTITY)->shouldBeCalled();
105
        $event->setMessage('Cannot remove currently logged in user.')->shouldBeCalled();
106
        $flashBag->add('error', 'Cannot remove currently logged in user.')->shouldBeCalled();
107
108
        $this->deleteUser($event);
109
    }
110
}
111