1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
4
|
|
|
|
5
|
|
|
use AbstractSmrPlayer; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Smr\Container\DiContainer; |
8
|
|
|
use Smr\Page\Page; |
9
|
|
|
use Smr\Session; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This is an integration test, but does not need to extend BaseIntegrationTest |
13
|
|
|
* since we are not (or should not be!) writing any data. |
14
|
|
|
* |
15
|
|
|
* @covers Smr\Page\Page |
16
|
|
|
*/ |
17
|
|
|
class PageIntegrationTest extends TestCase { |
18
|
|
|
|
19
|
|
|
protected function setUp(): void { |
20
|
|
|
// Reset the DI container for each test to ensure independence. |
21
|
|
|
DiContainer::initialize(false); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function test_href(): void { |
25
|
|
|
// Create an arbitrary Page |
26
|
|
|
$page = new Page(); |
27
|
|
|
|
28
|
|
|
// Pre-initialize the Smr\Session, since it uses 'rand', and we don't |
29
|
|
|
// want it to interfere with our rand seed when we call `href`, which |
30
|
|
|
// internally requires an Smr\Session. |
31
|
|
|
Session::getInstance(); |
32
|
|
|
|
33
|
|
|
// The Page should not be modified when href() is called |
34
|
|
|
$expected = clone $page; |
35
|
|
|
srand(0); // for a deterministic SN |
36
|
|
|
$href = $page->href(); |
37
|
|
|
self::assertSame(LOADER_URI . '?sn=qpbqzr', $href); |
38
|
|
|
self::assertEquals($expected, $page); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Tests the showUnderAttack method in a variety of scenarios. We test |
43
|
|
|
* consecutive page loads to ensure that a positive result persists |
44
|
|
|
* across page loads (both ajax and non-ajax). |
45
|
|
|
* |
46
|
|
|
* @testWith [true, true, null, false, true, true] |
47
|
|
|
* [true, false, null, true, true, true] |
48
|
|
|
* [true, true, null, true, true, true] |
49
|
|
|
* [false, false, true, true, false, true] |
50
|
|
|
* [false, true, true, true, false, true] |
51
|
|
|
*/ |
52
|
|
|
public function test_showUnderAttack(bool $underAttack1, bool $ajax1, ?bool $underAttack2, bool $ajax2, bool $expected1, bool $expected2): void { |
53
|
|
|
$getPlayer = function(bool $underAttack, bool $ajax): AbstractSmrPlayer { |
54
|
|
|
$mockPlayer = $this->createMock(AbstractSmrPlayer::class); |
55
|
|
|
$mockPlayer |
56
|
|
|
->method('isUnderAttack') |
57
|
|
|
->willReturn($underAttack); |
58
|
|
|
$mockPlayer |
59
|
|
|
->expects(self::exactly($ajax ? 0 : 1)) |
60
|
|
|
->method('setUnderAttack') |
61
|
|
|
->with(false); |
62
|
|
|
return $mockPlayer; |
63
|
|
|
}; |
64
|
|
|
|
65
|
|
|
// Simulated player state for the first page load |
66
|
|
|
$mockPlayerPage1 = $getPlayer($underAttack1, $ajax1); |
67
|
|
|
|
68
|
|
|
// Simulated player state for the second page load |
69
|
|
|
if ($underAttack2 === null) { |
70
|
|
|
// This condition implies that there was no external factor changing the |
71
|
|
|
// player state between page loads, so the expected return value of its |
72
|
|
|
// isUnderAttack method depends on if setUnderAttack was called before. |
73
|
|
|
$underAttack2 = $ajax1 ? $underAttack1 : false; |
74
|
|
|
} |
75
|
|
|
$mockPlayerPage2 = $getPlayer($underAttack2, $ajax2); |
76
|
|
|
|
77
|
|
|
$page1 = new Page(); |
78
|
|
|
$result1 = $page1->showUnderAttack($mockPlayerPage1, $ajax1); |
79
|
|
|
self::assertSame($expected1, $result1); |
80
|
|
|
|
81
|
|
|
// If the second page is ajax, it reuses the previous Page object |
82
|
|
|
$page2 = $ajax2 ? $page1 : new Page(); |
83
|
|
|
$result2 = $page2->showUnderAttack($mockPlayerPage2, $ajax2); |
84
|
|
|
self::assertSame($expected2, $result2); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|