-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBasicUsage.php
More file actions
77 lines (68 loc) · 2.46 KB
/
BasicUsage.php
File metadata and controls
77 lines (68 loc) · 2.46 KB
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
<?php
/**
* Made with love.
*/
declare(strict_types = 1);
namespace FallegaHQ\JsonTestUtils\Examples;
use FallegaHQ\JsonTestUtils\JsonAssertions;
use PHPUnit\Framework\TestCase;
class BasicUsage extends TestCase {
use JsonAssertions;
/**
* Example of validating a simple JSON response
*/
public function testSimpleJsonValidation(): void {
// Sample JSON response (as string or already decoded array)
$json = <<<'EOD'
{
"status": "success",
"code": 200,
"data": {
"user": {
"id": 123,
"name": "John Doe",
"email": "john@example.com",
"is_active": true,
"tags": ["customer", "premium"]
}
}
}
EOD;
// Basic assertions using trait methods
$this->assertJsonHasKey($json, 'status');
$this->assertJsonEquals($json, 'status', 'success');
$this->assertJsonType($json, 'code', 'integer');
$this->assertJsonHasKey($json, 'data.user');
$this->assertJsonType($json, 'data.user.tags', 'array');
// Using a custom condition
$this->assertJsonCondition($json, 'data.user.id', function ($value) {
return $value > 0 && $value < 1_000;
});
}
/**
* Example of using fluent syntax for more readable tests
*/
public function testFluentSyntax(): void {
$json = <<<'EOD'
{
"product": {
"id": "prod-123",
"name": "Premium Widget",
"price": 49.99,
"in_stock": true,
"categories": ["electronics", "gadgets"]
}
}
EOD;
// Fluent API offers a chainable, readable syntax
$this->assertValidJson($json)
->hasKey('product')
->hasKey('product.name')
->equals('product.name', 'Premium Widget')
->isType('product.price', 'float')
->isType('product.in_stock', 'boolean')
->isType('product.categories', 'array')
->hasLength('product.categories', 2) // Exactly 2 categories
->assert();
}
}