Skip to content

Commit 28a3213

Browse files
committed
nette/tester 2.5.6
1 parent b664304 commit 28a3213

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

tester/cs/assertions.texy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ Assert::match('Error in file %a% on line %i%', $errorMessage);
227227
```
228228

229229

230+
Assert::notMatch(string $pattern, $actual, ?string $description=null) .[method]{data-version:2.5.6}
231+
---------------------------------------------------------------------------------------------------
232+
Opak `Assert::match()`.
233+
234+
230235
Assert::matchFile(string $file, $actual, ?string $description=null) .[method]
231236
-----------------------------------------------------------------------------
232237
Aserce je totožná s [#Assert::match()], ale vzor se načítá ze souboru `$file`. To je užitečné pro testování velmi dlouhých řetězců. Soubor s testem zůstane přehledný.

tester/cs/helpers.texy

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,87 @@ Pomocné třídy
22
*************
33

44

5+
HttpAssert .{data-version:2.5.6}
6+
--------------------------------
7+
Třída `Tester\HttpAssert` slouží k testování HTTP serveru. Umožňuje jednoduše provádět HTTP požadavky a ověřovat jejich stavové kódy, hlavičky i obsah odpovědi pomocí fluent interface.
8+
9+
```php
10+
# Základní HTTP požadavek a ověření odpovědi
11+
$response = Tester\HttpAssert::fetch('https://example.com/api/users');
12+
$response
13+
->expectCode(200)
14+
->expectHeader('Content-Type', contains: 'json')
15+
->expectBody(contains: 'users');
16+
```
17+
18+
Metoda `fetch()` standardně vytváří GET požadavek, ale všechny parametry lze přizpůsobit:
19+
20+
```php
21+
HttpAssert::fetch(
22+
'https://api.example.com/users',
23+
method: 'POST',
24+
headers: [
25+
'Authorization' => 'Bearer token123', # asociativní pole
26+
'Accept: application/json', # nebo string formát
27+
],
28+
cookies: ['session' => 'abc123'],
29+
follow: false, # nesledovat redirecty
30+
body: '{"name": "John"}'
31+
)
32+
->expectCode(201);
33+
```
34+
35+
Stavové kódy můžete ověřovat metodami `expectCode()` a `denyCode()`. Jako parametr předáte buď konkrétní číslo, nebo validační funkci:
36+
37+
```php
38+
$response
39+
->expectCode(200) # přesný kód
40+
->expectCode(fn($code) => $code < 400) # vlastní validace
41+
->denyCode(404) # nesmí být 404
42+
->denyCode(fn($code) => $code >= 500); # nesmí být chyba serveru
43+
```
44+
45+
Pro práci s hlavičkami slouží metody `expectHeader()` a `denyHeader()`. Můžete kontrolovat existenci hlavičky, její přesnou hodnotu nebo jen část obsahu:
46+
47+
```php
48+
$response
49+
->expectHeader('Content-Type') # hlavička musí existovat
50+
->expectHeader('Content-Type', 'application/json') # přesná hodnota
51+
->expectHeader('Content-Type', contains: 'json') # obsahuje text
52+
->expectHeader('Server', matches: 'nginx %a%') # odpovídá vzoru
53+
->denyHeader('X-Powered-By') # hlavička nesmí existovat
54+
->denyHeader('X-Debug', contains: 'sensitive') # nesmí obsahovat text
55+
->denyHeader('X-Debug', matches: '~debug~i'); # nesmí odpovídat vzoru
56+
```
57+
58+
Obdobně funguje ověřování těla odpovědi prostřednictvím metod `expectBody()` a `denyBody()`:
59+
60+
```php
61+
$response
62+
->expectBody('OK') # přesná hodnota
63+
->expectBody(contains: '"status": "success"') # obsahuje část JSON
64+
->expectBody(matches: '%A% hello %A%') # odpovídá vzoru
65+
->expectBody(fn($body) => json_decode($body)) # vlastní validace
66+
->denyBody('Error occurred') # nesmí mít přesnou hodnotu
67+
->denyBody(contains: 'error') # nesmí obsahovat text
68+
->denyBody(matches: '~exception|fatal~i'); # nesmí odpovídat vzoru
69+
```
70+
71+
Parametr `follow` řídí, jak HttpAssert zachází s HTTP přesměrováními:
72+
73+
```php
74+
# Testování redirectu bez následování (výchozí)
75+
HttpAssert::fetch('https://example.com/redirect', follow: false)
76+
->expectCode(301)
77+
->expectHeader('Location', 'https://example.com/new-url');
78+
79+
# Následování všech redirectů až do konečné odpovědi
80+
HttpAssert::fetch('https://example.com/redirect', follow: true)
81+
->expectCode(200)
82+
->expectBody(contains: 'final content');
83+
```
84+
85+
586
DomQuery
687
--------
788
`Tester\DomQuery` je třída rozšiřující `SimpleXMLElement` o snadné vyhledávání v HTML nebo XML pomocí CSS selektorů.

tester/en/assertions.texy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ Assert::match('Error in file %a% on line %i%', $errorMessage);
227227
```
228228

229229

230+
Assert::notMatch(string $pattern, $actual, ?string $description=null) .[method]{data-version:2.5.6}
231+
---------------------------------------------------------------------------------------------------
232+
Opposite of `Assert::match()`.
233+
234+
230235
Assert::matchFile(string $file, $actual, ?string $description=null) .[method]
231236
-----------------------------------------------------------------------------
232237
This assertion is identical to [#Assert::match()], but the pattern is loaded from the file `$file`. This is useful for testing very long strings. The test file remains clear.

tester/en/helpers.texy

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,87 @@ Helpers
22
*******
33

44

5+
HttpAssert .{data-version:2.5.6}
6+
--------------------------------
7+
The `Tester\HttpAssert` class provides tools for testing HTTP servers. It allows you to easily perform HTTP requests and verify status codes, headers, and response body content using a fluent interface.
8+
9+
```php
10+
# Basic HTTP request and response verification
11+
$response = Tester\HttpAssert::fetch('https://example.com/api/users');
12+
$response
13+
->expectCode(200)
14+
->expectHeader('Content-Type', contains: 'json')
15+
->expectBody(contains: 'users');
16+
```
17+
18+
The `fetch()` method creates a GET request by default, but all parameters can be customized:
19+
20+
```php
21+
HttpAssert::fetch(
22+
'https://api.example.com/users',
23+
method: 'POST',
24+
headers: [
25+
'Authorization' => 'Bearer token123', # associative array
26+
'Accept: application/json', # or string format
27+
],
28+
cookies: ['session' => 'abc123'],
29+
follow: false, # do not follow redirects
30+
body: '{"name": "John"}'
31+
)
32+
->expectCode(201);
33+
```
34+
35+
Status codes can be verified using `expectCode()` and `denyCode()` methods. You can pass either a specific number or a validation function:
36+
37+
```php
38+
$response
39+
->expectCode(200) # exact code
40+
->expectCode(fn($code) => $code < 400) # custom validation
41+
->denyCode(404) # must not be 404
42+
->denyCode(fn($code) => $code >= 500); # must not be server error
43+
```
44+
45+
For header verification, use `expectHeader()` and `denyHeader()` methods. You can check whether a header exists, verify its exact value, or match part of its content:
46+
47+
```php
48+
$response
49+
->expectHeader('Content-Type') # header must exist
50+
->expectHeader('Content-Type', 'application/json') # exact value
51+
->expectHeader('Content-Type', contains: 'json') # contains text
52+
->expectHeader('Server', matches: 'nginx %a%') # matches pattern
53+
->denyHeader('X-Powered-By') # header must not exist
54+
->denyHeader('X-Debug', contains: 'sensitive') # must not contain text
55+
->denyHeader('X-Debug', matches: '~debug~i'); # must not match pattern
56+
```
57+
58+
Response body verification works similarly with `expectBody()` and `denyBody()` methods:
59+
60+
```php
61+
$response
62+
->expectBody('OK') # exact value
63+
->expectBody(contains: '"status": "success"') # contains JSON fragment
64+
->expectBody(matches: '%A% hello %A%') # matches pattern
65+
->expectBody(fn($body) => json_decode($body)) # custom validation
66+
->denyBody('Error occurred') # must not have exact value
67+
->denyBody(contains: 'error') # must not contain text
68+
->denyBody(matches: '~exception|fatal~i'); # must not match pattern
69+
```
70+
71+
The `follow` parameter controls how HttpAssert handles HTTP redirects:
72+
73+
```php
74+
# Testing redirect without following (default)
75+
HttpAssert::fetch('https://example.com/redirect', follow: false)
76+
->expectCode(301)
77+
->expectHeader('Location', 'https://example.com/new-url');
78+
79+
# Following all redirects to the final response
80+
HttpAssert::fetch('https://example.com/redirect', follow: true)
81+
->expectCode(200)
82+
->expectBody(contains: 'final content');
83+
```
84+
85+
586
DomQuery
687
--------
788
`Tester\DomQuery` is a class extending `SimpleXMLElement` with easy querying in HTML or XML using CSS selectors.

0 commit comments

Comments
 (0)