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\Support\Arr;
7
use PHPUnit\Framework\Assert as PHPUnit;
8
 
9
trait Has
10
{
11
    /**
12
     * Assert that the prop is of the expected size.
13
     *
14
     * @param  string|int  $key
15
     * @param  int|null  $length
16
     * @return $this
17
     */
18
    public function count($key, int $length = null): self
19
    {
20
        if (is_null($length)) {
21
            $path = $this->dotPath();
22
 
23
            PHPUnit::assertCount(
24
                $key,
25
                $this->prop(),
26
                $path
27
                    ? sprintf('Property [%s] does not have the expected size.', $path)
28
                    : sprintf('Root level does not have the expected size.')
29
            );
30
 
31
            return $this;
32
        }
33
 
34
        PHPUnit::assertCount(
35
            $length,
36
            $this->prop($key),
37
            sprintf('Property [%s] does not have the expected size.', $this->dotPath($key))
38
        );
39
 
40
        return $this;
41
    }
42
 
43
    /**
44
     * Ensure that the given prop exists.
45
     *
46
     * @param  string|int  $key
47
     * @param  int|\Closure|null  $length
48
     * @param  \Closure|null  $callback
49
     * @return $this
50
     */
51
    public function has($key, $length = null, Closure $callback = null): self
52
    {
53
        $prop = $this->prop();
54
 
55
        if (is_int($key) && is_null($length)) {
56
            return $this->count($key);
57
        }
58
 
59
        PHPUnit::assertTrue(
60
            Arr::has($prop, $key),
61
            sprintf('Property [%s] does not exist.', $this->dotPath($key))
62
        );
63
 
64
        $this->interactsWith($key);
65
 
66
        if (! is_null($callback)) {
67
            return $this->has($key, function (self $scope) use ($length, $callback) {
68
                return $scope
69
                    ->tap(function (self $scope) use ($length) {
70
                        if (! is_null($length)) {
71
                            $scope->count($length);
72
                        }
73
                    })
74
                    ->first($callback)
75
                    ->etc();
76
            });
77
        }
78
 
79
        if (is_callable($length)) {
80
            return $this->scope($key, $length);
81
        }
82
 
83
        if (! is_null($length)) {
84
            return $this->count($key, $length);
85
        }
86
 
87
        return $this;
88
    }
89
 
90
    /**
91
     * Assert that all of the given props exist.
92
     *
93
     * @param  array|string  $key
94
     * @return $this
95
     */
96
    public function hasAll($key): self
97
    {
98
        $keys = is_array($key) ? $key : func_get_args();
99
 
100
        foreach ($keys as $prop => $count) {
101
            if (is_int($prop)) {
102
                $this->has($count);
103
            } else {
104
                $this->has($prop, $count);
105
            }
106
        }
107
 
108
        return $this;
109
    }
110
 
111
    /**
112
     * Assert that at least one of the given props exists.
113
     *
114
     * @param  array|string  $key
115
     * @return $this
116
     */
117
    public function hasAny($key): self
118
    {
119
        $keys = is_array($key) ? $key : func_get_args();
120
 
121
        PHPUnit::assertTrue(
122
            Arr::hasAny($this->prop(), $keys),
123
            sprintf('None of properties [%s] exist.', implode(', ', $keys))
124
        );
125
 
126
        foreach ($keys as $key) {
127
            $this->interactsWith($key);
128
        }
129
 
130
        return $this;
131
    }
132
 
133
    /**
134
     * Assert that none of the given props exist.
135
     *
136
     * @param  array|string  $key
137
     * @return $this
138
     */
139
    public function missingAll($key): self
140
    {
141
        $keys = is_array($key) ? $key : func_get_args();
142
 
143
        foreach ($keys as $prop) {
144
            $this->missing($prop);
145
        }
146
 
147
        return $this;
148
    }
149
 
150
    /**
151
     * Assert that the given prop does not exist.
152
     *
153
     * @param  string  $key
154
     * @return $this
155
     */
156
    public function missing(string $key): self
157
    {
158
        PHPUnit::assertNotTrue(
159
            Arr::has($this->prop(), $key),
160
            sprintf('Property [%s] was found while it was expected to be missing.', $this->dotPath($key))
161
        );
162
 
163
        return $this;
164
    }
165
 
166
    /**
167
     * Compose the absolute "dot" path to the given key.
168
     *
169
     * @param  string  $key
170
     * @return string
171
     */
172
    abstract protected function dotPath(string $key = ''): string;
173
 
174
    /**
175
     * Marks the property as interacted.
176
     *
177
     * @param  string  $key
178
     * @return void
179
     */
180
    abstract protected function interactsWith(string $key): void;
181
 
182
    /**
183
     * Retrieve a prop within the current scope using "dot" notation.
184
     *
185
     * @param  string|null  $key
186
     * @return mixed
187
     */
188
    abstract protected function prop(string $key = null);
189
 
190
    /**
191
     * Instantiate a new "scope" at the path of the given key.
192
     *
193
     * @param  string  $key
194
     * @param  \Closure  $callback
195
     * @return $this
196
     */
197
    abstract protected function scope(string $key, Closure $callback);
198
 
199
    /**
200
     * Disables the interaction check.
201
     *
202
     * @return $this
203
     */
204
    abstract public function etc();
205
 
206
    /**
207
     * Instantiate a new "scope" on the first element.
208
     *
209
     * @param  \Closure  $callback
210
     * @return $this
211
     */
212
    abstract public function first(Closure $callback);
213
}