| 1 | <?php |
| 2 | |
| 3 | namespace Liuch\DmarcSrg; |
| 4 | |
| 5 | use Liuch\DmarcSrg\Sources\SourceAction; |
| 6 | |
| 7 | class SourceActionTest extends \PHPUnit\Framework\TestCase |
| 8 | { |
| 9 | public function testUnknowAction(): void |
| 10 | { |
| 11 | $this->assertCount(0, SourceAction::fromSetting('some_action', 0, '')); |
| 12 | } |
| 13 | |
| 14 | public function testMarkSeen(): void |
| 15 | { |
| 16 | $this->assertCount(0, SourceAction::fromSetting('mark_seen:??', 0, '')); |
| 17 | $sa = SourceAction::fromSetting('mark_seen', 0, '')[0]; |
| 18 | $this->assertSame(SourceAction::ACTION_SEEN, $sa->type); |
| 19 | } |
| 20 | |
| 21 | public function testMoveTo(): void |
| 22 | { |
| 23 | $this->assertCount(0, SourceAction::fromSetting('move_to', 0, '')); |
| 24 | $sa = SourceAction::fromSetting('move_to:target', 0, '')[0]; |
| 25 | $this->assertSame(SourceAction::ACTION_MOVE, $sa->type); |
| 26 | $this->assertSame('target', $sa->param); |
| 27 | } |
| 28 | |
| 29 | public function testMoveToAndBasename(): void |
| 30 | { |
| 31 | $this->assertCount(1, SourceAction::fromSetting('move_to:/target', 0, '')); |
| 32 | $this->assertCount(0, SourceAction::fromSetting('move_to:/target', SourceAction::FLAG_BASENAME, '')); |
| 33 | } |
| 34 | |
| 35 | public function testDelete(): void |
| 36 | { |
| 37 | $this->assertCount(0, SourceAction::fromSetting('delete:??', 0, '')); |
| 38 | $sa = SourceAction::fromSetting('delete', 0, '')[0]; |
| 39 | $this->assertSame(SourceAction::ACTION_DELETE, $sa->type); |
| 40 | } |
| 41 | |
| 42 | public function testMultipleActions(): void |
| 43 | { |
| 44 | $list = SourceAction::fromSetting([ 'mark_seen', 'move_to:target', 'delete' ], 0, ''); |
| 45 | $this->assertCount(3, $list); |
| 46 | $this->assertSame(SourceAction::ACTION_SEEN, $list[0]->type); |
| 47 | $this->assertSame(SourceAction::ACTION_MOVE, $list[1]->type); |
| 48 | $this->assertSame(SourceAction::ACTION_DELETE, $list[2]->type); |
| 49 | } |
| 50 | |
| 51 | public function testMultipleActionsWithWrongItem(): void |
| 52 | { |
| 53 | $list = SourceAction::fromSetting([ 'mark_seen', 'move_to:', 'delete' ], 0, ''); |
| 54 | $this->assertCount(2, $list); |
| 55 | $this->assertSame(SourceAction::ACTION_SEEN, $list[0]->type); |
| 56 | $this->assertSame(SourceAction::ACTION_DELETE, $list[1]->type); |
| 57 | } |
| 58 | |
| 59 | public function testDefaultAction(): void |
| 60 | { |
| 61 | $list = SourceAction::fromSetting([], 0, 'mark_seen'); |
| 62 | $this->assertCount(1, $list); |
| 63 | $this->assertSame(SourceAction::ACTION_SEEN, $list[0]->type); |
| 64 | |
| 65 | $list = SourceAction::fromSetting('', 0, 'mark_seen'); |
| 66 | $this->assertCount(1, $list); |
| 67 | $this->assertSame(SourceAction::ACTION_SEEN, $list[0]->type); |
| 68 | } |
| 69 | |
| 70 | public function testWrongDefaultAction(): void |
| 71 | { |
| 72 | $list = SourceAction::fromSetting([], 0, 'wrong_default'); |
| 73 | $this->assertCount(0, $list); |
| 74 | } |
| 75 | |
| 76 | public function testDuplicates(): void |
| 77 | { |
| 78 | $s_list1 = [ 'mark_seen', 'move_to:target', 'delete' ]; |
| 79 | $s_list2 = $s_list1; |
| 80 | $this->assertCount(3, SourceAction::fromSetting(array_merge($s_list1, $s_list2), 0, '')); |
| 81 | } |
| 82 | } |