File size: 3,978 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

declare(strict_types=1);

namespace Mautic\IntegrationsBundle\Tests\Unit\Helper;

use Mautic\IntegrationsBundle\Exception\IntegrationNotFoundException;
use Mautic\IntegrationsBundle\Helper\BuilderIntegrationsHelper;
use Mautic\IntegrationsBundle\Helper\IntegrationsHelper;
use Mautic\IntegrationsBundle\Integration\Interfaces\BuilderInterface;
use Mautic\PluginBundle\Entity\Integration;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class BuilderIntegrationsHelperTest extends TestCase
{
    /**
     * @var IntegrationsHelper|MockObject
     */
    private MockObject $integrationsHelper;

    private BuilderIntegrationsHelper $builderIntegrationsHelper;

    protected function setUp(): void
    {
        $this->integrationsHelper        = $this->createMock(IntegrationsHelper::class);
        $this->builderIntegrationsHelper = new BuilderIntegrationsHelper($this->integrationsHelper);
    }

    public function testBuilderNotFoundIfFeatureSupportedButNotEnabled(): void
    {
        $builder     = $this->createMock(BuilderInterface::class);
        $integration = new Integration();

        $builder->expects($this->once())
            ->method('isSupported')
            ->with('page')
            ->willReturn(true);

        $builder->expects($this->once())
            ->method('getIntegrationConfiguration')
            ->willReturn($integration);

        $this->builderIntegrationsHelper->addIntegration($builder);

        $this->expectException(IntegrationNotFoundException::class);

        $this->builderIntegrationsHelper->getBuilder('page');
    }

    public function testBuilderNotFoundIfFeatureIsNotSupported(): void
    {
        $builder = $this->createMock(BuilderInterface::class);
        $builder->expects($this->once())
            ->method('isSupported')
            ->with('page')
            ->willReturn(false);

        $builder->expects($this->never())
            ->method('getIntegrationConfiguration');

        $this->builderIntegrationsHelper->addIntegration($builder);

        $this->expectException(IntegrationNotFoundException::class);

        $this->builderIntegrationsHelper->getBuilder('page');
    }

    public function testBuilderFoundIfFeatureIsSupportedAndBuilderEnabled(): void
    {
        $builder = $this->createMock(BuilderInterface::class);

        $integration = new Integration();
        $integration->setIsPublished(true);

        $builder->expects($this->once())
            ->method('isSupported')
            ->with('page')
            ->willReturn(true);

        $builder->expects($this->once())
            ->method('getIntegrationConfiguration')
            ->willReturn($integration);

        $this->builderIntegrationsHelper->addIntegration($builder);

        $foundBuilder = $this->builderIntegrationsHelper->getBuilder('page');

        Assert::assertSame($builder, $foundBuilder);
    }

    public function testBuilderNamesAreReturned(): void
    {
        $builder1 = $this->createMock(BuilderInterface::class);
        $builder1->expects($this->exactly(2))
            ->method('getName')
            ->willReturn('builder1');
        $builder1->expects($this->once())
            ->method('getDisplayName')
            ->willReturn('Builder One');
        $this->builderIntegrationsHelper->addIntegration($builder1);

        $builder2 = $this->createMock(BuilderInterface::class);
        $builder2->expects($this->exactly(2))
            ->method('getName')
            ->willReturn('builder2');
        $builder2->expects($this->once())
            ->method('getDisplayName')
            ->willReturn('Builder Two');
        $this->builderIntegrationsHelper->addIntegration($builder2);

        Assert::assertSame(
            [
                'builder1' => 'Builder One',
                'builder2' => 'Builder Two',
            ],
            $this->builderIntegrationsHelper->getBuilderNames()
        );
    }
}