|
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() |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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
|
|
|
|
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.