Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Failed Conditions
Push — live ( 5fc72e...903a76 )
by Dan
07:31 queued 02:29
created

SectorLockTest::tablesToTruncate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace SmrTest\lib\DefaultGame;
4
5
use Exception;
6
use Smr\Container\DiContainer;
7
use Smr\Exceptions\UserError;
8
use Smr\SectorLock;
9
use SmrTest\BaseIntegrationSpec;
10
11
/**
12
 * @covers Smr\SectorLock
13
 */
14
class SectorLockTest extends BaseIntegrationSpec {
15
16
	protected function tablesToTruncate(): array {
17
		return ['locks_queue'];
18
	}
19
20
	protected function tearDown(): void {
21
		// Reset the DI container to avoid contaminating other tests
22
		DiContainer::initialize(false);
23
	}
24
25
	public function test_getInstance_always_returns_same_instance(): void {
26
		// Given a SectorLock object
27
		$original = SectorLock::getInstance();
28
		// When calling getInstance again
29
		$second = SectorLock::getInstance();
30
		self::assertSame($original, $second);
31
	}
32
33
	/**
34
	 * Test that the `resetInstance` function works properly when NPC_SCRIPT is set.
35
	 */
36
	public function test_resetInstance_cli(): void {
37
		// Set the NPC_SCRIPT variable as if this were a CLI program
38
		DiContainer::getContainer()->set('NPC_SCRIPT', true);
39
40
		// Given a SectorLock object
41
		$original = SectorLock::getInstance();
42
		// When calling resetInstance
43
		SectorLock::resetInstance();
44
		// Expect a new instance when calling getInstance again
45
		$second = SectorLock::getInstance();
46
		self::assertNotSame($original, $second);
47
	}
48
49
	/**
50
	 * resetInstance should throw if NPC_SCRIPT is false (its default value).
51
	 */
52
	public function test_resetInstance(): void {
53
		$this->expectException(Exception::class);
54
		$this->expectExceptionMessage('Only call this function from CLI programs');
55
		SectorLock::resetInstance();
56
	}
57
58
	public function test_acquire_same_lock_twice(): void {
59
		$lock = SectorLock::getInstance();
60
		// Given that we acquire a lock in sector 1
61
		self::assertTrue($lock->acquire(1, 1, 1));
62
		// We can call acquire again for sector 1 without throwing
63
		self::assertFalse($lock->acquire(1, 1, 1));
64
	}
65
66
	public function test_acquire_two_locks_in_the_same_request_throws(): void {
67
		$lock = SectorLock::getInstance();
68
		// Given that we acquire a lock in sector 1
69
		$lock->acquire(1, 1, 1);
70
		// We throw if we acquire a lock in sector 2 without first releasing
71
		$this->expectException(Exception::class);
72
		$this->expectExceptionMessage('This instance has an active lock in a different sector!');
73
		$lock->acquire(1, 1, 2);
74
	}
75
76
	public function test_acquire_two_locks_in_different_requests_throws(): void {
77
		// Whereas two different locks in the same request likely indicates
78
		// a coding error, two different locks in different request likely
79
		// indicates malicious use (or perhaps spam clicking?).
80
		$lock1 = SectorLock::getInstance();
81
		// Given that we acquire a lock
82
		$lock1->acquire(1, 1, 1);
83
		// And we use a new SectorLock instance to simulate a separate request
84
		$lock2 = DiContainer::make(SectorLock::class);
85
		// Then if that instance tries to acquire a lock (even for the same
86
		// sector) before the other instance releases, we throw a UserError.
87
		$this->expectException(UserError::class);
88
		$this->expectExceptionMessage('Multiple actions cannot be performed at the same time!');
89
		try {
90
			$lock2->acquire(1, 1, 1);
91
		} catch (UserError $e) {
92
			// Incidentally check that we have set the failed bit correctly
93
			self::assertTrue($lock2->hasFailed());
94
			self::assertFalse($lock1->hasFailed());
95
			throw $e;
96
		}
97
	}
98
99
	public function test_acquire_after_release(): void {
100
		$lock = SectorLock::getInstance();
101
		// Given that we acquire a lock in sector 1
102
		$lock->acquire(1, 1, 1);
103
		// Then release
104
		$lock->release();
105
		// Expect that we can acquire a lock in sector 2 without throwing
106
		self::assertTrue($lock->acquire(1, 1, 2));
107
	}
108
109
	public function test_release_same_lock_twice(): void {
110
		$lock = SectorLock::getInstance();
111
		// Given that we acquire a lock in sector 1
112
		$lock->acquire(1, 1, 1);
113
		// Expect that we can call release
114
		self::assertTrue($lock->release());
115
		// Expect that we can call release again without throwing
116
		self::assertFalse($lock->release());
117
	}
118
119
	public function test_isActive(): void {
120
		$lock = SectorLock::getInstance();
121
		// Returns false on a fresh instance
122
		self::assertFalse($lock->isActive());
123
		// Returns true after acquire
124
		$lock->acquire(1, 1, 1);
125
		self::assertTrue($lock->isActive());
126
		// Returns false after releasing
127
		$lock->release();
128
		self::assertFalse($lock->isActive());
129
	}
130
131
}
132