Completed
Pull Request — master (#170)
by Simonas
03:06
created

SettingsManagerTest::getDocumentIterator()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 101
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 101
rs 8.2857
cc 1
eloc 67
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[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 ONGR\SettingsBundle\Tests\Unit\Service;
13
14
use Doctrine\Common\Cache\PhpFileCache;
15
use ONGR\CookiesBundle\Cookie\Model\GenericCookie;
16
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
17
use ONGR\ElasticsearchBundle\Service\Manager;
18
use ONGR\ElasticsearchBundle\Service\Repository;
19
use ONGR\ElasticsearchDSL\Search;
20
use ONGR\SettingsBundle\Document\Setting;
21
use ONGR\SettingsBundle\Service\SettingsManager;
22
23
class SettingsManagerTest extends \PHPUnit_Framework_TestCase
24
{
25
26
    /**
27
     * @var Manager|\PHPUnit_Framework_MockObject_MockObject
28
     */
29
    private $manager;
30
31
    /**
32
     * @var Repository|\PHPUnit_Framework_MockObject_MockObject
33
     */
34
    private $repository;
35
36
    /**
37
     * @var PhpFileCache|\PHPUnit_Framework_MockObject_MockObject
38
     */
39
    private $cache;
40
41
    /**
42
     * @var GenericCookie|\PHPUnit_Framework_MockObject_MockObject
43
     */
44
    private $cookie;
45
46
    public function setUp()
47
    {
48
        $this->manager = $this->getMockBuilder('ONGR\ElasticsearchBundle\Service\Manager')
49
            ->disableOriginalConstructor()
50
            ->setMethods(['persist', 'commit'])
51
            ->getMock();
52
        $this->cache = $this->getMockBuilder('Doctrine\Common\Cache\PhpFileCache')
53
            ->disableOriginalConstructor()
54
            ->setMethods(['contains', 'fetch', 'save'])
55
            ->getMock();
56
        $this->cookie = $this->getMockBuilder('ONGR\CookiesBundle\Cookie\Model\GenericCookie')
57
            ->disableOriginalConstructor()
58
            ->setMethods(['getValue'])
59
            ->getMock();
60
        $this->repository = $this->getMockBuilder('ONGR\ElasticsearchBundle\Service\Repository')
61
            ->disableOriginalConstructor()
62
            ->setMethods(['findOneBy', 'remove', 'createSearch', 'execute', 'getClassName', 'getManager', 'getAggregation'])
63
            ->getMock();
64
65
        $this->repository->expects($this->any())->method('getClassName')->willReturn(Setting::class);
66
        $this->repository->expects($this->any())->method('getManager')->willReturn($this->manager);
67
        $this->repository->expects($this->any())->method('createSearch')->willReturn(new Search());
68
    }
69
70
    /**
71
     * Test get cache getter.
72
     */
73
    public function testGetCache()
74
    {
75
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
76
        $manager->setCache($this->cache);
77
78
        $this->assertInstanceOf('Doctrine\Common\Cache\PhpFileCache', $manager->getCache());
79
    }
80
81
    /**
82
     * Test get cookie getter.
83
     */
84
    public function testGetActiveProfilesCookie()
85
    {
86
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
87
        $manager->setActiveProfilesCookie($this->cookie);
88
89
        $this->assertInstanceOf('ONGR\CookiesBundle\Cookie\Model\GenericCookie', $manager->getActiveProfilesCookie());
90
    }
91
92
    /**
93
     * Test get cookie getter.
94
     */
95
    public function testGetActiveProfilesSettingName()
96
    {
97
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
98
        $manager->setActiveProfilesSettingName('ongr');
99
100
        $this->assertEquals('ongr', $manager->getActiveProfilesSettingName());
101
    }
102
103
    /**
104
     * Test setting create function.
105
     */
106
    public function testCreate()
107
    {
108
        $data = [
109
            'name' => 'acme',
110
            'type' => 'string',
111
            'value' => 'foo',
112
        ];
113
114
        $this->manager->expects($this->once())->method('persist')->with($this->callback(function ($obj) {return $obj instanceof Setting;}))->willReturn(null);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Manager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
115
        $this->manager->expects($this->once())->method('persist')->willReturn(null);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Manager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
116
117
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
118
        $document = $manager->create($data);
119
120
        foreach ($data as $key => $value) {
121
            $this->assertEquals($value, $document->{'get'.ucfirst($key)}());
122
        }
123
    }
124
125
    /**
126
     * @expectedException \LogicException
127
     * @expectedExceptionMessage Missing one of the mandatory field!
128
     */
129
    public function testCreateMandatoryParameters()
130
    {
131
        $data = ['bar' => 'foo'];
132
133
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
134
        $manager->create($data);
135
    }
136
137
    /**
138
     * @expectedException \LogicException
139
     * @expectedExceptionMessage Setting acme already exists.
140
     */
141
    public function testCreateWhenSettingExists()
142
    {
143
        $data = [
144
            'name' => 'acme',
145
            'type' => 'string',
146
            'value' => 'foo',
147
        ];
148
149
        $this->repository->expects($this->once())->method('findOneBy')->willReturn(new Setting());
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
150
151
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
152
        $manager->create($data);
153
    }
154
155
    /**
156
     * Tests setting create without value set. Should be set to 0 by default.
157
     */
158
    public function testWithoutDataValueSet()
159
    {
160
        $data = [
161
            'name' => 'acme',
162
            'type' => 'string',
163
        ];
164
165
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
166
        $document = $manager->create($data);
167
168
        $this->assertEquals(0, $document->getValue());
169
    }
170
171
    /**
172
     * Tests setting update.
173
     */
174
    public function testUpdate()
175
    {
176
        $setting = new Setting();
177
        $setting->setName('acme');
178
        $setting->setValue('foo');
179
180
        $this->repository->expects($this->once())->method('findOneBy')->willReturn($setting);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
181
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
182
183
        $document = $manager->update('acme', ['value' => 'bar']);
184
        $this->assertEquals('acme', $document->getName());
185
        $this->assertEquals('bar', $document->getValue());
186
    }
187
188
    /**
189
     * @expectedException \LogicException
190
     * @expectedExceptionMessage Setting acme not exist.
191
     */
192
    public function testUpdateWhenSettingNotExists()
193
    {
194
        $this->repository->expects($this->once())->method('findOneBy')->willReturn(null);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
195
196
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
197
        $manager->update('acme', ['value' => 'foo']);
198
    }
199
200
    /**
201
     * Tests setting delete.
202
     */
203 View Code Duplication
    public function testDelete()
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...
204
    {
205
        $setting = new Setting();
206
        $setting->setId('acme');
207
        $setting->setName('acme');
208
209
        $this->repository->expects($this->once())->method('findOneBy')->with($this->equalTo(['name' => 'acme']))->willReturn($setting);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
210
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
211
212
        $manager->delete('acme');
213
    }
214
215 View Code Duplication
    public function testHas()
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...
216
    {
217
        $setting = new Setting();
218
        $setting->setName('acme');
219
        $setting->setValue('foo');
220
221
        $this->repository->expects($this->once())->method('findOneBy')->with($this->equalTo(['name' => 'acme']))->willReturn($setting);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
222
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
223
224
        $result = $manager->has('acme');
225
226
        $this->assertTrue($result);
227
    }
228
229
    /**
230
     * Test has method when there is no setting.
231
     */
232 View Code Duplication
    public function testHasWhenThereIsNoSetting()
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...
233
    {
234
        $this->repository->expects($this->once())->method('findOneBy')->with($this->equalTo(['name' => 'acme']))->willReturn(null);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
235
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
236
237
        $result = $manager->has('acme');
238
239
        $this->assertFalse($result);
240
    }
241
242
    /**
243
     * Tests setting update.
244
     */
245 View Code Duplication
    public function testGetValue()
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...
246
    {
247
        $setting = new Setting();
248
        $setting->setName('acme');
249
        $setting->setValue('foo');
250
251
        $this->repository->expects($this->once())->method('findOneBy')->willReturn($setting);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
252
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
253
254
        $result = $manager->getValue('acme');
255
        $this->assertEquals('foo', $result);
256
    }
257
258
    /**
259
     * Tests setting update.
260
     */
261
    public function testGetValueWhenThereIsNoSetting()
262
    {
263
        $this->repository->expects($this->once())->method('findOneBy')->willReturn(null);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
264
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
265
266
        $result = $manager->getValue('acme', 'bar');
0 ignored issues
show
Documentation introduced by
'bar' is of type string, but the function expects a boolean|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
267
        $this->assertEquals('bar', $result);
268
    }
269
270
    /**
271
     * Returns document iterator with pre-loaded aggregations.
272
     *
273
     * @return DocumentIterator
274
     */
275
    private function getDocumentIterator()
276
    {
277
        $rawData = [
278
            'aggregations' => [
279
                'profiles' => [
280
                    'buckets' => [
281
                        [
282
                            'key' => 'default',
283
                            'doc_count' => 2,
284
                            'documents' => [
285
                                'hits' => [
286
                                    'total' => 2,
287
                                    'max_score' => 1,
288
                                    'hits' => [
289
                                        [
290
                                            '_index' => 'settings',
291
                                            '_type' => 'setting',
292
                                            '_id' => 'kk',
293
                                            '_score' => 1,
294
                                            '_source' => [
295
                                                'name' => 'foo',
296
                                                'profile' => [
297
                                                    'bar'
298
                                                ],
299
                                                'type' => 'bool',
300
                                                'value' => 1
301
                                            ]
302
                                        ],
303
                                        [
304
                                            '_index' => 'settings',
305
                                            '_type' => 'setting',
306
                                            '_id' => 'xx',
307
                                            '_score' => 1,
308
                                            '_source' => [
309
                                                'name' => 'kk',
310
                                                'profile' => [
311
                                                    'kk'
312
                                                ],
313
                                                'type' => 'bool',
314
                                                'value' => 1
315
                                            ]
316
                                        ]
317
                                    ]
318
                                ]
319
                            ],
320
                            'key' => 'foo',
321
                            'doc_count' => 1,
322
                            'documents' => [
323
                                'hits' => [
324
                                    'total' => 1,
325
                                    'max_score' => 1,
326
                                    'hits' => [
327
                                        [
328
                                            '_index' => 'settings',
329
                                            '_type' => 'setting',
330
                                            '_id' => 'kk',
331
                                            '_score' => 1,
332
                                            '_source' => [
333
                                                'name' => 'foo',
334
                                                'profile' => [
335
                                                    'bar'
336
                                                ],
337
                                                'type' => 'bool',
338
                                                'value' => 1
339
                                            ]
340
                                        ]
341
                                    ]
342
                                ]
343
                            ],
344
                            'key' => 'kk',
345
                            'doc_count' => 1,
346
                            'documents' => [
347
                                'hits' => [
348
                                    'total' => 1,
349
                                    'max_score' => 1,
350
                                    'hits' => [
351
                                        [
352
                                            '_index' => 'settings',
353
                                            '_type' => 'setting',
354
                                            '_id' => 'kk',
355
                                            '_score' => 1,
356
                                            '_source' => [
357
                                                'name' => 'foo',
358
                                                'profile' => [
359
                                                    'bar'
360
                                                ],
361
                                                'type' => 'bool',
362
                                                'value' => 1
363
                                            ]
364
                                        ]
365
                                    ]
366
                                ]
367
                            ]
368
                        ]
369
                    ]
370
                ]
371
            ]
372
        ];
373
374
        return new DocumentIterator($rawData, $this->manager);
375
    }
376
377
    /**
378
     * Tests setting update.
379
     */
380
    public function testGetCachedValue()
381
    {
382
383
        $activeProfilesSetting = 'active_profiles';
384
385
        $this->repository->expects($this->any())->method('findOneBy')->willReturnCallback(
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
386
            function ($arg) use ($activeProfilesSetting) {
387
                $settingName = $arg['name'];
388
                $setting = new Setting();
389
                switch ($settingName) {
390
                    case 'active_profiles':
391
                        $setting->setName($activeProfilesSetting);
392
                        $setting->setValue(['bar', 'foo']);
0 ignored issues
show
Documentation introduced by
array('bar', 'foo') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
393
                        return $setting;
394
                        break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
395
                    case 'acme':
396
                        $setting->setName('acme');
397
                        $setting->setValue('foo');
398
                        $setting->setProfile(['foo', 'default']);
399
                        break;
400
                }
401
                return $setting;
402
            }
403
        );
404
405
        $this->repository->expects($this->any())->method('execute')->willReturn($this->getDocumentIterator());
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
406
        $this->cookie->expects($this->any())->method('getValue')->willReturn(['foo']);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\CookiesBundle\Cookie\Model\GenericCookie.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
407
408
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
409
        $manager->setCache($this->cache);
410
        $manager->setActiveProfilesSettingName($activeProfilesSetting);
411
        $manager->setActiveProfilesCookie($this->cookie);
412
413
        $result = $manager->getCachedValue('acme');
414
        $this->assertEquals('foo', $result);
415
    }
416
417
    /**
418
     * Tests setting update.
419
     */
420
    public function testGetCachedValueFromCache()
421
    {
422
423
        $activeProfilesSetting = 'active_profiles';
424
425
        $this->repository->expects($this->any())->method('execute')->willReturn($this->getDocumentIterator());
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
426
        $this->cookie->expects($this->any())->method('getValue')->willReturn(['foo']);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\CookiesBundle\Cookie\Model\GenericCookie.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
427
        $this->cache->expects($this->any())->method('contains')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Cache\PhpFileCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
428
        $this->cache->expects($this->any())->method('fetch')->willReturnCallback(
429
            function ($arg) use ($activeProfilesSetting) {
430
                if ($arg == $activeProfilesSetting) {
431
                    return ['foo'];
432
                }
433
                return ['value' => 'foo', 'profiles' => ['foo']];
434
            }
435
        );
436
437
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
438
        $manager->setCache($this->cache);
439
        $manager->setActiveProfilesSettingName($activeProfilesSetting);
440
        $manager->setActiveProfilesCookie($this->cookie);
441
442
        $result = $manager->getCachedValue('acme');
443
        $this->assertEquals('foo', $result);
444
    }
445
446
    /**
447
     * Tests setting update.
448
     */
449
    public function testGetCachedValueWithoutActiveProfiles()
450
    {
451
        $activeProfilesSetting = 'active_profiles';
452
        $this->repository->expects($this->any())->method('execute')->willReturn($this->getDocumentIterator());
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
453
        $this->cookie->expects($this->any())->method('getValue')->willReturn(['foo']);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\CookiesBundle\Cookie\Model\GenericCookie.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
454
        $this->cache->expects($this->any())->method('contains')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\Common\Cache\PhpFileCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
455
        $this->cache->expects($this->any())->method('fetch')->willReturn(['value' => 'foo', 'profiles' => ['foo']]);
456
457
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
458
        $manager->setCache($this->cache);
459
        $manager->setActiveProfilesSettingName($activeProfilesSetting);
460
        $manager->setActiveProfilesCookie($this->cookie);
461
462
        $result = $manager->getCachedValue('acme', false);
463
        $this->assertEquals('foo', $result);
464
    }
465
466
    /**
467
     * Tests if there is no setting.
468
     */
469
    public function testGetCachedValueWhenItsNotExist()
470
    {
471
        $activeProfilesSetting = 'active_profiles';
472
        $this->repository->expects($this->any())->method('execute')->willReturn($this->getDocumentIterator());
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
473
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
474
        $manager->setCache($this->cache);
475
        $manager->setActiveProfilesSettingName($activeProfilesSetting);
476
477
        $value = $manager->getCachedValue('acme');
478
        $this->assertNull($value);
479
    }
480
481 View Code Duplication
    public function testGetAllProfilesNameList()
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...
482
    {
483
        $this->repository->expects($this->any())->method('execute')->willReturn($this->getDocumentIterator());
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in ONGR\ElasticsearchBundle\Service\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
484
        $manager = new SettingsManager($this->repository, $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
485
        $manager->setCache($this->cache);
486
487
        $value = $manager->getAllProfilesNameList();
488
        $this->assertEquals(['kk'], $value);
489
490
    }
491
}
492