Failed Conditions
Pull Request — master (#790)
by Guilherme
05:12
created

PersonSerializeEventListenerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 4
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\PhoneVerificationBundle\Tests\Event;
12
13
use libphonenumber\PhoneNumberUtil;
14
use LoginCidadao\CoreBundle\Entity\Person;
15
use LoginCidadao\PhoneVerificationBundle\Entity\PhoneVerification;
16
use LoginCidadao\PhoneVerificationBundle\Event\PersonSerializeEventListener;
17
use LoginCidadao\PhoneVerificationBundle\Service\PhoneVerificationService;
18
use PHPUnit\Framework\TestCase;
19
20
class PersonSerializeEventListenerTest extends TestCase
21
{
22
    /**
23
     * @throws \libphonenumber\NumberParseException
24
     */
25
    public function testOnPreSerialize()
26
    {
27
        $phoneVerificationService = $this->getPhoneVerificationService();
28
29
        $eventListener = new PersonSerializeEventListener($phoneVerificationService);
30
31
        $person = new Person();
32
        $person->setMobile(PhoneNumberUtil::getInstance()->parse('+5551999999999', 'BR'));
33
34
        $event = $this->getMockBuilder('JMS\Serializer\EventDispatcher\PreSerializeEvent')
35
            ->disableOriginalConstructor()
36
            ->getMock();
37
        $event->expects($this->once())->method('getObject')->willReturn($person);
38
        $eventListener->onPreSerialize($event);
39
40
        $this->assertTrue($person->getPhoneNumberVerified());
41
    }
42
43
    public function testNoPhone()
44
    {
45
        $phoneVerificationService = $this->getPhoneVerificationService();
46
47
        $eventListener = new PersonSerializeEventListener($phoneVerificationService);
48
49
        $person = new Person();
50
51
        $event = $this->getMockBuilder('JMS\Serializer\EventDispatcher\PreSerializeEvent')
52
            ->disableOriginalConstructor()
53
            ->getMock();
54
        $event->expects($this->once())->method('getObject')->willReturn($person);
55
        $eventListener->onPreSerialize($event);
56
57
        $this->assertFalse($person->getPhoneNumberVerified());
58
    }
59
60
    public function testNotPerson()
61
    {
62
        $phoneVerificationService = $this->getPhoneVerificationService();
63
64
        $eventListener = new PersonSerializeEventListener($phoneVerificationService);
65
66
        $event = $this->getMockBuilder('JMS\Serializer\EventDispatcher\PreSerializeEvent')
67
            ->disableOriginalConstructor()
68
            ->getMock();
69
        $event->expects($this->once())->method('getObject')->willReturn(new \DateTime());
70
        $eventListener->onPreSerialize($event);
71
    }
72
73
    /**
74
     * @return PhoneVerificationService
75
     */
76
    private function getPhoneVerificationService()
77
    {
78
        $phoneVerificationServiceClass = 'LoginCidadao\PhoneVerificationBundle\Service\PhoneVerificationServiceInterface';
79
        $phoneVerificationService = $this->createMock($phoneVerificationServiceClass);
80
81
        $phoneVerificationService->expects($this->any())->method('getPhoneVerification')
82
            ->willReturnCallback(
83
                function ($person, $phone) {
84
                    $phoneVerification = new PhoneVerification();
85
                    $phoneVerification->setPerson($person)
86
                        ->setPhone($phone)
87
                        ->setVerificationCode('123456')
88
                        ->setVerifiedAt(new \DateTime());
89
90
                    return $phoneVerification;
91
                }
92
            );
93
94
        return $phoneVerificationService;
95
    }
96
}
97