testGetUsersWithEntityVersionLockReturnsArrayWithOnlyUsernames()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Kunstmaan\AdminListBundle\Tests\Service;
4
5
use Kunstmaan\AdminBundle\Entity\User;
6
use Kunstmaan\AdminListBundle\Entity\EntityVersionLock;
7
use Kunstmaan\AdminListBundle\Entity\LockableEntity;
8
use Kunstmaan\AdminListBundle\Service\EntityVersionLockService;
9
use Kunstmaan\AdminListBundle\Tests\unit\Model\TestLockableEntityInterfaceImplementation;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * class EntityVersionLockServiceTest
14
 */
15
class EntityVersionLockServiceTest extends TestCase
16
{
17
    protected static $TEST_CLASS = 'Kunstmaan\\AdminListBundle\\Tests\\unit\\Model\\TestLockableEntityInterfaceImplementation';
18
19
    protected static $TEST_NEW_ENTITY_ID = '1';
20
21
    protected static $TEST_ID = '5';
22
23
    protected static $TEST_ENTITY_ID = '5';
24
25
    protected static $ALTERNATIVE_TEST_ID = '391';
26
27
    protected static $ALTERNATIVE_TEST_ENTITY_ID = '391';
28
29
    protected static $USER_ID = '104';
30
31
    protected static $USER_NAME = 'Kevin Test';
32
33
    protected static $ALTERNATIVE_USER = 'Alternative Test';
34
35
    protected static $THRESHOLD = 35;
36
37
    /**
38
     * @var EntityVersionLockService
39
     */
40
    protected $object;
41
42
    /**
43
     * @var User
44
     */
45
    protected $user;
46
47
    /**
48
     * Sets up the fixture, for example, opens a network connection.
49
     * This method is called before a test is executed.
50
     */
51
    protected function setUp(): void
52
    {
53
        $user = new User();
54
        $user->setId(self::$USER_ID);
55
        $user->setUsername(self::$USER_NAME);
56
        $this->user = $user;
57
58
        $newEntity = $this->getMockBuilder(LockableEntity::class)->getMock();
59
        $newEntity->method('getId')->willReturn(null);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
60
        $newEntity->method('getEntityClass')->willReturn(self::$TEST_CLASS);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
61
        $newEntity->method('getEntityId')->willReturn(self::$TEST_NEW_ENTITY_ID);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
62
        $newEntity->method('getUpdated')->willReturn(new \DateTime());
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
63
64
        $entity = $this->getMockBuilder(LockableEntity::class)->getMock();
65
        $entity->method('getId')->willReturn(self::$TEST_ID);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
66
        $entity->method('getEntityClass')->willReturn(self::$TEST_CLASS);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
67
        $entity->method('getEntityId')->willReturn(self::$TEST_ENTITY_ID);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
68
        $entity->method('getUpdated')->willReturn(new \DateTime());
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
69
70
        $outDatedEntity = $this->getMockBuilder(LockableEntity::class)->getMock();
71
        $outDatedEntity->method('getId')->willReturn(self::$ALTERNATIVE_TEST_ID);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
72
        $outDatedEntity->method('getEntityClass')->willReturn(self::$TEST_CLASS);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
73
        $outDatedEntity->method('getEntityId')->willReturn(self::$ALTERNATIVE_TEST_ENTITY_ID);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
74
        $outDatedEntity->method('getUpdated')->willReturn(new \DateTime('-1 days'));
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
75
76
        $newEntityVersionLock = new EntityVersionLock();
77
        $newEntityVersionLock->setOwner(self::$ALTERNATIVE_USER);
78
        $newEntityVersionLock->setLockableEntity($newEntity);
0 ignored issues
show
Documentation introduced by
$newEntity is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\AdminLi...\Entity\LockableEntity>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
        $newEntityVersionLock->setCreatedAt(new \DateTime());
80
81
        $entityVersionLock = new EntityVersionLock();
82
        $entityVersionLock->setOwner(self::$ALTERNATIVE_USER);
83
        $entityVersionLock->setLockableEntity($entity);
0 ignored issues
show
Documentation introduced by
$entity is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\AdminLi...\Entity\LockableEntity>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
        $entityVersionLock->setCreatedAt(new \DateTime());
85
86
        $expiredEntityVersionLock = new EntityVersionLock();
87
        $expiredEntityVersionLock->setOwner($user->getUsername());
88
        $expiredEntityVersionLock->setLockableEntity($outDatedEntity);
0 ignored issues
show
Documentation introduced by
$outDatedEntity is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\AdminLi...\Entity\LockableEntity>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
        $expiredEntityVersionLock->setCreatedAt(new \DateTime('-1 days'));
90
91
        $locksMap = [
92
            [$newEntity, self::$THRESHOLD, $this->user, [$newEntityVersionLock]],
93
            [$entity, self::$THRESHOLD, $this->user, [$entityVersionLock]],
94
            [$outDatedEntity, self::$THRESHOLD, $this->user, []],
95
        ];
96
        $mockLockRepository = $this->getMockBuilder('Kunstmaan\AdminListBundle\Repository\EntityVersionLockRepository')
97
            ->disableOriginalConstructor()
98
            ->getMock();
99
        $mockLockRepository
100
            ->expects($this->any())
101
            ->method('findOneBy')
102
            ->will($this->returnValue($entityVersionLock));
103
        $mockLockRepository
104
            ->expects($this->any())
105
            ->method('getExpiredLocks')
106
            ->will($this->returnValue([$expiredEntityVersionLock]));
107
        $mockLockRepository
108
            ->expects($this->any())
109
            ->method('getLocksForLockableEntity')
110
            ->will($this->returnValueMap($locksMap));
111
112
        $lockableMap = [
113
            [self::$TEST_NEW_ENTITY_ID, self::$TEST_CLASS, $newEntity],
114
            [self::$TEST_ENTITY_ID, self::$TEST_CLASS, $entity],
115
            [self::$ALTERNATIVE_TEST_ENTITY_ID, self::$TEST_CLASS, $outDatedEntity],
116
        ];
117
        $mockLockableRepository = $this->getMockBuilder('Kunstmaan\AdminListBundle\Repository\LockableEntityRepository')
118
            ->disableOriginalConstructor()
119
            ->getMock();
120
        $mockLockableRepository
121
            ->expects($this->any())
122
            ->method('getOrCreate')
123
            ->will($this->returnValueMap($lockableMap));
124
125
        $repositoryMap = [
126
            [EntityVersionLock::class, $mockLockRepository],
127
            [LockableEntity::class, $mockLockableRepository],
128
        ];
129
        $mockObjectManager = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
130
            ->disableOriginalConstructor()
131
            ->getMock();
132
        $mockObjectManager
133
            ->expects($this->any())
134
            ->method('getRepository')
135
            ->will($this->returnValueMap($repositoryMap));
136
137
        $this->object = new EntityVersionLockService($mockObjectManager, self::$THRESHOLD, true);
0 ignored issues
show
Documentation introduced by
$mockObjectManager is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\Common\Persistence\ObjectManager>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
    }
139
140
    public function testIsEntityBelowThresholdReturnsFalseWhenLockIsNew()
141
    {
142
        $result = $this->object->isEntityBelowThreshold(new TestLockableEntityInterfaceImplementation(self::$TEST_NEW_ENTITY_ID));
143
144
        $this->assertFalse($result);
145
    }
146
147
    public function testIsEntityBelowThresholdReturnsTrueWhenEntityUpdatedAtIsBelowThreshold()
148
    {
149
        $result = $this->object->isEntityBelowThreshold(new TestLockableEntityInterfaceImplementation(self::$TEST_ENTITY_ID));
150
151
        $this->assertTrue($result);
152
    }
153
154
    public function testIsEntityBelowThresholdReturnsFalseWhenEntityUpdatedAtIsOverTreshold()
155
    {
156
        $result = $this->object->isEntityBelowThreshold(new TestLockableEntityInterfaceImplementation(self::$ALTERNATIVE_TEST_ENTITY_ID));
157
158
        $this->assertFalse($result);
159
    }
160
161
    public function testIsEntityLockedReturnsTrueWhenEntityLocked()
162
    {
163
        $result = $this->object->isEntityLocked($this->user, new TestLockableEntityInterfaceImplementation(self::$TEST_ENTITY_ID));
164
165
        $this->assertTrue($result);
166
    }
167
168
    public function testIsEntityLockedReturnsFalseWhenEntityIsNotLocked()
169
    {
170
        $result = $this->object->isEntityLocked($this->user, new TestLockableEntityInterfaceImplementation(self::$ALTERNATIVE_TEST_ENTITY_ID));
171
172
        $this->assertFalse($result);
173
    }
174
175
    public function testGetUsersWithEntityVersionLockReturnsArrayWithOnlyUsernames()
176
    {
177
        $result = $this->object->getUsersWithEntityVersionLock(new TestLockableEntityInterfaceImplementation(self::$TEST_ENTITY_ID), $this->user);
178
179
        $this->assertContains(self::$ALTERNATIVE_USER, $result);
180
    }
181
}
182