Completed
Push — master ( a787fa...dc9af5 )
by Vincent
05:35
created

AssertErrors::assertErrorsContains()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 18
rs 9.9
cc 3
nc 4
nop 3
1
<?php
2
3
namespace VGirol\JsonApiAssert\Asserts\Content;
4
5
use PHPUnit\Framework\Assert as PHPUnit;
6
use PHPUnit\Framework\ExpectationFailedException;
7
use VGirol\JsonApiAssert\Messages;
8
9
trait AssertErrors
10
{
11
    /**
12
     * Asserts that an errors array contains a given subset of expected errors.
13
     *
14
     * @param array $expectedErrors
15
     * @param array $errors
16
     * @param boolean $strict   If true, unsafe characters are not allowed when checking members name.
17
     *
18
     * @throws \PHPUnit\Framework\ExpectationFailedException
19
     */
20
    public static function assertErrorsContains($expectedErrors, $errors, $strict)
21
    {
22
        try {
23
            static::assertIsValidErrorsObject($expectedErrors, $strict);
24
        } catch (ExpectationFailedException $e) {
25
            static::invalidArgument(1, 'errors object', $expectedErrors);
26
        }
27
28
        static::assertIsValidErrorsObject($errors, $strict);
29
30
        PHPUnit::assertGreaterThanOrEqual(
31
            count($expectedErrors),
32
            count($errors),
33
            Messages::ERRORS_OBJECT_CONTAINS_NOT_ENOUGH_ERRORS
34
        );
35
36
        foreach ($expectedErrors as $expectedError) {
37
            PHPUnit::assertContains($expectedError, $errors);
38
        }
39
    }
40
}
41