Passed
Branch dev (f56f10)
by Wilmer
04:41 queued 01:34
created

IsOneOfAssert   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 9 1
A matches() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\TestSupport;
6
7
use PHPUnit\Framework\Constraint\Constraint;
8
use Yiisoft\VarDumper\VarDumper;
9
10
use function array_map;
11
use function implode;
12
use function in_array;
13
14
/**
15
 * IsOneOfAssert asserts that the value is one of the expected values.
16
 */
17
final class IsOneOfAssert extends Constraint
18
{
19
    private array $allowedValues;
20
21
    public function __construct(array $allowedValues)
22
    {
23
        $this->allowedValues = $allowedValues;
24
    }
25
26
    /**
27
     * Returns a string representation of the object.
28
     *
29
     * @return string
30
     */
31
    public function toString(): string
32
    {
33
        $allowedValues = array_map(static function ($value) {
34
            return VarDumper::create($value)->asString();
35
        }, $this->allowedValues);
36
37
        $expectedAsString = implode(', ', $allowedValues);
38
39
        return "is one of $expectedAsString";
40
    }
41
42
    protected function matches($other): bool
43
    {
44
        return in_array($other, $this->allowedValues, false);
45
    }
46
}
47