Completed
Push — master ( bd1580...0ea8fb )
by Florian
09:56
created

ClearIndexCommandTest::testExecute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Stinger Entity Search package.
5
 *
6
 * (c) Oliver Kotte <[email protected]>
7
 * (c) Florian Meyer <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace StingerSoft\EntitySearchBundle\Tests\Command;
14
15
use PHPUnit\Framework\MockObject\MockObject;
16
use PHPUnit\Framework\TestCase;
17
use StingerSoft\EntitySearchBundle\Command\ClearIndexCommand;
18
use StingerSoft\EntitySearchBundle\Services\SearchService;
19
use Symfony\Component\Console\Application;
20
use Symfony\Component\Console\Tester\CommandTester;
21
22
class ClearIndexCommandTest extends TestCase {
23
24
	public function testExecute(): void {
25
		/**
26
		 * @var $searchService SearchService|MockObject
27
		 */
28
		$searchService = $this->getMockBuilder(SearchService::class)->setMethods(array(
29
			'clearIndex',
30
		))->disableOriginalConstructor()->getMockForAbstractClass();
31
32
		$searchService->expects($this->once())->method('clearIndex');
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in StingerSoft\EntitySearch...\Services\SearchService.

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...
33
34
		$clearCommand = new ClearIndexCommand($searchService);
0 ignored issues
show
Bug introduced by
It seems like $searchService can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, StingerSoft\EntitySearch...xCommand::__construct() does only seem to accept object<StingerSoft\Entit...Services\SearchService>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
35
36
		$application = new Application();
37
		$application->add($clearCommand);
38
39
		$command = $application->find('stinger:search:clear');
40
41
		$commandTester = new CommandTester($command);
42
43
		$commandTester->execute(array(
44
			'command' => 'stinger:search:clear',
45
		));
46
	}
47
}