1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the IrishDan\ResponsiveImageBundle package. |
4
|
|
|
* |
5
|
|
|
* (c) Daniel Byrne <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE file that was distributed with this source |
8
|
|
|
* code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace IrishDan\ResponsiveImageBundle\Tests\Validator\Constraints; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use IrishDan\ResponsiveImageBundle\Validator\Constraints\CropFocusCoordinates; |
15
|
|
|
use IrishDan\ResponsiveImageBundle\Validator\Constraints\CropFocusCoordinatesValidator; |
16
|
|
|
use Symfony\Component\Validator\Constraints\NotNull; |
17
|
|
|
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
18
|
|
|
|
19
|
|
|
class CropFocusCoordinatesValidatorTest extends ConstraintValidatorTestCase |
20
|
|
|
{ |
21
|
|
|
protected function setUp() |
22
|
|
|
{ |
23
|
|
|
$this->group = 'MyGroup'; |
24
|
|
|
$this->metadata = null; |
25
|
|
|
$this->object = null; |
26
|
|
|
$this->value = 'InvalidValue'; |
27
|
|
|
$this->root = 'root'; |
28
|
|
|
$this->propertyPath = 'property.path'; |
29
|
|
|
|
30
|
|
|
// Initialize the context with some constraint so that we can |
31
|
|
|
// successfully build a violation. |
32
|
|
|
$this->constraint = new NotNull(); |
33
|
|
|
|
34
|
|
|
$this->context = $this->createContext(); |
35
|
|
|
$this->validator = $this->createValidator(); |
36
|
|
|
$this->validator->initialize($this->context); |
37
|
|
|
|
38
|
|
|
$this->setDefaultTimezone('UTC'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function createValidator() |
42
|
|
|
{ |
43
|
|
|
return new CropFocusCoordinatesValidator(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testValidCoordinates() |
47
|
|
|
{ |
48
|
|
|
$this->validator->validate('10, 20, 80, 90: 11, 21, 79, 90', new CropFocusCoordinates()); |
49
|
|
|
|
50
|
|
|
$this->assertNoViolation(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function testNullIsInValid() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$this->validator->validate(null, new CropFocusCoordinates()); |
56
|
|
|
|
57
|
|
|
$this->assertSame( |
58
|
|
|
1, |
59
|
|
|
$violationsCount = count($this->context->getViolations()), |
60
|
|
|
sprintf('0 violation expected. Got %u.', $violationsCount) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
public function testFocusIsLargerThanCrop() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$this->validator->validate('10, 20, 80, 90: 9, 21, 79, 90', new CropFocusCoordinates()); |
67
|
|
|
|
68
|
|
|
$this->assertSame( |
69
|
|
|
1, |
70
|
|
|
$violationsCount = count($this->context->getViolations()), |
71
|
|
|
sprintf('0 violation expected. Got %u.', $violationsCount) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function testNoFocusIsSet() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
// @TODO: We need to allow for no focus to be set |
78
|
|
|
$this->validator->validate('10, 20, 80, 90:', new CropFocusCoordinates()); |
79
|
|
|
|
80
|
|
|
$this->assertSame( |
81
|
|
|
1, |
82
|
|
|
$violationsCount = count($this->context->getViolations()), |
83
|
|
|
sprintf('0 violation expected. Got %u.', $violationsCount) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
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.