Test Failed
Push — master ( 2424f5...f04472 )
by Marc
03:02
created

TestUtilityTrait::stub()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.288

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 8
cts 10
cp 0.8
rs 8.439
c 0
b 0
f 0
cc 6
eloc 19
nc 12
nop 4
crap 6.288
1
<?php
2
/*
3
 * This file is part of the phpflo/phpflo-flowtrace package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
namespace PhpFlo\Test;
12
13
/**
14
 * Mock class helper.
15
 *
16
 * @package PhpFlo\Test
17
 * @author Marc Aschmann <[email protected]>
18
 */
19
trait TestUtilityTrait
20
{
21
    /**
22
     * Will create a stub with several methods and defined return values.
23
     * definition:
24
     * [
25
     *   'myMethod' => 'somevalue',
26
     *   'myOtherMethod' => $callback,
27
     *   'anotherMethod' => function ($x) use ($y) {},
28
     * ]
29
     *
30
     * @param string $class
31
     * @param array $methods
32
     * @param string $className classname for mock object
33
     * @param bool $forceMethods
34 2
     * @return \PHPUnit_Framework_MockObject_MockObject
35
     */
36 2
    protected function stub(
37 2
        string $class,
38 2
        array $methods = [],
39
        string $className = '',
40 2
        bool $forceMethods = false
41 1
    ) : \PHPUnit_Framework_MockObject_MockObject {
42
        $builder = $this->getMockBuilder($class)
0 ignored issues
show
Bug introduced by
It seems like getMockBuilder() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
43
            ->disableOriginalConstructor();
44 1
45
        if (!empty($methods) && $forceMethods) {
46 2
            $builder->setMethods(array_keys($methods));
47
        }
48 2
49
        if ('' !== $className) {
50
            $builder->setMockClassName($className);
51
        }
52
53
        $stub = $builder->getMock();
54
        foreach ($methods as $method => $value) {
55
            if (is_callable($value)) {
56
                $stub->expects($this->any())->method($method)->willReturnCallback($value);
0 ignored issues
show
Bug introduced by
It seems like any() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
57
            } else {
58
                $stub->expects($this->any())->method($method)->willReturn($value);
0 ignored issues
show
Bug introduced by
It seems like any() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
59
            }
60
        }
61
62
        return $stub;
63
    }
64
}
65