|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Tests\Admin; |
|
15
|
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
18
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
|
19
|
|
|
use Sonata\ClassificationBundle\Model\CategoryInterface; |
|
20
|
|
|
use Sonata\ClassificationBundle\Model\ContextInterface; |
|
21
|
|
|
use Sonata\MediaBundle\Entity\BaseMedia; |
|
22
|
|
|
use Sonata\MediaBundle\Model\CategoryManagerInterface; |
|
23
|
|
|
use Sonata\MediaBundle\Model\Media; |
|
24
|
|
|
use Sonata\MediaBundle\Provider\MediaProviderInterface; |
|
25
|
|
|
use Sonata\MediaBundle\Provider\Pool; |
|
26
|
|
|
use Sonata\MediaBundle\Tests\Fixtures\EntityWithGetId; |
|
27
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
|
28
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
29
|
|
|
|
|
30
|
|
|
class BaseMediaAdminTest extends TestCase |
|
31
|
|
|
{ |
|
32
|
|
|
private $pool; |
|
33
|
|
|
private $categoryManager; |
|
34
|
|
|
private $request; |
|
35
|
|
|
private $modelManager; |
|
36
|
|
|
private $mediaAdmin; |
|
37
|
|
|
|
|
38
|
|
|
protected function setUp(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->pool = $this->prophesize(Pool::class); |
|
41
|
|
|
$this->categoryManager = $this->prophesize(CategoryManagerInterface::class); |
|
42
|
|
|
$this->request = $this->prophesize(Request::class); |
|
43
|
|
|
$this->modelManager = $this->prophesize(ModelManagerInterface::class); |
|
44
|
|
|
|
|
45
|
|
|
$this->mediaAdmin = new TestMediaAdmin( |
|
46
|
|
|
null, |
|
47
|
|
|
BaseMedia::class, |
|
48
|
|
|
'SonataMediaBundle:MediaAdmin', |
|
49
|
|
|
$this->pool->reveal(), |
|
50
|
|
|
$this->categoryManager->reveal() |
|
51
|
|
|
); |
|
52
|
|
|
$this->mediaAdmin->setRequest($this->request->reveal()); |
|
53
|
|
|
$this->mediaAdmin->setModelManager($this->modelManager->reveal()); |
|
54
|
|
|
$this->mediaAdmin->setUniqid('uniqid'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testGetNewInstance(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$media = $this->prophesize(Media::class); |
|
60
|
|
|
$category = $this->prophesize(); |
|
61
|
|
|
$category->willExtend(EntityWithGetId::class); |
|
62
|
|
|
$category->willImplement(CategoryInterface::class); |
|
63
|
|
|
$context = $this->prophesize(); |
|
64
|
|
|
$context->willExtend(EntityWithGetId::class); |
|
65
|
|
|
$context->willImplement(ContextInterface::class); |
|
66
|
|
|
|
|
67
|
|
|
$this->configureGetPersistentParameters(); |
|
68
|
|
|
$this->configureGetProviderName($media); |
|
69
|
|
|
$this->modelManager->getModelInstance(BaseMedia::class)->willReturn($media->reveal()); |
|
70
|
|
|
$this->categoryManager->find(1)->willReturn($category->reveal()); |
|
71
|
|
|
$this->request->isMethod('POST')->willReturn(true); |
|
72
|
|
|
$this->request->get('context')->willReturn('context'); |
|
73
|
|
|
$this->request->get('id')->willReturn(null); |
|
74
|
|
|
$category->getContext()->willReturn($context->reveal()); |
|
75
|
|
|
$context->getId()->willReturn('context'); |
|
76
|
|
|
$media->setContext('context')->shouldBeCalled(); |
|
77
|
|
|
$media->setCategory($category->reveal())->shouldBeCalled(); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertSame($media->reveal(), $this->mediaAdmin->getNewInstance()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testGetPersistentParametersWithMultipleProvidersInContext() |
|
83
|
|
|
{ |
|
84
|
|
|
$category = $this->prophesize(); |
|
85
|
|
|
$category->willExtend(EntityWithGetId::class); |
|
86
|
|
|
$category->willImplement(CategoryInterface::class); |
|
87
|
|
|
$category->getId()->willReturn(1); |
|
88
|
|
|
$this->categoryManager->getRootCategory('context')->willReturn($category->reveal()); |
|
89
|
|
|
$this->request->isMethod('POST')->willReturn(true); |
|
90
|
|
|
$this->request->get('filter')->willReturn([]); |
|
91
|
|
|
$this->request->get('context', 'default_context')->willReturn('context'); |
|
92
|
|
|
$this->request->get('provider')->willReturn('providerName'); |
|
93
|
|
|
$this->request->get('category')->willReturn(null); |
|
94
|
|
|
$this->request->get('hide_context')->willReturn(true); |
|
95
|
|
|
$provider = $this->prophesize(MediaProviderInterface::class); |
|
96
|
|
|
$this->pool->getDefaultContext()->willReturn('default_context'); |
|
97
|
|
|
$this->pool->getProvidersByContext('context')->willReturn([$provider->reveal(), $provider->reveal()]); |
|
98
|
|
|
$this->assertSame( |
|
99
|
|
|
[ |
|
100
|
|
|
'provider' => 'providerName', |
|
101
|
|
|
'context' => 'context', |
|
102
|
|
|
'category' => 1, |
|
103
|
|
|
'hide_context' => true, |
|
104
|
|
|
], $this->mediaAdmin->getPersistentParameters()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function configureGetPersistentParameters() |
|
108
|
|
|
{ |
|
109
|
|
|
$provider = $this->prophesize(MediaProviderInterface::class); |
|
110
|
|
|
$category = $this->prophesize(); |
|
111
|
|
|
$category->willExtend(EntityWithGetId::class); |
|
112
|
|
|
$category->willImplement(CategoryInterface::class); |
|
113
|
|
|
$query = $this->prophesize(ParameterBag::class); |
|
114
|
|
|
$this->request->query = $query->reveal(); |
|
115
|
|
|
$query->set('provider', null)->shouldBeCalled(); |
|
116
|
|
|
|
|
117
|
|
|
$this->pool->getDefaultContext()->willReturn('default_context'); |
|
118
|
|
|
$this->pool->getProvidersByContext('context')->willReturn([$provider->reveal()]); |
|
119
|
|
|
$this->categoryManager->getRootCategory('context')->willReturn($category->reveal()); |
|
120
|
|
|
$this->request->get('filter')->willReturn([]); |
|
121
|
|
|
$this->request->get('provider')->willReturn(null); |
|
122
|
|
|
$this->request->get('category')->willReturn(null); |
|
123
|
|
|
$this->request->get('hide_context')->willReturn(true); |
|
124
|
|
|
$this->request->get('context', 'default_context')->willReturn('context'); |
|
125
|
|
|
$category->getId()->willReturn(1); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
private function configureGetProviderName(ObjectProphecy $media): void |
|
129
|
|
|
{ |
|
130
|
|
|
$this->request->get('uniqid')->willReturn(['providerName' => 'providerName']); |
|
131
|
|
|
$media->setProviderName('providerName')->shouldBeCalled(); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|