Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

unit/Service/EntityVersionLockServiceTest.php (2 issues)

mismatching argument types.

Documentation Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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()
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);
60
        $newEntity->method('getEntityClass')->willReturn(self::$TEST_CLASS);
61
        $newEntity->method('getEntityId')->willReturn(self::$TEST_NEW_ENTITY_ID);
62
        $newEntity->method('getUpdated')->willReturn(new \DateTime());
63
64
        $entity = $this->getMockBuilder(LockableEntity::class)->getMock();
65
        $entity->method('getId')->willReturn(self::$TEST_ID);
66
        $entity->method('getEntityClass')->willReturn(self::$TEST_CLASS);
67
        $entity->method('getEntityId')->willReturn(self::$TEST_ENTITY_ID);
68
        $entity->method('getUpdated')->willReturn(new \DateTime());
69
70
        $outDatedEntity = $this->getMockBuilder(LockableEntity::class)->getMock();
71
        $outDatedEntity->method('getId')->willReturn(self::$ALTERNATIVE_TEST_ID);
72
        $outDatedEntity->method('getEntityClass')->willReturn(self::$TEST_CLASS);
73
        $outDatedEntity->method('getEntityId')->willReturn(self::$ALTERNATIVE_TEST_ENTITY_ID);
74
        $outDatedEntity->method('getUpdated')->willReturn(new \DateTime('-1 days'));
75
76
        $newEntityVersionLock = new EntityVersionLock();
77
        $newEntityVersionLock->setOwner(self::$ALTERNATIVE_USER);
78
        $newEntityVersionLock->setLockableEntity($newEntity);
0 ignored issues
show
$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);
84
        $entityVersionLock->setCreatedAt(new \DateTime());
85
86
        $expiredEntityVersionLock = new EntityVersionLock();
87
        $expiredEntityVersionLock->setOwner($user->getUsername());
88
        $expiredEntityVersionLock->setLockableEntity($outDatedEntity);
0 ignored issues
show
$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
            ['KunstmaanAdminListBundle:EntityVersionLock', $mockLockRepository],
127
            ['KunstmaanAdminListBundle:LockableEntity', $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);
138
    }
139
140
    /**
141
     * Tears down the fixture, for example, closes a network connection.
142
     * This method is called after a test is executed.
143
     */
144
    protected function tearDown()
145
    {
146
    }
147
148
    public function testIsEntityBelowThresholdReturnsFalseWhenLockIsNew()
149
    {
150
        $result = $this->object->isEntityBelowThreshold(new TestLockableEntityInterfaceImplementation(self::$TEST_NEW_ENTITY_ID));
151
152
        $this->assertFalse($result);
153
    }
154
155
    public function testIsEntityBelowThresholdReturnsTrueWhenEntityUpdatedAtIsBelowThreshold()
156
    {
157
        $result = $this->object->isEntityBelowThreshold(new TestLockableEntityInterfaceImplementation(self::$TEST_ENTITY_ID));
158
159
        $this->assertTrue($result);
160
    }
161
162
    public function testIsEntityBelowThresholdReturnsFalseWhenEntityUpdatedAtIsOverTreshold()
163
    {
164
        $result = $this->object->isEntityBelowThreshold(new TestLockableEntityInterfaceImplementation(self::$ALTERNATIVE_TEST_ENTITY_ID));
165
166
        $this->assertFalse($result);
167
    }
168
169
    public function testIsEntityLockedReturnsTrueWhenEntityLocked()
170
    {
171
        $result = $this->object->isEntityLocked($this->user, new TestLockableEntityInterfaceImplementation(self::$TEST_ENTITY_ID));
172
173
        $this->assertTrue($result);
174
    }
175
176
    public function testIsEntityLockedReturnsFalseWhenEntityIsNotLocked()
177
    {
178
        $result = $this->object->isEntityLocked($this->user, new TestLockableEntityInterfaceImplementation(self::$ALTERNATIVE_TEST_ENTITY_ID));
179
180
        $this->assertFalse($result);
181
    }
182
183
    public function testGetUsersWithEntityVersionLockReturnsArrayWithOnlyUsernames()
184
    {
185
        $result = $this->object->getUsersWithEntityVersionLock(new TestLockableEntityInterfaceImplementation(self::$TEST_ENTITY_ID), $this->user);
186
187
        $this->assertContains(self::$ALTERNATIVE_USER, $result);
188
    }
189
}
190