TestUtilityTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 46
ccs 9
cts 11
cp 0.8182
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B stub() 0 28 6
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