Debian Codegraph @

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