Skip to content

Commit e11bf68

Browse files
authored
Document new Symfony assertions (#208)
1 parent 12fdf09 commit e11bf68

5 files changed

+226
-15
lines changed

src/Codeception/Module/Symfony/BrowserAssertionsTrait.php

+86-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ trait BrowserAssertionsTrait
2525
{
2626
/**
2727
* Asserts that the given cookie in the test client is set to the expected value.
28+
*
29+
* ```php
30+
* <?php
31+
* $I->assertBrowserCookieValueSame('cookie_name', 'expected_value');
32+
* ```
2833
*/
2934
public function assertBrowserCookieValueSame(string $name, string $expectedValue, bool $raw = false, string $path = '/', ?string $domain = null, string $message = ''): void
3035
{
@@ -35,6 +40,11 @@ public function assertBrowserCookieValueSame(string $name, string $expectedValue
3540
/**
3641
* Asserts that the test client has the specified cookie set.
3742
* This indicates that the cookie was set by any response during the test.
43+
*
44+
* ```
45+
* <?php
46+
* $I->assertBrowserHasCookie('cookie_name');
47+
* ```
3848
*/
3949
public function assertBrowserHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void
4050
{
@@ -44,6 +54,11 @@ public function assertBrowserHasCookie(string $name, string $path = '/', ?string
4454
/**
4555
* Asserts that the test client does not have the specified cookie set.
4656
* This indicates that the cookie was not set by any response during the test.
57+
*
58+
* ```php
59+
* <?php
60+
* $I->assertBrowserNotHasCookie('cookie_name');
61+
* ```
4762
*/
4863
public function assertBrowserNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void
4964
{
@@ -52,6 +67,11 @@ public function assertBrowserNotHasCookie(string $name, string $path = '/', ?str
5267

5368
/**
5469
* Asserts that the specified request attribute matches the expected value.
70+
*
71+
* ```php
72+
* <?php
73+
* $I->assertRequestAttributeValueSame('attribute_name', 'expected_value');
74+
* ```
5575
*/
5676
public function assertRequestAttributeValueSame(string $name, string $expectedValue, string $message = ''): void
5777
{
@@ -60,6 +80,11 @@ public function assertRequestAttributeValueSame(string $name, string $expectedVa
6080

6181
/**
6282
* Asserts that the specified response cookie is present and matches the expected value.
83+
*
84+
* ```php
85+
* <?php
86+
* $I->assertResponseCookieValueSame('cookie_name', 'expected_value');
87+
* ```
6388
*/
6489
public function assertResponseCookieValueSame(string $name, string $expectedValue, string $path = '/', ?string $domain = null, string $message = ''): void
6590
{
@@ -69,6 +94,11 @@ public function assertResponseCookieValueSame(string $name, string $expectedValu
6994

7095
/**
7196
* Asserts that the response format matches the expected format. This checks the format returned by the `Response::getFormat()` method.
97+
*
98+
* ```php
99+
* <?php
100+
* $I->assertResponseFormatSame('json');
101+
* ```
72102
*/
73103
public function assertResponseFormatSame(?string $expectedFormat, string $message = ''): void
74104
{
@@ -77,6 +107,11 @@ public function assertResponseFormatSame(?string $expectedFormat, string $messag
77107

78108
/**
79109
* Asserts that the specified cookie is present in the response. Optionally, it can check for a specific cookie path or domain.
110+
*
111+
* ```php
112+
* <?php
113+
* $I->assertResponseHasCookie('cookie_name');
114+
* ```
80115
*/
81116
public function assertResponseHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void
82117
{
@@ -86,6 +121,11 @@ public function assertResponseHasCookie(string $name, string $path = '/', ?strin
86121
/**
87122
* Asserts that the specified header is available in the response.
88123
* For example, use `assertResponseHasHeader('content-type');`.
124+
*
125+
* ```php
126+
* <?php
127+
* $I->assertResponseHasHeader('content-type');
128+
* ```
89129
*/
90130
public function assertResponseHasHeader(string $headerName, string $message = ''): void
91131
{
@@ -95,6 +135,11 @@ public function assertResponseHasHeader(string $headerName, string $message = ''
95135
/**
96136
* Asserts that the specified header does not contain the expected value in the response.
97137
* For example, use `assertResponseHeaderNotSame('content-type', 'application/octet-stream');`.
138+
*
139+
* ```php
140+
* <?php
141+
* $I->assertResponseHeaderNotSame('content-type', 'application/json');
142+
* ```
98143
*/
99144
public function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void
100145
{
@@ -104,6 +149,11 @@ public function assertResponseHeaderNotSame(string $headerName, string $expected
104149
/**
105150
* Asserts that the specified header contains the expected value in the response.
106151
* For example, use `assertResponseHeaderSame('content-type', 'application/octet-stream');`.
152+
*
153+
* ```php
154+
* <?php
155+
* $I->assertResponseHeaderSame('content-type', 'application/json');
156+
* ```
107157
*/
108158
public function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void
109159
{
@@ -112,6 +162,11 @@ public function assertResponseHeaderSame(string $headerName, string $expectedVal
112162

113163
/**
114164
* Asserts that the response was successful (HTTP status code is in the 2xx range).
165+
*
166+
* ```php
167+
* <?php
168+
* $I->assertResponseIsSuccessful();
169+
* ```
115170
*/
116171
public function assertResponseIsSuccessful(string $message = '', bool $verbose = true): void
117172
{
@@ -120,6 +175,11 @@ public function assertResponseIsSuccessful(string $message = '', bool $verbose =
120175

121176
/**
122177
* Asserts that the response is unprocessable (HTTP status code is 422).
178+
*
179+
* ```php
180+
* <?php
181+
* $I->assertResponseIsUnprocessable();
182+
* ```
123183
*/
124184
public function assertResponseIsUnprocessable(string $message = '', bool $verbose = true): void
125185
{
@@ -128,6 +188,11 @@ public function assertResponseIsUnprocessable(string $message = '', bool $verbos
128188

129189
/**
130190
* Asserts that the specified cookie is not present in the response. Optionally, it can check for a specific cookie path or domain.
191+
*
192+
* ```php
193+
* <?php
194+
* $I->assertResponseNotHasCookie('cookie_name');
195+
* ```
131196
*/
132197
public function assertResponseNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void
133198
{
@@ -136,7 +201,11 @@ public function assertResponseNotHasCookie(string $name, string $path = '/', ?st
136201

137202
/**
138203
* Asserts that the specified header is not available in the response.
139-
* For example, use `assertResponseNotHasHeader('content-type');`.
204+
*
205+
* ```php
206+
* <?php
207+
* $I->assertResponseNotHasHeader('content-type');
208+
* ```
140209
*/
141210
public function assertResponseNotHasHeader(string $headerName, string $message = ''): void
142211
{
@@ -146,6 +215,12 @@ public function assertResponseNotHasHeader(string $headerName, string $message =
146215
/**
147216
* Asserts that the response is a redirect. Optionally, you can check the target location and status code.
148217
* The expected location can be either an absolute or a relative path.
218+
*
219+
* ```php
220+
* <?php
221+
* // Check that '/admin' redirects to '/login' with status code 302
222+
* $I->assertResponseRedirects('/login', 302);
223+
* ```
149224
*/
150225
public function assertResponseRedirects(?string $expectedLocation = null, ?int $expectedCode = null, string $message = '', bool $verbose = true): void
151226
{
@@ -165,6 +240,11 @@ public function assertResponseRedirects(?string $expectedLocation = null, ?int $
165240

166241
/**
167242
* Asserts that the response status code matches the expected code.
243+
*
244+
* ```php
245+
* <?php
246+
* $I->assertResponseStatusCodeSame(200);
247+
* ```
168248
*/
169249
public function assertResponseStatusCodeSame(int $expectedCode, string $message = '', bool $verbose = true): void
170250
{
@@ -173,6 +253,11 @@ public function assertResponseStatusCodeSame(int $expectedCode, string $message
173253

174254
/**
175255
* Asserts the request matches the given route and optionally route parameters.
256+
*
257+
* ```php
258+
* <?php
259+
* $I->assertRouteSame('profile', ['id' => 123]);
260+
* ```
176261
*/
177262
public function assertRouteSame(string $expectedRoute, array $parameters = [], string $message = ''): void {
178263
$request = $this->getClient()->getRequest();

src/Codeception/Module/Symfony/DomCrawlerAssertionsTrait.php

+55
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ trait DomCrawlerAssertionsTrait
1515
{
1616
/**
1717
* Asserts that the checkbox with the given name is checked.
18+
*
19+
* ```php
20+
* <?php
21+
* $I->assertCheckboxChecked('agree_terms');
22+
* ```
1823
*/
1924
public function assertCheckboxChecked(string $fieldName, string $message = ''): void
2025
{
@@ -23,6 +28,11 @@ public function assertCheckboxChecked(string $fieldName, string $message = ''):
2328

2429
/**
2530
* Asserts that the checkbox with the given name is not checked.
31+
*
32+
* ```php
33+
* <?php
34+
* $I->assertCheckboxNotChecked('subscribe');
35+
* ```
2636
*/
2737
public function assertCheckboxNotChecked(string $fieldName, string $message = ''): void
2838
{
@@ -33,6 +43,11 @@ public function assertCheckboxNotChecked(string $fieldName, string $message = ''
3343

3444
/**
3545
* Asserts that the value of the form input with the given name does not equal the expected value.
46+
*
47+
* ```php
48+
* <?php
49+
* $I->assertInputValueNotSame('username', 'admin');
50+
* ```
3651
*/
3752
public function assertInputValueNotSame(string $fieldName, string $expectedValue, string $message = ''): void
3853
{
@@ -44,6 +59,11 @@ public function assertInputValueNotSame(string $fieldName, string $expectedValue
4459

4560
/**
4661
* Asserts that the value of the form input with the given name equals the expected value.
62+
*
63+
* ```php
64+
* <?php
65+
* $I->assertInputValueSame('username', 'johndoe');
66+
* ```
4767
*/
4868
public function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void
4969
{
@@ -56,6 +76,11 @@ public function assertInputValueSame(string $fieldName, string $expectedValue, s
5676

5777
/**
5878
* Asserts that the `<title>` element contains the given title.
79+
*
80+
* ```php
81+
* <?php
82+
* $I->assertPageTitleContains('Welcome');
83+
* ```
5984
*/
6085
public function assertPageTitleContains(string $expectedTitle, string $message = ''): void
6186
{
@@ -64,6 +89,11 @@ public function assertPageTitleContains(string $expectedTitle, string $message =
6489

6590
/**
6691
* Asserts that the `<title>` element equals the given title.
92+
*
93+
* ```php
94+
* <?php
95+
* $I->assertPageTitleSame('Home Page');
96+
* ```
6797
*/
6898
public function assertPageTitleSame(string $expectedTitle, string $message = ''): void
6999
{
@@ -72,6 +102,11 @@ public function assertPageTitleSame(string $expectedTitle, string $message = '')
72102

73103
/**
74104
* Asserts that the given selector matches at least one element in the response.
105+
*
106+
* ```php
107+
* <?php
108+
* $I->assertSelectorExists('.main-content');
109+
* ```
75110
*/
76111
public function assertSelectorExists(string $selector, string $message = ''): void
77112
{
@@ -80,6 +115,11 @@ public function assertSelectorExists(string $selector, string $message = ''): vo
80115

81116
/**
82117
* Asserts that the given selector does not match at least one element in the response.
118+
*
119+
* ```php
120+
* <?php
121+
* $I->assertSelectorNotExists('.error');
122+
* ```
83123
*/
84124
public function assertSelectorNotExists(string $selector, string $message = ''): void
85125
{
@@ -88,6 +128,11 @@ public function assertSelectorNotExists(string $selector, string $message = ''):
88128

89129
/**
90130
* Asserts that the first element matching the given selector contains the expected text.
131+
*
132+
* ```php
133+
* <?php
134+
* $I->assertSelectorTextContains('h1', 'Dashboard');
135+
* ```
91136
*/
92137
public function assertSelectorTextContains(string $selector, string $text, string $message = ''): void
93138
{
@@ -97,6 +142,11 @@ public function assertSelectorTextContains(string $selector, string $text, strin
97142

98143
/**
99144
* Asserts that the first element matching the given selector does not contain the expected text.
145+
*
146+
* ```php
147+
* <?php
148+
* $I->assertSelectorTextNotContains('p', 'error');
149+
* ```
100150
*/
101151
public function assertSelectorTextNotContains(string $selector, string $text, string $message = ''): void
102152
{
@@ -106,6 +156,11 @@ public function assertSelectorTextNotContains(string $selector, string $text, st
106156

107157
/**
108158
* Asserts that the text of the first element matching the given selector equals the expected text.
159+
*
160+
* ```php
161+
* <?php
162+
* $I->assertSelectorTextSame('h1', 'Dashboard');
163+
* ```
109164
*/
110165
public function assertSelectorTextSame(string $selector, string $text, string $message = ''): void
111166
{

src/Codeception/Module/Symfony/FormAssertionsTrait.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ trait FormAssertionsTrait
1414
{
1515
/**
1616
* Asserts that value of the field of the first form matching the given selector does equal the expected value.
17+
*
18+
* ```php
19+
* <?php
20+
* $I->assertFormValue('#loginForm', 'username', 'john_doe');
21+
* ```
1722
*/
1823
public function assertFormValue(string $formSelector, string $fieldName, string $value, string $message = ''): void
1924
{
@@ -25,7 +30,12 @@ public function assertFormValue(string $formSelector, string $fieldName, string
2530
}
2631

2732
/**
28-
* Asserts that value of the field of the first form matching the given selector does equal the expected value.
33+
* Asserts that the field of the first form matching the given selector does not have a value.
34+
*
35+
* ```php
36+
* <?php
37+
* $I->assertNoFormValue('#registrationForm', 'middle_name');
38+
* ```
2939
*/
3040
public function assertNoFormValue(string $formSelector, string $fieldName, string $message = ''): void
3141
{
@@ -128,15 +138,14 @@ public function seeFormErrorMessage(string $field, ?string $message = null): voi
128138
* If you want to specify the error messages, you can do so
129139
* by sending an associative array instead, with the key being
130140
* the name of the field and the error message the value.
131-
*
132141
* This method will validate that the expected error message
133142
* is contained in the actual error message, that is,
134143
* you can specify either the entire error message or just a part of it:
135144
*
136145
* ```php
137146
* <?php
138147
* $I->seeFormErrorMessages([
139-
* 'address' => 'The address is too long'
148+
* 'address' => 'The address is too long',
140149
* 'telephone' => 'too short', // the full error message is 'The telephone is too short'
141150
* ]);
142151
* ```
@@ -191,4 +200,4 @@ protected function grabFormCollector(string $function): FormDataCollector
191200
{
192201
return $this->grabCollector('form', $function);
193202
}
194-
}
203+
}

0 commit comments

Comments
 (0)