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 Sonata\AdminBundle\Model\ModelManagerInterface; |
18
|
|
|
use Sonata\ClassificationBundle\Model\CategoryInterface; |
19
|
|
|
use Sonata\ClassificationBundle\Model\ContextInterface; |
20
|
|
|
use Sonata\MediaBundle\Entity\BaseMedia; |
21
|
|
|
use Sonata\MediaBundle\Model\CategoryManagerInterface; |
22
|
|
|
use Sonata\MediaBundle\Model\Media; |
23
|
|
|
use Sonata\MediaBundle\Provider\MediaProviderInterface; |
24
|
|
|
use Sonata\MediaBundle\Provider\Pool; |
25
|
|
|
use Sonata\MediaBundle\Tests\Fixtures\EntityWithGetId; |
26
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
27
|
|
|
use Symfony\Component\HttpFoundation\Request; |
28
|
|
|
|
29
|
|
|
class BaseMediaAdminTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
private $pool; |
32
|
|
|
private $categoryManager; |
33
|
|
|
private $request; |
34
|
|
|
private $modelManager; |
35
|
|
|
private $mediaAdmin; |
36
|
|
|
|
37
|
|
|
protected function setUp(): void |
38
|
|
|
{ |
39
|
|
|
$this->pool = $this->prophesize(Pool::class); |
40
|
|
|
$this->categoryManager = $this->prophesize(CategoryManagerInterface::class); |
41
|
|
|
$this->request = $this->prophesize(Request::class); |
42
|
|
|
$this->modelManager = $this->prophesize(ModelManagerInterface::class); |
43
|
|
|
|
44
|
|
|
$this->mediaAdmin = new TestMediaAdmin( |
45
|
|
|
null, |
46
|
|
|
BaseMedia::class, |
47
|
|
|
'SonataMediaBundle:MediaAdmin', |
48
|
|
|
$this->pool->reveal(), |
49
|
|
|
$this->categoryManager->reveal() |
50
|
|
|
); |
51
|
|
|
$this->mediaAdmin->setRequest($this->request->reveal()); |
52
|
|
|
$this->mediaAdmin->setModelManager($this->modelManager->reveal()); |
53
|
|
|
$this->mediaAdmin->setUniqid('uniqid'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testGetNewInstance(): void |
57
|
|
|
{ |
58
|
|
|
$media = $this->prophesize(Media::class); |
59
|
|
|
$category = $this->prophesize(); |
60
|
|
|
$category->willExtend(EntityWithGetId::class); |
61
|
|
|
$category->willImplement(CategoryInterface::class); |
62
|
|
|
$context = $this->prophesize(); |
63
|
|
|
$context->willExtend(EntityWithGetId::class); |
64
|
|
|
$context->willImplement(ContextInterface::class); |
65
|
|
|
|
66
|
|
|
$this->configureGetPersistentParameters(); |
67
|
|
|
$this->configureGetProviderName($media); |
68
|
|
|
$this->modelManager->getModelInstance(BaseMedia::class)->willReturn($media->reveal()); |
69
|
|
|
$this->categoryManager->find(1)->willReturn($category->reveal()); |
70
|
|
|
$this->request->isMethod('POST')->willReturn(true); |
71
|
|
|
$this->request->get('context')->willReturn('context'); |
72
|
|
|
$this->request->get('id')->willReturn(null); |
73
|
|
|
$category->getContext()->willReturn($context->reveal()); |
74
|
|
|
$context->getId()->willReturn('context'); |
75
|
|
|
$media->setContext('context')->shouldBeCalled(); |
76
|
|
|
$media->setCategory($category->reveal())->shouldBeCalled(); |
77
|
|
|
|
78
|
|
|
$this->assertSame($media->reveal(), $this->mediaAdmin->getNewInstance()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function configureGetPersistentParameters(): void |
82
|
|
|
{ |
83
|
|
|
$provider = $this->prophesize(MediaProviderInterface::class); |
84
|
|
|
$category = $this->prophesize(); |
85
|
|
|
$category->willExtend(EntityWithGetId::class); |
86
|
|
|
$category->willImplement(CategoryInterface::class); |
87
|
|
|
$query = $this->prophesize(ParameterBag::class); |
88
|
|
|
$this->request->query = $query->reveal(); |
89
|
|
|
|
90
|
|
|
$this->pool->getDefaultContext()->willReturn('default_context'); |
91
|
|
|
$this->pool->getProvidersByContext('context')->willReturn([$provider->reveal()]); |
92
|
|
|
$this->categoryManager->getRootCategory('context')->willReturn($category->reveal()); |
93
|
|
|
$this->request->get('filter')->willReturn([]); |
94
|
|
|
$this->request->get('provider')->willReturn(null); |
95
|
|
|
$this->request->get('category')->willReturn(null); |
96
|
|
|
$this->request->get('hide_context')->willReturn(true); |
97
|
|
|
$this->request->get('context', 'default_context')->willReturn('context'); |
98
|
|
|
$category->getId()->willReturn(1); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function configureGetProviderName($media): void |
102
|
|
|
{ |
103
|
|
|
$this->request->get('uniqid')->willReturn(['providerName' => 'providerName']); |
104
|
|
|
$media->setProviderName('providerName')->shouldBeCalled(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|