Completed
Push — staging ( b71873...14487f )
by
unknown
10:05 queued 09:56
created

testBuildQueryForGetLeadsByFieldValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 0
dl 0
loc 23
rs 9.7998
c 0
b 0
f 0
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\LeadBundle\Tests\Entity;
13
14
use Mautic\CoreBundle\Test\Doctrine\DBALMocker;
15
use Mautic\LeadBundle\Entity\CustomFieldRepositoryTrait;
16
use Mautic\LeadBundle\Entity\Lead;
17
use Mautic\LeadBundle\Entity\LeadRepository;
18
19
class LeadRepositoryTest extends \PHPUnit_Framework_TestCase
20
{
21
    public function testBooleanWithPrepareDbalFieldsForSave()
22
    {
23
        $trait  = $this->getMockForTrait(CustomFieldRepositoryTrait::class);
24
        $fields = [
25
            'true'   => true,
26
            'false'  => false,
27
            'string' => 'blah',
28
        ];
29
30
        $reflection = new \ReflectionObject($trait);
31
        $method     = $reflection->getMethod('prepareDbalFieldsForSave');
32
        $method->setAccessible(true);
33
        $method->invokeArgs($trait, [&$fields]);
34
35
        $this->assertEquals(1, $fields['true']);
36
        $this->assertEquals(0, $fields['false']);
37
        $this->assertEquals('blah', $fields['string']);
38
    }
39
40
    /**
41
     * Ensure that the emails are bound separately as parameters according to
42
     * https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/query-builder.html#line-number-0a267d5a2c69797a7656aae33fcc140d16b0a566-72.
43
     */
44
    public function testBuildQueryForGetLeadsByFieldValue()
45
    {
46
        defined('MAUTIC_TABLE_PREFIX') or define('MAUTIC_TABLE_PREFIX', 'mtc_');
47
48
        $dbalMock = new DBALMocker($this);
49
50
        $mock = $this->getMockBuilder(LeadRepository::class)
51
            ->disableOriginalConstructor()
52
            ->setMethods(['getEntityManager'])
53
            ->getMock();
54
55
        $mock->method('getEntityManager')
56
            ->will($this->returnValue($dbalMock->getMockEm()));
57
58
        $reflection = new \ReflectionClass(LeadRepository::class);
59
        $refMethod  = $reflection->getMethod('buildQueryForGetLeadsByFieldValue');
60
        $refMethod->setAccessible(true);
61
62
        $refMethod->invoke($mock, 'email', ['[email protected]', '[email protected]']);
63
64
        $parameters = $dbalMock->getQueryPart('parameters');
65
66
        $this->assertCount(2, $parameters, 'There should be two parameters bound because that\'s the number of emails we passed into the method.');
67
    }
68
69
    /**
70
     * Ensure that the array_combine return value matches the old style.
71
     */
72
    public function testGetLeadsByFieldValueArrayMapReturn()
73
    {
74
        $mock = $this->getMockBuilder(LeadRepository::class)
75
            ->disableOriginalConstructor()
76
            ->setMethods(['getEntities', 'buildQueryForGetLeadsByFieldValue'])
77
            ->getMock();
78
79
        // Mock the
80
        $mockEntity = $this->getMockBuilder(Lead::class)
81
            ->disableOriginalConstructor()
82
            ->setMethods(['loadMetadata'])
83
            ->getMock();
84
85
        $mockEntity->setEmail('[email protected]');
86
87
        $mockEntity2 = clone $mockEntity;
88
        $mockEntity2->setEmail('[email protected]');
89
90
        $entities = [
91
            $mockEntity,
92
            $mockEntity2,
93
        ];
94
95
        $mock->method('getEntities')
96
            ->will($this->returnValue($entities));
97
98
        $mock->method('buildQueryForGetLeadsByFieldValue')
99
            ->will($this->returnValue(null));
100
101
        $contacts = $mock->getLeadsByFieldValue('email', ['[email protected]', '[email protected]']);
102
103
        $this->assertSame($entities, $contacts, 'When getting leads without indexing by column, it should match the expected result.');
104
105
        $contacts = $mock->getLeadsByFieldValue('email', ['[email protected]', '[email protected]'], null, true);
106
107
        $expected = [
108
            '[email protected]',
109
            '[email protected]',
110
        ];
111
112
        $this->assertSame($expected, array_keys($contacts), 'When getting leads with indexing by column, it should match the expected result.');
113
    }
114
}
115