Add in software files and templates

This commit is contained in:
2023-06-21 14:19:40 +01:00
parent f42fdb947c
commit 5228fc5e9f
143 changed files with 23175 additions and 2 deletions

View File

@@ -0,0 +1,121 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Settings\SettingInteger;
use Liuch\DmarcSrg\Settings\SettingString;
use Liuch\DmarcSrg\Settings\SettingStringSelect;
use Liuch\DmarcSrg\Settings\SettingsList;
use Liuch\DmarcSrg\Database\DatabaseController;
use Liuch\DmarcSrg\Exception\DatabaseNotFoundException;
class SettingDefaultValueTest extends \PHPUnit\Framework\TestCase
{
public function testSettingDefaultValue(): void
{
foreach (SettingsList::$schema as $name => &$props) {
switch ($props['type']) {
case 'string':
$t2 = false;
$val = 0;
$ss = 'Liuch\DmarcSrg\Settings\SettingString';
break;
case 'integer':
$t2 = true;
$val = '';
$ss = 'Liuch\DmarcSrg\Settings\SettingInteger';
break;
case 'select':
$t2 = true;
$val = '0';
$ss = 'Liuch\DmarcSrg\Settings\SettingStringSelect';
break;
}
$cc = new $ss([ 'name' => $name, 'value' => $val ], true, $this->getDbMapperNever());
$this->assertSame($props['default'], $cc->value(), "Name: {$name}; Constructor Value");
if ($t2) {
$cc = new $ss($name, true, $this->getDbMapperOnce($name, $val));
$this->assertSame($props['default'], $cc->value(), "Name: {$name}; Database Value");
}
unset($ss);
}
unset($props);
}
public function testSettingNotFoundDefaultValue(): void
{
foreach (SettingsList::$schema as $name => &$props) {
switch ($props['type']) {
case 'string':
$cc = new SettingString($name, true, $this->getDbMapperNotFound($name));
break;
case 'integer':
$cc = new SettingInteger($name, true, $this->getDbMapperNotFound($name));
break;
case 'select':
$cc = new SettingStringSelect($name, true, $this->getDbMapperNotFound($name));
break;
}
$cc->value();
unset($cc);
}
unset($props);
}
private function getDbMapperNever(): object
{
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->getMock();
$db->expects($this->never())->method('getMapper');
return $db;
}
private function getDbMapperOnce($parameter, $value): object
{
$mapper = $this->getMockBuilder(StdClass::class)
->disableOriginalConstructor()
->setMethods([ 'value' ])
->getMock();
$mapper->expects($this->once())
->method('value')
->with($this->equalTo($parameter))
->willReturn($value);
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->getMock();
$db->expects($this->once())
->method('getMapper')
->willReturn($mapper);
return $db;
}
private function getDbMapperNotFound($parameter): object
{
$mapper = $this->getMockBuilder(StdClass::class)
->disableOriginalConstructor()
->setMethods([ 'value' ])
->getMock();
$mapper->expects($this->once())
->method('value')
->with($this->equalTo($parameter))
->willThrowException(new DatabaseNotFoundException());
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->getMock();
$db->expects($this->once())
->method('getMapper')
->willReturn($mapper);
return $db;
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Settings\SettingsList;
use Liuch\DmarcSrg\Settings\SettingInteger;
use Liuch\DmarcSrg\Exception\SoftException;
use Liuch\DmarcSrg\Database\DatabaseController;
class SettingIntegerTest extends \PHPUnit\Framework\TestCase
{
public function testCreatingWithCorrectValue(): void
{
$this->assertSame(
222,
(new SettingInteger([
'name' => 'status.emails-for-last-n-days',
'value' => 222
], false, $this->getDbMapperNever()))->value()
);
}
public function testCreatingWithIncorrectValue(): void
{
$this->expectException(SoftException::class);
$this->expectExceptionMessage('Wrong setting data');
(new SettingInteger([
'name' => 'status.emails-for-last-n-days',
'value' => 'someStringValue'
], false, $this->getDbMapperNever()))->value();
}
public function testCreatingWithIncorrectValueWithIgnoring(): void
{
$this->assertSame(
SettingsList::$schema['status.emails-for-last-n-days']['default'],
(new SettingInteger([
'name' => 'status.emails-for-last-n-days',
'value' => 'incorrectIntegerValue'
], true, $this->getDbMapperNever()))->value()
);
}
public function testGettingValueByName(): void
{
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->getMock();
$db->expects($this->once())
->method('getMapper')
->with($this->equalTo('setting'))
->willReturn($this->getDbMapperOnce('value', 'status.emails-for-last-n-days', 333));
$this->assertSame(
333,
(new SettingInteger('status.emails-for-last-n-days', false, $db))->value()
);
}
private function getDbMapperNever(): object
{
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->getMock();
$db->expects($this->never())->method('getMapper');
return $db;
}
private function getDbMapperOnce(string $method, $parameter, $value): object
{
$mapper = $this->getMockBuilder(StdClass::class)
->disableOriginalConstructor()
->setMethods([ $method ])
->getMock();
$mapper->expects($this->once())
->method($method)
->with($this->equalTo($parameter))
->willReturn($value);
return $mapper;
}
}

View File

@@ -0,0 +1,103 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Settings\SettingsList;
use Liuch\DmarcSrg\Settings\SettingStringSelect;
use Liuch\DmarcSrg\Exception\SoftException;
use Liuch\DmarcSrg\Database\DatabaseController;
class SettingStringSelectTest extends \PHPUnit\Framework\TestCase
{
public function testCreatingWithCorrectValue(): void
{
$this->assertSame(
'auto',
(new SettingStringSelect([
'name' => 'ui.datetime.offset',
'value' => 'auto'
], false, $this->getDbMapperNever()))->value()
);
}
public function testCreatingWithIncorrectValueType(): void
{
$this->expectException(SoftException::class);
$this->expectExceptionMessage('Wrong setting data');
(new SettingStringSelect([
'name' => 'ui.datetime.offset',
'value' => 333
], false, $this->getDbMapperNever()))->value();
}
public function testCreatingWithIncorrectValueRange(): void
{
$this->expectException(SoftException::class);
$this->expectExceptionMessage('Wrong setting data');
(new SettingStringSelect([
'name' => 'ui.datetime.offset',
'value' => 'IncorrectValue'
], false, $this->getDbMapperNever()))->value();
}
public function testCreatingWithIncorrectValueTypeWithIgnoring(): void
{
$this->assertSame(
SettingsList::$schema['ui.datetime.offset']['default'],
(new SettingStringSelect([
'name' => 'ui.datetime.offset',
'value' => 333
], true, $this->getDbMapperNever()))->value()
);
}
public function testCreatingWithIncorrectValueRangeWithIgnoring(): void
{
$this->assertSame(
SettingsList::$schema['ui.datetime.offset']['default'],
(new SettingStringSelect([
'name' => 'ui.datetime.offset',
'value' => 'incorrectValue'
], true, $this->getDbMapperNever()))->value()
);
}
public function testGettingValueByName(): void
{
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->getMock();
$db->expects($this->once())
->method('getMapper')
->with($this->equalTo('setting'))
->willReturn($this->getDbMapperOnce('value', 'ui.datetime.offset', 'utc'));
$this->assertSame(
'utc',
(new SettingStringSelect('ui.datetime.offset', false, $db))->value()
);
}
private function getDbMapperNever(): object
{
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->getMock();
$db->expects($this->never())->method('getMapper');
return $db;
}
private function getDbMapperOnce(string $method, $parameter, $value): object
{
$mapper = $this->getMockBuilder(StdClass::class)
->disableOriginalConstructor()
->setMethods([ $method ])
->getMock();
$mapper->expects($this->once())
->method($method)
->with($this->equalTo($parameter))
->willReturn($value);
return $mapper;
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Settings\SettingsList;
use Liuch\DmarcSrg\Settings\SettingString;
use Liuch\DmarcSrg\Exception\SoftException;
use Liuch\DmarcSrg\Database\DatabaseController;
class SettingStringTest extends \PHPUnit\Framework\TestCase
{
public function testCreatingWithCorrectValue(): void
{
$this->assertSame(
'someValue',
(new SettingString([
'name' => 'version',
'value' => 'someValue'
], false, $this->getDbMapperNever()))->value()
);
}
public function testCreatingWithIncorrectValue(): void
{
$this->expectException(SoftException::class);
$this->expectExceptionMessage('Wrong setting data');
(new SettingString([
'name' => 'version',
'value' => 111
], false, $this->getDbMapperNever()))->value();
}
public function testCreatingWithIncorrectValueWithIgnoring(): void
{
$this->assertSame(
SettingsList::$schema['version']['default'],
(new SettingString([
'name' => 'version',
'value' => 111
], true, $this->getDbMapperNever()))->value()
);
}
public function testGettingValueByName(): void
{
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->getMock();
$db->expects($this->once())
->method('getMapper')
->with($this->equalTo('setting'))
->willReturn($this->getDbMapperOnce('value', 'version', 'stringValue'));
$this->assertSame('stringValue', (new SettingString('version', false, $db))->value());
}
private function getDbMapperNever(): object
{
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->getMock();
$db->expects($this->never())->method('getMapper');
return $db;
}
private function getDbMapperOnce(string $method, $parameter, $value): object
{
$mapper = $this->getMockBuilder(StdClass::class)
->disableOriginalConstructor()
->setMethods([ $method ])
->getMock();
$mapper->expects($this->once())
->method($method)
->with($this->equalTo($parameter))
->willReturn($value);
return $mapper;
}
}

View File

@@ -0,0 +1,96 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Settings\SettingsList;
use Liuch\DmarcSrg\Settings\SettingString;
use Liuch\DmarcSrg\Settings\SettingInteger;
use Liuch\DmarcSrg\Exception\SoftException;
use Liuch\DmarcSrg\Database\DatabaseController;
class SettingTest extends \PHPUnit\Framework\TestCase
{
public function testCreatingWithCorrectName(): void
{
$this->assertInstanceOf(SettingString::class, new SettingString('version', false, $this->getDbMapperNever()));
}
public function testCreatingWithIncorrectName(): void
{
$this->expectException(SoftException::class);
$this->expectExceptionMessage('Unknown setting name: some-setting');
new SettingString('some-setting', false, $this->getDbMapperNever());
}
public function testCreatingWithIncorrectDataType(): void
{
$this->expectException(SoftException::class);
$this->expectExceptionMessage('Wrong setting data');
new SettingString(1, false, $this->getDbMapperNever());
}
public function testCreatingWithIncorrectNameType(): void
{
$this->expectException(SoftException::class);
$this->expectExceptionMessage('Wrong setting data');
new SettingString([ 'name' => 1], false, $this->getDbMapperNever());
}
public function testSetValue(): void
{
$ss = new SettingString('version', false, $this->getDbMapperNever());
$ss->setValue('someString');
$this->assertSame('someString', $ss->value());
}
public function testToArray(): void
{
$ss = new SettingString([ 'name' => 'version', 'value' => 'someString' ], false, $this->getDbMapperNever());
$this->assertEquals([
'type' => 'string',
'name' => 'version',
'value' => 'someString',
'default' => SettingsList::$schema['version']['default']
], $ss->toArray());
}
public function testSave(): void
{
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->getMock();
$db->expects($this->once())
->method('getMapper')
->with($this->equalTo('setting'))
->willReturn($this->getDbMapperOnce('save', 'status.emails-for-last-n-days', '231'));
$ss = new SettingInteger(
[ 'name' => 'status.emails-for-last-n-days', 'value' => 231 ],
false,
$db
);
$ss->save();
}
private function getDbMapperNever(): object
{
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->getMock();
$db->expects($this->never())->method('getMapper');
return $db;
}
private function getDbMapperOnce(string $method, string $param1, string $param2): object
{
$mapper = $this->getMockBuilder(StdClass::class)
->disableOriginalConstructor()
->setMethods([ $method ])
->getMock();
$mapper->expects($this->once())
->method($method)
->with($this->equalTo($param1), $this->equalTo($param2));
return $mapper;
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Settings\SettingsList;
use Liuch\DmarcSrg\Exception\SoftException;
use Liuch\DmarcSrg\Database\DatabaseController;
class SettingsListTest extends \PHPUnit\Framework\TestCase
{
public function testSettingList(): void
{
$real_list = $this->realSettingsList(SettingsList::ORDER_ASCENT);
$mapp_list = (new SettingsList($this->getDbMapperListOnce()))->setOrder(SettingsList::ORDER_ASCENT)->getList();
$this->assertIsArray($mapp_list);
$this->assertFalse($mapp_list['more']);
$this->assertCount(count($real_list), $mapp_list['list']);
$this->assertSame($real_list[0], $mapp_list['list'][0]->name());
$cnt = count($real_list);
$this->assertSame($real_list[$cnt - 1], $mapp_list['list'][$cnt - 1]->name());
$real_list = $this->realSettingsList(SettingsList::ORDER_DESCENT);
$mapp_list = (new SettingsList($this->getDbMapperListOnce()))->setOrder(SettingsList::ORDER_DESCENT)->getList();
$this->assertSame($real_list[0], $mapp_list['list'][0]->name());
$cnt = count($real_list);
$this->assertSame($real_list[$cnt - 1], $mapp_list['list'][$cnt - 1]->name());
}
public function testCheckingCorrectSettingName(): void
{
$this->assertNull(SettingsList::checkName('version'));
}
public function testCheckingWrongSettingName(): void
{
$this->expectException(SoftException::class);
$this->expectExceptionMessage('Unknown setting name: wrongName');
$this->assertNull(SettingsList::checkName('wrongName'));
}
public function testGettingUnknownSettingByName(): void
{
$this->expectException(SoftException::class);
$this->expectExceptionMessage('Unknown setting name: someUnknownSetting');
SettingsList::getSettingByName('someUnknownSetting');
}
public function testGettingInternalSetting(): void
{
$this->expectException(SoftException::class);
$this->expectExceptionMessage('Attempt to access an internal variable');
SettingsList::getSettingByName('version');
}
private function getDbMapperListOnce(): object
{
$mapper = $this->getMockBuilder(StdClass::class)
->disableOriginalConstructor()
->setMethods([ 'list' ])
->getMock();
$mapper->expects($this->once())
->method('list')
->willReturn([]);
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->getMock();
$db->expects($this->once())
->method('getMapper')
->willReturn($mapper);
return $db;
}
private function realSettingsList(int $order): array
{
$list = [];
foreach (SettingsList::$schema as $name => &$props) {
if (isset($props['public']) && $props['public'] === true) {
$list[] = $name;
}
}
unset($props);
if ($order === SettingsList::ORDER_ASCENT) {
usort($list, static function ($a, $b) {
return $a <=> $b;
});
} elseif ($order === SettingsList::ORDER_DESCENT) {
usort($list, static function ($a, $b) {
return $b <=> $a;
});
}
return $list;
}
}