1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
4
|
|
|
|
5
|
|
|
use Page; |
6
|
|
|
use Smr\Container\DiContainer; |
7
|
|
|
use Smr\Session; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This is an integration test, but does not need to extend BaseIntegrationTest |
11
|
|
|
* since we are not (or should not be!) writing any data. |
12
|
|
|
* @covers Page |
13
|
|
|
*/ |
14
|
|
|
class PageIntegrationTest extends \PHPUnit\Framework\TestCase { |
15
|
|
|
|
16
|
|
|
protected function setUp() : void { |
17
|
|
|
// Reset the DI container for each test to ensure independence. |
18
|
|
|
DiContainer::initializeContainer(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Insert a mock Session into the DI container to return the input $var |
23
|
|
|
* when getCurrentVar is called on it. |
24
|
|
|
*/ |
25
|
|
|
private function setVar(array $var) : void { |
26
|
|
|
$page = new Page($var); |
27
|
|
|
$session = $this->createMock(Session::class); |
28
|
|
|
$session |
29
|
|
|
->method('getCurrentVar') |
30
|
|
|
->willReturn($page); |
31
|
|
|
DiContainer::getContainer()->set(Session::class, $session); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
//------------------------------------------------------------------------ |
35
|
|
|
|
36
|
|
|
public function test_create() { |
37
|
|
|
// Test create with $extra as array |
38
|
|
|
$page = Page::create('file', 'body', ['extra' => 'data']); |
39
|
|
|
// Check that the expected keys of the ArrayObject are set |
40
|
|
|
$expected = ['extra' => 'data', 'url' => 'file', 'body' => 'body']; |
41
|
|
|
self::assertSame($expected, $page->getArrayCopy()); |
42
|
|
|
|
43
|
|
|
// Test create with $extra as a Page object |
44
|
|
|
$page2 = Page::create('file2', extra: $page); |
45
|
|
|
// Check that the expected keys of the ArrayObject are set |
46
|
|
|
$expected2 = ['extra' => 'data', 'url' => 'file2', 'body' => '']; |
47
|
|
|
self::assertSame($expected2, $page2->getArrayCopy()); |
48
|
|
|
|
49
|
|
|
// Make sure they are not references to the same underlying object |
50
|
|
|
self::assertNotSame($page, $page2); |
51
|
|
|
// Make sure passing $page to create didn't modify the original |
52
|
|
|
self::assertSame($expected, $page->getArrayCopy()); |
53
|
|
|
|
54
|
|
|
// Test create when setting $remainingPageLoads |
55
|
|
|
$page3 = Page::create('file', remainingPageLoads: 2); |
56
|
|
|
self::assertSame(2, $page3['RemainingPageLoads']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function test_copy() { |
60
|
|
|
// Create an arbitrary Page |
61
|
|
|
$page = Page::create('file'); |
62
|
|
|
// The copy should be equal, but not the same |
63
|
|
|
$copy = Page::copy($page); |
64
|
|
|
self::assertNotSame($page, $copy); |
65
|
|
|
self::assertEquals($page, $copy); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function test_href() { |
69
|
|
|
// Create an arbitrary Page |
70
|
|
|
$page = Page::create('file'); |
71
|
|
|
|
72
|
|
|
// Pre-initialize the Smr\Session, since it uses 'rand', and we don't |
73
|
|
|
// want it to interfere with our rand seed when we call `href`, which |
74
|
|
|
// internally requires an Smr\Session. |
75
|
|
|
Session::getInstance(); |
76
|
|
|
|
77
|
|
|
// The Page should not be modified when href() is called |
78
|
|
|
$expected = $page->getArrayCopy(); |
79
|
|
|
srand(0); // for a deterministic SN |
80
|
|
|
$_SERVER['REQUEST_URI'] = 'loader.php'; // prevent "Undefined array key" |
81
|
|
|
$href = $page->href(); |
82
|
|
|
self::assertSame('?sn=qpbqzr', $href); |
83
|
|
|
self::assertSame($expected, $page->getArrayCopy()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function test_addVar() { |
87
|
|
|
$page = Page::create('file'); |
88
|
|
|
|
89
|
|
|
// Mock the current global $var |
90
|
|
|
$this->setVar(['index1' => 'value1', 'index2' => 'value2']); |
91
|
|
|
|
92
|
|
|
// Using the default $dest in addVar should reuse $source |
93
|
|
|
$page->addVar('index1'); |
94
|
|
|
self::assertSame('value1', $page['index1']); |
95
|
|
|
|
96
|
|
|
// Specifying $dest should change the index in $page |
97
|
|
|
$page->addVar('index2', 'index3'); |
98
|
|
|
self::assertSame('value2', $page['index3']); |
99
|
|
|
self::assertFalse(isset($page['index2'])); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function test_addVar_missing_source_raises() { |
103
|
|
|
// Create an arbitrary Page |
104
|
|
|
$page = Page::create('file'); |
105
|
|
|
|
106
|
|
|
// Mock an empty global $var |
107
|
|
|
$this->setVar([]); |
108
|
|
|
|
109
|
|
|
$this->expectException(\Exception::class); |
110
|
|
|
$this->expectExceptionMessage('Could not find "does_not_exist" in var!'); |
111
|
|
|
$page->addVar('does_not_exist'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|