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