Issues (3627)

bundles/ReportBundle/Tests/Entity/ReportTest.php (6 issues)

1
<?php
2
3
/*
4
 * @copyright   2016 Mautic Contributors. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\ReportBundle\Tests\Entity;
13
14
use Mautic\ReportBundle\Entity\Report;
15
use Mautic\ReportBundle\Scheduler\Enum\SchedulerEnum;
16
use Mautic\ReportBundle\Scheduler\Exception\ScheduleNotValidException;
17
18
class ReportTest extends \PHPUnit\Framework\TestCase
19
{
20
    public function testNotScheduled()
21
    {
22
        $report = $this->getInvalidReport();
23
24
        $report->setAsNotScheduled();
25
26
        $this->assertFalse($report->isScheduled());
27
        $this->assertNull($report->getToAddress());
0 ignored issues
show
Are you sure the usage of $report->getToAddress() targeting Mautic\ReportBundle\Entity\Report::getToAddress() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
28
        $this->assertNull($report->getScheduleUnit());
0 ignored issues
show
Are you sure the usage of $report->getScheduleUnit() targeting Mautic\ReportBundle\Enti...port::getScheduleUnit() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
29
        $this->assertNull($report->getScheduleDay());
0 ignored issues
show
Are you sure the usage of $report->getScheduleDay() targeting Mautic\ReportBundle\Enti...eport::getScheduleDay() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
30
        $this->assertNull($report->getScheduleMonthFrequency());
0 ignored issues
show
Are you sure the usage of $report->getScheduleMonthFrequency() targeting Mautic\ReportBundle\Enti...cheduleMonthFrequency() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
31
    }
32
33
    public function testDailyScheduled()
34
    {
35
        $report = $this->getInvalidReport();
36
37
        $report->ensureIsDailyScheduled();
38
39
        $this->assertTrue($report->isScheduled());
40
        $this->assertSame('xxx', $report->getToAddress());
41
        $this->assertSame('DAILY', $report->getScheduleUnit());
42
        $this->assertNull($report->getScheduleDay());
0 ignored issues
show
Are you sure the usage of $report->getScheduleDay() targeting Mautic\ReportBundle\Enti...eport::getScheduleDay() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
43
        $this->assertNull($report->getScheduleMonthFrequency());
0 ignored issues
show
Are you sure the usage of $report->getScheduleMonthFrequency() targeting Mautic\ReportBundle\Enti...cheduleMonthFrequency() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
44
    }
45
46
    public function testWeeklyScheduled()
47
    {
48
        $report = $this->getInvalidReport();
49
        $report->setScheduleDay('WEEK_DAYS');
50
51
        $report->ensureIsWeeklyScheduled();
52
        $this->assertTrue($report->isScheduled());
53
        $this->assertSame('xxx', $report->getToAddress());
54
        $this->assertSame(SchedulerEnum::UNIT_WEEKLY, $report->getScheduleUnit());
55
        $this->assertSame(SchedulerEnum::DAY_WEEK_DAYS, $report->getScheduleDay());
56
        $this->assertNull($report->getScheduleMonthFrequency());
57
    }
58
59
    public function testMonthlyScheduled()
60
    {
61
        $report = $this->getInvalidReport();
62
        $report->setScheduleDay('WEEK_DAYS');
63
        $report->setScheduleMonthFrequency('-1');
64
65
        $report->ensureIsMonthlyScheduled();
66
        $this->assertTrue($report->isScheduled());
67
        $this->assertSame('xxx', $report->getToAddress());
68
        $this->assertSame(SchedulerEnum::UNIT_MONTHLY, $report->getScheduleUnit());
69
        $this->assertSame(SchedulerEnum::DAY_WEEK_DAYS, $report->getScheduleDay());
70
        $this->assertSame(SchedulerEnum::MONTH_FREQUENCY_LAST, $report->getScheduleMonthFrequency());
71
    }
72
73
    public function testInvalidMonthlyScheduled()
74
    {
75
        $this->expectException(ScheduleNotValidException::class);
76
77
        $report = $this->getInvalidReport();
78
        $report->ensureIsMonthlyScheduled();
79
    }
80
81
    public function testInvalidWeeklyScheduled()
82
    {
83
        $this->expectException(ScheduleNotValidException::class);
84
85
        $report = $this->getInvalidReport();
86
        $report->ensureIsWeeklyScheduled();
87
    }
88
89
    public function testGetFilterValueIfFIltersAreEmpty()
90
    {
91
        $report = $this->getInvalidReport();
92
93
        $this->expectException(\UnexpectedValueException::class);
94
        $this->assertSame('1234', $report->getFilterValue('e.test'));
95
    }
96
97
    public function testGetFilterValueIfExists()
98
    {
99
        $report = $this->getInvalidReport();
100
        $report->setFilters([
101
            [
102
                'column' => 'e.test',
103
                'value'  => '1234',
104
            ],
105
        ]);
106
107
        $this->assertSame('1234', $report->getFilterValue('e.test'));
108
    }
109
110
    public function testGetFilterValueIfDoesNotExist()
111
    {
112
        $report = $this->getInvalidReport();
113
        $report->setFilters([
114
            [
115
                'column' => 'e.test',
116
                'value'  => '1234',
117
            ],
118
        ]);
119
120
        $this->expectException(\UnexpectedValueException::class);
121
        $report->getFilterValue('I need coffee');
122
    }
123
124
    public function testSetAsScheduledNow()
125
    {
126
        $email  = '[email protected]';
127
        $report = new Report();
128
        $report->setAsScheduledNow($email);
129
130
        $this->assertTrue($report->isScheduled());
131
        $this->assertSame($email, $report->getToAddress());
132
        $this->assertSame(SchedulerEnum::UNIT_NOW, $report->getScheduleUnit());
133
    }
134
135
    public function testIsScheduledNowIfNot()
136
    {
137
        $report = new Report();
138
139
        $this->assertFalse($report->isScheduledNow());
140
141
        $report->setScheduleUnit(SchedulerEnum::UNIT_NOW);
142
143
        $this->assertTrue($report->isScheduledNow());
144
    }
145
146
    /**
147
     * @return Report
148
     */
149
    private function getInvalidReport()
150
    {
151
        $report = new Report();
152
        $report->setIsScheduled(true);
153
        $report->setToAddress('xxx');
154
        $report->setScheduleUnit('unit');
155
        $report->setScheduleDay('day');
156
        $report->setScheduleMonthFrequency('frequency');
157
158
        return $report;
159
    }
160
}
161