| 1 | <?php |
| 2 | |
| 3 | namespace Liuch\DmarcSrg; |
| 4 | |
| 5 | use Liuch\DmarcSrg\Sources\Source; |
| 6 | use Liuch\DmarcSrg\ReportLog\ReportLog; |
| 7 | |
| 8 | class ReportLogTest extends \PHPUnit\Framework\TestCase |
| 9 | { |
| 10 | protected static $from = null; |
| 11 | protected static $till = null; |
| 12 | |
| 13 | public static function setUpBeforeClass(): void |
| 14 | { |
| 15 | self::$from = (new \DateTime)->sub(new \DateInterval('P10D')); |
| 16 | self::$till = new \DateTime(); |
| 17 | } |
| 18 | |
| 19 | public static function tearDownAfterClass(): void |
| 20 | { |
| 21 | self::$from = null; |
| 22 | self::$till = null; |
| 23 | } |
| 24 | |
| 25 | public function testRangeFilter(): void |
| 26 | { |
| 27 | $callback = function ($filter, $order, $limit) { |
| 28 | $this->assertSame([ 'from_time' => self::$from, 'till_time' => self::$till ], $filter); |
| 29 | $this->assertSame(11, $limit['offset']); |
| 30 | return []; |
| 31 | }; |
| 32 | (new ReportLog($this->getDbMapperOnce('list', $callback))) |
| 33 | ->setFilter([ 'from_time' => self::$from, 'till_time' => self::$till ]) |
| 34 | ->getList(11); |
| 35 | } |
| 36 | |
| 37 | public function testFilterWithSuccessValue(): void |
| 38 | { |
| 39 | $callback1 = function ($filter, $order, $limit) { |
| 40 | $this->assertSame([ 'success' => true ], $filter); |
| 41 | return []; |
| 42 | }; |
| 43 | $callback2 = function ($filter, $order, $limit) { |
| 44 | $this->assertSame([ 'success' => false ], $filter); |
| 45 | return []; |
| 46 | }; |
| 47 | (new ReportLog($this->getDbMapperOnce('list', $callback1))) |
| 48 | ->setFilter([ 'success' => true ]) |
| 49 | ->getList(0); |
| 50 | (new ReportLog($this->getDbMapperOnce('list', $callback2))) |
| 51 | ->setFilter([ 'success' => false ]) |
| 52 | ->getList(0); |
| 53 | } |
| 54 | |
| 55 | public function testFilterWithSourceValue(): void |
| 56 | { |
| 57 | $callback1 = function ($filter, $order, $limit) { |
| 58 | $this->assertSame([ 'source' => Source::SOURCE_UPLOADED_FILE ], $filter); |
| 59 | return []; |
| 60 | }; |
| 61 | $callback2 = function ($filter, $order, $limit) { |
| 62 | $this->assertSame([ 'source' => Source::SOURCE_MAILBOX ], $filter); |
| 63 | return []; |
| 64 | }; |
| 65 | $callback3 = function ($filter, $order, $limit) { |
| 66 | $this->assertSame([ 'source' => Source::SOURCE_DIRECTORY ], $filter); |
| 67 | return []; |
| 68 | }; |
| 69 | (new ReportLog($this->getDbMapperOnce('list', $callback1))) |
| 70 | ->setFilter([ 'source' => 'uploaded_file' ]) |
| 71 | ->getList(0); |
| 72 | (new ReportLog($this->getDbMapperOnce('list', $callback2))) |
| 73 | ->setFilter([ 'source' => 'email' ]) |
| 74 | ->getList(0); |
| 75 | (new ReportLog($this->getDbMapperOnce('list', $callback3))) |
| 76 | ->setFilter([ 'source' => 'directory' ]) |
| 77 | ->getList(0); |
| 78 | } |
| 79 | |
| 80 | public function testSettingOrder(): void |
| 81 | { |
| 82 | $callback_ascent = function ($filter, $order, $limit) { |
| 83 | $this->assertSame([ 'direction' => 'ascent' ], $order); |
| 84 | return []; |
| 85 | }; |
| 86 | $callback_descent = function ($filter, $order, $limit) { |
| 87 | $this->assertSame([ 'direction' => 'descent' ], $order); |
| 88 | return []; |
| 89 | }; |
| 90 | (new ReportLog($this->getDbMapperOnce('list', $callback_ascent))) |
| 91 | ->getList(0); |
| 92 | |
| 93 | (new ReportLog($this->getDbMapperOnce('list', $callback_ascent))) |
| 94 | ->setOrder(ReportLog::ORDER_ASCENT) |
| 95 | ->getList(0); |
| 96 | (new ReportLog($this->getDbMapperOnce('list', $callback_descent))) |
| 97 | ->setOrder(ReportLog::ORDER_DESCENT) |
| 98 | ->getList(0); |
| 99 | } |
| 100 | |
| 101 | public function testSettingMaxCount(): void |
| 102 | { |
| 103 | $callback26 = function ($filter, $order, $limit) { |
| 104 | $this->assertSame(26, $limit['count']); |
| 105 | return []; |
| 106 | }; |
| 107 | $callback51 = function ($filter, $order, $limit) { |
| 108 | $this->assertSame(51, $limit['count']); |
| 109 | return []; |
| 110 | }; |
| 111 | (new ReportLog($this->getDbMapperOnce('list', $callback26))) |
| 112 | ->getList(0); |
| 113 | (new ReportLog($this->getDbMapperOnce('list', $callback51))) |
| 114 | ->setMaxCount(50) |
| 115 | ->getList(0); |
| 116 | } |
| 117 | |
| 118 | public function testGettingCount(): void |
| 119 | { |
| 120 | $callback = function ($filter, $limit) { |
| 121 | $this->assertSame([ 'from_time' => self::$from, 'till_time' => self::$till ], $filter); |
| 122 | $this->assertSame([ 'offset' => 0, 'count' => 44 ], $limit); |
| 123 | return 55; |
| 124 | }; |
| 125 | $rl = (new ReportLog($this->getDbMapperOnce('count', $callback))) |
| 126 | ->setFilter([ 'from_time' => self::$from, 'till_time' => self::$till ]) |
| 127 | ->setMaxCount(44); |
| 128 | $this->assertSame(55, $rl->count()); |
| 129 | } |
| 130 | |
| 131 | public function testGettingList(): void |
| 132 | { |
| 133 | $callback = function () { |
| 134 | return [ |
| 135 | [ |
| 136 | 'id' => 1, |
| 137 | 'domain' => null, |
| 138 | 'report_id' => null, |
| 139 | 'event_time' => null, |
| 140 | 'filename' => null, |
| 141 | 'source' => 0, |
| 142 | 'success' => false, |
| 143 | 'message' => null |
| 144 | ], |
| 145 | [ |
| 146 | 'id' => 2, |
| 147 | 'domain' => null, |
| 148 | 'report_id' => null, |
| 149 | 'event_time' => null, |
| 150 | 'filename' => null, |
| 151 | 'source' => 0, |
| 152 | 'success' => false, |
| 153 | 'message' => null |
| 154 | ] |
| 155 | |
| 156 | ]; |
| 157 | }; |
| 158 | |
| 159 | $res = (new ReportLog($this->getDbMapperOnce('list', $callback))) |
| 160 | ->setMaxCount(1) |
| 161 | ->getList(0); |
| 162 | $this->assertTrue($res['more']); |
| 163 | $this->assertCount(1, $res['items']); |
| 164 | |
| 165 | $res = (new ReportLog($this->getDbMapperOnce('list', $callback))) |
| 166 | ->setMaxCount(2) |
| 167 | ->getList(0); |
| 168 | $this->assertFalse($res['more']); |
| 169 | $this->assertCount(2, $res['items']); |
| 170 | } |
| 171 | |
| 172 | public function testDeleting(): void |
| 173 | { |
| 174 | $callback = function ($filter, $order, $limit) { |
| 175 | $this->assertSame([ 'from_time' => self::$from, 'till_time' => self::$till ], $filter); |
| 176 | $this->assertSame([ 'direction' => 'ascent' ], $order); |
| 177 | $this->assertSame([ 'offset' => 0, 'count' => 33 ], $limit); |
| 178 | }; |
| 179 | (new ReportLog($this->getDbMapperOnce('delete', $callback))) |
| 180 | ->setFilter([ 'from_time' => self::$from, 'till_time' => self::$till ]) |
| 181 | ->setOrder(ReportLog::ORDER_ASCENT) |
| 182 | ->setMaxCount(33) |
| 183 | ->delete(); |
| 184 | } |
| 185 | |
| 186 | private function getDbMapperOnce(string $method, $callback): object |
| 187 | { |
| 188 | $mapper = $this->getMockBuilder(Database\ReportLogMapperInterface::class) |
| 189 | ->getMock(); |
| 190 | $mapper->expects($this->once()) |
| 191 | ->method($method) |
| 192 | ->willReturnCallback($callback); |
| 193 | |
| 194 | $db = $this->getMockBuilder(Database\DatabaseConnector::class) |
| 195 | ->disableOriginalConstructor() |
| 196 | ->getMock(); |
| 197 | $db->method('getMapper') |
| 198 | ->with('report-log') |
| 199 | ->willReturn($mapper); |
| 200 | |
| 201 | return $db; |
| 202 | } |
| 203 | } |