Completed
Push — master ( c9a513...b3d424 )
by Simonas
12s
created

SettingsManagerTest   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 539
Duplicated Lines 6.68 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
c 1
b 0
f 0
lcom 1
cbo 6
dl 36
loc 539
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 27 1
A testGetCache() 0 10 1
A testGetActiveProfilesCookie() 0 10 1
A testGetActiveProfilesSettingName() 0 10 1
A testCreate() 0 23 2
A testCreateMandatoryParameters() 0 10 1
A testCreateWhenSettingExists() 0 16 1
A testWithoutDataValueSet() 0 15 1
A testUpdate() 0 16 1
A testUpdateWhenSettingNotExists() 0 10 1
A testDelete() 11 15 1
A testHas() 13 17 1
A testGetValue() 12 15 1
A testGetValueWhenThereIsNoSetting() 0 11 1
B getDocumentIterator() 0 101 1
A testHasWhenThereIsNoSetting() 0 13 1
B testGetCachedValue() 0 40 3
B testGetCachedValueFromCache() 0 28 2
A testGetCachedValueWithoutActiveProfiles() 0 19 1
A testGetCachedValueWhenItsNotExist() 0 14 1
A testGetAllActiveProfilesNameList() 0 16 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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(
63
                [
64
                    'findOneBy', 'remove', 'createSearch', 'execute', 'getClassName', 'getManager', 'getAggregation'
65
                ]
66
            )
67
            ->getMock();
68
69
        $this->repository->expects($this->any())->method('getClassName')->willReturn(Setting::class);
70
        $this->repository->expects($this->any())->method('getManager')->willReturn($this->manager);
71
        $this->repository->expects($this->any())->method('createSearch')->willReturn(new Search());
72
    }
73
74
    /**
75
     * Test get cache getter.
76
     */
77
    public function testGetCache()
78
    {
79
        $manager = new SettingsManager(
80
            $this->repository,
81
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
82
        );
83
        $manager->setCache($this->cache);
84
85
        $this->assertInstanceOf('Doctrine\Common\Cache\PhpFileCache', $manager->getCache());
86
    }
87
88
    /**
89
     * Test get cookie getter.
90
     */
91
    public function testGetActiveProfilesCookie()
92
    {
93
        $manager = new SettingsManager(
94
            $this->repository,
95
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
96
        );
97
        $manager->setActiveProfilesCookie($this->cookie);
98
99
        $this->assertInstanceOf('ONGR\CookiesBundle\Cookie\Model\GenericCookie', $manager->getActiveProfilesCookie());
100
    }
101
102
    /**
103
     * Test get cookie getter.
104
     */
105
    public function testGetActiveProfilesSettingName()
106
    {
107
        $manager = new SettingsManager(
108
            $this->repository,
109
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
110
        );
111
        $manager->setActiveProfilesSettingName('ongr');
112
113
        $this->assertEquals('ongr', $manager->getActiveProfilesSettingName());
114
    }
115
116
    /**
117
     * Test setting create function.
118
     */
119
    public function testCreate()
120
    {
121
        $data = [
122
            'name' => 'acme',
123
            'type' => 'string',
124
            'value' => 'foo',
125
        ];
126
127
        $this->manager->expects($this->once())->method('persist')->with($this->callback(function ($obj) {
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...
128
            return $obj instanceof Setting;
129
        }))->willReturn(null);
130
        $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...
131
132
        $manager = new SettingsManager(
133
            $this->repository,
134
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
135
        );
136
        $document = $manager->create($data);
137
138
        foreach ($data as $key => $value) {
139
            $this->assertEquals($value, $document->{'get' . ucfirst($key)}());
140
        }
141
    }
142
143
    /**
144
     * @expectedException \LogicException
145
     * @expectedExceptionMessage Missing one of the mandatory field!
146
     */
147
    public function testCreateMandatoryParameters()
148
    {
149
        $data = ['bar' => 'foo'];
150
151
        $manager = new SettingsManager(
152
            $this->repository,
153
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
154
        );
155
        $manager->create($data);
156
    }
157
158
    /**
159
     * @expectedException \LogicException
160
     * @expectedExceptionMessage Setting acme already exists.
161
     */
162
    public function testCreateWhenSettingExists()
163
    {
164
        $data = [
165
            'name' => 'acme',
166
            'type' => 'string',
167
            'value' => 'foo',
168
        ];
169
170
        $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...
171
172
        $manager = new SettingsManager(
173
            $this->repository,
174
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
175
        );
176
        $manager->create($data);
177
    }
178
179
    /**
180
     * Tests setting create without value set. Should be set to 0 by default.
181
     */
182
    public function testWithoutDataValueSet()
183
    {
184
        $data = [
185
            'name' => 'acme',
186
            'type' => 'string',
187
        ];
188
189
        $manager = new SettingsManager(
190
            $this->repository,
191
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
192
        );
193
        $document = $manager->create($data);
194
195
        $this->assertEquals(0, $document->getValue());
196
    }
197
198
    /**
199
     * Tests setting update.
200
     */
201
    public function testUpdate()
202
    {
203
        $setting = new Setting();
204
        $setting->setName('acme');
205
        $setting->setValue('foo');
206
207
        $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...
208
        $manager = new SettingsManager(
209
            $this->repository,
210
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
211
        );
212
213
        $document = $manager->update('acme', ['value' => 'bar']);
214
        $this->assertEquals('acme', $document->getName());
215
        $this->assertEquals('bar', $document->getValue());
216
    }
217
218
    /**
219
     * @expectedException \LogicException
220
     * @expectedExceptionMessage Setting acme not exist.
221
     */
222
    public function testUpdateWhenSettingNotExists()
223
    {
224
        $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...
225
226
        $manager = new SettingsManager(
227
            $this->repository,
228
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
229
        );
230
        $manager->update('acme', ['value' => 'foo']);
231
    }
232
233
    /**
234
     * Tests setting delete.
235
     */
236 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...
237
    {
238
        $setting = new Setting();
239
        $setting->setId('acme');
240
        $setting->setName('acme');
241
242
        $this->repository->expects($this->any())
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...
243
            ->method('findOneBy')->with($this->equalTo(['name' => 'acme']))->willReturn($setting);
244
        $manager = new SettingsManager(
245
            $this->repository,
246
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
247
        );
248
249
        $manager->delete('acme');
250
    }
251
252 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...
253
    {
254
        $setting = new Setting();
255
        $setting->setName('acme');
256
        $setting->setValue('foo');
257
258
        $this->repository->expects($this->once())
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...
259
            ->method('findOneBy')->with($this->equalTo(['name' => 'acme']))->willReturn($setting);
260
        $manager = new SettingsManager(
261
            $this->repository,
262
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
263
        );
264
265
        $result = $manager->has('acme');
266
267
        $this->assertTrue($result);
268
    }
269
270
    /**
271
     * Test has method when there is no setting.
272
     */
273
    public function testHasWhenThereIsNoSetting()
274
    {
275
        $this->repository->expects($this->once())
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...
276
            ->method('findOneBy')->with($this->equalTo(['name' => 'acme']))->willReturn(null);
277
        $manager = new SettingsManager(
278
            $this->repository,
279
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
280
        );
281
282
        $result = $manager->has('acme');
283
284
        $this->assertFalse($result);
285
    }
286
287
    /**
288
     * Tests setting update.
289
     */
290 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...
291
    {
292
        $setting = new Setting();
293
        $setting->setName('acme');
294
        $setting->setValue('foo');
295
296
        $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...
297
        $manager = new SettingsManager(
298
            $this->repository,
299
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
300
        );
301
302
        $result = $manager->getValue('acme');
303
        $this->assertEquals('foo', $result);
304
    }
305
306
    /**
307
     * Tests setting update.
308
     */
309
    public function testGetValueWhenThereIsNoSetting()
310
    {
311
        $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...
312
        $manager = new SettingsManager(
313
            $this->repository,
314
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
315
        );
316
317
        $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...
318
        $this->assertEquals('bar', $result);
319
    }
320
321
    /**
322
     * Returns document iterator with pre-loaded aggregations.
323
     *
324
     * @return DocumentIterator
325
     */
326
    private function getDocumentIterator()
327
    {
328
        $rawData = [
329
            'aggregations' => [
330
                'profiles' => [
331
                    'buckets' => [
332
                        [
333
                            'key' => 'default',
334
                            'doc_count' => 2,
335
                            'documents' => [
336
                                'hits' => [
337
                                    'total' => 2,
338
                                    'max_score' => 1,
339
                                    'hits' => [
340
                                        [
341
                                            '_index' => 'settings',
342
                                            '_type' => 'setting',
343
                                            '_id' => 'kk',
344
                                            '_score' => 1,
345
                                            '_source' => [
346
                                                'name' => 'foo',
347
                                                'profile' => [
348
                                                    'bar'
349
                                                ],
350
                                                'type' => 'bool',
351
                                                'value' => 1
352
                                            ]
353
                                        ],
354
                                        [
355
                                            '_index' => 'settings',
356
                                            '_type' => 'setting',
357
                                            '_id' => 'xx',
358
                                            '_score' => 1,
359
                                            '_source' => [
360
                                                'name' => 'kk',
361
                                                'profile' => [
362
                                                    'kk'
363
                                                ],
364
                                                'type' => 'bool',
365
                                                'value' => 1
366
                                            ]
367
                                        ]
368
                                    ]
369
                                ]
370
                            ],
371
                            'key' => 'foo',
372
                            'doc_count' => 1,
373
                            'documents' => [
374
                                'hits' => [
375
                                    'total' => 1,
376
                                    'max_score' => 1,
377
                                    'hits' => [
378
                                        [
379
                                            '_index' => 'settings',
380
                                            '_type' => 'setting',
381
                                            '_id' => 'kk',
382
                                            '_score' => 1,
383
                                            '_source' => [
384
                                                'name' => 'foo',
385
                                                'profile' => [
386
                                                    'bar'
387
                                                ],
388
                                                'type' => 'bool',
389
                                                'value' => 1
390
                                            ]
391
                                        ]
392
                                    ]
393
                                ]
394
                            ],
395
                            'key' => 'kk',
396
                            'doc_count' => 1,
397
                            'documents' => [
398
                                'hits' => [
399
                                    'total' => 1,
400
                                    'max_score' => 1,
401
                                    'hits' => [
402
                                        [
403
                                            '_index' => 'settings',
404
                                            '_type' => 'setting',
405
                                            '_id' => 'kk',
406
                                            '_score' => 1,
407
                                            '_source' => [
408
                                                'name' => 'foo',
409
                                                'profile' => [
410
                                                    'bar'
411
                                                ],
412
                                                'type' => 'bool',
413
                                                'value' => 1
414
                                            ]
415
                                        ]
416
                                    ]
417
                                ]
418
                            ]
419
                        ]
420
                    ]
421
                ]
422
            ]
423
        ];
424
425
        return new DocumentIterator($rawData, $this->manager);
426
    }
427
428
    /**
429
     * Tests setting update.
430
     */
431
    public function testGetCachedValue()
432
    {
433
434
        $activeProfilesSetting = 'active_profiles';
435
436
        $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...
437
            function ($arg) use ($activeProfilesSetting) {
438
                $settingName = $arg['name'];
439
                $setting = new Setting();
440
                switch ($settingName) {
441
                    case 'active_profiles':
442
                        $setting->setName($activeProfilesSetting);
443
                        $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...
444
                        return $setting;
445
                        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...
446
                    case 'acme':
447
                        $setting->setName('acme');
448
                        $setting->setValue('foo');
449
                        $setting->setProfile(['foo', 'default']);
450
                        break;
451
                }
452
                return $setting;
453
            }
454
        );
455
456
        $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...
457
        $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...
458
459
        $manager = new SettingsManager(
460
            $this->repository,
461
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
462
        );
463
        $manager->setCache($this->cache);
464
        $manager->setActiveProfilesSettingName($activeProfilesSetting);
465
        $manager->setActiveProfilesCookie($this->cookie);
466
        $manager->setActiveProfilesList(['default']);
467
468
        $result = $manager->getCachedValue('acme');
469
        $this->assertEquals('foo', $result);
470
    }
471
472
    /**
473
     * Tests setting update.
474
     */
475
    public function testGetCachedValueFromCache()
476
    {
477
478
        $activeProfilesSetting = 'active_profiles';
479
480
        $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...
481
        $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...
482
        $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...
483
        $this->cache->expects($this->any())->method('fetch')->willReturnCallback(
484
            function ($arg) use ($activeProfilesSetting) {
485
                if ($arg == $activeProfilesSetting) {
486
                    return ['foo'];
487
                }
488
                return ['value' => 'foo', 'profiles' => ['foo']];
489
            }
490
        );
491
492
        $manager = new SettingsManager(
493
            $this->repository,
494
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
495
        );
496
        $manager->setCache($this->cache);
497
        $manager->setActiveProfilesSettingName($activeProfilesSetting);
498
        $manager->setActiveProfilesCookie($this->cookie);
499
500
        $result = $manager->getCachedValue('acme');
501
        $this->assertEquals('foo', $result);
502
    }
503
504
    /**
505
     * Tests setting update.
506
     */
507
    public function testGetCachedValueWithoutActiveProfiles()
508
    {
509
        $activeProfilesSetting = 'active_profiles';
510
        $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...
511
        $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...
512
        $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...
513
        $this->cache->expects($this->any())->method('fetch')->willReturn(['value' => 'foo', 'profiles' => ['foo']]);
514
515
        $manager = new SettingsManager(
516
            $this->repository,
517
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
518
        );
519
        $manager->setCache($this->cache);
520
        $manager->setActiveProfilesSettingName($activeProfilesSetting);
521
        $manager->setActiveProfilesCookie($this->cookie);
522
523
        $result = $manager->getCachedValue('acme', false);
524
        $this->assertEquals('foo', $result);
525
    }
526
527
    /**
528
     * Tests if there is no setting.
529
     */
530
    public function testGetCachedValueWhenItsNotExist()
531
    {
532
        $activeProfilesSetting = 'active_profiles';
533
        $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...
534
        $manager = new SettingsManager(
535
            $this->repository,
536
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
537
        );
538
        $manager->setCache($this->cache);
539
        $manager->setActiveProfilesSettingName($activeProfilesSetting);
540
541
        $value = $manager->getCachedValue('acme');
542
        $this->assertNull($value);
543
    }
544
545
    public function testGetAllActiveProfilesNameList()
546
    {
547
        $document = new Setting();
548
        $document->setName('active_profiles');
549
        $document->setValue(['kk']);
0 ignored issues
show
Documentation introduced by
array('kk') is of type array<integer,string,{"0":"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...
550
        $this->repository->expects($this->any())->method('findOneBy')->willReturn($document);
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...
551
        $this->repository->expects($this->any())->method('execute')->willReturn($this->getDocumentIterator());
552
        $manager = new SettingsManager(
553
            $this->repository,
554
            $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface')
555
        );
556
        $manager->setCache($this->cache);
557
558
        $value = $manager->getActiveProfiles();
559
        $this->assertEquals(['kk'], $value);
560
    }
561
}
562