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

Completed
Push — main ( 0c7305...af1a75 )
by Dan
25s queued 19s
created

test_getMinTimeUntilFreeTurns_all_links_clicked()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 13
rs 9.9666
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\VoteLink;
7
use Smr\VoteSite;
8
use SmrTest\BaseIntegrationSpec;
9
10
/**
11
 * @covers Smr\VoteLink
12
 * @covers Smr\VoteSite
13
 */
14
class VoteSiteIntegrationTest extends BaseIntegrationSpec {
15
16
	protected function tablesToTruncate(): array {
17
		return ['vote_links'];
18
	}
19
20
	protected function tearDown(): void {
21
		VoteLink::clearCache();
22
	}
23
24
	public function test_getTimeUntilFreeTurns_invalid(): void {
25
		// Get a vote site that is not configured to award free turns
26
		$link = new VoteLink(VoteSite::PBBG, 1, 1);
27
28
		// Make sure it raises an exception if we call getTimeUntilFreeTurns
29
		$this->expectException(Exception::class);
30
		$this->expectExceptionMessage('This vote site cannot award free turns!');
31
		$link->getTimeUntilFreeTurns();
32
	}
33
34
	public function test_vote_callback_happy_path(): void {
35
		// Get a vote site that is configured to award free turns
36
		$link = new VoteLink(VoteSite::TWG, 1, 2);
37
38
		// Test that the site is eligible to award free turns by default
39
		self::assertTrue($link->freeTurnsReady());
40
41
		// Simulate clicking a vote link
42
		$link->setClicked();
43
44
		// Simulate awarding free turns
45
		self::assertTrue($link->setFreeTurnsAwarded());
46
47
		// Test that we're not ready to award free turns anymore
48
		self::assertFalse($link->freeTurnsReady(true));
49
	}
50
51
	public function test_vote_callback_abuse_detection(): void {
52
		// Get a vote site that is configured to award free turns
53
		$link = new VoteLink(VoteSite::TWG, 1, 2);
54
55
		// Award free turns normally
56
		$link->setClicked();
57
		$link->setFreeTurnsAwarded();
58
59
		// Test that we do not award free turns again
60
		self::assertFalse($link->setFreeTurnsAwarded());
61
62
		// Test that circumventing the timeout throws an exception
63
		$this->expectException(Exception::class);
64
		$this->expectExceptionMessage('Account ID 1 attempted vote link abuse');
65
		$link->setClicked();
66
	}
67
68
	public function test_vote_button_properties(): void {
69
		// Set some arbitrary test data
70
		$accountID = 7;
71
		$gameID = 42;
72
73
		// Set expected results when free turns are available
74
		$expected = [
75
			VoteSite::TWG->value => [
76
				'img' => 'twg_vote.png',
77
				'url' => 'http://topwebgames.com/in.aspx?ID=136&account=7&game=42&link=3&alwaysreward=1',
78
				'sn' => LOADER_URI . '?sn=gbuyay',
79
			],
80
			VoteSite::DOG->value => [
81
				'img' => 'dog_vote.png',
82
				'url' => 'http://www.directoryofgames.com/main.php?view=topgames&action=vote&v_tgame=2315&votedef=7,42,4',
83
				'sn' => LOADER_URI . '?sn=wnpclr',
84
			],
85
			VoteSite::PBBG->value => [
86
				'img' => 'pbbg.png',
87
				'url' => 'https://pbbg.com/games/space-merchant-realms',
88
				'sn' => false,
89
			],
90
		];
91
92
		srand(123); // set rand seed for session href generation
93
		foreach ($expected as $siteID => $data) {
94
			$link = new VoteLink(VoteSite::from($siteID), $accountID, $gameID);
95
			self::assertSame($data['img'], $link->getImg());
96
			self::assertSame($data['url'], $link->getUrl());
97
			self::assertSame($data['sn'], $link->getSN());
98
		}
99
100
		// Set expected results when free turns are NOT available
101
		$expected = [
102
			VoteSite::TWG->value => [
103
				'img' => 'twg.png',
104
				'url' => 'http://topwebgames.com/in.aspx?ID=136',
105
				'sn' => false,
106
			],
107
			VoteSite::DOG->value => [
108
				'img' => 'dog.png',
109
				'url' => 'http://www.directoryofgames.com/main.php?view=topgames&action=vote&v_tgame=2315',
110
				'sn' => false,
111
			],
112
			VoteSite::PBBG->value => [
113
				'img' => 'pbbg.png',
114
				'url' => 'https://pbbg.com/games/space-merchant-realms',
115
				'sn' => false,
116
			],
117
		];
118
119
		foreach ($expected as $siteID => $data) {
120
			// game ID 0 suppresses free turn links
121
			$link = new VoteLink(VoteSite::from($siteID), $accountID, 0);
122
			self::assertSame($data['img'], $link->getImg());
123
			self::assertSame($data['url'], $link->getUrl());
124
			self::assertSame($data['sn'], $link->getSN());
125
		}
126
	}
127
128
	public function test_getMinTimeUntilFreeTurns_default(): void {
129
		// Test that by default that the min time is negative
130
		$accountID = 9;
131
		$gameID = 17;
132
		self::assertTrue(VoteLink::getMinTimeUntilFreeTurns($accountID, $gameID) < 0);
133
	}
134
135
	public function test_getMinTimeUntilFreeTurns_some_links_clicked(): void {
136
		// Test that min time is still negative if we claim turns on one site
137
		$accountID = 9;
138
		$gameID = 17;
139
		$link = new VoteLink(VoteSite::DOG, $accountID, $gameID);
140
		$link->setClicked();
141
		$link->setFreeTurnsAwarded();
142
		VoteLink::clearCache();
143
		self::assertTrue(VoteLink::getMinTimeUntilFreeTurns($accountID, $gameID) < 0);
144
	}
145
146
	public function test_getMinTimeUntilFreeTurns_all_links_clicked(): void {
147
		// Test that the min time is positive if we claim turns on all sites
148
		$accountID = 9;
149
		$gameID = 17;
150
		foreach (VoteSite::cases() as $site) {
151
			$link = new VoteLink($site, $accountID, $gameID);
152
			if ($link->freeTurnsReady()) {
153
				$link->setClicked();
154
				$link->setFreeTurnsAwarded();
155
			}
156
		}
157
		VoteLink::clearCache();
158
		self::assertSame(VoteLink::TIME_BETWEEN_VOTING, VoteLink::getMinTimeUntilFreeTurns($accountID, $gameID));
159
	}
160
161
}
162