1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Page; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Smr\Container\DiContainer; |
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 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
|
|
|
/** |
25
|
|
|
* Insert a mock Session into the DI container to return the input $var |
26
|
|
|
* when getCurrentVar is called on it. |
27
|
|
|
*/ |
28
|
|
|
private function setVar(array $var): void { |
29
|
|
|
$page = new Page($var); |
30
|
|
|
$session = $this->createMock(Session::class); |
31
|
|
|
$session |
32
|
|
|
->method('getCurrentVar') |
33
|
|
|
->willReturn($page); |
34
|
|
|
DiContainer::getContainer()->set(Session::class, $session); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
//------------------------------------------------------------------------ |
38
|
|
|
|
39
|
|
|
public function test_create(): void { |
40
|
|
|
// Test create with $extra as array |
41
|
|
|
$page = Page::create('file', 'body', ['extra' => 'data']); |
42
|
|
|
// Check that the expected keys of the ArrayObject are set |
43
|
|
|
$expected = ['extra' => 'data', 'url' => 'file', 'body' => 'body']; |
44
|
|
|
self::assertSame($expected, $page->getArrayCopy()); |
45
|
|
|
|
46
|
|
|
// Test create with $extra as a Page object |
47
|
|
|
$page2 = Page::create('file2', extra: $page); |
48
|
|
|
// Check that the expected keys of the ArrayObject are set |
49
|
|
|
$expected2 = ['extra' => 'data', 'url' => 'file2', 'body' => '']; |
50
|
|
|
self::assertSame($expected2, $page2->getArrayCopy()); |
51
|
|
|
|
52
|
|
|
// Make sure they are not references to the same underlying object |
53
|
|
|
self::assertNotSame($page, $page2); |
54
|
|
|
// Make sure passing $page to create didn't modify the original |
55
|
|
|
self::assertSame($expected, $page->getArrayCopy()); |
56
|
|
|
|
57
|
|
|
// Test create when setting $remainingPageLoads |
58
|
|
|
$page3 = Page::create('file', remainingPageLoads: 2); |
59
|
|
|
self::assertSame(2, $page3['RemainingPageLoads']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function test_copy(): void { |
63
|
|
|
// Create an arbitrary Page |
64
|
|
|
$page = Page::create('file'); |
65
|
|
|
// The copy should be equal, but not the same |
66
|
|
|
$copy = Page::copy($page); |
67
|
|
|
self::assertNotSame($page, $copy); |
68
|
|
|
self::assertEquals($page, $copy); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function test_href(): void { |
72
|
|
|
// Create an arbitrary Page |
73
|
|
|
$page = Page::create('file'); |
74
|
|
|
|
75
|
|
|
// Pre-initialize the Smr\Session, since it uses 'rand', and we don't |
76
|
|
|
// want it to interfere with our rand seed when we call `href`, which |
77
|
|
|
// internally requires an Smr\Session. |
78
|
|
|
Session::getInstance(); |
79
|
|
|
|
80
|
|
|
// The Page should not be modified when href() is called |
81
|
|
|
$expected = $page->getArrayCopy(); |
82
|
|
|
srand(0); // for a deterministic SN |
83
|
|
|
$href = $page->href(); |
84
|
|
|
self::assertSame(LOADER_URI . '?sn=qpbqzr', $href); |
85
|
|
|
self::assertSame($expected, $page->getArrayCopy()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function test_addVar(): void { |
89
|
|
|
$page = Page::create('file'); |
90
|
|
|
|
91
|
|
|
// Mock the current global $var |
92
|
|
|
$this->setVar(['index1' => 'value1', 'index2' => 'value2']); |
93
|
|
|
|
94
|
|
|
// Using the default $dest in addVar should reuse $source |
95
|
|
|
$page->addVar('index1'); |
96
|
|
|
self::assertSame('value1', $page['index1']); |
97
|
|
|
|
98
|
|
|
// Specifying $dest should change the index in $page |
99
|
|
|
$page->addVar('index2', 'index3'); |
100
|
|
|
self::assertSame('value2', $page['index3']); |
101
|
|
|
self::assertFalse(isset($page['index2'])); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function test_addVar_missing_source_raises(): void { |
105
|
|
|
// Create an arbitrary Page |
106
|
|
|
$page = Page::create('file'); |
107
|
|
|
|
108
|
|
|
// Mock an empty global $var |
109
|
|
|
$this->setVar([]); |
110
|
|
|
|
111
|
|
|
$this->expectException(Exception::class); |
112
|
|
|
$this->expectExceptionMessage('Could not find "does_not_exist" in var!'); |
113
|
|
|
$page->addVar('does_not_exist'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function test_skipRedirect(): void { |
117
|
|
|
// Create an arbitrary Page with skipRedirect turned on |
118
|
|
|
$page = Page::create('file', skipRedirect: true); |
119
|
|
|
// Then skipRedirect should be on |
120
|
|
|
self::assertTrue($page->skipRedirect()); |
121
|
|
|
|
122
|
|
|
// Create a new Page inheriting from the Page with skipRedirect |
123
|
|
|
$page2 = Page::create('file', extra: $page); |
124
|
|
|
// Then skipRedirect should NOT be inherited (false by default) |
125
|
|
|
self::assertFalse($page2->skipRedirect()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|