Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
EvaluateTrait
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
12
100.00% covered (success)
100.00%
1 / 1
 evaluate
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
5
 evaluateSuccess
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
7
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Testing Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\Testing\Constraints;
11
12use SebastianBergmann\Comparator\ComparisonFailure;
13
14/**
15 * Trait EvaluateTrait.
16 *
17 * @package testing
18 */
19trait EvaluateTrait
20{
21    public function evaluate(
22        mixed $other,
23        string $description = '',
24        bool $returnResult = false
25    ) : ?bool {
26        $success = $this->evaluateSuccess($other);
27        if ($returnResult) {
28            return $success;
29        }
30        if ( ! $success) {
31            $comparison = null;
32            if (\is_string($this->value) && \is_string($other)) {
33                $comparison = new ComparisonFailure(
34                    $this->value,
35                    $other,
36                    \sprintf("'%s'", $this->value),
37                    \sprintf("'%s'", $other)
38                );
39            }
40            $this->fail($other, $description, $comparison);
41        }
42        return null;
43    }
44
45    private function evaluateSuccess(mixed $other) : bool
46    {
47        if (\is_float($this->value) && \is_float($other) &&
48            ! \is_infinite($this->value) && ! \is_infinite($other) &&
49            ! \is_nan($this->value) && ! \is_nan($other)) {
50            return \abs($this->value - $other) < \PHP_FLOAT_EPSILON;
51        }
52        return $this->value === $other;
53    }
54}