Issues (3627)

Tests/EventListener/ReportSubscriberTest.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\FormBundle\Tests\EventListener;
13
14
use Doctrine\DBAL\Query\QueryBuilder;
15
use Mautic\CoreBundle\Helper\Chart\ChartQuery;
16
use Mautic\CoreBundle\Translation\Translator;
17
use Mautic\FormBundle\Entity\SubmissionRepository;
18
use Mautic\FormBundle\EventListener\ReportSubscriber;
19
use Mautic\LeadBundle\Model\CompanyReportData;
20
use Mautic\ReportBundle\Event\ReportBuilderEvent;
21
use Mautic\ReportBundle\Event\ReportGeneratorEvent;
22
use Mautic\ReportBundle\Event\ReportGraphEvent;
23
use PHPUnit\Framework\TestCase;
24
25
class ReportSubscriberTest extends TestCase
26
{
27
    private $submissionRepository;
28
    private $companyReportData;
29
30
    /**
31
     * @var ReportSubscriber
32
     */
33
    private $subscriber;
34
35
    public function setUp(): void
36
    {
37
        parent::setUp();
38
        defined('MAUTIC_TABLE_PREFIX') or define('MAUTIC_TABLE_PREFIX', '');
39
40
        $this->companyReportData    = $this->createMock(CompanyReportData::class);
41
        $this->submissionRepository = $this->createMock(SubmissionRepository::class);
42
        $this->subscriber           = new ReportSubscriber($this->companyReportData, $this->submissionRepository);
43
    }
44
45
    public function testOnReportBuilderAddsFormAndFormSubmissionReports()
46
    {
47
        $mockEvent = $this->getMockBuilder(ReportBuilderEvent::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

47
        $mockEvent = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(ReportBuilderEvent::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
48
            ->disableOriginalConstructor()
49
            ->setMethods([
50
                'checkContext',
51
                'addGraph',
52
                'getStandardColumns',
53
                'getCategoryColumns',
54
                'getCampaignByChannelColumns',
55
                'getLeadColumns',
56
                'getIpColumn',
57
                'addTable',
58
            ])
59
            ->getMock();
60
61
        $mockEvent->expects($this->once())
62
            ->method('getStandardColumns')
63
            ->willReturn([]);
64
65
        $mockEvent->expects($this->once())
66
            ->method('getCategoryColumns')
67
            ->willReturn([]);
68
69
        $mockEvent->expects($this->once())
70
            ->method('getCampaignByChannelColumns')
71
            ->willReturn([]);
72
73
        $mockEvent->expects($this->once())
74
            ->method('getLeadColumns')
75
            ->willReturn([]);
76
77
        $mockEvent->expects($this->once())
78
            ->method('getIpColumn')
79
            ->willReturn([]);
80
81
        $mockEvent->expects($this->exactly(2))
82
            ->method('checkContext')
83
            ->willReturn(true);
84
85
        $setTables = [];
86
        $setGraphs = [];
87
88
        $mockEvent->expects($this->exactly(2))
89
            ->method('addTable')
90
            ->willReturnCallback(function () use (&$setTables) {
91
                $args = func_get_args();
92
93
                $setTables[] = $args;
94
            });
95
96
        $mockEvent->expects($this->exactly(3))
97
            ->method('addGraph')
98
            ->willReturnCallback(function () use (&$setGraphs) {
99
                $args = func_get_args();
100
101
                $setGraphs[] = $args;
102
            });
103
104
        $this->companyReportData->expects($this->once())
105
            ->method('getCompanyData')
106
            ->with()
107
            ->willReturn([]);
108
109
        $this->subscriber->onReportBuilder($mockEvent);
110
111
        $this->assertCount(2, $setTables);
112
        $this->assertCount(3, $setGraphs);
113
    }
114
115
    public function testOnReportGenerateFormsContext()
116
    {
117
        $mockQueryBuilder = $this->createMock(QueryBuilder::class);
118
        $mockEvent        = $this->getMockBuilder(ReportGeneratorEvent::class)
119
            ->disableOriginalConstructor()
120
            ->setMethods([
121
                'getContext',
122
                'getQueryBuilder',
123
                'addCategoryLeftJoin',
124
                'setQueryBuilder',
125
            ])
126
            ->getMock();
127
128
        $mockQueryBuilder->expects($this->once())
129
            ->method('from')
130
            ->willReturn($mockQueryBuilder);
131
132
        $mockEvent->expects($this->once())
133
            ->method('getQueryBuilder')
134
            ->willReturn($mockQueryBuilder);
135
136
        $mockEvent->expects($this->once())
137
            ->method('getContext')
138
            ->willReturn('forms');
139
140
        $this->subscriber->onReportGenerate($mockEvent);
141
    }
142
143
    public function testOnReportGenerateFormSubmissionContext()
144
    {
145
        $mockQueryBuilder = $this->createMock(QueryBuilder::class);
146
        $mockEvent        = $this->getMockBuilder(ReportGeneratorEvent::class)
147
            ->disableOriginalConstructor()
148
            ->setMethods([
149
                'getContext',
150
                'getQueryBuilder',
151
                'addCategoryLeftJoin',
152
                'addIpAddressLeftJoin',
153
                'addLeadLeftJoin',
154
                'addCampaignByChannelJoin',
155
                'applyDateFilters',
156
                'setQueryBuilder',
157
            ])
158
            ->getMock();
159
160
        $mockQueryBuilder->expects($this->once())
161
            ->method('from')
162
            ->willReturn($mockQueryBuilder);
163
164
        $mockQueryBuilder->expects($this->exactly(2))
165
            ->method('leftJoin')
166
            ->willReturn($mockQueryBuilder);
167
168
        $mockEvent->expects($this->once())
169
            ->method('getQueryBuilder')
170
            ->willReturn($mockQueryBuilder);
171
172
        $mockEvent->expects($this->once())
173
            ->method('getContext')
174
            ->willReturn('form.submissions');
175
176
        $this->subscriber->onReportGenerate($mockEvent);
177
    }
178
179
    public function testOnReportGraphGenerateBadContextWillReturn()
180
    {
181
        $mockEvent = $this->createMock(ReportGraphEvent::class);
182
183
        $mockEvent->expects($this->once())
184
            ->method('checkContext')
185
            ->willReturn(false);
186
187
        $mockEvent->expects($this->never())
188
            ->method('getRequestedGraphs');
189
190
        $this->subscriber->onReportGraphGenerate($mockEvent);
191
    }
192
193
    public function testOnReportGraphGenerate()
194
    {
195
        $mockEvent        = $this->createMock(ReportGraphEvent::class);
196
        $mockTrans        = $this->createMock(Translator::class);
197
        $mockQueryBuilder = $this->createMock(QueryBuilder::class);
198
        $mockChartQuery   = $this->createMock(ChartQuery::class);
199
200
        $mockTrans->expects($this->any())
201
            ->method('trans')
202
            ->willReturnArgument(0);
203
204
        $mockEvent->expects($this->once())
205
            ->method('getQueryBuilder')
206
            ->willReturn($mockQueryBuilder);
207
208
        $mockChartQuery->expects($this->any())
209
            ->method('loadAndBuildTimeData')
210
            ->willReturn(['a', 'b', 'c']);
211
212
        $mockChartQuery->expects($this->any())
213
            ->method('fetchCount')
214
            ->willReturn(2);
215
216
        $mockChartQuery->expects($this->any())
217
            ->method('fetchCountDateDiff')
218
            ->willReturn(2);
219
220
        $graphOptions = [
221
            'chartQuery' => $mockChartQuery,
222
            'translator' => $mockTrans,
223
            'dateFrom'   => new \DateTime(),
224
            'dateTo'     => new \DateTime(),
225
        ];
226
227
        $mockEvent->expects($this->once())
228
            ->method('checkContext')
229
            ->willReturn(true);
230
231
        $mockEvent->expects($this->any())
232
            ->method('getOptions')
233
            ->willReturn($graphOptions);
234
235
        $mockEvent->expects($this->once())
236
            ->method('getRequestedGraphs')
237
            ->willReturn(
238
                [
239
                    'mautic.form.graph.line.submissions',
240
                    'mautic.form.table.top.referrers',
241
                    'mautic.form.table.most.submitted',
242
                ]
243
            );
244
245
        $this->submissionRepository->expects($this->once())
246
            ->method('getTopReferrers')
247
            ->willReturn(['a', 'b', 'c']);
248
249
        $this->submissionRepository->expects($this->once())
250
            ->method('getMostSubmitted')
251
            ->willReturn(['a', 'b', 'c']);
252
253
        $this->subscriber->onReportGraphGenerate($mockEvent);
254
    }
255
}
256