Skip to content

Commit 644cc39

Browse files
committed
Add example exception messages to README
1 parent 17db15e commit 644cc39

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,28 @@ $this->assertAssociativeArray([
7575
], $data);
7676
```
7777

78+
**Debugging:**
79+
80+
```php
81+
$data = [
82+
'answer' => 21 /* half the truth */
83+
];
84+
85+
$this->assertAssociativeArray([
86+
'answer' => $this->identicalTo(42)
87+
], $data);
88+
89+
// Will fail with the following message:
90+
//
91+
// Failed asserting that associative array matches constraints.
92+
// +----------+-------+--------------------------+
93+
// | Key | Value | Constraint |
94+
// +----------+-------+--------------------------+
95+
// | 'answer' | 21 | Value is identical to 42 |
96+
// +----------+-------+--------------------------+
97+
// [ ] Allow missing; [x] Allow additional
98+
```
99+
78100
### Constraint `ArrayHasKeyWith`
79101

80102
The `ArrayHasKeyWith` constraint asserts that an array has a given key and that its value passes another constraint.
@@ -112,6 +134,19 @@ $data = [
112134
$this->assertArrayHasKeyWith('name', $this->identicalTo('Arthur Dent'), $data);
113135
```
114136

137+
**Debugging:**
138+
139+
```php
140+
$data = [];
141+
142+
$this->assertArrayHasKeyWith('answer', $this->identicalTo(42), $data);
143+
144+
// Will fail with the following message:
145+
//
146+
// Failed asserting that Array &0 () is an array that
147+
// has the key 'answer' whose value is identical to 42.
148+
```
149+
115150
### Constraint `SequentialArray`
116151

117152
The `SequentialArray` constraint asserts that a value is like a sequential array, has a minimum and/or maximum number of items, and that all items pass another constraint.
@@ -156,6 +191,19 @@ $data = [
156191
$this->assertSequentialArray($data, 1, null, $this->logicalNot($this->isEmpty()));
157192
```
158193

194+
**Debugging:**
195+
196+
```php
197+
$data = [];
198+
199+
$this->assertSequentialArray($data, 4, null, $this->is(IsType::TYPE_STRING));
200+
201+
// Will fail with the following message:
202+
//
203+
// Failed asserting that Array &0 () is is a sequential array
204+
// with ≥ 4 items matching the constraint "is of type "string"".
205+
```
206+
159207
### Constraint `ArrayHasItemWith`
160208

161209
The `ArrayHasItemWith` constraint asserts that an array has a item at a given index and that its value passes another constraint.
@@ -198,6 +246,19 @@ $data = [
198246
$this->assertArrayHasItemWith(2, $this->identicalTo("Life, the Universe and Everything"));
199247
```
200248

249+
**Debugging:**
250+
251+
```php
252+
$data = [];
253+
254+
$this->assertArrayHasItemWith(2, $this->identicalTo('Arthur Dent'), $data);
255+
256+
// Will fail with the following message:
257+
//
258+
// Failed asserting that Array &0 () is an array that
259+
// has a value at index 2 which is identical to 'Arthur Dent'.
260+
```
261+
201262
Example
202263
-------
203264

0 commit comments

Comments
 (0)