Debian Codegraph @

tests/classes/Settings/SettingIntegerTest.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\SettingInteger;
8 use Liuch\DmarcSrg\Exception\SoftException;
9
10 class SettingIntegerTest extends \PHPUnit\Framework\TestCase
11 {
12 public function testCreatingWithCorrectValue(): void
13 {
14 $this->assertSame(
15 222,
16 (new SettingInteger([
17 'name' => 'status.emails-for-last-n-days',
18 'value' => 222
19 ], false, $this->getCoreWithDatabaseNever()))->value()
20 );
21 }
22
23 public function testCreatingWithIncorrectValue(): void
24 {
25 $this->expectException(SoftException::class);
26 $this->expectExceptionMessage('Wrong setting data');
27 (new SettingInteger([
28 'name' => 'status.emails-for-last-n-days',
29 'value' => 'someStringValue'
30 ], false, $this->getCoreWithDatabaseNever()))->value();
31 }
32
33 public function testCreatingWithIncorrectValueWithIgnoring(): void
34 {
35 $this->assertSame(
36 SettingsList::$schema['status.emails-for-last-n-days']['default'],
37 strval(
38 (new SettingInteger([
39 'name' => 'status.emails-for-last-n-days',
40 'value' => 'incorrectIntegerValue'
41 ], true, $this->getCoreWithDatabaseNever()))->value()
42 )
43 );
44 }
45
46 public function testGettingValueByName(): void
47 {
48 $user = new AdminUser($this->getCore());
49 $this->assertSame(333, (new SettingInteger(
50 'status.emails-for-last-n-days',
51 false,
52 $this->getCoreWithDatabaseOnce('value', 'status.emails-for-last-n-days', '333', $user)
53 ))->value());
54 }
55
56 private function getCore(): object
57 {
58 return $this->getMockBuilder(Core::class)
59 ->disableOriginalConstructor()
60 ->onlyMethods([ 'getCurrentUser', 'database' ])
61 ->getMock();
62 }
63
64 private function getCoreWithDatabaseNever(): object
65 {
66 $core = $this->getCore();
67 $core->expects($this->never())->method('database');
68 return $core;
69 }
70
71 private function getCoreWithDatabaseOnce(string $method, $parameter, $value, $user): object
72 {
73 $mapper = $this->getMockBuilder(Database\SettingMapperInterface::class)
74 ->onlyMethods([ 'value', 'list', 'save' ])
75 ->getMock();
76 $mapper->expects($this->once())
77 ->method($method)
78 ->with($this->equalTo($parameter))
79 ->willReturn($value);
80 $db = $this->getMockBuilder(Database\DatabaseController::class)
81 ->disableOriginalConstructor()
82 ->onlyMethods([ 'getMapper' ])
83 ->getMock();
84 $db->expects($this->once())
85 ->method('getMapper')
86 ->with($this->equalTo('setting'))
87 ->willReturn($mapper);
88
89 $core = $this->getCore();
90 $core->expects($this->once())->method('getCurrentUser')->willReturn($user);
91 $core->expects($this->once())->method('database')->willReturn($db);
92 return $core;
93 }
94 }