Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Tests/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\Model\TestLockableEntityInterfaceImplementation;
10
use PHPUnit\Framework\TestCase;
11
12
class EntityVersionLockServiceTest extends TestCase
13
{
14
    protected static $TEST_CLASS = 'Kunstmaan\\AdminListBundle\\Tests\\Model\\TestLockableEntityInterfaceImplementation';
15
16
    protected static $TEST_NEW_ENTITY_ID = '1';
17
18
    protected static $TEST_ID = '5';
19
20
    protected static $TEST_ENTITY_ID = '5';
21
22
    protected static $ALTERNATIVE_TEST_ID = '391';
23
24
    protected static $ALTERNATIVE_TEST_ENTITY_ID = '391';
25
26
    protected static $USER_ID = '104';
27
28
    protected static $USER_NAME = 'Kevin Test';
29
30
    protected static $ALTERNATIVE_USER = 'Alternative Test';
31
32
    protected static $THRESHOLD = 35;
33
34
    /**
35
     * @var EntityVersionLockService
36
     */
37
    protected $object;
38
39
    /**
40
     * @var User
41
     */
42
    protected $user;
43
44
    protected function setUp()
45
    {
46
        $user = new User();
47
        $user->setId(self::$USER_ID);
48
        $user->setUsername(self::$USER_NAME);
49
        $this->user = $user;
50
51
        $newEntity = $this->getMockBuilder(LockableEntity::class)->getMock();
52
        $newEntity->method('getId')->willReturn(null);
53
        $newEntity->method('getEntityClass')->willReturn(self::$TEST_CLASS);
54
        $newEntity->method('getEntityId')->willReturn(self::$TEST_NEW_ENTITY_ID);
55
        $newEntity->method('getUpdated')->willReturn(new \DateTime());
56
57
        $entity = $this->getMockBuilder(LockableEntity::class)->getMock();
58
        $entity->method('getId')->willReturn(self::$TEST_ID);
59
        $entity->method('getEntityClass')->willReturn(self::$TEST_CLASS);
60
        $entity->method('getEntityId')->willReturn(self::$TEST_ENTITY_ID);
61
        $entity->method('getUpdated')->willReturn(new \DateTime());
62
63
        $outDatedEntity = $this->getMockBuilder(LockableEntity::class)->getMock();
64
        $outDatedEntity->method('getId')->willReturn(self::$ALTERNATIVE_TEST_ID);
65
        $outDatedEntity->method('getEntityClass')->willReturn(self::$TEST_CLASS);
66
        $outDatedEntity->method('getEntityId')->willReturn(self::$ALTERNATIVE_TEST_ENTITY_ID);
67
        $outDatedEntity->method('getUpdated')->willReturn(new \DateTime('-1 days'));
68
69
        $newEntityVersionLock = new EntityVersionLock();
70
        $newEntityVersionLock->setOwner(self::$ALTERNATIVE_USER);
71
        $newEntityVersionLock->setLockableEntity($newEntity);
72
        $newEntityVersionLock->setCreatedAt(new \DateTime());
73
74
        $entityVersionLock = new EntityVersionLock();
75
        $entityVersionLock->setOwner(self::$ALTERNATIVE_USER);
76
        $entityVersionLock->setLockableEntity($entity);
0 ignored issues
show
$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...
77
        $entityVersionLock->setCreatedAt(new \DateTime());
78
79
        $expiredEntityVersionLock = new EntityVersionLock();
80
        $expiredEntityVersionLock->setOwner($user->getUsername());
81
        $expiredEntityVersionLock->setLockableEntity($outDatedEntity);
82
        $expiredEntityVersionLock->setCreatedAt(new \DateTime('-1 days'));
83
84
        $locksMap = [
85
            [$newEntity, self::$THRESHOLD, $this->user, [$newEntityVersionLock]],
86
            [$entity, self::$THRESHOLD, $this->user, [$entityVersionLock]],
87
            [$outDatedEntity, self::$THRESHOLD, $this->user, []],
88
        ];
89
        $mockLockRepository = $this->getMockBuilder('Kunstmaan\AdminListBundle\Repository\EntityVersionLockRepository')
90
            ->disableOriginalConstructor()
91
            ->getMock();
92
        $mockLockRepository
93
            ->expects($this->any())
94
            ->method('findOneBy')
95
            ->will($this->returnValue($entityVersionLock));
96
        $mockLockRepository
97
            ->expects($this->any())
98
            ->method('getExpiredLocks')
99
            ->will($this->returnValue([$expiredEntityVersionLock]));
100
        $mockLockRepository
101
            ->expects($this->any())
102
            ->method('getLocksForLockableEntity')
103
            ->will($this->returnValueMap($locksMap));
104
105
        $lockableMap = [
106
            [self::$TEST_NEW_ENTITY_ID, self::$TEST_CLASS, $newEntity],
107
            [self::$TEST_ENTITY_ID, self::$TEST_CLASS, $entity],
108
            [self::$ALTERNATIVE_TEST_ENTITY_ID, self::$TEST_CLASS, $outDatedEntity],
109
        ];
110
        $mockLockableRepository = $this->getMockBuilder('Kunstmaan\AdminListBundle\Repository\LockableEntityRepository')
111
            ->disableOriginalConstructor()
112
            ->getMock();
113
        $mockLockableRepository
114
            ->expects($this->any())
115
            ->method('getOrCreate')
116
            ->will($this->returnValueMap($lockableMap));
117
118
        $repositoryMap = [
119
            [EntityVersionLock::class, $mockLockRepository],
120
            [LockableEntity::class, $mockLockableRepository],
121
        ];
122
        $mockObjectManager = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
123
            ->disableOriginalConstructor()
124
            ->getMock();
125
        $mockObjectManager
126
            ->expects($this->any())
127
            ->method('getRepository')
128
            ->will($this->returnValueMap($repositoryMap));
129
130
        $this->object = new EntityVersionLockService($mockObjectManager, self::$THRESHOLD, true);
0 ignored issues
show
$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...
131
    }
132
133
    public function testIsEntityBelowThresholdReturnsFalseWhenLockIsNew()
134
    {
135
        $result = $this->object->isEntityBelowThreshold(new TestLockableEntityInterfaceImplementation(self::$TEST_NEW_ENTITY_ID));
136
137
        $this->assertFalse($result);
138
    }
139
140
    public function testIsEntityBelowThresholdReturnsTrueWhenEntityUpdatedAtIsBelowThreshold()
141
    {
142
        $result = $this->object->isEntityBelowThreshold(new TestLockableEntityInterfaceImplementation(self::$TEST_ENTITY_ID));
143
144
        $this->assertTrue($result);
145
    }
146
147
    public function testIsEntityBelowThresholdReturnsFalseWhenEntityUpdatedAtIsOverTreshold()
148
    {
149
        $result = $this->object->isEntityBelowThreshold(new TestLockableEntityInterfaceImplementation(self::$ALTERNATIVE_TEST_ENTITY_ID));
150
151
        $this->assertFalse($result);
152
    }
153
154
    public function testIsEntityLockedReturnsTrueWhenEntityLocked()
155
    {
156
        $result = $this->object->isEntityLocked($this->user, new TestLockableEntityInterfaceImplementation(self::$TEST_ENTITY_ID));
157
158
        $this->assertTrue($result);
159
    }
160
161
    public function testIsEntityLockedReturnsFalseWhenEntityIsNotLocked()
162
    {
163
        $result = $this->object->isEntityLocked($this->user, new TestLockableEntityInterfaceImplementation(self::$ALTERNATIVE_TEST_ENTITY_ID));
164
165
        $this->assertFalse($result);
166
    }
167
168
    public function testGetUsersWithEntityVersionLockReturnsArrayWithOnlyUsernames()
169
    {
170
        $result = $this->object->getUsersWithEntityVersionLock(new TestLockableEntityInterfaceImplementation(self::$TEST_ENTITY_ID), $this->user);
171
172
        $this->assertContains(self::$ALTERNATIVE_USER, $result);
173
    }
174
}
175