Skip to content

Commit 2b0586e

Browse files
committed
nette/tester 2.5.6
1 parent 5b43931 commit 2b0586e

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,84 @@ 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.
8+
9+
Metoda `fetch()` provede HTTP požadavek a vrátí instanci `HttpAssert` pro následné ověřování pomocí fluent interface.
10+
11+
```php
12+
# Základní HTTP požadavek a ověření odpovědi
13+
$response = Tester\HttpAssert::fetch('https://example.com/api/users');
14+
$response
15+
->expectCode(200)
16+
->expectHeader('Content-Type', contains: 'json')
17+
->expectBody(contains: 'users');
18+
```
19+
20+
Metoda `fetch()` standarně vytváří GET požadavek, ale metodu lze snadno změnit:
21+
22+
```php
23+
# POST s obsahem požadavku
24+
HttpAssert::fetch(
25+
'https://api.example.com/users',
26+
method: 'POST',
27+
body: '{"name": "John", "email": "[email protected]"}'
28+
)
29+
->expectCode(201)
30+
->expectBody(contains: 'created');
31+
```
32+
33+
Stavové kódy můžete ověřovat metodami `expectCode()` a `denyCode()`. Jako parametr předáte buď konkrétní číslo, nebo validační funkci:
34+
35+
```php
36+
$response
37+
->expectCode(200) # přesný kód
38+
->expectCode(fn($code) => $code < 400) # vlastní validace
39+
->denyCode(404) # nesmí být 404
40+
->denyCode(fn($code) => $code >= 500); # nesmí být chyba serveru
41+
```
42+
43+
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:
44+
45+
```php
46+
$response
47+
->expectHeader('Content-Type') # hlavička musí existovat
48+
->expectHeader('Content-Type', 'application/json') # přesná hodnota
49+
->expectHeader('Content-Type', contains: 'json') # obsahuje text
50+
->expectHeader('Server', matches: 'nginx %a%') # odpovídá vzoru
51+
->denyHeader('X-Powered-By'); # hlavička nesmí existovat
52+
```
53+
54+
Obdobně funguje ověřování těla odpovědi prostřednictvím metod `expectBody()` a `denyBody()`:
55+
56+
```php
57+
$response
58+
->expectBody(contains: '"status": "success"') # obsahuje část JSON
59+
->expectBody(matches: '%A% hello %A%') # odpovídá vzoru
60+
->expectBody(fn($body) => json_decode($body)) # vlastní validace
61+
->denyBody(contains: 'error') # nesmí obsahovat text
62+
->denyBody(matches: '~exception|fatal~i'); # nesmí odpovídat vzoru
63+
```
64+
65+
Při vytváření požadavků můžete nastavit vlastní hlavičky a cookies:
66+
67+
```php
68+
HttpAssert::fetch(
69+
'https://api.example.com/protected',
70+
headers: [
71+
'Authorization' => 'Bearer token123',
72+
'Accept' => 'application/json',
73+
],
74+
cookies: [
75+
'session' => 'abc123',
76+
'preferences' => 'dark-mode',
77+
]
78+
)
79+
->expectCode(200);
80+
```
81+
82+
583
DomQuery
684
--------
785
`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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,84 @@ Helpers
22
*******
33

44

5+
HttpAssert .{data-version:2.5.6}
6+
----------
7+
The `Tester\HttpAssert` class provides tools for testing HTTP responses. It allows you to perform HTTP requests and verify status codes, headers, and response body content using a fluent interface.
8+
9+
The `fetch()` method makes an HTTP request and returns an HttpAssert instance for chaining verification methods:
10+
11+
```php
12+
# Basic HTTP request and response verification
13+
$response = Tester\HttpAssert::fetch('https://example.com/api/users');
14+
$response
15+
->expectCode(200)
16+
->expectHeader('Content-Type', contains: 'json')
17+
->expectBody(contains: 'users');
18+
```
19+
20+
By default, `fetch()` performs a GET request, but you can specify different HTTP methods:
21+
22+
```php
23+
# POST with request body
24+
HttpAssert::fetch(
25+
'https://api.example.com/users',
26+
method: 'POST',
27+
body: '{"name": "John", "email": "[email protected]"}'
28+
)
29+
->expectCode(201)
30+
->expectBody(contains: 'created');
31+
```
32+
33+
Status codes can be verified using `expectCode()` and `denyCode()` methods. You can pass either a specific number or a validation function:
34+
35+
```php
36+
$response
37+
->expectCode(200) # exact code
38+
->expectCode(fn($code) => $code < 400) # custom validation
39+
->denyCode(404) # must not be 404
40+
->denyCode(fn($code) => $code >= 500); # must not be server error
41+
```
42+
43+
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:
44+
45+
```php
46+
$response
47+
->expectHeader('Content-Type') # header must exist
48+
->expectHeader('Content-Type', 'application/json') # exact value
49+
->expectHeader('Content-Type', contains: 'json') # contains text
50+
->expectHeader('Server', matches: 'nginx %a%') # matches pattern
51+
->denyHeader('X-Powered-By'); # header must not exist
52+
```
53+
54+
Response body verification works similarly with `expectBody()` and `denyBody()` methods:
55+
56+
```php
57+
$response
58+
->expectBody(contains: '"status": "success"') # contains JSON fragment
59+
->expectBody(matches: '%A% hello %A%') # matches pattern
60+
->expectBody(fn($body) => json_decode($body)) # custom validation
61+
->denyBody(contains: 'error') # must not contain text
62+
->denyBody(matches: '~exception|fatal~i'); # must not match pattern
63+
```
64+
65+
You can customize requests by setting headers and cookies:
66+
67+
```php
68+
HttpAssert::fetch(
69+
'https://api.example.com/protected',
70+
headers: [
71+
'Authorization' => 'Bearer token123',
72+
'Accept' => 'application/json',
73+
],
74+
cookies: [
75+
'session' => 'abc123',
76+
'preferences' => 'dark-mode',
77+
]
78+
)
79+
->expectCode(200);
80+
```
81+
82+
583
DomQuery
684
--------
785
`Tester\DomQuery` is a class extending `SimpleXMLElement` with easy querying in HTML or XML using CSS selectors.

0 commit comments

Comments
 (0)