File size: 2,735 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
<?php

namespace Mautic\LeadBundle\Tests\Field;

use Mautic\CoreBundle\Doctrine\Helper\IndexSchemaHelper;
use Mautic\LeadBundle\Entity\LeadField;
use Mautic\LeadBundle\Field\CustomFieldIndex;
use Mautic\LeadBundle\Field\FieldsWithUniqueIdentifier;
use Monolog\Logger;

final class CustomFieldIndexTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \PHPUnit\Framework\MockObject\MockObject|IndexSchemaHelper
     */
    private $indexSchemaHelperMock;

    /**
     * @var \PHPUnit\Framework\MockObject\MockObject|Logger
     */
    private $loggerMock;

    /**
     * @var \PHPUnit\Framework\MockObject\MockObject|FieldsWithUniqueIdentifier
     */
    private $fieldsWithUniqueIdentifierMock;

    /**
     * @var \PHPUnit\Framework\MockObject\MockObject|LeadField
     */
    private $leadFieldMock;

    /**
     * @var CustomFieldIndex
     */
    private $customFieldIndex;

    protected function setUp(): void
    {
        $this->indexSchemaHelperMock          = $this->createMock(IndexSchemaHelper::class);
        $this->loggerMock                     = $this->createMock(Logger::class);
        $this->fieldsWithUniqueIdentifierMock = $this->createMock(FieldsWithUniqueIdentifier::class);
        $this->customFieldIndex               = new CustomFieldIndex($this->indexSchemaHelperMock, $this->loggerMock, $this->fieldsWithUniqueIdentifierMock);
        $this->leadFieldMock                  = $this->createMock(LeadField::class);
    }

    /**
     * @dataProvider getHasMatchingUniqueIdentifierIndexProvider
     *
     * Test getting unique identifier if object is lead or company.
     */
    public function testHasMatchingUniqueIdentifierIndex(string $object, string $field, string $fieldKey): void
    {
        $this->leadFieldMock->expects($this->once())
            ->method('getObject')
            ->willReturn($object);
        $this->fieldsWithUniqueIdentifierMock->expects($this->once())
            ->method('getLiveFields')
            ->with(['object' => $object])
            ->willReturn([$fieldKey => $field]);
        $this->indexSchemaHelperMock->expects($this->once())
            ->method('hasMatchingUniqueIdentifierIndex')
            ->with($this->leadFieldMock, [$fieldKey])
            ->willReturn(true);
        $this->customFieldIndex->hasMatchingUniqueIdentifierIndex($this->leadFieldMock);
    }

    /**
     * Provides data for testHasMatchingUniqueIdentifierIndex.
     *
     * @return array<mixed>>
     */
    public function getHasMatchingUniqueIdentifierIndexProvider(): array
    {
        return [
            'Lead object'    => ['lead', 'email', 'email_key'],
            'Company object' => ['company', 'company_email', 'company_email_key'],
        ];
    }
}