forked from laracasts/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCypressControllerTest.php
231 lines (184 loc) · 6.37 KB
/
CypressControllerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
namespace Laracasts\Cypress\Tests;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;
use Laracasts\Cypress\CypressServiceProvider;
use Laracasts\Cypress\Tests\Support\TestUser;
use Orchestra\Testbench\TestCase;
use Spatie\LaravelRay\RayServiceProvider;
class CypressControllerTest extends TestCase
{
protected function getPackageProviders($app)
{
return [CypressServiceProvider::class, RayServiceProvider::class];
}
protected function setUp(): void
{
parent::setUp();
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
config(['auth.providers.users.model' => TestUser::class]);
}
/** @test */
function it_fetches_a_collection_of_named_routes()
{
Route::get('foo')->name('home');
$response = $this->post(route('cypress.routes'));
$response->assertJsonFragment([
'uri' => 'foo',
'name' => 'home',
'method' => ['GET', 'HEAD'],
'action' => 'Closure',
'domain' => null,
]);
$this->assertArrayHasKey('home', $response->json());
}
/** @test */
public function it_logs_a_new_user_in()
{
$this->post(route('cypress.login'));
$this->assertTrue(auth()->check());
}
/** @test */
public function it_fetches_the_currently_authenticated_user()
{
$this->post(route('cypress.login'), ['attributes' => ['email' => '[email protected]']]);
$response = $this->post(route('cypress.current-user'));
$this->assertNotNull($response->json());
$this->assertEquals('[email protected]', $response->json()['email']);
}
/** @test */
public function it_makes_all_logged_in_user_attributes_visible()
{
$response = $this->post(route('cypress.login'));
$this->assertTrue(auth()->check());
// The TestUser model sets the "plan" field to hidden. But
// when we fetch it with Cypress, it should be visible.
$this->assertEquals('monthly', $response->json()['plan']);
}
/** @test */
public function it_logs_a_user_in()
{
$this->post(route('cypress.login'), [
'attributes' => ['name' => 'Frank'],
'state' => ['guest']
]);
$this->assertDatabaseHas('users', ['name' => 'Frank']);
}
/** @test */
public function it_logs_an_existing_user_in()
{
TestUser::factory()->create(['name' => 'Joe']);
$frank = TestUser::factory()->create([
'name' => 'Frank',
'plan' => 'monthly',
]);
$response = $this->post(route('cypress.login'), [
'attributes' => ['name' => 'Frank', 'plan' => 'monthly'],
]);
$this->assertEquals(2, TestUser::count());
$this->assertEquals($frank->id, $response->json()['id']);
}
/** @test */
public function it_logs_a_user_out()
{
$this->post(route('cypress.login'));
$this->post(route('cypress.logout'));
$this->assertFalse(auth()->check());
}
/** @test */
public function it_builds_a_model_factory()
{
$response = $this->post(route('cypress.factory'), [
'model' => TestUser::class,
'attributes' => [
'name' => 'John Doe',
],
'load' => ['profile'],
'state' => ['guest']
]);
$this->assertDatabaseHas('users', ['name' => 'John Doe']);
$this->assertEquals('John Doe', $response->json()['name']);
$this->assertEquals('USA', $response->json()['profile']['location']);
$this->assertEquals('guest', $response->json()['plan']);
}
/** @test */
public function it_accepts_arguments_to_model_factory_states()
{
$response = $this->post(route('cypress.factory'), [
'model' => TestUser::class,
'state' => ['guest' => 'forum']
]);
$this->assertEquals('forum', $response->json()['plan']);
// When passing an array of arguments.
$response = $this->post(route('cypress.factory'), [
'model' => TestUser::class,
'state' => ['guest' => ['forum']]
]);
$this->assertEquals('forum', $response->json()['plan']);
}
/** @test */
public function it_builds_a_collection_of_model_factories()
{
$response = $this->post(route('cypress.factory'), [
'model' => TestUser::class,
'count' => 2
]);
$this->assertCount(2, $response->json());
}
/** @test */
function it_makes_model_attributes_visible()
{
$response = $this->post(route('cypress.factory'), [
'model' => TestUser::class,
'attributes' => [
'name' => 'John Doe',
],
]);
// The TestUser model sets the "plan" field to hidden. But
// when we fetch it with Cypress, it should be visible.
$this->assertEquals('monthly', $response->json()['plan']);
}
/** @test */
function it_makes_collection_model_attributes_visible()
{
$response = $this->post(route('cypress.factory'), [
'model' => TestUser::class,
'count' => 2,
'attributes' => [
'name' => 'John Doe',
],
]);
// The TestUser model sets the "plan" field to hidden. But
// when we fetch it with Cypress, it should be visible.
$this->assertEquals('monthly', $response->json()[0]['plan']);
$this->assertEquals('monthly', $response->json()[1]['plan']);
}
/** @test */
public function it_runs_an_artisan_command()
{
$called = false;
Artisan::command('testing', function () use (&$called) {
$called = true;
});
$this->post(route('cypress.artisan'), [
'command' => 'testing',
]);
$this->assertTrue($called);
}
/** @test */
public function it_runs_single_line_arbitrary_php()
{
$response = $this->post(route('cypress.run-php'), [
'command' => '2 + 3',
]);
$this->assertEquals(5, $response->json()['result']);
}
/** @test */
public function it_runs_multi_line_arbitrary_php()
{
$response = $this->post(route('cypress.run-php'), [
'command' => '$a = 2; $b = 3; return $a + $b;',
]);
$this->assertEquals(5, $response->json()['result']);
}
}