File size: 3,203 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
87
88
89
90
91
92
93
94
95
<?php

declare(strict_types=1);

namespace Mautic\EmailBundle\Tests\Entity;

use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Query\QueryBuilder;
use Mautic\CoreBundle\Test\Doctrine\RepositoryConfiguratorTrait;
use Mautic\EmailBundle\Entity\Email;
use Mautic\EmailBundle\Entity\EmailRepository;

class EmailRepositoryIncrementReadTest extends \PHPUnit\Framework\TestCase
{
    use RepositoryConfiguratorTrait;

    private QueryBuilder $queryBuilder;

    private QueryBuilder $subQueryBuilder;

    /**
     * @var EmailRepository|object
     */
    private $repo;

    protected function setUp(): void
    {
        parent::setUp();
        $this->repo             = $this->configureRepository(Email::class);
        $this->queryBuilder     = new QueryBuilder($this->connection);
        $this->subQueryBuilder  = new QueryBuilder($this->connection);
        $this->connection->method('createQueryBuilder')->willReturnOnConsecutiveCalls(
            $this->queryBuilder,
            $this->subQueryBuilder
        );
    }

    public function testIncrementRead(): void
    {
        $this->connection
            ->expects($this->exactly(1))
            ->method('executeStatement')
            ->willReturn(1);

        $this->repo->incrementRead(11, 21);
        $generatedSql = $this->queryBuilder->getSQL();

        // Assert that the generated SQL matches our expectations
        $expectedSql = 'UPDATE test_emails e SET read_count = read_count + 1 WHERE (e.id = :emailId) AND (e.id NOT IN (SELECT es.email_id FROM test_email_stats es WHERE (es.id = :statId) AND (es.is_read = 1)))';
        $this->assertEquals($expectedSql, $generatedSql);
    }

    public function testIncrementReadWithVariant(): void
    {
        $this->connection
            ->expects($this->exactly(1))
            ->method('executeStatement')
            ->willReturn(1);

        $this->repo->incrementRead(11, 21, true);
        $generatedSql = $this->queryBuilder->getSQL();

        // Assert that the generated SQL matches our expectations
        $expectedSql = 'UPDATE test_emails e SET read_count = read_count + 1, variant_read_count = variant_read_count + 1 WHERE (e.id = :emailId) AND (e.id NOT IN (SELECT es.email_id FROM test_email_stats es WHERE (es.id = :statId) AND (es.is_read = 1)))';
        $this->assertEquals($expectedSql, $generatedSql);
    }

    public function testUpCountWithTwoErrors(): void
    {
        $this->connection
            ->expects($this->exactly(3))
            ->method('executeStatement')
                ->will(
                    $this->onConsecutiveCalls(
                        $this->throwException(new DBALException()),
                        $this->throwException(new DBALException()),
                        1
                    )
                );

        $this->repo->incrementRead(45, 616);
    }

    public function testUpCountWithFourErrors(): void
    {
        $this->connection
            ->expects($this->exactly(3))
            ->method('executeStatement')
            ->will($this->throwException(new DBALException()));

        $this->expectException(DBALException::class);
        $this->repo->incrementRead(45, 616);
    }
}