Completed
Push — master ( 4339a8...5a694b )
by André
70:16 queued 52:05
created

ValidationErrorOccurs::matches()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 5
nop 1
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\API\Repository\Tests\PHPUnitConstraint;
10
11
/**
12
 * PHPUnit constraint checking that the given ValidationError message occurs in asserted ContentFieldValidationException.
13
 *
14
 * @see \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
15
 * @see \eZ\Publish\SPI\FieldType\ValidationError
16
 */
17
class ValidationErrorOccurs extends \PHPUnit_Framework_Constraint
18
{
19
    /**
20
     * @var string
21
     */
22
    private $expectedValidationErrorMessage;
23
24
    /**
25
     * @param string $expectedValidationErrorMessage
26
     */
27
    public function __construct($expectedValidationErrorMessage)
28
    {
29
        parent::__construct();
30
31
        $this->expectedValidationErrorMessage = $expectedValidationErrorMessage;
32
    }
33
34
    /**
35
     * @param \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $other
36
     *
37
     * @return bool
38
     */
39
    protected function matches($other)
40
    {
41
        foreach ($other->getFieldErrors() as $fieldId => $errors) {
42
            foreach ($errors as $languageCode => $fieldErrors) {
43
                foreach ($fieldErrors as $fieldError) {
44
                    /** @var \eZ\Publish\Core\FieldType\ValidationError $fieldError */
45
                    if ($fieldError->getTranslatableMessage()->message === $this->expectedValidationErrorMessage) {
46
                        return true;
47
                    }
48
                }
49
            }
50
        }
51
52
        return false;
53
    }
54
55
    /**
56
     * @param \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $other
57
     *
58
     * @return string
59
     */
60
    protected function failureDescription($other)
61
    {
62
        return sprintf(
63
            '%s::getFieldErrors = %s %s',
64
            get_class($other),
65
            var_export($other->getFieldErrors(), true),
66
            $this->toString()
67
        );
68
    }
69
70
    /**
71
     * Returns a string representation of the object.
72
     *
73
     * @return string
74
     */
75
    public function toString()
76
    {
77
        return "contains a ValidationError with the message '{$this->expectedValidationErrorMessage}'";
78
    }
79
}
80