Completed
Push — 6.13.7 ( b1546d )
by
unknown
14:00
created

AllValidationErrorsOccur::matches()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\API\Repository\Tests\PHPUnitConstraint;
10
11
use eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException;
12
use eZ\Publish\API\Repository\Translatable;
13
use PHPUnit\Framework\Constraint\Constraint as AbstractPHPUnitConstraint;
14
15
/**
16
 * PHPUnit constraint checking that all the given validation error messages occur in the asserted
17
 * ContentFieldValidationException.
18
 *
19
 * @see \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
20
 * @see \eZ\Publish\SPI\FieldType\ValidationError
21
 */
22
class AllValidationErrorsOccur extends AbstractPHPUnitConstraint
23
{
24
    /** @var string[] */
25
    private $expectedValidationErrorMessages;
26
27
    /**
28
     * @var string[]
29
     */
30
    private $missingValidationErrorMessages = [];
31
32
    /**
33
     * @param string[] $expectedValidationErrorMessages
34
     */
35
    public function __construct(array $expectedValidationErrorMessages)
36
    {
37
        $this->expectedValidationErrorMessages = $expectedValidationErrorMessages;
38
    }
39
40
    /**
41
     * @param \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $other
42
     *
43
     * @return bool
44
     */
45
    protected function matches($other): bool
46
    {
47
        $allFieldErrors = $this->extractAllFieldErrorMessages($other);
48
49
        $this->missingValidationErrorMessages = array_diff(
50
            $this->expectedValidationErrorMessages,
51
            $allFieldErrors
52
        );
53
54
        return empty($this->missingValidationErrorMessages);
55
    }
56
57
    /**
58
     * @param \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $exception
59
     *
60
     * @return string[]
61
     */
62
    private function extractAllFieldErrorMessages(ContentFieldValidationException $exception): array
63
    {
64
        $allFieldErrors = [];
65
        foreach ($exception->getFieldErrors() as $errors) {
66
            foreach ($errors as $fieldErrors) {
67
                $allFieldErrors = array_merge(
68
                    $allFieldErrors,
69
                    array_map(
70
                        function (Translatable $translatableFieldError) {
71
                            return $translatableFieldError->getTranslatableMessage()->message;
0 ignored issues
show
Documentation introduced by
The property message does not exist on object<eZ\Publish\API\Re...ory\Values\Translation>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
72
                        },
73
                        $fieldErrors
74
                    )
75
                );
76
            }
77
        }
78
79
        return $allFieldErrors;
80
    }
81
82
    /**
83
     * @param \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $other
84
     *
85
     * @return string
86
     */
87
    protected function failureDescription($other): string
88
    {
89
        return sprintf(
90
            "the following Content Field validation error messages:\n%s\n%s",
91
            var_export($this->extractAllFieldErrorMessages($other), true),
92
            $this->toString()
93
        );
94
    }
95
96
    /**
97
     * Returns a string representation of the object.
98
     *
99
     * @return string
100
     */
101
    public function toString(): string
102
    {
103
        $messages = implode(', ', $this->missingValidationErrorMessages);
104
105
        return "contain the messages: '{$messages}'";
106
    }
107
}
108