Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Testing\Constraints;
4
 
5
use ArrayObject;
6
use PHPUnit\Framework\Constraint\Constraint;
7
use PHPUnit\Runner\Version;
8
use SebastianBergmann\Comparator\ComparisonFailure;
9
use Traversable;
10
 
11
if (class_exists(Version::class) && (int) Version::series()[0] >= 9) {
12
    /**
13
     * @internal This class is not meant to be used or overwritten outside the framework itself.
14
     */
15
    final class ArraySubset extends Constraint
16
    {
17
        /**
18
         * @var iterable
19
         */
20
        private $subset;
21
 
22
        /**
23
         * @var bool
24
         */
25
        private $strict;
26
 
27
        /**
28
         * Create a new array subset constraint instance.
29
         *
30
         * @param  iterable  $subset
31
         * @param  bool  $strict
32
         * @return void
33
         */
34
        public function __construct(iterable $subset, bool $strict = false)
35
        {
36
            $this->strict = $strict;
37
            $this->subset = $subset;
38
        }
39
 
40
        /**
41
         * Evaluates the constraint for parameter $other.
42
         *
43
         * If $returnResult is set to false (the default), an exception is thrown
44
         * in case of a failure. null is returned otherwise.
45
         *
46
         * If $returnResult is true, the result of the evaluation is returned as
47
         * a boolean value instead: true in case of success, false in case of a
48
         * failure.
49
         *
50
         * @param  mixed  $other
51
         * @param  string  $description
52
         * @param  bool  $returnResult
53
         * @return bool|null
54
         *
55
         * @throws \PHPUnit\Framework\ExpectationFailedException
56
         * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
57
         */
58
        public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
59
        {
60
            // type cast $other & $this->subset as an array to allow
61
            // support in standard array functions.
62
            $other = $this->toArray($other);
63
            $this->subset = $this->toArray($this->subset);
64
 
65
            $patched = array_replace_recursive($other, $this->subset);
66
 
67
            if ($this->strict) {
68
                $result = $other === $patched;
69
            } else {
70
                $result = $other == $patched;
71
            }
72
 
73
            if ($returnResult) {
74
                return $result;
75
            }
76
 
77
            if (! $result) {
78
                $f = new ComparisonFailure(
79
                    $patched,
80
                    $other,
81
                    var_export($patched, true),
82
                    var_export($other, true)
83
                );
84
 
85
                $this->fail($other, $description, $f);
86
            }
87
 
88
            return null;
89
        }
90
 
91
        /**
92
         * Returns a string representation of the constraint.
93
         *
94
         * @return string
95
         *
96
         * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
97
         */
98
        public function toString(): string
99
        {
100
            return 'has the subset '.$this->exporter()->export($this->subset);
101
        }
102
 
103
        /**
104
         * Returns the description of the failure.
105
         *
106
         * The beginning of failure messages is "Failed asserting that" in most
107
         * cases. This method should return the second part of that sentence.
108
         *
109
         * @param  mixed  $other
110
         * @return string
111
         *
112
         * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
113
         */
114
        protected function failureDescription($other): string
115
        {
116
            return 'an array '.$this->toString();
117
        }
118
 
119
        /**
120
         * Returns the description of the failure.
121
         *
122
         * The beginning of failure messages is "Failed asserting that" in most
123
         * cases. This method should return the second part of that sentence.
124
         *
125
         * @param  iterable  $other
126
         * @return array
127
         */
128
        private function toArray(iterable $other): array
129
        {
130
            if (is_array($other)) {
131
                return $other;
132
            }
133
 
134
            if ($other instanceof ArrayObject) {
135
                return $other->getArrayCopy();
136
            }
137
 
138
            if ($other instanceof Traversable) {
139
                return iterator_to_array($other);
140
            }
141
 
142
            // Keep BC even if we know that array would not be the expected one
143
            return (array) $other;
144
        }
145
    }
146
} else {
147
    /**
148
     * @internal This class is not meant to be used or overwritten outside the framework itself.
149
     */
150
    final class ArraySubset extends Constraint
151
    {
152
        /**
153
         * @var iterable
154
         */
155
        private $subset;
156
 
157
        /**
158
         * @var bool
159
         */
160
        private $strict;
161
 
162
        /**
163
         * Create a new array subset constraint instance.
164
         *
165
         * @param  iterable  $subset
166
         * @param  bool  $strict
167
         * @return void
168
         */
169
        public function __construct(iterable $subset, bool $strict = false)
170
        {
171
            $this->strict = $strict;
172
            $this->subset = $subset;
173
        }
174
 
175
        /**
176
         * Evaluates the constraint for parameter $other.
177
         *
178
         * If $returnResult is set to false (the default), an exception is thrown
179
         * in case of a failure. null is returned otherwise.
180
         *
181
         * If $returnResult is true, the result of the evaluation is returned as
182
         * a boolean value instead: true in case of success, false in case of a
183
         * failure.
184
         *
185
         * @param  mixed  $other
186
         * @param  string  $description
187
         * @param  bool  $returnResult
188
         * @return bool|null
189
         *
190
         * @throws \PHPUnit\Framework\ExpectationFailedException
191
         * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
192
         */
193
        public function evaluate($other, string $description = '', bool $returnResult = false)
194
        {
195
            // type cast $other & $this->subset as an array to allow
196
            // support in standard array functions.
197
            $other = $this->toArray($other);
198
            $this->subset = $this->toArray($this->subset);
199
 
200
            $patched = array_replace_recursive($other, $this->subset);
201
 
202
            if ($this->strict) {
203
                $result = $other === $patched;
204
            } else {
205
                $result = $other == $patched;
206
            }
207
 
208
            if ($returnResult) {
209
                return $result;
210
            }
211
 
212
            if (! $result) {
213
                $f = new ComparisonFailure(
214
                    $patched,
215
                    $other,
216
                    var_export($patched, true),
217
                    var_export($other, true)
218
                );
219
 
220
                $this->fail($other, $description, $f);
221
            }
222
        }
223
 
224
        /**
225
         * Returns a string representation of the constraint.
226
         *
227
         * @return string
228
         *
229
         * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
230
         */
231
        public function toString(): string
232
        {
233
            return 'has the subset '.$this->exporter()->export($this->subset);
234
        }
235
 
236
        /**
237
         * Returns the description of the failure.
238
         *
239
         * The beginning of failure messages is "Failed asserting that" in most
240
         * cases. This method should return the second part of that sentence.
241
         *
242
         * @param  mixed  $other
243
         * @return string
244
         *
245
         * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
246
         */
247
        protected function failureDescription($other): string
248
        {
249
            return 'an array '.$this->toString();
250
        }
251
 
252
        /**
253
         * Returns the description of the failure.
254
         *
255
         * The beginning of failure messages is "Failed asserting that" in most
256
         * cases. This method should return the second part of that sentence.
257
         *
258
         * @param  iterable  $other
259
         * @return array
260
         */
261
        private function toArray(iterable $other): array
262
        {
263
            if (is_array($other)) {
264
                return $other;
265
            }
266
 
267
            if ($other instanceof ArrayObject) {
268
                return $other->getArrayCopy();
269
            }
270
 
271
            if ($other instanceof Traversable) {
272
                return iterator_to_array($other);
273
            }
274
 
275
            // Keep BC even if we know that array would not be the expected one
276
            return (array) $other;
277
        }
278
    }
279
}