File size: 4,723 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php

namespace Mautic\ReportBundle\Tests\Entity;

use Mautic\ReportBundle\Entity\Report;
use Mautic\ReportBundle\Scheduler\Enum\SchedulerEnum;
use Mautic\ReportBundle\Scheduler\Exception\ScheduleNotValidException;

class ReportTest extends \PHPUnit\Framework\TestCase
{
    public function testNotScheduled(): void
    {
        $report = $this->getInvalidReport();

        $report->setAsNotScheduled();

        $this->assertFalse($report->isScheduled());
        $this->assertNull($report->getToAddress());
        $this->assertNull($report->getScheduleUnit());
        $this->assertNull($report->getScheduleDay());
        $this->assertNull($report->getScheduleMonthFrequency());
    }

    public function testDailyScheduled(): void
    {
        $report = $this->getInvalidReport();

        $report->ensureIsDailyScheduled();

        $this->assertTrue($report->isScheduled());
        $this->assertSame('xxx', $report->getToAddress());
        $this->assertSame('DAILY', $report->getScheduleUnit());
        $this->assertNull($report->getScheduleDay());
        $this->assertNull($report->getScheduleMonthFrequency());
    }

    public function testWeeklyScheduled(): void
    {
        $report = $this->getInvalidReport();
        $report->setScheduleDay('WEEK_DAYS');

        $report->ensureIsWeeklyScheduled();
        $this->assertTrue($report->isScheduled());
        $this->assertSame('xxx', $report->getToAddress());
        $this->assertSame(SchedulerEnum::UNIT_WEEKLY, $report->getScheduleUnit());
        $this->assertSame(SchedulerEnum::DAY_WEEK_DAYS, $report->getScheduleDay());
        $this->assertNull($report->getScheduleMonthFrequency());
    }

    public function testMonthlyScheduled(): void
    {
        $report = $this->getInvalidReport();
        $report->setScheduleDay('WEEK_DAYS');
        $report->setScheduleMonthFrequency('-1');

        $report->ensureIsMonthlyScheduled();
        $this->assertTrue($report->isScheduled());
        $this->assertSame('xxx', $report->getToAddress());
        $this->assertSame(SchedulerEnum::UNIT_MONTHLY, $report->getScheduleUnit());
        $this->assertSame(SchedulerEnum::DAY_WEEK_DAYS, $report->getScheduleDay());
        $this->assertSame(SchedulerEnum::MONTH_FREQUENCY_LAST, $report->getScheduleMonthFrequency());
    }

    public function testInvalidMonthlyScheduled(): void
    {
        $this->expectException(ScheduleNotValidException::class);

        $report = $this->getInvalidReport();
        $report->ensureIsMonthlyScheduled();
    }

    public function testInvalidWeeklyScheduled(): void
    {
        $this->expectException(ScheduleNotValidException::class);

        $report = $this->getInvalidReport();
        $report->ensureIsWeeklyScheduled();
    }

    public function testGetFilterValueIfFIltersAreEmpty(): void
    {
        $report = $this->getInvalidReport();

        $this->expectException(\UnexpectedValueException::class);
        $this->assertSame('1234', $report->getFilterValue('e.test'));
    }

    public function testGetFilterValueIfExists(): void
    {
        $report = $this->getInvalidReport();
        $report->setFilters([
            [
                'column' => 'e.test',
                'value'  => '1234',
            ],
        ]);

        $this->assertSame('1234', $report->getFilterValue('e.test'));
    }

    public function testGetFilterValueIfDoesNotExist(): void
    {
        $report = $this->getInvalidReport();
        $report->setFilters([
            [
                'column' => 'e.test',
                'value'  => '1234',
            ],
        ]);

        $this->expectException(\UnexpectedValueException::class);
        $report->getFilterValue('I need coffee');
    }

    public function testSetAsScheduledNow(): void
    {
        $email  = '[email protected]';
        $report = new Report();
        $report->setAsScheduledNow($email);

        $this->assertTrue($report->isScheduled());
        $this->assertSame($email, $report->getToAddress());
        $this->assertSame(SchedulerEnum::UNIT_NOW, $report->getScheduleUnit());
    }

    public function testIsScheduledNowIfNot(): void
    {
        $report = new Report();

        $this->assertFalse($report->isScheduledNow());

        $report->setScheduleUnit(SchedulerEnum::UNIT_NOW);

        $this->assertTrue($report->isScheduledNow());
    }

    /**
     * @return Report
     */
    private function getInvalidReport()
    {
        $report = new Report();
        $report->setIsScheduled(true);
        $report->setToAddress('xxx');
        $report->setScheduleUnit('unit');
        $report->setScheduleDay('day');
        $report->setScheduleMonthFrequency('frequency');

        return $report;
    }
}