|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Tests\Unit\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use App\Repository\PerformanceEventRepository; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Symfony\Component\Validator\Constraint; |
|
8
|
|
|
use App\Validator\TwoPerformanceEventsPerDayValidator; |
|
9
|
|
|
use App\Validator\TwoPerformanceEventsPerDay; |
|
10
|
|
|
use App\Entity\PerformanceEvent; |
|
11
|
|
|
use Symfony\Component\Validator\Context\ExecutionContext; |
|
12
|
|
|
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder; |
|
13
|
|
|
|
|
14
|
|
|
class TwoPerformanceEventsPerDayValidatorTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @dataProvider validateProvider |
|
18
|
|
|
*/ |
|
19
|
|
|
public function testValidate(PerformanceEvent $event, int $num, bool $isValid, string $constraint = null) |
|
20
|
|
|
{ |
|
21
|
|
|
$context = $this->getMockBuilder(ExecutionContext::class) |
|
22
|
|
|
->disableOriginalConstructor() |
|
23
|
|
|
->setMethods(['buildViolation']) |
|
24
|
|
|
->getMock(); |
|
25
|
|
|
$violationBuilder = $this->getMockBuilder(ConstraintViolationBuilder::class) |
|
26
|
|
|
->disableOriginalConstructor() |
|
27
|
|
|
->setMethods(['atPath', 'setParameter', 'addViolation']) |
|
28
|
|
|
->getMock(); |
|
29
|
|
|
$violationBuilder->method('atPath')->willReturn($violationBuilder); |
|
30
|
|
|
$violationBuilder->method('setParameter')->willReturn($violationBuilder); |
|
31
|
|
|
|
|
32
|
|
|
$mocker = $context->expects($isValid ? $this->never() : $this->once()) |
|
33
|
|
|
->method('buildViolation'); |
|
34
|
|
|
if (!$isValid) { |
|
35
|
|
|
$mocker->with($this->stringContains($constraint)); |
|
36
|
|
|
} |
|
37
|
|
|
$mocker->willReturn($violationBuilder); |
|
38
|
|
|
$repository = $this->getPerformanceEventRepositoryMock($num); |
|
39
|
|
|
$validator = new TwoPerformanceEventsPerDayValidator($repository); |
|
40
|
|
|
$validator->initialize($context); |
|
41
|
|
|
$validator->validate($event, new TwoPerformanceEventsPerDay()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function validateProvider() |
|
45
|
|
|
{ |
|
46
|
|
|
$eventNoDate = new PerformanceEvent(); |
|
47
|
|
|
$event = (new PerformanceEvent())->setDateTime(new \DateTime()); |
|
48
|
|
|
$constraint = new TwoPerformanceEventsPerDay(); |
|
49
|
|
|
|
|
50
|
|
|
return [ |
|
51
|
|
|
[$eventNoDate, 0, false, $constraint->performance_must_have_a_date], |
|
52
|
|
|
[$event, 0, true], |
|
53
|
|
|
[$event, 1, true], |
|
54
|
|
|
[$event, 2, false, $constraint->max_performances_per_day], |
|
55
|
|
|
[$event, 3, false, $constraint->max_performances_per_day], |
|
56
|
|
|
[$event, 4, false, $constraint->max_performances_per_day], |
|
57
|
|
|
[$event, 5, false, $constraint->max_performances_per_day], |
|
58
|
|
|
[$event, 6, false, $constraint->max_performances_per_day], |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getPerformanceEventRepositoryMock(int $num) |
|
63
|
|
|
{ |
|
64
|
|
|
$repository = $this->getMockBuilder(PerformanceEventRepository::class) |
|
65
|
|
|
->disableOriginalConstructor() |
|
66
|
|
|
->getMock(); |
|
67
|
|
|
|
|
68
|
|
|
$repository |
|
69
|
|
|
->method('findByDateRangeAndSlug') |
|
70
|
|
|
->will($this->returnValue(array_fill(0, $num, new PerformanceEvent()))) |
|
71
|
|
|
; |
|
72
|
|
|
|
|
73
|
|
|
return $repository; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|