Debian Codegraph @

tests/classes/Settings/SettingDefaultValueTest.php

raw symbols SCIP
1 <?php
2
3 namespace Liuch\DmarcSrg;
4
5 use Liuch\DmarcSrg\Users\AdminUser;
6 use Liuch\DmarcSrg\Settings\SettingInteger;
7 use Liuch\DmarcSrg\Settings\SettingString;
8 use Liuch\DmarcSrg\Settings\SettingStringSelect;
9 use Liuch\DmarcSrg\Settings\SettingsList;
10 use Liuch\DmarcSrg\Exception\DatabaseNotFoundException;
11
12 class SettingDefaultValueTest extends \PHPUnit\Framework\TestCase
13 {
14 public function testSettingDefaultValue(): void
15 {
16 foreach (SettingsList::$schema as $name => &$props) {
17 switch ($props['type']) {
18 case 'string':
19 $t2 = false;
20 $val = 0;
21 $ss = 'Liuch\DmarcSrg\Settings\SettingString';
22 break;
23 case 'integer':
24 $t2 = true;
25 $val = '';
26 $ss = 'Liuch\DmarcSrg\Settings\SettingInteger';
27 break;
28 case 'select':
29 $t2 = true;
30 $val = '0';
31 $ss = 'Liuch\DmarcSrg\Settings\SettingStringSelect';
32 break;
33 }
34
35 $user = new AdminUser($this->getCore());
36 $cc = new $ss([
37 'name' => $name,
38 'value' => $val
39 ], true, $this->getCoreWithDatabaseNever($user));
40 $this->assertSame($props['default'], strval($cc->value()), "Name: {$name}; Constructor Value");
41
42 if ($t2) {
43 $cc = new $ss($name, true, $this->getCoreWithDatabaseMapperOnce($name, $user, $val));
44 $this->assertSame($props['default'], strval($cc->value()), "Name: {$name}; Database Value");
45 }
46
47 unset($ss);
48 }
49 unset($props);
50 }
51
52 public function testSettingNotFoundDefaultValue(): void
53 {
54 foreach (SettingsList::$schema as $name => &$props) {
55 $user = new AdminUser($this->getCore());
56 switch ($props['type']) {
57 case 'string':
58 $cc = new SettingString($name, true, $this->getCoreWithDatabaseMapperNotFound($name, $user));
59 break;
60 case 'integer':
61 $cc = new SettingInteger($name, true, $this->getCoreWithDatabaseMapperNotFound($name, $user));
62 break;
63 case 'select':
64 $cc = new SettingStringSelect($name, true, $this->getCoreWithDatabaseMapperNotFound($name, $user));
65 break;
66 }
67 $cc->value();
68 unset($cc);
69 }
70 unset($props);
71 }
72
73 private function getCore(): object
74 {
75 return $this->getMockBuilder(Core::class)
76 ->disableOriginalConstructor()
77 ->onlyMethods([ 'getCurrentUser', 'database' ])
78 ->getMock();
79 }
80
81 private function getCoreWithDatabaseNever($user): object
82 {
83 $core = $this->getCore();
84 $core->expects($this->never())->method('getCurrentUser')->willReturn($user);
85 $core->expects($this->never())->method('database');
86 return $core;
87 }
88
89 private function getCoreWithDatabaseMapperOnce($key, $user, $value): object
90 {
91 $mapper = $this->getMockBuilder(Database\SettingMapperInterface::class)
92 ->onlyMethods([ 'value', 'list', 'save' ])
93 ->getMock();
94 $mapper->expects($this->once())
95 ->method('value')
96 ->with($key, $user->id())
97 ->willReturn($value);
98
99 $db = $this->getMockBuilder(Database\DatabaseController::class)
100 ->disableOriginalConstructor()
101 ->onlyMethods([ 'getMapper' ])
102 ->getMock();
103 $db->expects($this->once())
104 ->method('getMapper')
105 ->willReturn($mapper);
106
107 $core = $this->getCore();
108 $core->expects($this->once())->method('getCurrentUser')->willReturn($user);
109 $core->expects($this->once())->method('database')->willReturn($db);
110 return $core;
111 }
112
113 private function getCoreWithDatabaseMapperNotFound($parameter, $user): object
114 {
115 $mapper = $this->getMockBuilder(Database\SettingMapperInterface::class)
116 ->disableOriginalConstructor()
117 ->onlyMethods([ 'value', 'list', 'save' ])
118 ->getMock();
119 $mapper->expects($this->once())
120 ->method('value')
121 ->with($this->equalTo($parameter))
122 ->willThrowException(new DatabaseNotFoundException());
123
124 $db = $this->getMockBuilder(Database\DatabaseController::class)
125 ->disableOriginalConstructor()
126 ->onlyMethods([ 'getMapper' ])
127 ->getMock();
128 $db->expects($this->once())
129 ->method('getMapper')
130 ->willReturn($mapper);
131
132 $core = $this->getCore();
133 $core->expects($this->once())->method('getCurrentUser')->willReturn($user);
134 $core->expects($this->once())->method('database')->willReturn($db);
135 return $core;
136 }
137 }