Issues (3641)

Zed/StateMachine/Business/Lock/ItemLockTest.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerTest\Zed\StateMachine\Business\Lock;
9
10
use Orm\Zed\StateMachine\Persistence\SpyStateMachineLock;
11
use Orm\Zed\StateMachine\Persistence\SpyStateMachineLockQuery;
12
use Propel\Runtime\Exception\PropelException;
13
use Spryker\Zed\StateMachine\Business\Exception\LockException;
14
use Spryker\Zed\StateMachine\Business\Lock\ItemLock;
15
use Spryker\Zed\StateMachine\Business\Lock\ItemLockInterface;
16
use Spryker\Zed\StateMachine\Persistence\StateMachineQueryContainerInterface;
17
use SprykerTest\Zed\StateMachine\Mocks\StateMachineMocks;
18
19
/**
20
 * Auto-generated group annotations
21
 *
22
 * @group SprykerTest
23
 * @group Zed
24
 * @group StateMachine
25
 * @group Business
26
 * @group Lock
27
 * @group ItemLockTest
28
 * Add your own group annotations below this line
29
 */
30
class ItemLockTest extends StateMachineMocks
31
{
32
    /**
33
     * @return void
34
     */
35
    public function testAcquireLockShouldCreateItemWithLockInPersistence(): void
36
    {
37
        $stateMachineLockEntityMock = $this->createStateMachineItemLockEntityMock();
38
        $stateMachineLockEntityMock->method('save')
39
            ->willReturn(1);
40
41
        $itemLock = $this->createItemLock($stateMachineLockEntityMock);
42
43
        $lockResult = $itemLock->acquire(sha1(1));
44
45
        $this->assertTrue($lockResult);
46
    }
47
48
    /**
49
     * @return void
50
     */
51
    public function testAcquireWhenPropelExceptionThrownShouldReThrowLockException(): void
52
    {
53
        // Arrange
54
        $stateMachineLockEntityMock = $this->createStateMachineItemLockEntityMock();
55
        $stateMachineLockEntityMock->method('save')
56
            ->willThrowException(new PropelException());
57
        $itemLock = $this->createItemLock($stateMachineLockEntityMock);
58
59
        // Assert
60
        $this->expectException(LockException::class);
61
62
        // Act
63
        $itemLock->acquire(sha1(1));
64
    }
65
66
    /**
67
     * @return void
68
     */
69
    public function testReleaseLockShouldDeleteLockFromDatabase(): void
70
    {
71
        $stateMachineQueryContainerMock = $this->createStateMachineQueryContainerMock();
72
73
        $itemLockQuery = $this->createStateMachineQueryMock();
74
        $itemLockQuery
75
            ->expects($this->once())
76
            ->method('delete');
77
78
        $stateMachineQueryContainerMock->expects($this->once())
0 ignored issues
show
The method expects() does not exist on Spryker\Zed\StateMachine...QueryContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        $stateMachineQueryContainerMock->/** @scrutinizer ignore-call */ 
79
                                         expects($this->once())

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...
79
            ->method('queryLockItemsByIdentifier')
80
            ->willReturn($itemLockQuery);
81
82
        $itemLock = $this->createItemLock(null, $stateMachineQueryContainerMock);
83
84
        $itemLock->release(1);
85
    }
86
87
    /**
88
     * @param \Orm\Zed\StateMachine\Persistence\SpyStateMachineLock|null $stateMachineLockEntityMock
89
     * @param \Spryker\Zed\StateMachine\Persistence\StateMachineQueryContainerInterface|null $stateMachineQueryContainerMock
90
     *
91
     * @return \Spryker\Zed\StateMachine\Business\Lock\ItemLockInterface
92
     */
93
    protected function createItemLock(
94
        ?SpyStateMachineLock $stateMachineLockEntityMock = null,
95
        ?StateMachineQueryContainerInterface $stateMachineQueryContainerMock = null
96
    ): ItemLockInterface {
97
        if ($stateMachineQueryContainerMock === null) {
98
            $stateMachineQueryContainerMock = $this->createStateMachineQueryContainerMock();
99
        }
100
101
        $stateMachineConfigMock = $this->createStateMachineConfigMock();
102
103
        $itemLockPartialMock = $this->getMockBuilder(ItemLock::class)
104
            ->setMethods(['createStateMachineLockEntity'])
105
            ->setConstructorArgs([$stateMachineQueryContainerMock, $stateMachineConfigMock])
106
            ->getMock();
107
108
        $itemLockPartialMock->method('createStateMachineLockEntity')->willReturn($stateMachineLockEntityMock);
109
110
        return $itemLockPartialMock;
111
    }
112
113
    /**
114
     * @return \PHPUnit\Framework\MockObject\MockObject|\Orm\Zed\StateMachine\Persistence\SpyStateMachineLock
115
     */
116
    protected function createStateMachineItemLockEntityMock(): SpyStateMachineLock
117
    {
118
        $stateMachineLockEntityMock = $this->getMockBuilder(SpyStateMachineLock::class)->getMock();
119
120
        return $stateMachineLockEntityMock;
121
    }
122
123
    /**
124
     * @return \PHPUnit\Framework\MockObject\MockObject|\Orm\Zed\StateMachine\Persistence\SpyStateMachineLockQuery
125
     */
126
    protected function createStateMachineQueryMock(): SpyStateMachineLockQuery
127
    {
128
        return $this->getMockBuilder(SpyStateMachineLockQuery::class)->getMock();
129
    }
130
}
131