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\Fluent\Concerns;
4
 
5
use Closure;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Support\Collection;
8
use PHPUnit\Framework\Assert as PHPUnit;
9
 
10
trait Matching
11
{
12
    /**
13
     * Asserts that the property matches the expected value.
14
     *
15
     * @param  string  $key
16
     * @param  mixed|\Closure  $expected
17
     * @return $this
18
     */
19
    public function where(string $key, $expected): self
20
    {
21
        $this->has($key);
22
 
23
        $actual = $this->prop($key);
24
 
25
        if ($expected instanceof Closure) {
26
            PHPUnit::assertTrue(
27
                $expected(is_array($actual) ? Collection::make($actual) : $actual),
28
                sprintf('Property [%s] was marked as invalid using a closure.', $this->dotPath($key))
29
            );
30
 
31
            return $this;
32
        }
33
 
34
        if ($expected instanceof Arrayable) {
35
            $expected = $expected->toArray();
36
        }
37
 
38
        $this->ensureSorted($expected);
39
        $this->ensureSorted($actual);
40
 
41
        PHPUnit::assertSame(
42
            $expected,
43
            $actual,
44
            sprintf('Property [%s] does not match the expected value.', $this->dotPath($key))
45
        );
46
 
47
        return $this;
48
    }
49
 
50
    /**
51
     * Asserts that the property does not match the expected value.
52
     *
53
     * @param  string  $key
54
     * @param  mixed|\Closure  $expected
55
     * @return $this
56
     */
57
    public function whereNot(string $key, $expected): self
58
    {
59
        $this->has($key);
60
 
61
        $actual = $this->prop($key);
62
 
63
        if ($expected instanceof Closure) {
64
            PHPUnit::assertFalse(
65
                $expected(is_array($actual) ? Collection::make($actual) : $actual),
66
                sprintf('Property [%s] was marked as invalid using a closure.', $this->dotPath($key))
67
            );
68
 
69
            return $this;
70
        }
71
 
72
        if ($expected instanceof Arrayable) {
73
            $expected = $expected->toArray();
74
        }
75
 
76
        $this->ensureSorted($expected);
77
        $this->ensureSorted($actual);
78
 
79
        PHPUnit::assertNotSame(
80
            $expected,
81
            $actual,
82
            sprintf(
83
                'Property [%s] contains a value that should be missing: [%s, %s]',
84
                $this->dotPath($key),
85
                $key,
86
                $expected
87
            )
88
        );
89
 
90
        return $this;
91
    }
92
 
93
    /**
94
     * Asserts that all properties match their expected values.
95
     *
96
     * @param  array  $bindings
97
     * @return $this
98
     */
99
    public function whereAll(array $bindings): self
100
    {
101
        foreach ($bindings as $key => $value) {
102
            $this->where($key, $value);
103
        }
104
 
105
        return $this;
106
    }
107
 
108
    /**
109
     * Asserts that the property is of the expected type.
110
     *
111
     * @param  string  $key
112
     * @param  string|array  $expected
113
     * @return $this
114
     */
115
    public function whereType(string $key, $expected): self
116
    {
117
        $this->has($key);
118
 
119
        $actual = $this->prop($key);
120
 
121
        if (! is_array($expected)) {
122
            $expected = explode('|', $expected);
123
        }
124
 
125
        PHPUnit::assertContains(
126
            strtolower(gettype($actual)),
127
            $expected,
128
            sprintf('Property [%s] is not of expected type [%s].', $this->dotPath($key), implode('|', $expected))
129
        );
130
 
131
        return $this;
132
    }
133
 
134
    /**
135
     * Asserts that all properties are of their expected types.
136
     *
137
     * @param  array  $bindings
138
     * @return $this
139
     */
140
    public function whereAllType(array $bindings): self
141
    {
142
        foreach ($bindings as $key => $value) {
143
            $this->whereType($key, $value);
144
        }
145
 
146
        return $this;
147
    }
148
 
149
    /**
150
     * Asserts that the property contains the expected values.
151
     *
152
     * @param  string  $key
153
     * @param  mixed  $expected
154
     * @return $this
155
     */
156
    public function whereContains(string $key, $expected)
157
    {
158
        $actual = Collection::make(
159
            $this->prop($key) ?? $this->prop()
160
        );
161
 
162
        $missing = Collection::make($expected)->reject(function ($search) use ($key, $actual) {
163
            if ($actual->containsStrict($key, $search)) {
164
                return true;
165
            }
166
 
167
            return $actual->containsStrict($search);
168
        });
169
 
170
        if ($missing->whereInstanceOf('Closure')->isNotEmpty()) {
171
            PHPUnit::assertEmpty(
172
                $missing->toArray(),
173
                sprintf(
174
                    'Property [%s] does not contain a value that passes the truth test within the given closure.',
175
                    $key,
176
                )
177
            );
178
        } else {
179
            PHPUnit::assertEmpty(
180
                $missing->toArray(),
181
                sprintf(
182
                    'Property [%s] does not contain [%s].',
183
                    $key,
184
                    implode(', ', array_values($missing->toArray()))
185
                )
186
            );
187
        }
188
 
189
        return $this;
190
    }
191
 
192
    /**
193
     * Ensures that all properties are sorted the same way, recursively.
194
     *
195
     * @param  mixed  $value
196
     * @return void
197
     */
198
    protected function ensureSorted(&$value): void
199
    {
200
        if (! is_array($value)) {
201
            return;
202
        }
203
 
204
        foreach ($value as &$arg) {
205
            $this->ensureSorted($arg);
206
        }
207
 
208
        ksort($value);
209
    }
210
 
211
    /**
212
     * Compose the absolute "dot" path to the given key.
213
     *
214
     * @param  string  $key
215
     * @return string
216
     */
217
    abstract protected function dotPath(string $key = ''): string;
218
 
219
    /**
220
     * Ensure that the given prop exists.
221
     *
222
     * @param  string  $key
223
     * @param  null  $value
224
     * @param  \Closure|null  $scope
225
     * @return $this
226
     */
227
    abstract public function has(string $key, $value = null, Closure $scope = null);
228
 
229
    /**
230
     * Retrieve a prop within the current scope using "dot" notation.
231
     *
232
     * @param  string|null  $key
233
     * @return mixed
234
     */
235
    abstract protected function prop(string $key = null);
236
}