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,142 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Exception\AuthException;
class AuthTest extends \PHPUnit\Framework\TestCase
{
public function testIsEnabledWithNoPassword(): void
{
$core = $this->coreWithConfigValue('admin/password', null);
$this->assertFalse((new Auth($core))->isEnabled());
}
public function testIsEnabledWithPassword(): void
{
$core = $this->coreWithConfigValue('admin/password', 'some');
$this->assertTrue((new Auth($core))->isEnabled());
}
public function testCorrectAdminPassword(): void
{
$core = $this->coreWithConfigValue('admin/password', 'some');
$this->assertNull((new Auth($core))->checkAdminPassword('some'));
}
public function testWrongAdminPassword(): void
{
$this->expectException(AuthException::class);
$this->expectExceptionMessage('Incorrect password');
$core = $this->coreWithConfigValue('admin/password', 'some');
(new Auth($core))->checkAdminPassword('fake');
}
public function testEmptyAdminPassword(): void
{
$core = $this->getMockBuilder(Core::class)
->disableOriginalConstructor()
->setMethods([ 'config', 'userId' ])
->getMock();
$core->expects($this->once())
->method('config')
->with($this->equalTo('admin/password'))
->willReturn('some');
$core->expects($this->never())
->method('userId');
$this->expectException(AuthException::class);
$this->expectExceptionMessage('Incorrect password');
(new Auth($core))->checkAdminPassword('');
}
public function testCorrectLogin(): void
{
$core = $this->getMockBuilder(Core::class)
->disableOriginalConstructor()
->setMethods([ 'config', 'userId' ])
->getMock();
$core->expects($this->exactly(2))
->method('config')
->with($this->equalTo('admin/password'))
->willReturn('some');
$core->expects($this->once())
->method('userId')
->with($this->equalTo(0));
$result = (new Auth($core))->login('', 'some');
$this->assertIsArray($result);
$this->assertSame(0, $result['error_code']);
$this->assertSame('Authentication succeeded', $result['message']);
}
public function testLogout(): void
{
$core = $this->getMockBuilder(Core::class)
->disableOriginalConstructor()
->setMethods([ 'destroySession' ])
->getMock();
$core->expects($this->once())
->method('destroySession');
$result = (new Auth($core))->logout();
$this->assertArrayHasKey('error_code', $result);
$this->assertArrayHasKey('message', $result);
$this->assertEquals(0, $result['error_code']);
$this->assertEquals('Logged out successfully', $result['message']);
}
public function testIsAllowedWhenAuthDisabled(): void
{
$core = $this->getMockBuilder(Core::class)
->disableOriginalConstructor()
->setMethods([ 'config', 'userId' ])
->getMock();
$core->expects($this->once())
->method('config')
->with($this->equalTo('admin/password'))
->willReturn(null);
$core->expects($this->never())
->method('userId');
$this->assertNull((new Auth($core))->isAllowed());
}
public function testIsAllowedWithActiveSession(): void
{
$core = $this->getMockBuilder(Core::class)
->disableOriginalConstructor()
->setMethods([ 'config', 'userId' ])
->getMock();
$core->expects($this->once())
->method('config')
->with($this->equalTo('admin/password'))
->willReturn('some');
$core->expects($this->once())
->method('userId')
->willReturn(0);
$this->assertNull((new Auth($core))->isAllowed());
}
public function testIsAllowedWithoutSession(): void
{
$core = $this->getMockBuilder(Core::class)
->disableOriginalConstructor()
->setMethods([ 'config', 'userId' ])
->getMock();
$core->expects($this->once())
->method('config')
->with($this->equalTo('admin/password'))
->willReturn('some');
$core->expects($this->once())
->method('userId')
->willReturn(false);
$this->expectException(AuthException::class);
$this->expectExceptionCode(-2);
$this->expectExceptionMessage('Authentication needed');
(new Auth($core))->isAllowed();
}
private function coreWithConfigValue(string $key, $value)
{
$core = $this->getMockBuilder(Core::class)->disableOriginalConstructor()->setMethods([ 'config' ])->getMock();
$core->expects($this->once())->method('config')->with($this->equalTo($key))->willReturn($value);
return $core;
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Liuch\DmarcSrg;
class CommonTest extends \PHPUnit\Framework\TestCase
{
public function testAlignResultArray(): void
{
$this->assertCount(3, Common::$align_res);
$this->assertEquals('fail', Common::$align_res[0]);
$this->assertEquals('unknown', Common::$align_res[1]);
$this->assertEquals('pass', Common::$align_res[2]);
}
public function testDispositionArray(): void
{
$this->assertCount(3, Common::$disposition);
$this->assertEquals('reject', Common::$disposition[0]);
$this->assertEquals('quarantine', Common::$disposition[1]);
$this->assertEquals('none', Common::$disposition[2]);
}
}

View File

@@ -0,0 +1,93 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Exception\LogicException;
class ConfigTest extends \PHPUnit\Framework\TestCase
{
private $conf = null;
public function setUp(): void
{
$this->conf = new Config('tests/conf_test_file.php');
}
public function testEmptyName(): void
{
$this->expectException(LogicException::class);
$this->conf->get('');
}
public function testNestedEmptyName(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Incorrect ');
$this->conf->get('cleaner/');
}
public function testNotAllowedName(): void
{
$this->assertNull($this->conf->get('some'));
$this->assertEquals(1, $this->conf->get('some', 1));
}
public function testNonexistentParameter(): void
{
$this->assertNull($this->conf->get('unknown'));
$this->assertNull($this->conf->get('unknown', null));
$this->assertFalse($this->conf->get('unknown', false));
$this->assertSame(0, $this->conf->get('unknown', 0));
$this->assertSame('', $this->conf->get('unknown', ''));
$array = $this->conf->get('unknown', []);
$this->assertIsArray($array);
$this->assertEmpty($array);
}
public function testBoolParameter(): void
{
$this->assertSame(false, $this->conf->get('debug'));
}
public function testIntParameter(): void
{
$this->assertSame(0, $this->conf->get('database'));
}
public function testStringParameter(): void
{
$this->assertSame('', $this->conf->get('mailboxes'));
}
public function testNullParameter(): void
{
$this->assertNull($this->conf->get('directories'));
}
public function testArrayParameter(): void
{
$array = $this->conf->get('admin');
$this->assertIsArray($array);
$this->assertEmpty($array);
}
public function testArrayKeyIntParameter(): void
{
$this->assertSame(0, $this->conf->get('cleaner/key_int'));
}
public function testArrayKeyBoolParameter(): void
{
$this->assertFalse($this->conf->get('cleaner/key_bool'));
}
public function testArrayKeyStringParameter(): void
{
$this->assertSame('', $this->conf->get('cleaner/key_string'));
}
public function testArrayKeyNonexistentParameter(): void
{
$this->assertSame('default', $this->conf->get('cleaner/key_some', 'default'));
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace Liuch\DmarcSrg;
class CoreTest extends \PHPUnit\Framework\TestCase
{
private $core = null;
public function setUp(): void
{
$this->core = Core::instance();
}
public function testSelfInstance(): void
{
$this->assertSame($this->core, Core::instance());
}
public function testRequestMethod(): void
{
$_SERVER['REQUEST_METHOD'] = 'some_method';
$this->assertSame('some_method', $this->core->method());
}
public function testSendHtml(): void
{
ob_start();
$this->core->sendHtml();
$output = ob_get_contents();
ob_end_clean();
$this->assertIsString($output);
$this->assertStringEqualsFile('index.html', $output);
}
/**
* @runInSeparateProcess
*/
public function testSendJson(): void
{
$data = [ 'key1' => 'value1', [ 'key2' => 'value2' ] ];
ob_start();
$this->core->sendJson($data);
$output = ob_get_contents();
ob_end_clean();
$this->assertJsonStringEqualsJsonString(json_encode($data), $output);
}
public function testAuthInstance(): void
{
$this->assertSame($this->core->auth(), $this->core->auth());
}
public function testStatusInstance(): void
{
$this->assertSame($this->core->status(), $this->core->status());
}
public function testAdminIntance(): void
{
$this->assertSame($this->core->admin(), $this->core->admin());
}
public function testErrorHandlerInstance(): void
{
$this->assertSame($this->core->errorHandler(), $this->core->errorHandler());
}
public function testLoggerInstance(): void
{
$this->assertSame($this->core->logger(), $this->core->logger());
}
public function testConfigExistingParameters(): void
{
$config = $this->getMockBuilder(Config::class)
->disableOriginalConstructor()
->setMethods([ 'get' ])
->getMock();
$config->expects($this->once())
->method('get')
->with($this->equalTo('some/param'), $this->equalTo('default'))
->willReturn('some_value');
$core = new Core([ 'config' => $config ]);
$this->assertSame('some_value', $core->config('some/param', 'default'));
}
public function testConfigNonexistentParameters(): void
{
$core = new Core([ 'config' => [ 'Liuch\DmarcSrg\Config', [ 'tests/conf_test_file.php' ] ] ]);
$config = new Config('tests/conf_test_file.php');
$this->assertNull($core->config('some_unknown_parameter'));
$this->assertIsString($core->config('some_unknown_parameter', ''));
}
}

View File

@@ -0,0 +1,133 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Database\DatabaseController;
class DatabaseControllerTest extends \PHPUnit\Framework\TestCase
{
public function testGettingType(): void
{
$ctl = new DatabaseController($this->getCoreWithSettings([]));
$this->assertSame('', $ctl->type());
$ctl = new DatabaseController($this->getCoreWithSettings([ 'type' => 'dbType' ]));
$this->assertSame('dbType', $ctl->type());
}
public function testGettingName(): void
{
$ctl = new DatabaseController($this->getCoreWithSettings([]));
$this->assertSame('', $ctl->name());
$ctl = new DatabaseController($this->getCoreWithSettings([ 'name' => 'dbName' ]));
$this->assertSame('dbName', $ctl->name());
}
public function testGettingLocation(): void
{
$ctl = new DatabaseController($this->getCoreWithSettings([]));
$this->assertSame('', $ctl->location());
$ctl = new DatabaseController($this->getCoreWithSettings([ 'host' => 'dbLocation' ]));
$this->assertSame('dbLocation', $ctl->location());
}
public function testGettingState(): void
{
$callback = function () {
return [
'someParam' => 'someValue',
'correct' => true,
'version' => DatabaseController::REQUIRED_VERSION
];
};
$ctl = new DatabaseController(
$this->getCoreWithSettings([
'type' => 'dbType',
'name' => 'dbName',
'host' => 'dbLocation'
]),
$this->getConnector('state', $callback)
);
$res = $ctl->state();
$this->assertIsArray($res);
$this->assertTrue($res['correct']);
$this->assertFalse($res['needs_upgrade'] ?? false);
$this->assertSame('someValue', $res['someParam']);
$this->assertSame('dbType', $res['type']);
$this->assertSame('dbName', $res['name']);
$this->assertSame('dbLocation', $res['location']);
}
public function testGettingStateNeedsUpgrating(): void
{
$callback1 = function () {
return [ 'correct' => true ];
};
$callback2 = function () {
return [ 'correct' => false ];
};
$ctl = new DatabaseController($this->getCoreWithSettings([]), $this->getConnector('state', $callback1));
$res = $ctl->state();
$this->assertFalse($res['correct']);
$this->assertTrue($res['needs_upgrade']);
$this->assertArrayHasKey('message', $res);
$ctl = new DatabaseController($this->getCoreWithSettings([]), $this->getConnector('state', $callback2));
$res = $ctl->state();
$this->assertFalse($res['correct']);
$this->assertFalse($res['needs_upgrade'] ?? false);
}
public function testInitDb(): void
{
$callback = function () {
};
$ctl = new DatabaseController($this->getCoreWithSettings([]), $this->getConnector('initDb', $callback));
$res = $ctl->initDb();
$this->assertIsArray($res);
$this->assertSame(0, $res['error_code'] ?? 0);
$this->assertArrayHasKey('message', $res);
}
public function testCleanDb(): void
{
$callback = function () {
};
$ctl = new DatabaseController($this->getCoreWithSettings([]), $this->getConnector('cleanDb', $callback));
$res = $ctl->cleanDb();
$this->assertIsArray($res);
$this->assertSame(0, $res['error_code'] ?? 0);
$this->assertArrayHasKey('message', $res);
}
public function testGettingMapper(): void
{
$callback = function ($param) {
$this->assertSame('mapperId', $param);
return new \StdClass();
};
$ctl = new DatabaseController($this->getCoreWithSettings([]), $this->getConnector('getMapper', $callback));
$ctl->getMapper('mapperId');
}
private function getCoreWithSettings($data): object
{
$core = $this->createMock(Core::class);
$core->expects($this->once())
->method('config')
->with('database')
->willReturn($data);
return $core;
}
private function getConnector(string $method, $callback): object
{
$con = $this->getMockBuilder(\StdClass::class)
->setMethods([ $method ])
->getMock();
$con->expects($this->once())
->method($this->equalTo($method))
->willReturnCallback($callback);
return $con;
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Liuch\DmarcSrg;
class DateTimeTest extends \PHPUnit\Framework\TestCase
{
public function testSimpleJsonSerialize(): void
{
$this->assertJsonStringEqualsJsonString(
'[ "2022-10-15T18:35:20+00:00" ]',
\json_encode([ new DateTime('2022-10-15 18:35:20') ])
);
}
public function testUnixTimestampJsonSerialize(): void
{
$this->assertJsonStringEqualsJsonString(
'[ "1970-01-01T00:00:01+00:00" ]',
\json_encode([ new DateTime('@1') ])
);
}
public function testCurrentTimeJsonSerialize(): void
{
$now = new DateTime();
$this->assertJsonStringEqualsJsonString(
" [ \"{$now->format(\DateTime::ATOM)}\" ]",
\json_encode([ $now ])
);
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Domains\Domain;
use Liuch\DmarcSrg\Domains\DomainList;
use Liuch\DmarcSrg\Database\DomainMapperInterface;
class DomainListTest extends \PHPUnit\Framework\TestCase
{
public function testGettingList(): void
{
$data = [
[ 'id' => 1, 'fqdn' => 'example.org' ],
[ 'id' => 2, 'fqdn' => 'example.net' ],
[ 'id' => 3, 'fqdn' => 'example.com' ]
];
$result = (new DomainList($this->getDbMapperOnce('list', $data)))->getList();
$this->assertSame(false, $result['more']);
$list = $result['domains'];
$this->assertSame(count($data), count($list));
$this->assertContainsOnlyInstancesOf(Domain::class, $list);
foreach ($list as $key => $dom) {
$di = $data[$key];
$this->assertSame($di['id'], $dom->id());
$this->assertSame($di['fqdn'], $dom->fqdn());
}
}
public function testGettingNames(): void
{
$names = [ 'example.org', 'example.net', 'example.com' ];
$this->assertSame($names, (new DomainList($this->getDbMapperOnce('names', $names)))->names());
}
private function getDbMapperOnce(string $method, $value): object
{
$mapper = $this->getMockBuilder(DomainMapperInterface::class)
->disableOriginalConstructor()
->setMethods([ $method ])
->getMockForAbstractClass();
$mapper->expects($this->once())
->method($method)
->willReturnCallback(function () use ($value) {
return $value;
});
$db = $this->getMockBuilder(\StdClass::class)
->setMethods([ 'getMapper' ])
->getMock();
$db->method('getMapper')
->with('domain')
->willReturn($mapper);
return $db;
}
}

View File

@@ -0,0 +1,162 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\DateTime;
use Liuch\DmarcSrg\Domains\Domain;
use Liuch\DmarcSrg\Exception\SoftException;
use Liuch\DmarcSrg\Exception\LogicException;
use Liuch\DmarcSrg\Database\DatabaseController;
use Liuch\DmarcSrg\Database\DomainMapperInterface;
class DomainTest extends \PHPUnit\Framework\TestCase
{
public function testConstructorWrongArgumentType(): void
{
$this->expectException(LogicException::class);
new Domain(true, $this->getDbMapperNever());
}
public function testConstructorEmptyFqdn(): void
{
$this->expectException(SoftException::class);
new Domain('', $this->getDbMapperNever());
}
public function testConstructorFqdnIsDot(): void
{
$this->expectException(SoftException::class);
new Domain('.', $this->getDbMapperNever());
}
public function testConstructorFqdnEndWithDot(): void
{
$this->assertSame('example.org', (new Domain('example.org.', $this->getDbMapperNever()))->fqdn());
$this->assertSame('example.org', (new Domain([ 'fqdn' => 'example.org.' ], $this->getDbMapperNever()))->fqdn());
}
public function testConstructorIncorrectArrayData(): void
{
$data = [
'active' => true,
'description' => 'Descr',
'created_time' => new DateTime(),
'updated_time' => new DateTime()
];
$this->expectException(LogicException::class);
new Domain($data, $this->getDbMapperNever());
}
public function testExists(): void
{
$this->assertSame(false, (new Domain(1, $this->getDbMapperOnce('exists', '', false)))->exists());
$this->assertSame(true, (new Domain(1, $this->getDbMapperOnce('exists', '', true)))->exists());
}
public function testId(): void
{
$this->assertSame(1, (new Domain(1, $this->getDbMapperNever()))->id());
$this->assertSame(1, (new Domain([ 'id' => 1 ], $this->getDbMapperNever()))->id());
$this->assertSame(1, (new Domain([ 'fqdn' => 'example.org' ], $this->getDbMapperOnce('fetch', 'id', 1)))->id());
}
public function testFqdn(): void
{
$this->assertSame(
'example.org',
(new Domain('example.org', $this->getDbMapperNever()))->fqdn()
);
$this->assertSame(
'example.org',
(new Domain([ 'fqdn' => 'example.org' ], $this->getDbMapperNever()))->fqdn()
);
$this->assertSame(
'example.org',
(new Domain(1, $this->getDbMapperOnce('fetch', 'fqdn', 'example.org')))->fqdn()
);
}
public function testActive(): void
{
$this->assertSame(true, (new Domain([ 'id' => 1, 'active' => true ], $this->getDbMapperNever()))->active());
$this->assertSame(true, (new Domain(1, $this->getDbMapperOnce('fetch', 'active', true)))->active());
}
public function testDescription(): void
{
$this->assertSame(
'Descr',
(new Domain(
[ 'id'=> 1, 'fqdn' => 'example.org', 'description' => 'Descr' ],
$this->getDbMapperNever()
))->description()
);
$this->assertSame(
'Descr',
(new Domain(1, $this->getDbMapperOnce('fetch', 'description', 'Descr')))->description()
);
}
public function testToArray(): void
{
$data_in = [
'id' => 1,
'fqdn' => 'example.org',
'active' => true,
'description' => 'Descr',
'created_time' => new DateTime(),
'updated_time' => new DateTime()
];
$data_out = $data_in;
unset($data_out['id']);
$this->assertSame($data_out, (new Domain($data_in, $this->getDbMapperNever()))->toArray());
(new Domain([ 'id' => 1 ], $this->getDbMapperOnce('fetch', '', null)))->toArray();
(new Domain([ 'fqdn' => 'example.org' ], $this->getDbMapperOnce('fetch', '', null)))->toArray();
}
public function testSave(): void
{
(new Domain(1, $this->getDbMapperOnce('save', '', null)))->save();
}
public function testDelete(): void
{
(new Domain(1, $this->getDbMapperOnce('delete', '', null)))->delete();
}
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 $key, $value): object
{
$mapper = $this->getMockBuilder(DomainMapperInterface::class)
->disableOriginalConstructor()
->setMethods([ $method ])
->getMockForAbstractClass();
$mapper->expects($this->once())
->method($method)
->willReturnCallback(function (&$data) use ($key, $value) {
if (!empty($key)) {
$data[$key] = $value;
}
return $value;
});
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->getMock();
$db->method('getMapper')
->with('domain')
->willReturn($mapper);
return $db;
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Exception\DatabaseExceptionFactory;
class DatabaseExceptionFactoryTest extends \PHPUnit\Framework\TestCase
{
public function testWhenDatabaseAccessDenied(): void
{
$o = new \PDOException('', 1044);
$e = DatabaseExceptionFactory::fromException($o);
$this->checkException($e, 'Database access denied', $o);
$o = new \PDOException('', 1045);
$e = DatabaseExceptionFactory::fromException($o);
$this->checkException($e, 'Database access denied', $o);
}
public function testWhenDatabaseConnectionError(): void
{
$o = new \PDOException('', 2002);
$e = DatabaseExceptionFactory::fromException($o);
$this->checkException($e, 'Database connection error', $o);
$o = new \PDOException('', 2006);
$e = DatabaseExceptionFactory::fromException($o);
$this->checkException($e, 'Database connection error', $o);
}
public function testUnknownException(): void
{
$o = new \Exception('', 1044);
$e = DatabaseExceptionFactory::fromException($o);
$this->checkException($e, 'Database error', $o);
$o = new \Exception('Some error');
$e = DatabaseExceptionFactory::fromException($o);
$this->checkException($e, 'Database error', $o);
}
private function checkException($e, $m, $o): void
{
$this->assertSame('Liuch\DmarcSrg\Exception\DatabaseException', get_class($e));
$this->assertSame(-1, $e->getCode());
$this->assertSame($m, $e->getMessage());
$this->assertSame($o, $e->getPrevious());
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Exception\RuntimeException;
class RuntimeExceptionTest extends \PHPUnit\Framework\TestCase
{
public function testExceptionCode(): void
{
$this->assertSame(-1, (new RuntimeException())->getCode());
$this->assertSame(0, (new RuntimeException('', 0))->getCode());
$this->assertSame(-1, (new RuntimeException('', -1))->getCode());
$this->assertSame(77, (new RuntimeException('', 77))->getCode());
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Mail\MailBody;
class MailBodyTest extends \PHPUnit\Framework\TestCase
{
private $text = null;
private $html = null;
public function setUp(): void
{
$this->text = [ 'text string' ];
$this->html = [ 'html string' ];
}
public function testTextContentType(): void
{
$body = new MailBody();
$body->setText($this->text);
$this->assertSame('text/plain; charset=utf-8', $body->contentType());
}
public function testHtmlContentType(): void
{
$body = new MailBody();
$body->setHtml($this->html);
$this->assertSame('text/html; charset=utf-8', $body->contentType());
}
public function testMultipartContentType(): void
{
$body = new MailBody();
$body->setText($this->text);
$body->setHtml($this->html);
$this->assertStringStartsWith('multipart/alternative; boundary=', $body->contentType());
}
public function testTextContent(): void
{
$body = new MailBody();
$body->setText($this->text);
$content = $body->content();
$this->assertCount(1, $content);
$this->assertSame('text string', $content[0]);
}
public function testHtmlContent(): void
{
$body = new MailBody();
$body->setText($this->html);
$content = $body->content();
$this->assertCount(1, $content);
$this->assertSame('html string', $content[0]);
}
public function testMultipartContent(): void
{
$body = new MailBody();
$body->setText($this->text);
$body->setHtml($this->html);
$boundary = substr($body->contentType(), 33, -1);
$content = $body->content();
$this->assertCount(10, $content);
$this->assertSame('--' . $boundary, $content[0]);
$this->assertSame('Content-Type: text/plain; charset=utf-8', $content[1]);
$this->assertSame('Content-Transfer-Encoding: 7bit', $content[2]);
$this->assertSame('', $content[3]);
$this->assertSame('text string', $content[4]);
$this->assertSame('--' . $boundary, $content[5]);
$this->assertSame('Content-Type: text/html; charset=utf-8', $content[6]);
$this->assertSame('Content-Transfer-Encoding: 7bit', $content[7]);
$this->assertSame('', $content[8]);
$this->assertSame('html string', $content[9]);
}
}

View File

@@ -0,0 +1,175 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Report\Report;
use Liuch\DmarcSrg\Sources\Source;
use Liuch\DmarcSrg\Exception\SoftException;
use Liuch\DmarcSrg\Exception\DatabaseNotFoundException;
use Liuch\DmarcSrg\ReportLog\ReportLogItem;
use Liuch\DmarcSrg\Database\ReportLogMapperInterface;
class ReportLogItemTest extends \PHPUnit\Framework\TestCase
{
public function testForSuccess(): void
{
$rli = ReportLogItem::success(
Source::SOURCE_MAILBOX,
$this->getReportMock([ 'domain' => 'example.org', 'external_id' => 'rrggoo' ]),
'filename.gz',
'Success!',
$this->getDbMapperNever()
);
$this->assertTrue($rli->toArray()['success']);
}
public function testForFailed(): void
{
$rli = ReportLogItem::failed(
Source::SOURCE_UPLOADED_FILE,
null,
null,
'Failed!',
$this->getDbMapperNever()
);
$this->assertFalse($rli->toArray()['success']);
}
public function testGettingById(): void
{
$callback = function (&$data) {
$data['source'] = Source::SOURCE_MAILBOX;
$this->assertSame(55, $data['id']);
};
ReportLogItem::byId(55, $this->getDbMapperOnce('fetch', $callback));
}
public function testGettingByIdNotFound(): void
{
$callback = function (&$data) {
throw new DatabaseNotFoundException();
};
$this->expectException(SoftException::class);
ReportLogItem::byId(55, $this->getDbMapperOnce('fetch', $callback));
}
public function testSourceToString(): void
{
$this->assertSame('uploaded_file', ReportLogItem::sourceToString(Source::SOURCE_UPLOADED_FILE));
$this->assertSame('email', ReportLogItem::sourceToString(Source::SOURCE_MAILBOX));
$this->assertSame('directory', ReportLogItem::sourceToString(Source::SOURCE_DIRECTORY));
$this->assertSame('', ReportLogItem::sourceToString(-111));
}
public function testToArray(): void
{
$sdata = [
'id' => 66,
'domain' => 'example.org',
'external_id' => 'gg44dd',
'event_time' => new \DateTime(),
'filename' => 'filename.zip',
'source' => Source::SOURCE_DIRECTORY,
'success' => true,
'message' => 'Message!'
];
$callback = function (&$data) use ($sdata) {
foreach ($sdata as $key => $value) {
$data[$key] = $value;
}
};
$sdata['source'] = ReportLogItem::sourceToString($sdata['source']);
$this->assertSame($sdata, ReportLogItem::byId(66, $this->getDbMapperOnce('fetch', $callback))->toArray());
}
public function testSaving(): void
{
$callback1 = function ($data) {
$this->assertSame(
[
'id' => null,
'domain' => 'example.org',
'external_id' => 'xxvvbb',
'event_time' => null,
'filename' => 'filename.xml',
'source' => Source::SOURCE_MAILBOX,
'success' => true,
'message' => 'Success!'
],
$data
);
};
$callback2 = function ($data) {
$this->assertSame(
[
'id' => null,
'domain' => null,
'external_id' => null,
'event_time' => null,
'filename' => null,
'source' => Source::SOURCE_UPLOADED_FILE,
'success' => false,
'message' => 'Failed!'
],
$data
);
};
$rli = ReportLogItem::success(
Source::SOURCE_MAILBOX,
$this->getReportMock([ 'domain' => 'example.org', 'external_id' => 'xxvvbb' ]),
'filename.xml',
'Success!',
$this->getDbMapperOnce('save', $callback1)
);
$rli->save();
$rli = ReportLogItem::failed(
Source::SOURCE_UPLOADED_FILE,
null,
null,
'Failed!',
$this->getDbMapperOnce('save', $callback2)
);
$rli->save();
}
private function getReportMock($data): object
{
$mock = $this->createMock(Report::class);
$mock->method('get')
->willReturn($data);
return $mock;
}
private function getDbMapperOnce(string $method, $callback): object
{
$mapper = $this->getMockBuilder(ReportLogMapperInterface::class)
->disableOriginalConstructor()
->setMethods([ $method ])
->getMockForAbstractClass();
$mapper->expects($this->once())
->method($method)
->willReturnCallback($callback);
$db = $this->getMockBuilder(\StdClass::class)
->setMethods([ 'getMapper' ])
->getMock();
$db->method('getMapper')
->with('report-log')
->willReturn($mapper);
return $db;
}
private function getDbMapperNever(): object
{
$db = $this->getMockBuilder(\StdClass::class)
->setMethods([ 'getMapper' ])
->getMock();
$db->expects($this->never())
->method('getMapper');
return $db;
}
}

View File

@@ -0,0 +1,163 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\ReportLog\ReportLog;
use Liuch\DmarcSrg\Database\ReportLogMapperInterface;
class ReportLogTest extends \PHPUnit\Framework\TestCase
{
protected static $from = null;
protected static $till = null;
public static function setUpBeforeClass(): void
{
self::$from = (new \DateTime)->sub(new \DateInterval('P10D'));
self::$till = new \DateTime();
}
public static function tearDownAfterClass(): void
{
self::$from = null;
self::$till = null;
}
public function testRange(): void
{
$callback = function ($filter, $order, $limit) {
$this->assertSame([ 'from_time' => self::$from, 'till_time' => self::$till ], $filter);
$this->assertSame(11, $limit['offset']);
return [];
};
$rl = new ReportLog(self::$from, self::$till, $this->getDbMapperOnce('list', $callback));
$rl->getList(11);
}
public function testSettingOrder(): void
{
$callback_ascent = function ($filter, $order, $limit) {
$this->assertSame([ 'direction' => 'ascent' ], $order);
return [];
};
$callback_descent = function ($filter, $order, $limit) {
$this->assertSame([ 'direction' => 'descent' ], $order);
return [];
};
$rl = new ReportLog(self::$from, self::$till, $this->getDbMapperOnce('list', $callback_ascent));
$rl->getList(0);
$rl = new ReportLog(self::$from, self::$till, $this->getDbMapperOnce('list', $callback_ascent));
$rl->setOrder(ReportLog::ORDER_ASCENT);
$rl->getList(0);
$rl = new ReportLog(self::$from, self::$till, $this->getDbMapperOnce('list', $callback_descent));
$rl->setOrder(ReportLog::ORDER_DESCENT);
$rl->getList(0);
}
public function testSettingMaxCount(): void
{
$callback26 = function ($filter, $order, $limit) {
$this->assertSame(26, $limit['count']);
return [];
};
$callback51 = function ($filter, $order, $limit) {
$this->assertSame(51, $limit['count']);
return [];
};
$rl = new ReportLog(self::$from, self::$till, $this->getDbMapperOnce('list', $callback26));
$rl->getList(0);
$rl = new ReportLog(self::$from, self::$till, $this->getDbMapperOnce('list', $callback51));
$rl->setMaxCount(50);
$rl->getList(0);
}
public function testGettingCount(): void
{
$callback = function ($filter, $limit) {
$this->assertSame([ 'from_time' => self::$from, 'till_time' => self::$till ], $filter);
$this->assertSame([ 'offset' => 0, 'count' => 44 ], $limit);
return 55;
};
$rl = new ReportLog(self::$from, self::$till, $this->getDbMapperOnce('count', $callback));
$rl->setMaxCount(44);
$this->assertSame(55, $rl->count());
}
public function testGettingList(): void
{
$callback = function () {
return [
[
'id' => 1,
'domain' => null,
'external_id' => null,
'event_time' => null,
'filename' => null,
'source' => 0,
'success' => false,
'message' => null
],
[
'id' => 2,
'domain' => null,
'external_id' => null,
'event_time' => null,
'filename' => null,
'source' => 0,
'success' => false,
'message' => null
]
];
};
$rl = new ReportLog(self::$from, self::$till, $this->getDbMapperOnce('list', $callback));
$rl->setMaxCount(1);
$res = $rl->getList(0);
$this->assertTrue($res['more']);
$this->assertCount(1, $res['items']);
$rl = new ReportLog(self::$from, self::$till, $this->getDbMapperOnce('list', $callback));
$rl->setMaxCount(2);
$res = $rl->getList(0);
$this->assertFalse($res['more']);
$this->assertCount(2, $res['items']);
}
public function testDeleting(): void
{
$callback = function ($filter, $order, $limit) {
$this->assertSame([ 'from_time' => self::$from, 'till_time' => self::$till ], $filter);
$this->assertSame([ 'direction' => 'ascent' ], $order);
$this->assertSame([ 'offset' => 0, 'count' => 33 ], $limit);
};
$rl = new ReportLog(self::$from, self::$till, $this->getDbMapperOnce('delete', $callback));
$rl->setOrder(ReportLog::ORDER_ASCENT);
$rl->setMaxCount(33);
$rl->delete();
}
private function getDbMapperOnce(string $method, $callback): object
{
$mapper = $this->getMockBuilder(ReportLogMapperInterface::class)
->disableOriginalConstructor()
->setMethods([ $method ])
->getMockForAbstractClass();
$mapper->expects($this->once())
->method($method)
->willReturnCallback($callback);
$db = $this->getMockBuilder(\StdClass::class)
->setMethods([ 'getMapper' ])
->getMock();
$db->method('getMapper')
->with('report-log')
->willReturn($mapper);
return $db;
}
}

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;
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Liuch\DmarcSrg;
use Liuch\DmarcSrg\Sources\SourceAction;
class SourceActionTest extends \PHPUnit\Framework\TestCase
{
public function testUnknowAction(): void
{
$this->assertCount(0, SourceAction::fromSetting('some_action', 0, ''));
}
public function testMarkSeen(): void
{
$this->assertCount(0, SourceAction::fromSetting('mark_seen:??', 0, ''));
$sa = SourceAction::fromSetting('mark_seen', 0, '')[0];
$this->assertSame(SourceAction::ACTION_SEEN, $sa->type);
}
public function testMoveTo(): void
{
$this->assertCount(0, SourceAction::fromSetting('move_to', 0, ''));
$sa = SourceAction::fromSetting('move_to:target', 0, '')[0];
$this->assertSame(SourceAction::ACTION_MOVE, $sa->type);
$this->assertSame('target', $sa->param);
}
public function testMoveToAndBasename(): void
{
$this->assertCount(1, SourceAction::fromSetting('move_to:/target', 0, ''));
$this->assertCount(0, SourceAction::fromSetting('move_to:/target', SourceAction::FLAG_BASENAME, ''));
}
public function testDelete(): void
{
$this->assertCount(0, SourceAction::fromSetting('delete:??', 0, ''));
$sa = SourceAction::fromSetting('delete', 0, '')[0];
$this->assertSame(SourceAction::ACTION_DELETE, $sa->type);
}
public function testMultipleActions(): void
{
$list = SourceAction::fromSetting([ 'mark_seen', 'move_to:target', 'delete' ], 0, '');
$this->assertCount(3, $list);
$this->assertSame(SourceAction::ACTION_SEEN, $list[0]->type);
$this->assertSame(SourceAction::ACTION_MOVE, $list[1]->type);
$this->assertSame(SourceAction::ACTION_DELETE, $list[2]->type);
}
public function testMultipleActionsWithWrongItem(): void
{
$list = SourceAction::fromSetting([ 'mark_seen', 'move_to:', 'delete' ], 0, '');
$this->assertCount(2, $list);
$this->assertSame(SourceAction::ACTION_SEEN, $list[0]->type);
$this->assertSame(SourceAction::ACTION_DELETE, $list[1]->type);
}
public function testDefaultAction(): void
{
$list = SourceAction::fromSetting([], 0, 'mark_seen');
$this->assertCount(1, $list);
$this->assertSame(SourceAction::ACTION_SEEN, $list[0]->type);
$list = SourceAction::fromSetting('', 0, 'mark_seen');
$this->assertCount(1, $list);
$this->assertSame(SourceAction::ACTION_SEEN, $list[0]->type);
}
public function testWrongDefaultAction(): void
{
$list = SourceAction::fromSetting([], 0, 'wrong_default');
$this->assertCount(0, $list);
}
public function testDuplicates(): void
{
$s_list1 = [ 'mark_seen', 'move_to:target', 'delete' ];
$s_list2 = $s_list1;
$this->assertCount(3, SourceAction::fromSetting(array_merge($s_list1, $s_list2), 0, ''));
}
}

View File

@@ -0,0 +1,14 @@
<?php
$some = 'some';
$debug = false;
$database = 0;
$mailboxes = '';
$directories = null;
$admin = [];
$mailer = [];
$fetcher = [];
$cleaner = [
'key_int' => 0,
'key_bool' => false,
'key_string' => ''
];

View File

@@ -0,0 +1,4 @@
#!/usr/bin/env sh
phpunit --color --bootstrap init.php tests