Passed
Branch master (022a1c)
by Darwin
03:39
created

UniqueValidatorTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 34.25 %

Importance

Changes 0
Metric Value
dl 25
loc 73
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 15 2
A testWrongConstraint() 0 4 1
A testNoViolationsOnUniqueUserProperty() 11 11 1
A testBadType() 0 4 1
A testViolationsOnDuplicateUserProperty() 12 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
    public function testViolationsOnDuplicateUserProperty()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testNoViolationsOnUniqueUserProperty()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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