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

declare(strict_types=1);

namespace Mautic\IntegrationsBundle\Tests\Unit\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\QueryBuilder;
use Mautic\CoreBundle\Test\Doctrine\RepositoryConfiguratorTrait;
use Mautic\IntegrationsBundle\Entity\ObjectMapping;
use Mautic\IntegrationsBundle\Entity\ObjectMappingRepository;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

final class ObjectMappingRepositoryTest extends TestCase
{
    use RepositoryConfiguratorTrait;

    /**
     * @var MockObject&AbstractQuery<mixed>
     */
    private MockObject $query;

    private ObjectMappingRepository $repository;

    protected function setUp(): void
    {
        parent::setUp();

        $this->repository = $this->configureRepository(ObjectMapping::class);

        $this->entityManager->method('createQueryBuilder')->willReturnCallback(fn () => new QueryBuilder($this->entityManager));

        // This is terrible, but the Query class is final and AbstractQuery doesn't have some methods used.
        $this->query = $this->getMockBuilder(AbstractQuery::class)
            ->disableOriginalConstructor()
            ->onlyMethods(['setParameters', 'getSingleResult', 'getSQL', '_doExecute'])
            ->addMethods(['setFirstResult', 'setMaxResults'])
            ->getMock();

        $this->query->expects($this->once())
            ->method('setFirstResult')
            ->willReturnSelf();

        $this->query->expects($this->once())
            ->method('setMaxResults')
            ->willReturnSelf();
    }

    public function testDeleteEntitiesForObject(): void
    {
        $this->entityManager->expects($this->once())
            ->method('createQuery')
            ->with('DELETE Mautic\IntegrationsBundle\Entity\ObjectMapping m WHERE m.internalObjectName = :internalObject AND m.internalObjectId = :internalObjectId')
            ->willReturn($this->query);

        $this->query->expects($this->once())
            ->method('setParameters')
            ->with($this->callback(function (ArrayCollection $collection) {
                /** @var Parameter $parameter */
                $parameter = $collection[0];
                $this->assertSame('internalObject', $parameter->getName());
                $this->assertSame('company', $parameter->getValue());

                /** @var Parameter $parameter */
                $parameter = $collection[1];
                $this->assertSame('internalObjectId', $parameter->getName());
                $this->assertSame(123, $parameter->getValue());

                return true;
            }))
            ->willReturnSelf();

        // // Stopping early to avoid Mocking hell. We have what we needed.
        $this->query->expects($this->once())
            ->method('_doExecute')
            ->willReturn(0);

        $this->repository->deleteEntitiesForObject(123, 'company');
    }
}