1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SmartWeb\ModuleTesting\Assertions; |
5
|
|
|
|
6
|
|
|
use Closure; |
7
|
|
|
use SmartWeb\ModuleTesting\DomainAssert\DomainRule; |
8
|
|
|
use function call_user_func; |
9
|
|
|
use function call_user_func_array; |
10
|
|
|
use function is_callable; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait ArrayPluck |
15
|
|
|
* |
16
|
|
|
* @package SmartWeb\Testing\Assertions |
17
|
|
|
*/ |
18
|
|
|
trait ArrayPluck |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string|callable $plucker |
23
|
|
|
* @param array $input |
24
|
|
|
* @param int|string|array $keys |
25
|
|
|
* @param array $expected |
26
|
|
|
*/ |
27
|
|
|
protected function assertArrayWasCorrectlyPlucked($plucker, array $input, $keys, array $expected) |
28
|
|
|
{ |
29
|
|
|
$plucker = is_callable($plucker) || ($plucker instanceof Closure) |
30
|
|
|
? $plucker |
31
|
|
|
: [$this, $plucker]; |
32
|
|
|
|
33
|
|
|
$actual = call_user_func_array($plucker, [$input, $keys]); |
34
|
|
|
|
35
|
|
|
$this->assertThat( |
|
|
|
|
36
|
|
|
compact('actual', 'expected'), |
37
|
|
|
new DomainRule("actual == expected") |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param callable|string $plucker |
43
|
|
|
* @param array $input |
44
|
|
|
* @param int|string|array $keys |
45
|
|
|
* @param array $expected |
46
|
|
|
*/ |
47
|
|
|
protected function assertArrayWasCorrectlyPlucked2($plucker, array $input, $keys, array $expected) |
48
|
|
|
{ |
49
|
|
|
$plucker = is_callable($plucker) || ($plucker instanceof Closure) |
50
|
|
|
? $plucker |
51
|
|
|
: [$this, $plucker]; |
52
|
|
|
|
53
|
|
|
$plucker = function ($input, $keys) use ($plucker) |
54
|
|
|
{ |
55
|
|
|
return call_user_func($plucker, $input, $keys); |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
$params = [$input, $keys]; |
59
|
|
|
|
60
|
|
|
$this->assertThat( |
|
|
|
|
61
|
|
|
compact('input', 'keys', 'expected', 'plucker', 'params'), |
62
|
|
|
new DomainRule("call(plucker, params) == expected") |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.