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