Issues (3627)

Tests/Entity/CampaignRepositoryTest.php (3 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * @copyright   2020 Mautic Contributors. All rights reserved
7
 * @author      Mautic
8
 *
9
 * @link        https://mautic.org
10
 *
11
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Mautic\CampaignBundle\Tests\Entity;
15
16
use Doctrine\DBAL\Connection;
17
use Doctrine\DBAL\Query\QueryBuilder;
18
use Doctrine\ORM\AbstractQuery;
19
use Doctrine\ORM\EntityManager;
20
use Doctrine\ORM\Mapping\ClassMetadata;
21
use Doctrine\ORM\Query;
22
use Mautic\CampaignBundle\Entity\CampaignRepository;
23
use PHPUnit\Framework\MockObject\MockObject;
24
use PHPUnit\Framework\TestCase;
25
26
class CampaignRepositoryTest extends TestCase
27
{
28
    /**
29
     * @var EntityManager|MockObject
30
     */
31
    private $entityManager;
32
33
    /**
34
     * @var ClassMetadata|MockObject
35
     */
36
    private $classMetadata;
37
38
    /**
39
     * @var Connection|MockObject
40
     */
41
    private $connection;
42
43
    /**
44
     * @var CampaignRepository
45
     */
46
    private $repository;
47
48
    protected function setUp(): void
49
    {
50
        parent::setUp();
51
52
        defined('MAUTIC_TABLE_PREFIX') or define('MAUTIC_TABLE_PREFIX', '');
53
54
        $this->entityManager = $this->createMock(EntityManager::class);
55
        $this->classMetadata = $this->createMock(ClassMetadata::class);
56
        $this->connection    = $this->createMock(Connection::class);
57
        $this->repository    = new CampaignRepository($this->entityManager, $this->classMetadata);
58
    }
59
60
    public function testFetchEmailIdsById(): void
61
    {
62
        $id          = 2;
63
        $queryResult = [
64
            1 => [
65
                'channelId' => 1,
66
            ],
67
            2 => [
68
                'channelId' => 2,
69
            ],
70
        ];
71
72
        $expectedResult = [
73
            1,
74
            2,
75
        ];
76
77
        $queryBuilder = $this->getMockBuilder(QueryBuilder::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
        $queryBuilder = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(QueryBuilder::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
78
            ->disableOriginalConstructor()
79
            ->setMethods(['select', 'from', 'where', 'setParameter', 'andWhere', 'getQuery'])
80
            ->getMock();
81
82
        $this->entityManager
83
            ->method('createQueryBuilder')
0 ignored issues
show
The method method() does not exist on Doctrine\ORM\EntityManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
            ->/** @scrutinizer ignore-call */ 
84
              method('createQueryBuilder')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
            ->willReturn($queryBuilder);
85
86
        $queryBuilder->expects(self::once())
87
            ->method('select')
88
            ->with('e.channelId')
89
            ->willReturn($queryBuilder);
90
91
        $queryBuilder->expects(self::once())
92
            ->method('from')
93
            ->with('MauticCampaignBundle:Campaign', $this->repository->getTableAlias(), $this->repository->getTableAlias().'.id')
94
            ->willReturn($queryBuilder);
95
96
        $queryBuilder->expects(self::once())
97
            ->method('where')
98
            ->with($this->repository->getTableAlias().'.id = :id')
99
            ->willReturn($queryBuilder);
100
101
        $queryBuilder->expects(self::once())
102
            ->method('setParameter')
103
            ->with('id', $id)
104
            ->willReturn($queryBuilder);
105
106
        $queryBuilder->expects(self::once())
107
            ->method('andWhere')
108
            ->with('e.channelId IS NOT NULL')
109
            ->willReturn($queryBuilder);
110
111
        $query = $this->getMockBuilder(AbstractQuery::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

111
        $query = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(AbstractQuery::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
112
            ->disableOriginalConstructor()
113
            ->setMethods(['setHydrationMode', 'getResult'])
114
            ->getMockForAbstractClass();
115
116
        $query->expects(self::once())
117
            ->method('setHydrationMode')
118
            ->with(Query::HYDRATE_ARRAY)
119
            ->willReturn($query);
120
121
        $queryBuilder->expects(self::once())
122
            ->method('getQuery')
123
            ->willReturn($query);
124
125
        $query->expects(self::once())
126
            ->method('getResult')
127
            ->willReturn($queryResult);
128
129
        $result = $this->repository->fetchEmailIdsById($id);
130
131
        $this->assertEquals($expectedResult, $result);
132
    }
133
}
134