|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Tests\Unit\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Media; |
|
6
|
|
|
use App\Entity\Performance; |
|
7
|
|
|
use App\Validator\MinSizeSliderImage; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use App\Validator\MinSizeSliderImageValidator; |
|
10
|
|
|
use Symfony\Component\Validator\Context\ExecutionContext; |
|
11
|
|
|
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder; |
|
12
|
|
|
|
|
13
|
|
|
class MinSizeSliderImageValidatorTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
public function testNullSliderIsNotValid() |
|
16
|
|
|
{ |
|
17
|
|
|
$performance = new Performance(); |
|
18
|
|
|
$validator = new MinSizeSliderImageValidator(); |
|
19
|
|
|
$this->assertEquals(false, $validator->isValid($performance)); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @dataProvider isValidProvider |
|
24
|
|
|
*/ |
|
25
|
|
|
public function testViolationIfNotValid(int $width, int $height, bool $isValid) |
|
26
|
|
|
{ |
|
27
|
|
|
$context = $this->getMockBuilder(ExecutionContext::class) |
|
28
|
|
|
->disableOriginalConstructor() |
|
29
|
|
|
->setMethods(['buildViolation']) |
|
30
|
|
|
->getMock(); |
|
31
|
|
|
$violationBuilder = $this->getMockBuilder(ConstraintViolationBuilder::class) |
|
32
|
|
|
->disableOriginalConstructor() |
|
33
|
|
|
->setMethods(['atPath', 'setParameter', 'addViolation']) |
|
34
|
|
|
->getMock(); |
|
35
|
|
|
$violationBuilder->method('atPath')->willReturn($violationBuilder); |
|
36
|
|
|
$violationBuilder->method('setParameter')->willReturn($violationBuilder); |
|
37
|
|
|
|
|
38
|
|
|
$context->expects($isValid ? $this->never() : $this->once()) |
|
39
|
|
|
->method('buildViolation') |
|
40
|
|
|
->willReturn($violationBuilder); |
|
41
|
|
|
|
|
42
|
|
|
$media = new Media(); |
|
43
|
|
|
$media->setWidth($width); |
|
44
|
|
|
$media->setHeight($height); |
|
45
|
|
|
|
|
46
|
|
|
$performance = new Performance(); |
|
47
|
|
|
$performance->setSliderImage($media); |
|
48
|
|
|
|
|
49
|
|
|
$validator = new MinSizeSliderImageValidator(); |
|
50
|
|
|
$validator->initialize($context); |
|
51
|
|
|
$validator->validate($performance, new MinSizeSliderImage()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @dataProvider isValidProvider |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testIsValid(int $width, int $height, bool $isValid) |
|
58
|
|
|
{ |
|
59
|
|
|
$media = new Media(); |
|
60
|
|
|
$media->setWidth($width); |
|
61
|
|
|
$media->setHeight($height); |
|
62
|
|
|
|
|
63
|
|
|
$performance = new Performance(); |
|
64
|
|
|
$performance->setSliderImage($media); |
|
65
|
|
|
|
|
66
|
|
|
$validator = new MinSizeSliderImageValidator(); |
|
67
|
|
|
$this->assertEquals($isValid, $validator->isValid($performance)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function isValidProvider() |
|
71
|
|
|
{ |
|
72
|
|
|
return [ |
|
73
|
|
|
['width' => 500, 'height' => 400, false], |
|
74
|
|
|
['width' => 500, 'height' => 500, false], |
|
75
|
|
|
['width' => 500, 'height' => 600, false], |
|
76
|
|
|
['width' => 1000, 'height' => 400, false], |
|
77
|
|
|
['width' => 1100, 'height' => 400, false], |
|
78
|
|
|
['width' => 1000, 'height' => 500, true], |
|
79
|
|
|
['width' => 1100, 'height' => 600, true], |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|