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;
4
 
5
use Closure;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Traits\Macroable;
10
use Illuminate\Testing\Assert as PHPUnit;
11
use Illuminate\Testing\Constraints\SeeInOrder;
12
use Illuminate\View\View;
13
 
14
class TestView
15
{
16
    use Macroable;
17
 
18
    /**
19
     * The original view.
20
     *
21
     * @var \Illuminate\View\View
22
     */
23
    protected $view;
24
 
25
    /**
26
     * The rendered view contents.
27
     *
28
     * @var string
29
     */
30
    protected $rendered;
31
 
32
    /**
33
     * Create a new test view instance.
34
     *
35
     * @param  \Illuminate\View\View  $view
36
     * @return void
37
     */
38
    public function __construct(View $view)
39
    {
40
        $this->view = $view;
41
        $this->rendered = $view->render();
42
    }
43
 
44
    /**
45
     * Assert that the response view has a given piece of bound data.
46
     *
47
     * @param  string|array  $key
48
     * @param  mixed  $value
49
     * @return $this
50
     */
51
    public function assertViewHas($key, $value = null)
52
    {
53
        if (is_array($key)) {
54
            return $this->assertViewHasAll($key);
55
        }
56
 
57
        if (is_null($value)) {
58
            PHPUnit::assertTrue(Arr::has($this->view->gatherData(), $key));
59
        } elseif ($value instanceof Closure) {
60
            PHPUnit::assertTrue($value(Arr::get($this->view->gatherData(), $key)));
61
        } elseif ($value instanceof Model) {
62
            PHPUnit::assertTrue($value->is(Arr::get($this->view->gatherData(), $key)));
63
        } elseif ($value instanceof Collection) {
64
            $actual = Arr::get($this->view->gatherData(), $key);
65
 
66
            PHPUnit::assertInstanceOf(Collection::class, $actual);
67
            PHPUnit::assertSameSize($value, $actual);
68
 
69
            $value->each(fn ($item, $index) => PHPUnit::assertTrue($actual->get($index)->is($item)));
70
        } else {
71
            PHPUnit::assertEquals($value, Arr::get($this->view->gatherData(), $key));
72
        }
73
 
74
        return $this;
75
    }
76
 
77
    /**
78
     * Assert that the response view has a given list of bound data.
79
     *
80
     * @param  array  $bindings
81
     * @return $this
82
     */
83
    public function assertViewHasAll(array $bindings)
84
    {
85
        foreach ($bindings as $key => $value) {
86
            if (is_int($key)) {
87
                $this->assertViewHas($value);
88
            } else {
89
                $this->assertViewHas($key, $value);
90
            }
91
        }
92
 
93
        return $this;
94
    }
95
 
96
    /**
97
     * Assert that the response view is missing a piece of bound data.
98
     *
99
     * @param  string  $key
100
     * @return $this
101
     */
102
    public function assertViewMissing($key)
103
    {
104
        PHPUnit::assertFalse(Arr::has($this->view->gatherData(), $key));
105
 
106
        return $this;
107
    }
108
 
109
    /**
110
     * Assert that the given string is contained within the view.
111
     *
112
     * @param  string  $value
113
     * @param  bool  $escape
114
     * @return $this
115
     */
116
    public function assertSee($value, $escape = true)
117
    {
118
        $value = $escape ? e($value) : $value;
119
 
120
        PHPUnit::assertStringContainsString((string) $value, $this->rendered);
121
 
122
        return $this;
123
    }
124
 
125
    /**
126
     * Assert that the given strings are contained in order within the view.
127
     *
128
     * @param  array  $values
129
     * @param  bool  $escape
130
     * @return $this
131
     */
132
    public function assertSeeInOrder(array $values, $escape = true)
133
    {
134
        $values = $escape ? array_map('e', $values) : $values;
135
 
136
        PHPUnit::assertThat($values, new SeeInOrder($this->rendered));
137
 
138
        return $this;
139
    }
140
 
141
    /**
142
     * Assert that the given string is contained within the view text.
143
     *
144
     * @param  string  $value
145
     * @param  bool  $escape
146
     * @return $this
147
     */
148
    public function assertSeeText($value, $escape = true)
149
    {
150
        $value = $escape ? e($value) : $value;
151
 
152
        PHPUnit::assertStringContainsString((string) $value, strip_tags($this->rendered));
153
 
154
        return $this;
155
    }
156
 
157
    /**
158
     * Assert that the given strings are contained in order within the view text.
159
     *
160
     * @param  array  $values
161
     * @param  bool  $escape
162
     * @return $this
163
     */
164
    public function assertSeeTextInOrder(array $values, $escape = true)
165
    {
166
        $values = $escape ? array_map('e', $values) : $values;
167
 
168
        PHPUnit::assertThat($values, new SeeInOrder(strip_tags($this->rendered)));
169
 
170
        return $this;
171
    }
172
 
173
    /**
174
     * Assert that the given string is not contained within the view.
175
     *
176
     * @param  string  $value
177
     * @param  bool  $escape
178
     * @return $this
179
     */
180
    public function assertDontSee($value, $escape = true)
181
    {
182
        $value = $escape ? e($value) : $value;
183
 
184
        PHPUnit::assertStringNotContainsString((string) $value, $this->rendered);
185
 
186
        return $this;
187
    }
188
 
189
    /**
190
     * Assert that the given string is not contained within the view text.
191
     *
192
     * @param  string  $value
193
     * @param  bool  $escape
194
     * @return $this
195
     */
196
    public function assertDontSeeText($value, $escape = true)
197
    {
198
        $value = $escape ? e($value) : $value;
199
 
200
        PHPUnit::assertStringNotContainsString((string) $value, strip_tags($this->rendered));
201
 
202
        return $this;
203
    }
204
 
205
    /**
206
     * Get the string contents of the rendered view.
207
     *
208
     * @return string
209
     */
210
    public function __toString()
211
    {
212
        return $this->rendered;
213
    }
214
}