Passed
Pull Request — master (#380)
by Wilmer
02:46
created

IsOneOfAssert   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 25
rs 10
wmc 4

3 Methods

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