Completed
Push — master ( eec98d...dbbf84 )
by Grégoire
05:16
created

testExecuteWithDisabledClassfication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
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\Command;
13
14
use Sonata\ClassificationBundle\Model\ContextManagerInterface;
15
use Sonata\MediaBundle\Command\FixMediaContextCommand;
16
use Sonata\MediaBundle\Model\CategoryManagerInterface;
17
use Sonata\MediaBundle\Provider\Pool;
18
use Symfony\Component\Console\Application;
19
use Symfony\Component\Console\Tester\CommandTester;
20
use Symfony\Component\Console\Tests\Command\CommandTest;
21
22
class FixMediaContextCommandTest extends CommandTest
23
{
24
    /**
25
     * @var \PHPUnit_Framework_MockObject_MockObject|ContainerInterface
26
     */
27
    protected $container;
28
29
    /**
30
     * @var Application
31
     */
32
    protected $application;
33
34
    /**
35
     * @var ContainerAwareCommand
36
     */
37
    protected $command;
38
39
    /**
40
     * @var CommandTester
41
     */
42
    protected $tester;
43
44
    /**
45
     * @var \PHPUnit_Framework_MockObject_MockObject|Pool
46
     */
47
    private $pool;
48
49
    /**
50
     * @var \PHPUnit_Framework_MockObject_MockObject|ContextManagerInterface
51
     */
52
    private $contextManger;
53
54
    /**
55
     * @var \PHPUnit_Framework_MockObject_MockObject|CategoryManagerInterface
56
     */
57
    private $categoryManger;
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function setUp()
63
    {
64
        $this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
65
66
        $this->command = new FixMediaContextCommand();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Sonata\MediaBundle\...ixMediaContextCommand() of type object<Sonata\MediaBundl...FixMediaContextCommand> is incompatible with the declared type object<Sonata\MediaBundl...\ContainerAwareCommand> of property $command.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
67
        $this->command->setContainer($this->container);
68
69
        $this->application = new Application();
70
        $this->application->add($this->command);
71
72
        $this->tester = new CommandTester($this->application->find('sonata:media:fix-media-context'));
73
74
        $this->pool = $pool = $this->getMockBuilder('Sonata\MediaBundle\Provider\Pool')->disableOriginalConstructor()->getMock();
75
76
        $this->contextManger = $contextManger = $this->getMock('Sonata\ClassificationBundle\Model\ContextManagerInterface');
77
78
        $this->categoryManger = $categoryManger = $this->getMock('Sonata\MediaBundle\Model\CategoryManagerInterface');
79
80
        $this->container->expects($this->any())
81
            ->method('get')
82
            ->will($this->returnCallback(function ($id) use ($pool, $contextManger, $categoryManger) {
83
                switch ($id) {
84
                    case 'sonata.media.pool':
85
                        return $pool;
86
                    case 'sonata.classification.manager.context':
87
                        return $contextManger;
88
                    case 'sonata.media.manager.category':
89
                        return $categoryManger;
90
                }
91
92
                return;
93
            }));
94
    }
95
96
    public function testExecuteWithDisabledClassfication()
97
    {
98
        $this->container->method('has')->with($this->equalTo('sonata.media.manager.category'))
99
            ->will($this->returnValue(false));
100
101
        $this->setExpectedException('\LogicException');
102
103
        $this->tester->execute(array('command' => $this->command->getName()));
104
    }
105
106
    public function testExecuteWithExisting()
107
    {
108
        $context = array(
109
            'providers' => array(),
110
            'formats' => array(),
111
            'download' => array(),
112
        );
113
114
        $this->container->method('has')->with($this->equalTo('sonata.media.manager.category'))
115
            ->will($this->returnValue(true));
116
117
        $this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context)));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\MediaBundle\Provider\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
119
        $contextModel = $this->getMock('Sonata\ClassificationBundle\Model\ContextInterface');
120
121
        $this->contextManger->expects($this->once())->method('findOneBy')->with($this->equalTo(array('id' => 'foo')))->will($this->returnValue($contextModel));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\Classifica...ontextManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
123
        $category = $this->getMock('Sonata\ClassificationBundle\Model\CategoryInterface');
124
125
        $this->categoryManger->expects($this->once())->method('getRootCategory')->with($this->equalTo($contextModel))->will($this->returnValue($category));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\MediaBundl...tegoryManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
127
        $output = $this->tester->execute(array('command' => $this->command->getName()));
128
129
        $this->assertRegExp('@Done!@', $this->tester->getDisplay());
130
131
        $this->assertSame(0, $output);
132
    }
133
134
    public function testExecuteWithEmptyRoot()
135
    {
136
        $context = array(
137
            'providers' => array(),
138
            'formats' => array(),
139
            'download' => array(),
140
        );
141
142
        $this->container->method('has')->with($this->equalTo('sonata.media.manager.category'))
143
            ->will($this->returnValue(true));
144
145
        $this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context)));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\MediaBundle\Provider\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
147
        $contextModel = $this->getMock('Sonata\ClassificationBundle\Model\ContextInterface');
148
149
        $this->contextManger->expects($this->once())->method('findOneBy')->with($this->equalTo(array('id' => 'foo')))->will($this->returnValue($contextModel));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\Classifica...ontextManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
151
        $category = $this->getMock('Sonata\ClassificationBundle\Model\CategoryInterface');
152
153
        $this->categoryManger->expects($this->once())->method('getRootCategory')->with($this->equalTo($contextModel))->will($this->returnValue(null));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\MediaBundl...tegoryManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
154
        $this->categoryManger->expects($this->once())->method('create')->will($this->returnValue($category));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\MediaBundl...tegoryManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
155
        $this->categoryManger->expects($this->once())->method('save')->with($this->equalTo($category));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\MediaBundl...tegoryManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
156
157
        $output = $this->tester->execute(array('command' => $this->command->getName()));
158
159
        $this->assertRegExp('@ > default category for \'foo\' is missing, creating one\s+Done!@', $this->tester->getDisplay());
160
161
        $this->assertSame(0, $output);
162
    }
163
164
    public function testExecuteWithNew()
165
    {
166
        $context = array(
167
            'providers' => array(),
168
            'formats' => array(),
169
            'download' => array(),
170
        );
171
172
        $this->container->method('has')->with($this->equalTo('sonata.media.manager.category'))
173
            ->will($this->returnValue(true));
174
175
        $this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context)));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\MediaBundle\Provider\Pool>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
176
177
        $contextModel = $this->getMock('Sonata\ClassificationBundle\Model\ContextInterface');
178
179
        $this->contextManger->expects($this->once())->method('findOneBy')->with($this->equalTo(array('id' => 'foo')))->will($this->returnValue(null));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\Classifica...ontextManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
180
        $this->contextManger->expects($this->once())->method('create')->will($this->returnValue($contextModel));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\Classifica...ontextManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
181
        $this->contextManger->expects($this->once())->method('save')->with($this->equalTo($contextModel));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\Classifica...ontextManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
182
183
        $category = $this->getMock('Sonata\ClassificationBundle\Model\CategoryInterface');
184
185
        $this->categoryManger->expects($this->once())->method('getRootCategory')->with($this->equalTo($contextModel))->will($this->returnValue(null));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\MediaBundl...tegoryManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
186
        $this->categoryManger->expects($this->once())->method('create')->will($this->returnValue($category));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\MediaBundl...tegoryManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
187
        $this->categoryManger->expects($this->once())->method('save')->with($this->equalTo($category));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\MediaBundl...tegoryManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
188
189
        $output = $this->tester->execute(array('command' => $this->command->getName()));
190
191
        $this->assertRegExp('@ > default category for \'foo\' is missing, creating one\s+Done!@', $this->tester->getDisplay());
192
193
        $this->assertSame(0, $output);
194
    }
195
}
196