UniqueValidatorTest::testWrongConstraint()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the FOSUserBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DoL\LdapBundle\Tests\Validation;
13
14
use DoL\LdapBundle\Tests\TestUser;
15
use DoL\LdapBundle\Validator\Unique;
16
use DoL\LdapBundle\Validator\UniqueValidator;
17
use Symfony\Component\Validator\Context\ExecutionContextInterface;
18
19
/**
20
 * @covers \DoL\LdapBundle\Validator\Unique
21
 * @covers \DoL\LdapBundle\Validator\UniqueValidator
22
 */
23
class UniqueValidatorTest extends \PHPUnit_Framework_TestCase
24
{
25
    /** @var UniqueValidator */
26
    private $validator;
27
    /** @var ExecutionContextInterface|\PHPUnit_Framework_MockObject_MockObject */
28
    private $validatorContext;
29
    /** @var \DoL\LdapBundle\Ldap\LdapManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
30
    private $ldapManagerMock;
31
    /** @var Unique */
32
    private $constraint;
33
    /** @var TestUser */
34
    private $user;
35
36
    public function setUp()
37
    {
38
        // SF 2.3 compatibility
39
        if (interface_exists('Symfony\Component\Validator\ExecutionContextInterface')) {
40
            $this->validatorContext = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
41
        } else {
42
            $this->validatorContext = $this->getMock('Symfony\Component\Validator\Context\ExecutionContextInterface');
43
        }
44
45
        $this->ldapManagerMock = $this->getMock('DoL\LdapBundle\Ldap\LdapManagerInterface');
46
        $this->constraint = new Unique();
47
        $this->validator = new UniqueValidator($this->ldapManagerMock);
48
        $this->validator->initialize($this->validatorContext);
49
50
        $this->user = new TestUser();
51
    }
52
53
    public function testViolationsOnDuplicateUserProperty()
54
    {
55
        $this->ldapManagerMock->expects($this->once())
56
            ->method('findUserByUsername')
57
            ->will($this->returnValue($this->user))
58
            ->with($this->equalTo($this->user->getUsername()));
59
60
        $this->validatorContext->expects($this->once())
61
            ->method('addViolation')
62
            ->with('User already exists.');
63
64
        $this->validator->validate($this->user, $this->constraint);
65
    }
66
67
    public function testNoViolationsOnUniqueUserProperty()
68
    {
69
        $this->ldapManagerMock->expects($this->once())
70
            ->method('findUserByUsername')
71
            ->will($this->returnValue(null))
72
            ->with($this->equalTo($this->user->getUsername()));
73
74
        $this->validatorContext->expects($this->never())
75
            ->method('addViolation');
76
77
        $this->validator->validate($this->user, $this->constraint);
78
    }
79
80
    /**
81
     * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
82
     */
83
    public function testBadType()
84
    {
85
        /* @noinspection PhpParamsInspection */
86
        $this->validator->validate('bad_type', $this->constraint);
0 ignored issues
show
Bug introduced by
'bad_type' of type string is incompatible with the type Symfony\Component\Security\Core\User\UserInterface expected by parameter $value of DoL\LdapBundle\Validator...ueValidator::validate(). ( Ignorable by Annotation )

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

86
        $this->validator->validate(/** @scrutinizer ignore-type */ 'bad_type', $this->constraint);
Loading history...
87
    }
88
89
    /**
90
     * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
91
     */
92
    public function testWrongConstraint()
93
    {
94
        /* @noinspection PhpParamsInspection */
95
        $this->validator->validate($this->user, $this->getMock('Symfony\Component\Validator\Constraint'));
96
    }
97
}
98