Skip to content

Commit 5a6e21a

Browse files
committed
simplify scaffold tests by using latest zenstruck/browser
1 parent 00195cb commit 5a6e21a

File tree

6 files changed

+55
-108
lines changed

6 files changed

+55
-108
lines changed

src/Resources/scaffolds/6.0/auth/tests/Browser/Authentication.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/Resources/scaffolds/6.0/auth/tests/Functional/AuthenticationTest.php

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace App\Tests\Functional;
44

55
use App\Factory\UserFactory;
6-
use App\Tests\Browser\Authentication;
76
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
7+
use Symfony\Component\BrowserKit\CookieJar;
88
use Zenstruck\Browser\Test\HasBrowser;
99
use Zenstruck\Foundry\Test\Factories;
1010
use Zenstruck\Foundry\Test\ResetDatabase;
@@ -20,17 +20,17 @@ public function testCanLoginAndLogout(): void
2020
UserFactory::createOne(['email' => 'mary@example.com', 'password' => '1234']);
2121

2222
$this->browser()
23-
->use(Authentication::assertNotAuthenticated())
23+
->assertNotAuthenticated()
2424
->visit('/login')
2525
->fillField('Email', 'mary@example.com')
2626
->fillField('Password', '1234')
2727
->click('Sign in')
2828
->assertOn('/')
2929
->assertSuccessful()
30-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
30+
->assertAuthenticated('mary@example.com')
3131
->visit('/logout')
3232
->assertOn('/')
33-
->use(Authentication::assertNotAuthenticated())
33+
->assertNotAuthenticated()
3434
;
3535
}
3636

@@ -39,13 +39,14 @@ public function testLoginWithTarget(): void
3939
UserFactory::createOne(['email' => 'mary@example.com', 'password' => '1234']);
4040

4141
$this->browser()
42-
->use(Authentication::assertNotAuthenticated())
42+
->assertNotAuthenticated()
4343
->visit('/login?target=/some/page')
4444
->fillField('Email', 'mary@example.com')
4545
->fillField('Password', '1234')
4646
->click('Sign in')
4747
->assertOn('/some/page')
48-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
48+
->visit('/')
49+
->assertAuthenticated('mary@example.com')
4950
;
5051
}
5152

@@ -62,7 +63,7 @@ public function testLoginWithInvalidPassword(): void
6263
->assertSuccessful()
6364
->assertFieldEquals('Email', 'mary@example.com')
6465
->assertSee('Invalid credentials.')
65-
->use(Authentication::assertNotAuthenticated())
66+
->assertNotAuthenticated()
6667
;
6768
}
6869

@@ -77,7 +78,7 @@ public function testLoginWithInvalidEmail(): void
7778
->assertSuccessful()
7879
->assertFieldEquals('Email', 'invalid@example.com')
7980
->assertSee('Invalid credentials.')
80-
->use(Authentication::assertNotAuthenticated())
81+
->assertNotAuthenticated()
8182
;
8283
}
8384

@@ -86,12 +87,12 @@ public function testLoginWithInvalidCsrf(): void
8687
UserFactory::createOne(['email' => 'mary@example.com', 'password' => '1234']);
8788

8889
$this->browser()
89-
->use(Authentication::assertNotAuthenticated())
90+
->assertNotAuthenticated()
9091
->post('/login', ['body' => ['email' => 'mary@example.com', 'password' => '1234']])
9192
->assertOn('/login')
9293
->assertSuccessful()
9394
->assertSee('Invalid CSRF token.')
94-
->use(Authentication::assertNotAuthenticated())
95+
->assertNotAuthenticated()
9596
;
9697
}
9798

@@ -106,9 +107,13 @@ public function testRememberMeEnabledByDefault(): void
106107
->click('Sign in')
107108
->assertOn('/')
108109
->assertSuccessful()
109-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
110-
->use(Authentication::expireSession())
111-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
110+
->assertAuthenticated('mary@example.com')
111+
->use(function(CookieJar $cookieJar) {
112+
$cookieJar->expire('MOCKSESSID');
113+
})
114+
->withProfiling()
115+
->visit('/')
116+
->assertAuthenticated('mary@example.com')
112117
;
113118
}
114119

@@ -124,9 +129,12 @@ public function testCanDisableRememberMe(): void
124129
->click('Sign in')
125130
->assertOn('/')
126131
->assertSuccessful()
127-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
128-
->use(Authentication::expireSession())
129-
->use(Authentication::assertNotAuthenticated())
132+
->assertAuthenticated('mary@example.com')
133+
->use(function(CookieJar $cookieJar) {
134+
$cookieJar->expire('MOCKSESSID');
135+
})
136+
->visit('/')
137+
->assertNotAuthenticated()
130138
;
131139
}
132140

@@ -140,10 +148,10 @@ public function testFullyAuthenticatedLoginRedirect(): void
140148
->fillField('Password', '1234')
141149
->click('Sign in')
142150
->assertOn('/')
143-
->use(Authentication::assertAuthenticated())
151+
->assertAuthenticated()
144152
->visit('/login')
145153
->assertOn('/')
146-
->use(Authentication::assertAuthenticated())
154+
->assertAuthenticated()
147155
;
148156
}
149157

@@ -157,10 +165,11 @@ public function testFullyAuthenticatedLoginTarget(): void
157165
->fillField('Password', '1234')
158166
->click('Sign in')
159167
->assertOn('/')
160-
->use(Authentication::assertAuthenticated())
168+
->assertAuthenticated()
161169
->visit('/login?target=/some/page')
162170
->assertOn('/some/page')
163-
->use(Authentication::assertAuthenticated())
171+
->visit('/')
172+
->assertAuthenticated()
164173
;
165174
}
166175

@@ -174,14 +183,16 @@ public function testCanFullyAuthenticateIfOnlyRemembered(): void
174183
->fillField('Password', '1234')
175184
->click('Sign in')
176185
->assertOn('/')
177-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
178-
->use(Authentication::expireSession())
186+
->assertAuthenticated('mary@example.com')
187+
->use(function(CookieJar $cookieJar) {
188+
$cookieJar->expire('MOCKSESSID');
189+
})
179190
->visit('/login')
180191
->assertOn('/login')
181192
->fillField('Password', '1234')
182193
->click('Sign in')
183194
->assertOn('/')
184-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
195+
->assertAuthenticated('mary@example.com')
185196
;
186197
}
187198

@@ -196,14 +207,14 @@ public function testLegacyPasswordHashIsAutomaticallyMigratedOnLogin(): void
196207
$this->assertSame(\PASSWORD_ARGON2ID, password_get_info($user->getPassword())['algo']);
197208

198209
$this->browser()
199-
->use(Authentication::assertNotAuthenticated())
210+
->assertNotAuthenticated()
200211
->visit('/login')
201212
->fillField('Email', 'mary@example.com')
202213
->fillField('Password', '1234')
203214
->click('Sign in')
204215
->assertOn('/')
205216
->assertSuccessful()
206-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
217+
->assertAuthenticated('mary@example.com')
207218
;
208219

209220
$this->assertSame(\PASSWORD_DEFAULT, password_get_info($user->getPassword())['algo']);

src/Resources/scaffolds/6.0/change-password/tests/Functional/ChangePasswordTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace App\Tests\Functional;
44

55
use App\Factory\UserFactory;
6-
use App\Tests\Browser\Authentication;
76
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
87
use Zenstruck\Browser\Test\HasBrowser;
98
use Zenstruck\Foundry\Test\Factories;
@@ -33,15 +32,15 @@ public function testCanChangePassword(): void
3332
->assertSuccessful()
3433
->assertOn('/')
3534
->assertSeeIn('.alert', 'You\'ve successfully changed your password.')
36-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
35+
->assertAuthenticated('mary@example.com')
3736
->visit('/logout')
3837
->visit('/login')
3938
->fillField('Email', 'mary@example.com')
4039
->fillField('Password', 'new-password')
4140
->click('Sign in')
4241
->assertOn('/')
4342
->assertSuccessful()
44-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
43+
->assertAuthenticated('mary@example.com')
4544
;
4645

4746
$this->assertNotSame($currentPassword, $user->getPassword());
@@ -62,7 +61,7 @@ public function testCurrentPasswordMustBeCorrect(): void
6261
->assertSuccessful()
6362
->assertOn('/user/change-password')
6463
->assertSee('This is not your current password.')
65-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
64+
->assertAuthenticated('mary@example.com')
6665
;
6766

6867
$this->assertSame($currentPassword, $user->getPassword());
@@ -82,7 +81,7 @@ public function testCurrentPasswordIsRequired(): void
8281
->assertSuccessful()
8382
->assertOn('/user/change-password')
8483
->assertSee('This is not your current password.')
85-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
84+
->assertAuthenticated('mary@example.com')
8685
;
8786

8887
$this->assertSame($currentPassword, $user->getPassword());
@@ -101,7 +100,7 @@ public function testNewPasswordIsRequired(): void
101100
->assertSuccessful()
102101
->assertOn('/user/change-password')
103102
->assertSee('Please enter a password.')
104-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
103+
->assertAuthenticated('mary@example.com')
105104
;
106105

107106
$this->assertSame($currentPassword, $user->getPassword());
@@ -122,7 +121,7 @@ public function testNewPasswordsMustMatch(): void
122121
->assertSuccessful()
123122
->assertOn('/user/change-password')
124123
->assertSee('The password fields must match.')
125-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
124+
->assertAuthenticated('mary@example.com')
126125
;
127126

128127
$this->assertSame($currentPassword, $user->getPassword());
@@ -143,7 +142,7 @@ public function testNewPasswordMustBeMinLength(): void
143142
->assertSuccessful()
144143
->assertOn('/user/change-password')
145144
->assertSee('Your password should be at least 6 characters')
146-
->use(Authentication::assertAuthenticatedAs('mary@example.com'))
145+
->assertAuthenticated('mary@example.com')
147146
;
148147

149148
$this->assertSame($currentPassword, $user->getPassword());

src/Resources/scaffolds/6.0/register/tests/Functional/RegisterTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace App\Tests\Functional;
44

55
use App\Factory\UserFactory;
6-
use App\Tests\Browser\Authentication;
76
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
87
use Zenstruck\Browser\Test\HasBrowser;
98
use Zenstruck\Foundry\Test\Factories;
@@ -28,15 +27,15 @@ public function testCanRegister(): void
2827
->click('Register')
2928
->assertOn('/')
3029
->assertSeeIn('.alert', 'You\'ve successfully registered and are now logged in.')
31-
->use(Authentication::assertAuthenticatedAs('madison@example.com'))
30+
->assertAuthenticated('madison@example.com')
3231
->visit('/logout')
33-
->use(Authentication::assertNotAuthenticated())
32+
->assertNotAuthenticated()
3433
->visit('/login')
3534
->fillField('Email', 'madison@example.com')
3635
->fillField('Password', 'password')
3736
->click('Sign in')
3837
->assertOn('/')
39-
->use(Authentication::assertAuthenticatedAs('madison@example.com'))
38+
->assertAuthenticated('madison@example.com')
4039
;
4140

4241
UserFactory::assert()->count(1);
@@ -54,7 +53,7 @@ public function testNameIsRequired(): void
5453
->click('Register')
5554
->assertOn('/register')
5655
->assertSee('Name is required')
57-
->use(Authentication::assertNotAuthenticated())
56+
->assertNotAuthenticated()
5857
;
5958
}
6059

@@ -69,7 +68,7 @@ public function testEmailIsRequired(): void
6968
->click('Register')
7069
->assertOn('/register')
7170
->assertSee('Email is required')
72-
->use(Authentication::assertNotAuthenticated())
71+
->assertNotAuthenticated()
7372
;
7473
}
7574

@@ -85,7 +84,7 @@ public function testEmailMustBeEmailAddress(): void
8584
->click('Register')
8685
->assertOn('/register')
8786
->assertSee('This is not a valid email address')
88-
->use(Authentication::assertNotAuthenticated())
87+
->assertNotAuthenticated()
8988
;
9089
}
9190

@@ -103,7 +102,7 @@ public function testEmailMustBeUnique(): void
103102
->click('Register')
104103
->assertOn('/register')
105104
->assertSee('There is already an account with this email')
106-
->use(Authentication::assertNotAuthenticated())
105+
->assertNotAuthenticated()
107106
;
108107
}
109108

@@ -118,7 +117,7 @@ public function testPasswordIsRequired(): void
118117
->click('Register')
119118
->assertOn('/register')
120119
->assertSee('Please enter a password')
121-
->use(Authentication::assertNotAuthenticated())
120+
->assertNotAuthenticated()
122121
;
123122
}
124123

@@ -134,7 +133,7 @@ public function testPasswordMustBeMinLength(): void
134133
->click('Register')
135134
->assertOn('/register')
136135
->assertSee('Your password should be at least 6 characters')
137-
->use(Authentication::assertNotAuthenticated())
136+
->assertNotAuthenticated()
138137
;
139138
}
140139
}

0 commit comments

Comments
 (0)