Completed
Pull Request — master (#1343)
by Grégoire
12:14
created

BaseMediaAdminTest::testGetNewInstance()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 24
rs 8.9713
c 1
b 0
f 0
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 PHPUnit\Framework\TestCase;
15
use Sonata\MediaBundle\Admin\BaseMediaAdmin;
16
17
class TestMediaAdmin extends BaseMediaAdmin
18
{
19
}
20
21
class EntityWithGetId
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
22
{
23
    public function getId()
24
    {
25
    }
26
}
27
28
class BaseMediaAdminTest extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

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