File size: 1,973 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
<?php

namespace Mautic\LeadBundle\Tests\Entity;

use Mautic\LeadBundle\Entity\CompanyLeadRepository;
use Mautic\LeadBundle\Exception\PrimaryCompanyNotFoundException;

class CompanyLeadRepositoryTest extends \PHPUnit\Framework\TestCase
{
    /** @var \PHPUnit\Framework\MockObject\MockObject|CompanyLeadRepository */
    private $repoMock;

    public function setUp(): void
    {
        parent::setUp();
        $this->repoMock = $this->getMockBuilder(CompanyLeadRepository::class)
            ->onlyMethods(['getCompaniesByLeadId'])
            ->disableOriginalConstructor()
            ->getMock();
    }

    public function testGetPrimaryCompanyByLeadIdThrowsExceptionIfPrimaryIsMissing(): void
    {
        $this->repoMock->expects($this->once())
            ->method('getCompaniesByLeadId')
            ->willReturn([
                [
                    'company_name' => 'ACME #1',
                    'is_primary'   => false,
                ],
            ]);

        $this->expectException(PrimaryCompanyNotFoundException::class);
        $this->repoMock->getPrimaryCompanyByLeadId(1);
    }

    public function testGetPrimaryCompanyByLeadIdReturnsCorrectRecord(): void
    {
        $this->repoMock->expects($this->once())
            ->method('getCompaniesByLeadId')
            ->willReturn([
                [
                    'company_name' => 'ACME #1',
                    'is_primary'   => false,
                ],
                [
                    'company_name' => 'ACME #2',
                    'is_primary'   => true,
                ],
                [
                    'company_name' => 'ACME #3',
                    'is_primary'   => false,
                ],
            ]);

        $primary = $this->repoMock->getPrimaryCompanyByLeadId(1);

        $this->assertEquals(
            [
                'company_name' => 'ACME #2',
                'is_primary'   => true,
            ],
            $primary
        );
    }
}