Completed
Pull Request — master (#917)
by Dmitry
08:52
created

ResetCommandTest::testResetAllIndexTemplates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace FOS\ElasticaBundle\Tests\Command;
4
5
use FOS\ElasticaBundle\Command\ResetCommand;
6
use FOS\ElasticaBundle\Index\IndexManager;
7
use FOS\ElasticaBundle\Index\Resetter;
8
use Symfony\Component\Console\Input\ArrayInput;
9
use Symfony\Component\Console\Output\NullOutput;
10
use Symfony\Component\DependencyInjection\Container;
11
12
class ResetCommandTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @var \PHPUnit_Framework_MockObject_MockObject|ResetCommand
16
     */
17
    private $command;
18
19
    /**
20
     * @var \PHPUnit_Framework_MockObject_MockObject|Resetter
21
     */
22
    private $resetter;
23
24
    /**
25
     * @var \PHPUnit_Framework_MockObject_MockObject|IndexManager
26
     */
27
    private $indexManager;
28
29
    public function setup()
30
    {
31
        $container = new Container();
32
33
        $this->resetter = $this->getMockBuilder('\FOS\ElasticaBundle\Resetter')
34
            ->disableOriginalConstructor()
35
            ->setMethods(array('resetIndex', 'resetIndexType', 'resetAllTemplates', 'resetTemplate'))
36
            ->getMock();
37
38
        $container->set('fos_elastica.resetter', $this->resetter);
39
40
        $this->indexManager = $this->getMockBuilder('\FOS\ElasticaBundle\IndexManager')
41
            ->disableOriginalConstructor()
42
            ->setMethods(array('getAllIndexes'))
43
            ->getMock();
44
45
        $container->set('fos_elastica.index_manager', $this->indexManager);
46
47
        $this->command = new ResetCommand();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \FOS\ElasticaBundle\Command\ResetCommand() of type object<FOS\ElasticaBundle\Command\ResetCommand> is incompatible with the declared type object<PHPUnit_Framework_MockObject_MockObject> 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...
48
        $this->command->setContainer($container);
49
    }
50
51
    public function testResetAllIndexes()
52
    {
53
        $this->indexManager->expects($this->any())
54
            ->method('getAllIndexes')
55
            ->will($this->returnValue(array('index1' => true, 'index2' => true)));
56
57
        $this->resetter->expects($this->at(0))
58
            ->method('resetAllTemplates');
59
60
        $this->resetter->expects($this->at(1))
61
            ->method('resetIndex')
62
            ->with($this->equalTo('index1'));
63
64
        $this->resetter->expects($this->at(2))
65
            ->method('resetIndex')
66
            ->with($this->equalTo('index2'));
67
68
        $this->command->run(
69
            new ArrayInput(array()),
70
            new NullOutput()
71
        );
72
    }
73
74
    public function testResetIndex()
75
    {
76
        $this->indexManager->expects($this->never())
77
            ->method('getAllIndexes');
78
79
        $this->resetter->expects($this->at(0))
80
            ->method('resetAllTemplates');
81
82
        $this->resetter->expects($this->at(1))
83
            ->method('resetIndex')
84
            ->with($this->equalTo('index1'));
85
86
        $this->command->run(
87
            new ArrayInput(array('--index' => 'index1')),
88
            new NullOutput()
89
        );
90
    }
91
92
    public function testResetIndexType()
93
    {
94
        $this->indexManager->expects($this->never())
95
            ->method('getAllIndexes');
96
97
        $this->resetter->expects($this->never())
98
            ->method('resetIndex');
99
100
        $this->resetter->expects($this->at(0))
101
            ->method('resetAllTemplates');
102
103
        $this->resetter->expects($this->at(1))
104
            ->method('resetIndexType')
105
            ->with($this->equalTo('index1'), $this->equalTo('type1'));
106
107
        $this->command->run(
108
            new ArrayInput(array('--index' => 'index1', '--type' => 'type1')),
109
            new NullOutput()
110
        );
111
    }
112
113
    public function testResetAllIndexTemplates()
114
    {
115
        $this->resetter->expects($this->once())
116
            ->method('resetAllTemplates')
117
            ->with(false)
118
        ;
119
120
        $this->resetter->expects($this->never())
121
            ->method('resetIndex');
122
123
        $this->command->run(
124
            new ArrayInput(array('--index-template' => null)),
125
            new NullOutput()
126
        );
127
    }
128
129 View Code Duplication
    public function testResetAndDeleteAllIndexTemplates()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        $this->resetter->expects($this->once())
132
            ->method('resetAllTemplates')
133
            ->with(true)
134
        ;
135
136
        $this->resetter->expects($this->never())
137
            ->method('resetIndex');
138
139
        $this->command->run(
140
            new ArrayInput(array('--index-template' => null, '--delete-template-indexes' => null)),
141
            new NullOutput()
142
        );
143
    }
144
145
    public function testResetOneIndexTemplate()
146
    {
147
        $this->resetter->expects($this->once())
148
            ->method('resetTemplate')
149
            ->with('template name', false)
150
        ;
151
152
        $this->resetter->expects($this->never())
153
            ->method('resetIndex');
154
155
        $this->command->run(
156
            new ArrayInput(array('--index-template' => 'template name')),
157
            new NullOutput()
158
        );
159
    }
160
161 View Code Duplication
    public function testResetAndDeleteOneIndexTemplate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
    {
163
        $this->resetter->expects($this->once())
164
            ->method('resetTemplate')
165
            ->with('template name', true)
166
        ;
167
168
        $this->resetter->expects($this->never())
169
            ->method('resetIndex');
170
171
        $this->command->run(
172
            new ArrayInput(array('--index-template' => 'template name', '--delete-template-indexes' => null)),
173
            new NullOutput()
174
        );
175
    }
176
177
    /**
178
     * @expectedException \InvalidArgumentException
179
     */
180
    public function testThrowExceptionWhenIndexAndIndexTemplateProvidedInSameTime()
181
    {
182
        $this->command->run(
183
            new ArrayInput(array('--index-template' => null, '--index' => 'some template')),
184
            new NullOutput()
185
        );
186
    }
187
}
188