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\Validator\Constraints\ValidMediaFormat; |
16
|
|
|
use Sonata\MediaBundle\Validator\FormatValidator; |
17
|
|
|
|
18
|
|
|
class FormatValidatorTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
public function testValidate() |
21
|
|
|
{ |
22
|
|
|
$pool = new Pool('defaultContext'); |
23
|
|
|
$pool->addContext('test', array(), array('format1' => array())); |
24
|
|
|
|
25
|
|
|
$gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface'); |
26
|
|
|
$gallery->expects($this->once())->method('getDefaultFormat')->will($this->returnValue('format1')); |
27
|
|
|
$gallery->expects($this->once())->method('getContext')->will($this->returnValue('test')); |
28
|
|
|
|
29
|
|
|
// Prefer the Syfmony 2.5+ API if available |
30
|
|
|
if (class_exists('Symfony\Component\Validator\Context\ExecutionContext')) { |
31
|
|
|
$contextClass = 'Symfony\Component\Validator\Context\ExecutionContext'; |
32
|
|
|
} else { |
33
|
|
|
$contextClass = 'Symfony\Component\Validator\ExecutionContext'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$context = $this->getMock($contextClass, array(), array(), '', false); |
37
|
|
|
$context->expects($this->never())->method('addViolation'); |
38
|
|
|
|
39
|
|
|
$validator = new FormatValidator($pool); |
40
|
|
|
$validator->initialize($context); |
41
|
|
|
|
42
|
|
|
$validator->validate($gallery, new ValidMediaFormat()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testValidateWithValidContext() |
46
|
|
|
{ |
47
|
|
|
$pool = new Pool('defaultContext'); |
48
|
|
|
$pool->addContext('test'); |
49
|
|
|
|
50
|
|
|
$gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface'); |
51
|
|
|
$gallery->expects($this->once())->method('getDefaultFormat')->will($this->returnValue('format1')); |
52
|
|
|
$gallery->expects($this->once())->method('getContext')->will($this->returnValue('test')); |
53
|
|
|
|
54
|
|
|
// Prefer the Syfmony 2.5+ API if available |
55
|
|
|
if (class_exists('Symfony\Component\Validator\Context\ExecutionContext')) { |
56
|
|
|
$contextClass = 'Symfony\Component\Validator\Context\ExecutionContext'; |
57
|
|
|
} else { |
58
|
|
|
$contextClass = 'Symfony\Component\Validator\ExecutionContext'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$context = $this->getMock($contextClass, array(), array(), '', false); |
62
|
|
|
$context->expects($this->once())->method('addViolation'); |
63
|
|
|
|
64
|
|
|
$validator = new FormatValidator($pool); |
65
|
|
|
$validator->initialize($context); |
66
|
|
|
|
67
|
|
|
$validator->validate($gallery, new ValidMediaFormat()); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|