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 Sonata\MediaBundle\Provider\Pool; |
15
|
|
|
use Sonata\MediaBundle\Tests\Helpers\PHPUnit_Framework_TestCase; |
16
|
|
|
use Sonata\MediaBundle\Validator\Constraints\ValidMediaFormat; |
17
|
|
|
use Sonata\MediaBundle\Validator\FormatValidator; |
18
|
|
|
|
19
|
|
|
class FormatValidatorTest extends PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
public function testValidate() |
22
|
|
|
{ |
23
|
|
|
$pool = new Pool('defaultContext'); |
24
|
|
|
$pool->addContext('test', array(), array('format1' => array())); |
25
|
|
|
|
26
|
|
|
$gallery = $this->createMock('Sonata\MediaBundle\Model\GalleryInterface'); |
27
|
|
|
$gallery->expects($this->once())->method('getDefaultFormat')->will($this->returnValue('format1')); |
28
|
|
|
$gallery->expects($this->once())->method('getContext')->will($this->returnValue('test')); |
29
|
|
|
|
30
|
|
|
// Prefer the Symfony 2.5+ API if available |
31
|
|
|
if (class_exists('Symfony\Component\Validator\Context\ExecutionContext')) { |
32
|
|
|
$contextClass = 'Symfony\Component\Validator\Context\ExecutionContext'; |
33
|
|
|
} else { |
34
|
|
|
$contextClass = 'Symfony\Component\Validator\ExecutionContext'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$context = $this->getMockBuilder($contextClass) |
38
|
|
|
->disableOriginalConstructor() |
39
|
|
|
->getMock(); |
40
|
|
|
$context->expects($this->never())->method('addViolation'); |
41
|
|
|
|
42
|
|
|
$validator = new FormatValidator($pool); |
43
|
|
|
$validator->initialize($context); |
44
|
|
|
|
45
|
|
|
$validator->validate($gallery, new ValidMediaFormat()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testValidateNotValidDefaultFormat() |
49
|
|
|
{ |
50
|
|
|
$pool = new Pool('defaultContext'); |
51
|
|
|
$pool->addContext('test', array(), array('format1' => array())); |
52
|
|
|
|
53
|
|
|
$gallery = $this->createMock('Sonata\MediaBundle\Model\GalleryInterface'); |
54
|
|
|
$gallery->expects($this->once())->method('getDefaultFormat')->will($this->returnValue('non_existing_format')); |
55
|
|
|
$gallery->expects($this->once())->method('getContext')->will($this->returnValue('test')); |
56
|
|
|
|
57
|
|
|
// Prefer the Symfony 2.5+ API if available |
58
|
|
|
if (class_exists('Symfony\Component\Validator\Context\ExecutionContext')) { |
59
|
|
|
$contextClass = 'Symfony\Component\Validator\Context\ExecutionContext'; |
60
|
|
|
} else { |
61
|
|
|
$contextClass = 'Symfony\Component\Validator\ExecutionContext'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$context = $this->getMockBuilder($contextClass) |
65
|
|
|
->disableOriginalConstructor() |
66
|
|
|
->getMock(); |
67
|
|
|
$context->expects($this->once())->method('addViolation'); |
68
|
|
|
|
69
|
|
|
$validator = new FormatValidator($pool); |
70
|
|
|
$validator->initialize($context); |
71
|
|
|
|
72
|
|
|
$validator->validate($gallery, new ValidMediaFormat()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testValidateOnlyReferenceIsAllowedIfNotFormats() |
76
|
|
|
{ |
77
|
|
|
$pool = new Pool('defaultContext'); |
78
|
|
|
$pool->addContext('test'); |
79
|
|
|
|
80
|
|
|
$gallery = $this->createMock('Sonata\MediaBundle\Model\GalleryInterface'); |
81
|
|
|
$gallery->expects($this->once())->method('getDefaultFormat')->will($this->returnValue('format_that_is_not_reference')); |
82
|
|
|
$gallery->expects($this->once())->method('getContext')->will($this->returnValue('test')); |
83
|
|
|
|
84
|
|
|
// Prefer the Symfony 2.5+ API if available |
85
|
|
|
if (class_exists('Symfony\Component\Validator\Context\ExecutionContext')) { |
86
|
|
|
$contextClass = 'Symfony\Component\Validator\Context\ExecutionContext'; |
87
|
|
|
} else { |
88
|
|
|
$contextClass = 'Symfony\Component\Validator\ExecutionContext'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$context = $this->getMockBuilder($contextClass) |
92
|
|
|
->disableOriginalConstructor() |
93
|
|
|
->getMock(); |
94
|
|
|
$context->expects($this->once())->method('addViolation'); |
95
|
|
|
|
96
|
|
|
$validator = new FormatValidator($pool); |
97
|
|
|
$validator->initialize($context); |
98
|
|
|
|
99
|
|
|
$validator->validate($gallery, new ValidMediaFormat()); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|