Issues (3627)

Unit/EventListener/CompanyObjectSubscriberTest.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * @copyright   2019 Mautic, Inc. All rights reserved
7
 * @author      Mautic, Inc.
8
 *
9
 * @link        https://mautic.com
10
 *
11
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Mautic\IntegrationsBundle\Tests\Unit\EventListener;
15
16
use Mautic\IntegrationsBundle\Event\InternalObjectCreateEvent;
17
use Mautic\IntegrationsBundle\Event\InternalObjectEvent;
18
use Mautic\IntegrationsBundle\Event\InternalObjectFindEvent;
19
use Mautic\IntegrationsBundle\Event\InternalObjectOwnerEvent;
20
use Mautic\IntegrationsBundle\Event\InternalObjectRouteEvent;
21
use Mautic\IntegrationsBundle\Event\InternalObjectUpdateEvent;
22
use Mautic\IntegrationsBundle\EventListener\CompanyObjectSubscriber;
23
use Mautic\IntegrationsBundle\IntegrationEvents;
24
use Mautic\IntegrationsBundle\Sync\DAO\DateRange;
25
use Mautic\IntegrationsBundle\Sync\SyncDataExchange\Internal\Object\Company;
26
use Mautic\IntegrationsBundle\Sync\SyncDataExchange\Internal\Object\Contact;
27
use Mautic\IntegrationsBundle\Sync\SyncDataExchange\Internal\ObjectHelper\CompanyObjectHelper;
28
use PHPUnit\Framework\TestCase;
29
use Symfony\Component\Routing\Router;
30
31
class CompanyObjectSubscriberTest extends TestCase
32
{
33
    /**
34
     * @var CompanyObjectHelper|\PHPUnit\Framework\MockObject\MockObject
35
     */
36
    private $companyObjectHelper;
37
38
    /**
39
     * @var Router|\PHPUnit\Framework\MockObject\MockObject
40
     */
41
    private $router;
42
43
    /**
44
     * @var CompanyObjectHelper
45
     */
46
    private $subscriber;
47
48
    public function setUp(): void
49
    {
50
        parent::setUp();
51
52
        $this->companyObjectHelper = $this->createMock(CompanyObjectHelper::class);
53
        $this->router              = $this->createMock(Router::class);
54
        $this->subscriber          = new CompanyObjectSubscriber(
0 ignored issues
show
Documentation Bug introduced by
It seems like new Mautic\IntegrationsB...tHelper, $this->router) of type Mautic\IntegrationsBundl...CompanyObjectSubscriber is incompatible with the declared type Mautic\IntegrationsBundl...per\CompanyObjectHelper of property $subscriber.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
            $this->companyObjectHelper,
56
            $this->router
57
        );
58
    }
59
60
    public function testGetSubscribedEvents(): void
61
    {
62
        $this->assertEquals(
63
            [
64
                IntegrationEvents::INTEGRATION_COLLECT_INTERNAL_OBJECTS => ['collectInternalObjects', 0],
65
                IntegrationEvents::INTEGRATION_UPDATE_INTERNAL_OBJECTS  => ['updateCompanies', 0],
66
                IntegrationEvents::INTEGRATION_CREATE_INTERNAL_OBJECTS  => ['createCompanies', 0],
67
                IntegrationEvents::INTEGRATION_FIND_INTERNAL_RECORDS    => [
68
                    ['findCompaniesByIds', 0],
69
                    ['findCompaniesByDateRange', 0],
70
                    ['findCompaniesByFieldValues', 0],
71
                ],
72
                IntegrationEvents::INTEGRATION_FIND_OWNER_IDS              => ['findOwnerIdsForCompanies', 0],
73
                IntegrationEvents::INTEGRATION_BUILD_INTERNAL_OBJECT_ROUTE => ['buildCompanyRoute', 0],
74
            ],
75
            CompanyObjectSubscriber::getSubscribedEvents()
76
        );
77
    }
78
79
    public function testCollectInternalObjects(): void
80
    {
81
        $event = new InternalObjectEvent();
82
83
        $this->subscriber->collectInternalObjects($event);
84
85
        $this->assertCount(1, $event->getObjects());
86
        $this->assertInstanceOf(
87
            Company::class,
88
            $event->getObjects()[0]
89
        );
90
    }
91
92
    public function testUpdateCompaniesWithWrongObject(): void
93
    {
94
        $event = new InternalObjectUpdateEvent(new Contact(), [], []);
95
96
        $this->companyObjectHelper->expects($this->never())
97
            ->method('update');
98
99
        $this->subscriber->updateCompanies($event);
100
101
        $this->assertSame([], $event->getUpdatedObjectMappings());
102
    }
103
104
    public function testUpdateCompaniesWithRightObject(): void
105
    {
106
        $event = new InternalObjectUpdateEvent(new Company(), [123], [['id' => 345]]);
107
108
        $this->companyObjectHelper->expects($this->once())
109
            ->method('update')
110
            ->with([123], [['id' => 345]])
111
            ->willReturn([['object_mapping_1']]);
112
113
        $this->subscriber->updateCompanies($event);
114
115
        $this->assertSame([['object_mapping_1']], $event->getUpdatedObjectMappings());
116
    }
117
118
    public function testCreateCompaniesWithWrongObject(): void
119
    {
120
        $event = new InternalObjectCreateEvent(new Contact(), []);
121
122
        $this->companyObjectHelper->expects($this->never())
123
            ->method('create');
124
125
        $this->subscriber->createCompanies($event);
126
127
        $this->assertSame([], $event->getObjectMappings());
128
    }
129
130
    public function testCreateCompaniesWithRightObject(): void
131
    {
132
        $event = new InternalObjectCreateEvent(new Company(), [['somefield' => 'somevalue']]);
133
134
        $this->companyObjectHelper->expects($this->once())
135
            ->method('create')
136
            ->with([['somefield' => 'somevalue']])
137
            ->willReturn([['object_mapping_1']]);
138
139
        $this->subscriber->createCompanies($event);
140
141
        $this->assertSame([['object_mapping_1']], $event->getObjectMappings());
142
    }
143
144
    public function testFindCompaniesByIdsWithWrongObject(): void
145
    {
146
        $event = new InternalObjectFindEvent(new Contact());
147
148
        $this->companyObjectHelper->expects($this->never())
149
            ->method('findObjectsByIds');
150
151
        $this->subscriber->findCompaniesByIds($event);
152
153
        $this->assertSame([], $event->getFoundObjects());
154
    }
155
156
    public function testFindCompaniesByIdsWithNoIds(): void
157
    {
158
        $event = new InternalObjectFindEvent(new Company());
159
160
        $this->companyObjectHelper->expects($this->never())
161
            ->method('findObjectsByIds');
162
163
        $this->subscriber->findCompaniesByIds($event);
164
165
        $this->assertSame([], $event->getFoundObjects());
166
    }
167
168
    public function testFindCompaniesByIdsWithRightObject(): void
169
    {
170
        $event = new InternalObjectFindEvent(new Company());
171
172
        $event->setIds([123]);
173
174
        $this->companyObjectHelper->expects($this->once())
175
            ->method('findObjectsByIds')
176
            ->with([123])
177
            ->willReturn([['object_1']]);
178
179
        $this->subscriber->findCompaniesByIds($event);
180
181
        $this->assertSame([['object_1']], $event->getFoundObjects());
182
    }
183
184
    public function testFindCompaniesByDateRangeWithWrongObject(): void
185
    {
186
        $event = new InternalObjectFindEvent(new Contact());
187
188
        $this->companyObjectHelper->expects($this->never())
189
            ->method('findObjectsBetweenDates');
190
191
        $this->subscriber->findCompaniesByDateRange($event);
192
193
        $this->assertSame([], $event->getFoundObjects());
194
    }
195
196
    public function testFindCompaniesByDateRangeWithNoDateRange(): void
197
    {
198
        $event = new InternalObjectFindEvent(new Company());
199
200
        $this->companyObjectHelper->expects($this->never())
201
            ->method('findObjectsBetweenDates');
202
203
        $this->subscriber->findCompaniesByDateRange($event);
204
205
        $this->assertSame([], $event->getFoundObjects());
206
    }
207
208
    public function testFindCompaniesByDateRangeWithRightObject(): void
209
    {
210
        $event     = new InternalObjectFindEvent(new Company());
211
        $fromDate  = new \DateTimeImmutable();
212
        $toDate    = new \DateTimeImmutable();
213
        $dateRange = new DateRange($fromDate, $toDate);
214
        $start     = 0;
215
        $limit     = 10;
216
217
        $event->setDateRange($dateRange);
218
        $event->setStart($start);
219
        $event->setLimit($limit);
220
221
        $this->companyObjectHelper->expects($this->once())
222
            ->method('findObjectsBetweenDates')
223
            ->with(
224
                $fromDate,
225
                $toDate,
226
                $start,
227
                $limit
228
            )
229
            ->willReturn([['object_1']]);
230
231
        $this->subscriber->findCompaniesByDateRange($event);
232
233
        $this->assertSame([['object_1']], $event->getFoundObjects());
234
    }
235
236
    public function testFindCompaniesByFieldValuesWithWrongObject(): void
237
    {
238
        $event = new InternalObjectFindEvent(new Contact());
239
240
        $this->companyObjectHelper->expects($this->never())
241
            ->method('findObjectsByFieldValues');
242
243
        $this->subscriber->findCompaniesByFieldValues($event);
244
245
        $this->assertSame([], $event->getFoundObjects());
246
    }
247
248
    public function testFindCompaniesByFieldValuesWithNoIds(): void
249
    {
250
        $event = new InternalObjectFindEvent(new Company());
251
252
        $this->companyObjectHelper->expects($this->never())
253
            ->method('findObjectsByFieldValues');
254
255
        $this->subscriber->findCompaniesByFieldValues($event);
256
257
        $this->assertSame([], $event->getFoundObjects());
258
    }
259
260
    public function testFindCompaniesByFieldValuesWithRightObject(): void
261
    {
262
        $event = new InternalObjectFindEvent(new Company());
263
264
        $event->setFieldValues(['field_a' => 123]);
265
266
        $this->companyObjectHelper->expects($this->once())
267
            ->method('findObjectsByFieldValues')
268
            ->with(['field_a' => 123])
269
            ->willReturn([['object_1']]);
270
271
        $this->subscriber->findCompaniesByFieldValues($event);
272
273
        $this->assertSame([['object_1']], $event->getFoundObjects());
274
    }
275
276
    public function testFindOwnerIdsForCompaniesWithWrongObject(): void
277
    {
278
        $event = new InternalObjectOwnerEvent(new Contact(), []);
279
280
        $this->companyObjectHelper->expects($this->never())
281
            ->method('findOwnerIds');
282
283
        $this->subscriber->findOwnerIdsForCompanies($event);
284
285
        $this->assertSame([], $event->getOwners());
286
    }
287
288
    public function testFindOwnerIdsForCompaniesWithRightObject(): void
289
    {
290
        $event = new InternalObjectOwnerEvent(new Company(), [567]);
291
292
        $this->companyObjectHelper->expects($this->once())
293
            ->method('findOwnerIds')
294
            ->with([567])
295
            ->willReturn([['object_1']]);
296
297
        $this->subscriber->findOwnerIdsForCompanies($event);
298
299
        $this->assertSame([['object_1']], $event->getOwners());
300
    }
301
302
    public function testBuildCompanyRouteWithWrongObject(): void
303
    {
304
        $event = new InternalObjectRouteEvent(new Contact(), 123);
305
306
        $this->router->expects($this->never())
307
            ->method('generate');
308
309
        $this->subscriber->buildCompanyRoute($event);
310
311
        $this->assertNull($event->getRoute());
312
    }
313
314
    public function testBuildCompanyRouteWithRightObject(): void
315
    {
316
        $event = new InternalObjectRouteEvent(new Company(), 123);
317
318
        $this->router->expects($this->once())
319
            ->method('generate')
320
            ->with(
321
                'mautic_company_action',
322
                [
323
                    'objectAction' => 'view',
324
                    'objectId'     => 123,
325
                ]
326
            )
327
            ->willReturn('some/route');
328
329
        $this->subscriber->buildCompanyRoute($event);
330
331
        $this->assertSame('some/route', $event->getRoute());
332
    }
333
}
334