1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
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 Sonata\MediaBundle\Tests\Validator; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Sonata\MediaBundle\Provider\Pool; |
16
|
|
|
use Sonata\MediaBundle\Validator\Constraints\ValidMediaFormat; |
17
|
|
|
use Sonata\MediaBundle\Validator\FormatValidator; |
18
|
|
|
use Symfony\Component\Validator\Context\ExecutionContext; |
19
|
|
|
|
20
|
|
|
class FormatValidatorTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function testValidate() |
23
|
|
|
{ |
24
|
|
|
$pool = new Pool('defaultContext'); |
25
|
|
|
$pool->addContext('test', [], ['format1' => []]); |
26
|
|
|
|
27
|
|
|
$gallery = $this->createMock('Sonata\MediaBundle\Model\GalleryInterface'); |
28
|
|
|
$gallery->expects($this->once())->method('getDefaultFormat')->will($this->returnValue('format1')); |
29
|
|
|
$gallery->expects($this->once())->method('getContext')->will($this->returnValue('test')); |
30
|
|
|
|
31
|
|
|
$context = $this->createMock(ExecutionContext::class); |
32
|
|
|
$context->expects($this->never())->method('addViolation'); |
33
|
|
|
|
34
|
|
|
$validator = new FormatValidator($pool); |
35
|
|
|
$validator->initialize($context); |
36
|
|
|
|
37
|
|
|
$validator->validate($gallery, new ValidMediaFormat()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testValidateNotValidDefaultFormat() |
41
|
|
|
{ |
42
|
|
|
$pool = new Pool('defaultContext'); |
43
|
|
|
$pool->addContext('test', [], ['format1' => []]); |
44
|
|
|
|
45
|
|
|
$gallery = $this->createMock('Sonata\MediaBundle\Model\GalleryInterface'); |
46
|
|
|
$gallery->expects($this->once())->method('getDefaultFormat')->will($this->returnValue('non_existing_format')); |
47
|
|
|
$gallery->expects($this->once())->method('getContext')->will($this->returnValue('test')); |
48
|
|
|
|
49
|
|
|
$context = $this->createMock(ExecutionContext::class); |
50
|
|
|
$context->expects($this->once())->method('addViolation'); |
51
|
|
|
|
52
|
|
|
$validator = new FormatValidator($pool); |
53
|
|
|
$validator->initialize($context); |
54
|
|
|
|
55
|
|
|
$validator->validate($gallery, new ValidMediaFormat()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testValidateOnlyReferenceIsAllowedIfNotFormats() |
59
|
|
|
{ |
60
|
|
|
$pool = new Pool('defaultContext'); |
61
|
|
|
$pool->addContext('test'); |
62
|
|
|
|
63
|
|
|
$gallery = $this->createMock('Sonata\MediaBundle\Model\GalleryInterface'); |
64
|
|
|
$gallery->expects($this->once())->method('getDefaultFormat')->will($this->returnValue('format_that_is_not_reference')); |
65
|
|
|
$gallery->expects($this->once())->method('getContext')->will($this->returnValue('test')); |
66
|
|
|
|
67
|
|
|
$context = $this->createMock(ExecutionContext::class); |
68
|
|
|
$context->expects($this->once())->method('addViolation'); |
69
|
|
|
|
70
|
|
|
$validator = new FormatValidator($pool); |
71
|
|
|
$validator->initialize($context); |
72
|
|
|
|
73
|
|
|
$validator->validate($gallery, new ValidMediaFormat()); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|