| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of PHPUnit.
|
|
|
4 |
*
|
|
|
5 |
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
namespace PHPUnit\Framework\Constraint;
|
|
|
11 |
|
|
|
12 |
use function get_class;
|
|
|
13 |
use function is_object;
|
|
|
14 |
use PHPUnit\Framework\ActualValueIsNotAnObjectException;
|
|
|
15 |
use PHPUnit\Framework\ComparisonMethodDoesNotAcceptParameterTypeException;
|
|
|
16 |
use PHPUnit\Framework\ComparisonMethodDoesNotDeclareBoolReturnTypeException;
|
|
|
17 |
use PHPUnit\Framework\ComparisonMethodDoesNotDeclareExactlyOneParameterException;
|
|
|
18 |
use PHPUnit\Framework\ComparisonMethodDoesNotDeclareParameterTypeException;
|
|
|
19 |
use PHPUnit\Framework\ComparisonMethodDoesNotExistException;
|
|
|
20 |
use ReflectionNamedType;
|
|
|
21 |
use ReflectionObject;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
|
|
|
25 |
*/
|
|
|
26 |
final class ObjectEquals extends Constraint
|
|
|
27 |
{
|
|
|
28 |
/**
|
|
|
29 |
* @var object
|
|
|
30 |
*/
|
|
|
31 |
private $expected;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* @var string
|
|
|
35 |
*/
|
|
|
36 |
private $method;
|
|
|
37 |
|
|
|
38 |
public function __construct(object $object, string $method = 'equals')
|
|
|
39 |
{
|
|
|
40 |
$this->expected = $object;
|
|
|
41 |
$this->method = $method;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
public function toString(): string
|
|
|
45 |
{
|
|
|
46 |
return 'two objects are equal';
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* @throws ActualValueIsNotAnObjectException
|
|
|
51 |
* @throws ComparisonMethodDoesNotAcceptParameterTypeException
|
|
|
52 |
* @throws ComparisonMethodDoesNotDeclareBoolReturnTypeException
|
|
|
53 |
* @throws ComparisonMethodDoesNotDeclareExactlyOneParameterException
|
|
|
54 |
* @throws ComparisonMethodDoesNotDeclareParameterTypeException
|
|
|
55 |
* @throws ComparisonMethodDoesNotExistException
|
|
|
56 |
*/
|
|
|
57 |
protected function matches($other): bool
|
|
|
58 |
{
|
|
|
59 |
if (!is_object($other)) {
|
|
|
60 |
throw new ActualValueIsNotAnObjectException;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
$object = new ReflectionObject($other);
|
|
|
64 |
|
|
|
65 |
if (!$object->hasMethod($this->method)) {
|
|
|
66 |
throw new ComparisonMethodDoesNotExistException(
|
|
|
67 |
get_class($other),
|
|
|
68 |
$this->method
|
|
|
69 |
);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/** @noinspection PhpUnhandledExceptionInspection */
|
|
|
73 |
$method = $object->getMethod($this->method);
|
|
|
74 |
|
|
|
75 |
if (!$method->hasReturnType()) {
|
|
|
76 |
throw new ComparisonMethodDoesNotDeclareBoolReturnTypeException(
|
|
|
77 |
get_class($other),
|
|
|
78 |
$this->method
|
|
|
79 |
);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
$returnType = $method->getReturnType();
|
|
|
83 |
|
|
|
84 |
if (!$returnType instanceof ReflectionNamedType) {
|
|
|
85 |
throw new ComparisonMethodDoesNotDeclareBoolReturnTypeException(
|
|
|
86 |
get_class($other),
|
|
|
87 |
$this->method
|
|
|
88 |
);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
if ($returnType->allowsNull()) {
|
|
|
92 |
throw new ComparisonMethodDoesNotDeclareBoolReturnTypeException(
|
|
|
93 |
get_class($other),
|
|
|
94 |
$this->method
|
|
|
95 |
);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
if ($returnType->getName() !== 'bool') {
|
|
|
99 |
throw new ComparisonMethodDoesNotDeclareBoolReturnTypeException(
|
|
|
100 |
get_class($other),
|
|
|
101 |
$this->method
|
|
|
102 |
);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
if ($method->getNumberOfParameters() !== 1 || $method->getNumberOfRequiredParameters() !== 1) {
|
|
|
106 |
throw new ComparisonMethodDoesNotDeclareExactlyOneParameterException(
|
|
|
107 |
get_class($other),
|
|
|
108 |
$this->method
|
|
|
109 |
);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
$parameter = $method->getParameters()[0];
|
|
|
113 |
|
|
|
114 |
if (!$parameter->hasType()) {
|
|
|
115 |
throw new ComparisonMethodDoesNotDeclareParameterTypeException(
|
|
|
116 |
get_class($other),
|
|
|
117 |
$this->method
|
|
|
118 |
);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
$type = $parameter->getType();
|
|
|
122 |
|
|
|
123 |
if (!$type instanceof ReflectionNamedType) {
|
|
|
124 |
throw new ComparisonMethodDoesNotDeclareParameterTypeException(
|
|
|
125 |
get_class($other),
|
|
|
126 |
$this->method
|
|
|
127 |
);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
$typeName = $type->getName();
|
|
|
131 |
|
|
|
132 |
if ($typeName === 'self') {
|
|
|
133 |
$typeName = get_class($other);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
if (!$this->expected instanceof $typeName) {
|
|
|
137 |
throw new ComparisonMethodDoesNotAcceptParameterTypeException(
|
|
|
138 |
get_class($other),
|
|
|
139 |
$this->method,
|
|
|
140 |
get_class($this->expected)
|
|
|
141 |
);
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
return $other->{$this->method}($this->expected);
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
protected function failureDescription($other): string
|
|
|
148 |
{
|
|
|
149 |
return $this->toString();
|
|
|
150 |
}
|
|
|
151 |
}
|