| 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\SettingStringSelect; |
| 8 | use Liuch\DmarcSrg\Exception\SoftException; |
| 9 | |
| 10 | class SettingStringSelectTest extends \PHPUnit\Framework\TestCase |
| 11 | { |
| 12 | public function testCreatingWithCorrectValue(): void |
| 13 | { |
| 14 | $this->assertSame( |
| 15 | 'auto', |
| 16 | (new SettingStringSelect([ |
| 17 | 'name' => 'ui.datetime.offset', |
| 18 | 'value' => 'auto' |
| 19 | ], false, $this->getCoreWithDatabaseNever()))->value() |
| 20 | ); |
| 21 | } |
| 22 | |
| 23 | public function testCreatingWithIncorrectValueType(): void |
| 24 | { |
| 25 | $this->expectException(SoftException::class); |
| 26 | $this->expectExceptionMessage('Wrong setting data'); |
| 27 | (new SettingStringSelect([ |
| 28 | 'name' => 'ui.datetime.offset', |
| 29 | 'value' => 333 |
| 30 | ], false, $this->getCoreWithDatabaseNever()))->value(); |
| 31 | } |
| 32 | |
| 33 | public function testCreatingWithIncorrectValueRange(): void |
| 34 | { |
| 35 | $this->expectException(SoftException::class); |
| 36 | $this->expectExceptionMessage('Wrong setting data'); |
| 37 | (new SettingStringSelect([ |
| 38 | 'name' => 'ui.datetime.offset', |
| 39 | 'value' => 'IncorrectValue' |
| 40 | ], false, $this->getCoreWithDatabaseNever()))->value(); |
| 41 | } |
| 42 | |
| 43 | public function testCreatingWithIncorrectValueTypeWithIgnoring(): void |
| 44 | { |
| 45 | $this->assertSame( |
| 46 | SettingsList::$schema['ui.datetime.offset']['default'], |
| 47 | (new SettingStringSelect([ |
| 48 | 'name' => 'ui.datetime.offset', |
| 49 | 'value' => 333 |
| 50 | ], true, $this->getCoreWithDatabaseNever()))->value() |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | public function testCreatingWithIncorrectValueRangeWithIgnoring(): void |
| 55 | { |
| 56 | $this->assertSame( |
| 57 | SettingsList::$schema['ui.datetime.offset']['default'], |
| 58 | (new SettingStringSelect([ |
| 59 | 'name' => 'ui.datetime.offset', |
| 60 | 'value' => 'incorrectValue' |
| 61 | ], true, $this->getCoreWithDatabaseNever()))->value() |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | public function testGettingValueByName(): void |
| 66 | { |
| 67 | $user = new AdminUser($this->getCore()); |
| 68 | $this->assertSame('utc', (new SettingStringSelect( |
| 69 | 'ui.datetime.offset', |
| 70 | false, |
| 71 | $this->getCoreWithDatabaseOnce('value', 'ui.datetime.offset', 'utc', $user) |
| 72 | ))->value()); |
| 73 | } |
| 74 | |
| 75 | private function getCore(): object |
| 76 | { |
| 77 | return $this->getMockBuilder(Core::class) |
| 78 | ->disableOriginalConstructor() |
| 79 | ->onlyMethods([ 'getCurrentUser', '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 getCoreWithDatabaseOnce(string $method, $parameter, $value, $user): 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($parameter)) |
| 98 | ->willReturn($value); |
| 99 | $db = $this->getMockBuilder(Database\DatabaseController::class) |
| 100 | ->disableOriginalConstructor() |
| 101 | ->onlyMethods([ 'getMapper' ]) |
| 102 | ->getMock(); |
| 103 | $db->expects($this->once()) |
| 104 | ->method('getMapper') |
| 105 | ->with($this->equalTo('setting')) |
| 106 | ->willReturn($mapper); |
| 107 | |
| 108 | $core = $this->getCore(); |
| 109 | $core->expects($this->once())->method('getCurrentUser')->willReturn($user); |
| 110 | $core->expects($this->once())->method('database')->willReturn($db); |
| 111 | return $core; |
| 112 | } |
| 113 | } |