Debian Codegraph @

tests/classes/Settings/SettingTest.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\Settings\SettingString;
8 use Liuch\DmarcSrg\Settings\SettingInteger;
9 use Liuch\DmarcSrg\Exception\SoftException;
10
11 class SettingTest extends \PHPUnit\Framework\TestCase
12 {
13 public function testCreatingWithCorrectName(): void
14 {
15 $this->assertInstanceOf(
16 SettingString::class,
17 new SettingString('version', false, $this->getCoreWithDatabaseNever())
18 );
19 }
20
21 public function testCreatingWithIncorrectName(): void
22 {
23 $this->expectException(SoftException::class);
24 $this->expectExceptionMessage('Unknown setting name: some-setting');
25 new SettingString('some-setting', false, $this->getCoreWithDatabaseNever());
26 }
27
28 public function testCreatingWithIncorrectDataType(): void
29 {
30 $this->expectException(SoftException::class);
31 $this->expectExceptionMessage('Wrong setting data');
32 new SettingString(1, false, $this->getCoreWithDatabaseNever());
33 }
34
35 public function testCreatingWithIncorrectNameType(): void
36 {
37 $this->expectException(SoftException::class);
38 $this->expectExceptionMessage('Wrong setting data');
39 new SettingString([ 'name' => 1], false, $this->getCoreWithDatabaseNever());
40 }
41
42 public function testSetValue(): void
43 {
44 $ss = new SettingString('version', false, $this->getCoreWithDatabaseNever());
45 $ss->setValue('someString');
46 $this->assertSame('someString', $ss->value());
47 }
48
49 public function testToArray(): void
50 {
51 $ss = new SettingString(
52 [ 'name' => 'version', 'value' => 'someString' ],
53 false,
54 $this->getCoreWithDatabaseNever()
55 );
56 $this->assertEquals([
57 'type' => 'string',
58 'name' => 'version',
59 'value' => 'someString',
60 'default' => SettingsList::$schema['version']['default']
61 ], $ss->toArray());
62 }
63
64 public function testSave(): void
65 {
66 $user = new AdminUser($this->getCore());
67 $ss = new SettingInteger(
68 [ 'name' => 'status.emails-for-last-n-days', 'value' => 231, 'user' => $user ],
69 false,
70 $this->getCoreWithDatabaseMapperOnce('save', 'status.emails-for-last-n-days', '231', 0)
71 );
72 $ss->save();
73 }
74
75 private function getCore(): object
76 {
77 return $this->getMockBuilder(Core::class)
78 ->disableOriginalConstructor()
79 ->onlyMethods([ 'database' ])
80 ->getMock();
81 }
82
83 private function getCoreWithDatabaseNever(): object
84 {
85 $core = $this->getCore();
86 $core->expects($this->never())->method('database');
87 return $core;
88 }
89
90 private function getCoreWithDatabaseMapperOnce(string $method, string $param1, string $param2, int $param3): object
91 {
92 $mapper = $this->getMockBuilder(Database\SettingMapperInterface::class)
93 ->onlyMethods([ 'value', 'list', 'save' ])
94 ->getMock();
95 $mapper->expects($this->once())
96 ->method($method)
97 ->with($this->equalTo($param1), $this->equalTo($param2), $this->equalTo($param3));
98 $db = $this->getMockBuilder(Database\DatabaseController::class)
99 ->disableOriginalConstructor()
100 ->onlyMethods([ 'getMapper' ])
101 ->getMock();
102 $db->expects($this->once())
103 ->method('getMapper')
104 ->willReturn($mapper);
105
106 $core = $this->getCore();
107 $core->expects($this->once())->method('database')->willReturn($db);
108 return $core;
109 }
110 }