| 1 | <?php |
| 2 | |
| 3 | namespace Liuch\DmarcSrg; |
| 4 | |
| 5 | use Liuch\DmarcSrg\Domains\Domain; |
| 6 | use Liuch\DmarcSrg\Database\DatabaseController; |
| 7 | |
| 8 | class StatisticsTest extends \PHPUnit\Framework\TestCase |
| 9 | { |
| 10 | private $db = null; |
| 11 | private $domain = null; |
| 12 | |
| 13 | public function setUp(): void |
| 14 | { |
| 15 | $this->db = $this->createStub(DatabaseController::class); |
| 16 | $this->domain = new Domain('example.org', $this->db); |
| 17 | } |
| 18 | |
| 19 | public function testFromTo(): void |
| 20 | { |
| 21 | $range = Statistics::fromTo( |
| 22 | $this->domain, |
| 23 | new DateTime('2020-01-11'), |
| 24 | new DateTime('2021-01-01'), |
| 25 | $this->db |
| 26 | )->range(); |
| 27 | $this->assertSame('2020-01-11T00:00:00+00:00', $range[0]->format('c')); |
| 28 | $this->assertSame('2020-12-31T23:59:59+00:00', $range[1]->format('c')); |
| 29 | } |
| 30 | |
| 31 | public function testLastWeek(): void |
| 32 | { |
| 33 | $range = Statistics::lastWeek($this->domain, 0, $this->db)->range(); |
| 34 | $date1 = new DateTime('midnight monday last week'); |
| 35 | $date2 = (clone $date1)->add(new \DateInterval('P1W'))->sub(new \DateInterval('PT1S')); |
| 36 | $this->assertSame($date1->format('c'), $range[0]->format('c')); |
| 37 | $this->assertSame($date2->format('c'), $range[1]->format('c')); |
| 38 | } |
| 39 | |
| 40 | public function testLastWeekOffset(): void |
| 41 | { |
| 42 | $range = Statistics::lastWeek($this->domain, 5, $this->db)->range(); |
| 43 | $date1 = (new DateTime('midnight monday last week'))->sub(new \DateInterval('P5W')); |
| 44 | $date2 = (clone $date1)->add(new \DateInterval('P1W'))->sub(new \DateInterval('PT1S')); |
| 45 | $this->assertSame($date1->format('c'), $range[0]->format('c')); |
| 46 | $this->assertSame($date2->format('c'), $range[1]->format('c')); |
| 47 | } |
| 48 | |
| 49 | public function testLastMonth(): void |
| 50 | { |
| 51 | $range = Statistics::lastMonth($this->domain, 0, $this->db)->range(); |
| 52 | $date1 = new DateTime('midnight first day of last month'); |
| 53 | $date2 = (clone $date1)->add(new \DateInterval('P1M'))->sub(new \DateInterval('PT1S')); |
| 54 | $this->assertSame($date1->format('c'), $range[0]->format('c')); |
| 55 | $this->assertSame($date2->format('c'), $range[1]->format('c')); |
| 56 | } |
| 57 | |
| 58 | public function testLastMonthOffset(): void |
| 59 | { |
| 60 | $range = Statistics::lastMonth($this->domain, 7, $this->db)->range(); |
| 61 | $date1 = (new DateTime('midnight first day of last month'))->sub(new \DateInterval('P7M')); |
| 62 | $date2 = (clone $date1)->add(new \DateInterval('P1M'))->sub(new \DateInterval('PT1S')); |
| 63 | $this->assertSame($date1->format('c'), $range[0]->format('c')); |
| 64 | $this->assertSame($date2->format('c'), $range[1]->format('c')); |
| 65 | } |
| 66 | |
| 67 | public function testLastNDays(): void |
| 68 | { |
| 69 | $range = Statistics::lastNDays($this->domain, 10, 0, $this->db)->range(); |
| 70 | $date2 = new DateTime('midnight'); |
| 71 | $date1 = (clone $date2)->sub(new \DateInterval('P10D')); |
| 72 | $date2->sub(new \DateInterval('PT1S')); |
| 73 | $this->assertSame($date1->format('c'), $range[0]->format('c')); |
| 74 | $this->assertSame($date2->format('c'), $range[1]->format('c')); |
| 75 | } |
| 76 | |
| 77 | public function testLastNDaysOffset(): void |
| 78 | { |
| 79 | $range = Statistics::lastNDays($this->domain, 10, 9, $this->db)->range(); |
| 80 | $date2 = (new DateTime('midnight'))->sub(new \DateInterval('P9D')); |
| 81 | $date1 = (clone $date2)->sub(new \DateInterval('P10D')); |
| 82 | $date2->sub(new \DateInterval('PT1S')); |
| 83 | $this->assertSame($date1->format('c'), $range[0]->format('c')); |
| 84 | $this->assertSame($date2->format('c'), $range[1]->format('c')); |
| 85 | } |
| 86 | } |