ArrayPluck::assertArrayWasCorrectlyPlucked()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 4
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Assertions;
5
6
use Closure;
7
use PHPUnit\Framework\Constraint\Constraint;
8
use SmartWeb\ModuleTesting\DomainAssert\DomainRule;
9
use function call_user_func;
10
use function call_user_func_array;
11
use function is_callable;
12
13
14
/**
15
 * Trait ArrayPluck
16
 *
17
 * @package SmartWeb\ModuleTesting\Assertions
18
 */
19
trait ArrayPluck
20
{
21
    
22
    /**
23
     * @param string|callable  $plucker
24
     * @param array            $input
25
     * @param int|string|array $keys
26
     * @param array            $expected
27
     */
28
    protected function assertArrayWasCorrectlyPlucked($plucker, array $input, $keys, array $expected)
29
    {
30
        $plucker = is_callable($plucker) || ($plucker instanceof Closure)
31
            ? $plucker
32
            : [$this, $plucker];
33
        
34
        $actual = call_user_func_array($plucker, [$input, $keys]);
35
        
36
        static::assertThat(
37
            compact('actual', 'expected'),
38
            new DomainRule("actual == expected")
39
        );
40
    }
41
    
42
    /**
43
     * @param callable|string  $plucker
44
     * @param array            $input
45
     * @param int|string|array $keys
46
     * @param array            $expected
47
     */
48
    protected function assertArrayWasCorrectlyPlucked2($plucker, array $input, $keys, array $expected)
49
    {
50
        $plucker = is_callable($plucker) || ($plucker instanceof Closure)
51
            ? $plucker
52
            : [$this, $plucker];
53
        
54
        $plucker = function ($input, $keys) use ($plucker)
55
        {
56
            return call_user_func($plucker, $input, $keys);
57
        };
58
        
59
        $params = [$input, $keys];
60
        
61
        static::assertThat(
62
            compact('input', 'keys', 'expected', 'plucker', 'params'),
63
            new DomainRule("call(plucker, params) == expected")
64
        );
65
    }
66
    
67
    /**
68
     * Evaluates a PHPUnit\Framework\Constraint matcher object.
69
     *
70
     * @param mixed      $value
71
     * @param Constraint $constraint
72
     * @param string     $message
73
     */
74
    abstract public static function assertThat($value, Constraint $constraint, $message = '');
75
}
76