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\Admin; |
13
|
|
|
|
14
|
|
|
use Sonata\MediaBundle\Admin\BaseMediaAdmin; |
15
|
|
|
|
16
|
|
|
class TestMediaAdmin extends BaseMediaAdmin |
17
|
|
|
{ |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
class EntityWithGetId |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
public function getId() |
23
|
|
|
{ |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
class BaseMediaAdminTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
private $pool; |
30
|
|
|
private $categoryManager; |
31
|
|
|
private $request; |
32
|
|
|
private $modelManager; |
33
|
|
|
private $mediaAdmin; |
34
|
|
|
|
35
|
|
|
protected function setUp() |
36
|
|
|
{ |
37
|
|
|
$this->pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool'); |
38
|
|
|
$this->categoryManager = $this->prophesize('Sonata\MediaBundle\Model\CategoryManagerInterface'); |
39
|
|
|
$this->request = $this->prophesize('Symfony\Component\HttpFoundation\Request'); |
40
|
|
|
$this->modelManager = $this->prophesize('Sonata\AdminBundle\Model\ModelManagerInterface'); |
41
|
|
|
|
42
|
|
|
$this->mediaAdmin = new TestMediaAdmin( |
43
|
|
|
null, |
44
|
|
|
'Sonata\MediaBundle\Entity\BaseMedia', |
45
|
|
|
'SonataMediaBundle:MediaAdmin', |
46
|
|
|
$this->pool->reveal(), |
47
|
|
|
$this->categoryManager->reveal() |
48
|
|
|
); |
49
|
|
|
$this->mediaAdmin->setRequest($this->request->reveal()); |
50
|
|
|
$this->mediaAdmin->setModelManager($this->modelManager->reveal()); |
51
|
|
|
$this->mediaAdmin->setUniqid('uniqid'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGetNewInstance() |
55
|
|
|
{ |
56
|
|
|
$media = $this->prophesize('Sonata\MediaBundle\Model\Media'); |
57
|
|
|
$category = $this->prophesize(); |
58
|
|
|
$category->willExtend('Sonata\MediaBundle\Tests\Admin\EntityWithGetId'); |
59
|
|
|
$category->willImplement('Sonata\ClassificationBundle\Model\CategoryInterface'); |
60
|
|
|
$context = $this->prophesize(); |
61
|
|
|
$context->willExtend('Sonata\MediaBundle\Tests\Admin\EntityWithGetId'); |
62
|
|
|
$context->willImplement('Sonata\ClassificationBundle\Model\ContextInterface'); |
63
|
|
|
|
64
|
|
|
$this->configureGetPersistentParameters(); |
65
|
|
|
$this->configureGetProviderName($media); |
66
|
|
|
$this->modelManager->getModelInstance('Sonata\MediaBundle\Entity\BaseMedia')->willReturn($media->reveal()); |
67
|
|
|
$this->categoryManager->find(1)->willReturn($category->reveal()); |
68
|
|
|
$this->request->isMethod('POST')->willReturn(true); |
69
|
|
|
$category->getContext()->willReturn($context->reveal()); |
70
|
|
|
$media->setContext('context')->shouldBeCalled(); |
71
|
|
|
|
72
|
|
|
$this->assertSame($media->reveal(), $this->mediaAdmin->getNewInstance()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function configureGetPersistentParameters() |
76
|
|
|
{ |
77
|
|
|
$category = $this->prophesize(); |
78
|
|
|
$category->willExtend('Sonata\MediaBundle\Tests\Admin\EntityWithGetId'); |
79
|
|
|
$category->willImplement('Sonata\ClassificationBundle\Model\CategoryInterface'); |
80
|
|
|
|
81
|
|
|
$this->categoryManager->getRootCategory('context')->willReturn($category->reveal()); |
82
|
|
|
$this->request->get('filter')->willReturn(array()); |
83
|
|
|
$this->request->get('provider')->willReturn('provider'); |
84
|
|
|
$this->request->get('category')->willReturn(1); |
85
|
|
|
$this->request->get('hide_context')->willReturn(true); |
86
|
|
|
$this->request->get('context', null)->willReturn('context'); |
87
|
|
|
$category->getId()->willReturn(1); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function configureGetProviderName($media) |
91
|
|
|
{ |
92
|
|
|
// NEXT_MAJOR remove this block when dropping sf < 2.8 compatibility |
93
|
|
|
if (method_exists('Symfony\Component\HttpFoundation\JsonResponse', 'transformJsonError')) { |
94
|
|
|
$this->request->get('uniqid[providerName]')->willReturn('providerName'); |
95
|
|
|
} else { |
96
|
|
|
$this->request->get('uniqid')->willReturn(array('providerName' => 'providerName')); |
97
|
|
|
} |
98
|
|
|
$media->setProviderName('providerName')->shouldBeCalled(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.