Debian Codegraph @

tests/classes/Settings/SettingsListTest.php

raw symbols SCIP
1 <?php
2
3 namespace Liuch\DmarcSrg;
4
5 use Liuch\DmarcSrg\Users\AdminUser;
6 use Liuch\DmarcSrg\Settings\SettingsList;
7 use Liuch\DmarcSrg\Exception\SoftException;
8
9 class SettingsListTest extends \PHPUnit\Framework\TestCase
10 {
11 public function testSettingList(): void
12 {
13 $core = $this->getCore();
14 $real_list = $this->realSettingsList(SettingsList::ORDER_ASCENT);
15 $mapp_list = (new SettingsList($this->getCoreWithDatabaseMapperListOnce(new AdminUser($core))))
16 ->setOrder(SettingsList::ORDER_ASCENT)->getList();
17 $this->assertIsArray($mapp_list);
18 $this->assertFalse($mapp_list['more']);
19 $this->assertCount(count($real_list), $mapp_list['list']);
20 $this->assertSame($real_list[0], $mapp_list['list'][0]->name());
21 $cnt = count($real_list);
22 $this->assertSame($real_list[$cnt - 1], $mapp_list['list'][$cnt - 1]->name());
23
24 $real_list = $this->realSettingsList(SettingsList::ORDER_DESCENT);
25 $mapp_list = (new SettingsList($this->getCoreWithDatabaseMapperListOnce(new AdminUser($core))))
26 ->setOrder(SettingsList::ORDER_DESCENT)->getList();
27 $this->assertSame($real_list[0], $mapp_list['list'][0]->name());
28 $cnt = count($real_list);
29 $this->assertSame($real_list[$cnt - 1], $mapp_list['list'][$cnt - 1]->name());
30 }
31
32 public function testCheckingCorrectSettingName(): void
33 {
34 $this->assertNull(SettingsList::checkName('version'));
35 }
36
37 public function testCheckingWrongSettingName(): void
38 {
39 $this->expectException(SoftException::class);
40 $this->expectExceptionMessage('Unknown setting name: wrongName');
41 $this->assertNull(SettingsList::checkName('wrongName'));
42 }
43
44 public function testGettingUnknownSettingByName(): void
45 {
46 $this->expectException(SoftException::class);
47 $this->expectExceptionMessage('Unknown setting name: someUnknownSetting');
48 SettingsList::getSettingByName('someUnknownSetting');
49 }
50
51 public function testGettingInternalSetting(): void
52 {
53 $this->expectException(SoftException::class);
54 $this->expectExceptionMessage('Attempt to access an internal variable');
55 SettingsList::getSettingByName('version');
56 }
57
58 private function getCore(): object
59 {
60 return $this->getMockBuilder(Core::class)
61 ->disableOriginalConstructor()
62 ->onlyMethods([ 'getCurrentUser', 'database' ])
63 ->getMock();
64 }
65
66 private function getCoreWithDatabaseMapperListOnce($user): object
67 {
68 $mapper = $this->getMockBuilder(Database\SettingMapperInterface::class)
69 ->onlyMethods([ 'value', 'list', 'save' ])
70 ->getMock();
71 $mapper->expects($this->once())
72 ->method('list')
73 ->willReturn([]);
74 $db = $this->getMockBuilder(Database\DatabaseController::class)
75 ->disableOriginalConstructor()
76 ->onlyMethods([ 'getMapper' ])
77 ->getMock();
78 $db->expects($this->once())
79 ->method('getMapper')
80 ->willReturn($mapper);
81
82 $core = $this->getCore();
83 $core->expects($this->once())->method('getCurrentUser')->willReturn($user);
84 $core->expects($this->once())->method('database')->willReturn($db);
85 return $core;
86 }
87
88 private function realSettingsList(int $order): array
89 {
90 $list = [];
91 foreach (SettingsList::$schema as $name => &$props) {
92 if (isset($props['public']) && $props['public'] === true) {
93 $list[] = $name;
94 }
95 }
96 unset($props);
97 if ($order === SettingsList::ORDER_ASCENT) {
98 usort($list, static function ($a, $b) {
99 return $a <=> $b;
100 });
101 } elseif ($order === SettingsList::ORDER_DESCENT) {
102 usort($list, static function ($a, $b) {
103 return $b <=> $a;
104 });
105 }
106 return $list;
107 }
108 }