File size: 2,611 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
82
83
84
85
86
<?php

declare(strict_types=1);

namespace Mautic\LeadBundle\Tests\Functional\Entity;

use Mautic\CoreBundle\Entity\IpAddress;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\LeadBundle\Entity\Lead;
use Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector;
use Symfony\Component\HttpFoundation\Request;

final class LeadRepositoryTest extends MauticMysqlTestCase
{
    public function setUp(): void
    {
        $this->clientOptions = ['debug' => true];

        parent::setUp();
    }

    /**
     * @return array<int, array<string, array<string, bool>>>
     */
    public static function joinIpAddressesProvider(): array
    {
        return [
            ['' => []],
            ['' => ['joinIpAddresses' => true]],
            ['' => ['joinIpAddresses' => false]],
        ];
    }

    /**
     * @dataProvider joinIpAddressesProvider
     *
     * @param array<string, bool> $args
     */
    public function testSaveIpAddressToContacts($args): void
    {
        $contactRepo = $this->em->getRepository(Lead::class);

        $ipRepo = $this->em->getRepository(IpAddress::class);

        $ip      = new IpAddress('127.0.0.1');
        $contact = new Lead();
        $contact->addIpAddress($ip);
        $this->em->persist($contact);
        $this->em->persist($ip);
        $this->em->flush();

        $q       = $contactRepo->getEntitiesOrmQueryBuilder('(CASE WHEN u.id=1 THEN 1 ELSE 2 END) AS HIDDEN ORD', $args);
        $results = $q->getQuery()
        ->getResult();

        /** @var Lead $r */
        foreach ($results as $r) {
            $ipAddresses = $r->getIpAddresses();
            $ipAddress   = $ipAddresses->first();
            $this->assertEquals($ipAddress->getIpAddress(), '127.0.0.1');
        }

        $this->client->enableProfiler();

        $this->client->request(Request::METHOD_GET, '/s/contacts');

        $profile = $this->client->getProfile();
        /** @var DoctrineDataCollector $dbCollector */
        $dbCollector = $profile->getCollector('db');
        $queries     = $dbCollector->getQueries();

        $finalQueries = array_filter(
            $queries['default'],
            fn (array $query) => str_contains($query['sql'], 'SELECT (CASE WHEN t0_.id = 1 THEN 1 ELSE 2 END)')
        );

        foreach ($finalQueries as $query) {
            if ($args['joinIpAddresses'] ?? true) {
                $this->assertStringContainsString('LEFT JOIN test_ip_addresses', $query['sql']);
            } else {
                $this->assertStringNotContainsString('LEFT JOIN test_ip_addresses', $query['sql']);
            }
        }
    }
}