Completed
Push — master ( e1eba3...584ae4 )
by Marc
02:55
created

TestUtilityTrait::stub()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 11
cp 0.8182
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 2
crap 3.054
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
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
     * @return \PHPUnit_Framework_MockObject_MockObject
33
     */
34 2
    public function stub($class, array $methods = [])
35
    {
36 2
        $stub = $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...
37 2
            ->disableOriginalConstructor()
38 2
            ->getMock();
39
40 2
        foreach ($methods as $method => $value) {
41 1
            if (is_callable($value)) {
42
                $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...
43
            } else {
44 1
                $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...
45
            }
46 2
        }
47
48 2
        return $stub;
49
    }
50
}
51