You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: blog/data-providers.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,12 +50,12 @@ Well, this article wouldn't exist if there was nothing to say.
50
50
51
51
**First**, I didn't like the `#[TestWith]` attribute name in PHPUnit. It conveys the intent well (*test with "this"*), but what about consistency? I wouldn't have known about this attribute if not by chance (do you know about it?).
52
52
53
-
::: tip ☝️ It would be better if this attribute appeared in IDE suggestions when typing "Data": next to `DataProvider`.
53
+
::: tip ☝️ It would be better if this attribute appeared in IDE suggestions when typing "Data": next to <attr>\Testo\Data\DataProvider</attr>.
54
54
:::
55
55
56
-
That's why in Testo this attribute is named: `#[DataSet]`.
56
+
That's why in Testo this attribute is named: <attr>\Testo\Data\DataSet</attr>.
57
57
58
-
**Second**, Testo has no separate `#[DataProviderExternal]` attribute: the need for it simply disappears, since you can pass any `callable` to `#[DataProvider]`.
58
+
**Second**, Testo has no separate `#[DataProviderExternal]` attribute: the need for it simply disappears, since you can pass any `callable` to <attr>\Testo\Data\DataProvider</attr>.
59
59
60
60
**And third**, datasets in Testo can merge not only vertically, but also horizontally and diagonally.
61
61
@@ -120,7 +120,7 @@ Like PHPUnit, Testo expects data providers to return dataset collections: `itera
120
120
121
121
### Combining Providers
122
122
123
-
You probably guessed that stacking multiple Data-attributes (`#[DataSet]` and `#[DataProvider]`) on a function will grow the dataset collection, similar to a UNION query in SQL.
123
+
You probably guessed that stacking multiple Data-attributes (<attr>\Testo\Data\DataSet</attr> and <attr>\Testo\Data\DataProvider</attr>) on a function will grow the dataset collection, similar to a UNION query in SQL.
124
124
125
125
```php
126
126
#[DataSet([1, 1, 2])]
@@ -129,7 +129,7 @@ You probably guessed that stacking multiple Data-attributes (`#[DataSet]` and `#
129
129
public function testSum(int $a, int $b, int $c): void { ... }
130
130
```
131
131
132
-
The test runs for all datasets sequentially: first `[1, 1, 2]` from `DataSet`, then all from `dataSum`, then all from `SomeClass::method()`.
132
+
The test runs for all datasets sequentially: first `[1, 1, 2]` from <attr>\Testo\Data\DataSet</attr>, then all from `dataSum`, then all from `SomeClass::method()`.
133
133
134
134
::: info 🤔 But what if you want to combine datasets in more interesting ways?
135
135
:::
@@ -171,18 +171,18 @@ Cartesian product — all possible combinations. Useful when parameters are inde
171
171
public function testResponsiveLayout(string $browser, int $width, int $height): void { ... }
172
172
```
173
173
174
-
3 browsers × 3 screen sizes = 9 tests. Three providers with 5 elements each — already 125 tests. `DataCross` grows fast, use wisely.
174
+
3 browsers × 3 screen sizes = 9 tests. Three providers with 5 elements each — already 125 tests. <attr>\Testo\Data\DataCross</attr> grows fast, use wisely.
175
175
176
176
### DataUnion
177
177
178
178

179
179
180
-
The `#[DataUnion]` attribute merges multiple providers into one — simply concatenates datasets into a single collection, just like stacking multiple `#[DataProvider]` attributes on a test.
180
+
The <attr>\Testo\Data\DataUnion</attr> attribute merges multiple providers into one — simply concatenates datasets into a single collection, just like stacking multiple <attr>\Testo\Data\DataProvider</attr> attributes on a test.
181
181
182
182
::: info 🫤 Wait, why a separate attribute?
183
183
:::
184
184
185
-
`DataUnion` is needed **inside**`DataCross` or `DataZip`:
185
+
<attr>\Testo\Data\DataUnion</attr> is needed **inside**<attr>\Testo\Data\DataCross</attr> or <attr>\Testo\Data\DataZip</attr>:
<short>Provides data for a parameterized test from a method or callable.</short>
39
+
<paramname="$provider">Data source: method name (`'method'`), callable (`[Class::class, 'method']`), closure, or invokable object. Must return `iterable`. String keys of elements become dataset labels in reports.</param>
40
+
<example>
41
41
```php
42
42
#[Test]
43
43
#[DataProvider('userDataProvider')]
@@ -52,13 +52,14 @@ public function userDataProvider(): iterable
52
52
yield ['valid@example.com', true];
53
53
yield ['invalid', false];
54
54
yield ['test@domain.co.uk', true];
55
-
// ... 50 more cases
56
55
}
57
56
```
57
+
</example>
58
+
</signature>
58
59
59
60
### Flexible Provider Sources
60
61
61
-
`DataProvider` accepts various callable types:
62
+
<attr>\Testo\Data\DataProvider</attr> accepts various callable types:
62
63
63
64
**Method name from the same class:**
64
65
```php
@@ -104,12 +105,13 @@ public function userDataProvider(): array
104
105
}
105
106
```
106
107
107
-
## DataZip
108
-
109
-
Pairs up multiple providers element by element. The first item from the first provider joins with the first item from the second, second with second, and so on.
110
-
111
-
Typical use case — testing related data where each pair forms a meaningful test case:
<short>Pairs up providers element by element.</short>
110
+
<description>
111
+
The first item from the first provider joins with the first from the second, second with second, and so on. Arguments from all providers merge into a single test call.
112
+
</description>
113
+
<paramname="$providers">Data providers to pair up.</param>
114
+
<example>
113
115
```php
114
116
#[DataZip(
115
117
new DataProvider('credentials'),
@@ -128,8 +130,8 @@ public function testUserPermissions(string $login, string $password, array $perm
128
130
// 1. admin/secret → ['read', 'write', 'delete']
129
131
// 2. guest/1234 → ['read']
130
132
```
131
-
132
-
Arguments from all providers merge into a single test call. In the example above, `credentials` provides two arguments (`$login`, `$password`), while `expectedPermissions` provides one (`$permissions`).
133
+
</example>
134
+
</signature>
133
135
134
136
### Providers of Different Lengths
135
137
@@ -155,10 +157,10 @@ public function testTransform(string $input, string $output): void { ... }
155
157
Dataset labels are joined with `|`. If datasets are named `admin` and `full-access`, the report shows `admin|full-access`.
156
158
:::
157
159
158
-
## DataCross
159
-
160
-
Creates all possible combinations of values from providers (cartesian product). Useful for testing independent parameters that can combine in any way.
<short>Merges data from multiple providers into a single sequential set.</short>
197
+
<description>
198
+
To combine data from multiple sources, you can simply list multiple <attr>\Testo\Data\DataProvider</attr> or <attr>\Testo\Data\DataSet</attr> above the method. <attr>\Testo\Data\DataUnion</attr> is needed when combining must happen inside another attribute — for example, inside <attr>\Testo\Data\DataCross</attr> or <attr>\Testo\Data\DataZip</attr>.
199
+
</description>
200
+
<paramname="$providers">Data providers to merge into a single set.</param>
201
+
<example>
207
202
```php
208
203
#[DataCross(
209
204
new DataUnion(
@@ -217,12 +212,12 @@ public function testExport(string $format, int $compression): void
217
212
// All formats (legacy + modern) are crossed with each compression level
218
213
}
219
214
```
220
-
221
-
Without `DataUnion`, you'd have to either create a separate provider that merges formats, or duplicate `DataCross` for each format source.
215
+
</example>
216
+
</signature>
222
217
223
218
## Combining Providers
224
219
225
-
Inside `DataZip`, `DataCross`, and `DataUnion` you can use any data providers — `DataProvider`, `DataSet`, and even nest them within each other.
220
+
Inside <attr>\Testo\Data\DataZip</attr>, <attr>\Testo\Data\DataCross</attr>, and <attr>\Testo\Data\DataUnion</attr> you can use any data providers — <attr>\Testo\Data\DataProvider</attr>, <attr>\Testo\Data\DataSet</attr>, and even nest them within each other.
226
221
227
222
### Mixing Types
228
223
@@ -237,7 +232,7 @@ Handy when some parameters are fixed while others come from a provider:
237
232
public function testMigration(string $driver, array $scenario): void { ... }
238
233
```
239
234
240
-
Or more compact with a `DataProvider` for drivers:
235
+
Or more compact with a <attr>\Testo\Data\DataProvider</attr> for drivers:
Copy file name to clipboardExpand all lines: ru/blog/data-providers.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,12 +50,12 @@ public function testSum(int $a, int $b, int $c): void { ... }
50
50
51
51
**Во-первых**, мне в PHPUnit не понравилось название атрибута `#[TestWith]`. Оно неплохо передаёт намерение (*протестировать с "этим"*), но что насчёт консистентности? Я бы так и не узнал об этом атрибуте, если бы не случайность (а вы знаете о нём?).
52
52
53
-
::: tip ☝️ Было бы лучше, если бы такой атрибут вылезал в подсказках IDE при вводе слова "Data": рядом с `DataProvider`.
53
+
::: tip ☝️ Было бы лучше, если бы такой атрибут вылезал в подсказках IDE при вводе слова "Data": рядом с <attr>\Testo\Data\DataProvider</attr>.
54
54
:::
55
55
56
-
Поэтому в Testo этот атрибут назван: `#[DataSet]`.
56
+
Поэтому в Testo этот атрибут назван: <attr>\Testo\Data\DataSet</attr>.
57
57
58
-
**Во-вторых**, в Testo нет отдельного атрибута `#[DataProviderExternal]`: надобность в нём просто исчезает, поскольку в `#[DataProvider]` можно просто передать любой `callable`.
58
+
**Во-вторых**, в Testo нет отдельного атрибута `#[DataProviderExternal]`: надобность в нём просто исчезает, поскольку в <attr>\Testo\Data\DataProvider</attr> можно просто передать любой `callable`.
59
59
60
60
**И в-третьих**, датасеты в Testo могут мержиться не только вертикально, но и горизонтально, и по-диагонали.
61
61
@@ -120,7 +120,7 @@ public function testSum(int $a, int $b, int $c): void { ... }
120
120
121
121
### Комбинирование провайдеров
122
122
123
-
Вы, наверное, догадываетесь, что если Data-атрибуты (`#[DataSet]` и `#[DataProvider]`) повесить на функцию несколько раз, то это приведёт к увеличению коллекции датасетов подобно UNION запросу в SQL.
123
+
Вы, наверное, догадываетесь, что если Data-атрибуты (<attr>\Testo\Data\DataSet</attr> и <attr>\Testo\Data\DataProvider</attr>) повесить на функцию несколько раз, то это приведёт к увеличению коллекции датасетов подобно UNION запросу в SQL.
124
124
125
125
```php
126
126
#[DataSet([1, 1, 2])]
@@ -129,7 +129,7 @@ public function testSum(int $a, int $b, int $c): void { ... }
129
129
public function testSum(int $a, int $b, int $c): void { ... }
130
130
```
131
131
132
-
Тест запустится для всех датасетов последовательно: сначала `[1, 1, 2]` из `DataSet`, затем все из `dataSum`, затем все из `SomeClass::method()`.
132
+
Тест запустится для всех датасетов последовательно: сначала `[1, 1, 2]` из <attr>\Testo\Data\DataSet</attr>, затем все из `dataSum`, затем все из `SomeClass::method()`.
133
133
134
134
::: info 🤔 Но что, если хочется соединить датасеты как-то поинтереснее?
135
135
:::
@@ -171,18 +171,18 @@ public function testDeletePermission(string $user, bool $expected): void { ... }
171
171
public function testResponsiveLayout(string $browser, int $width, int $height): void { ... }
172
172
```
173
173
174
-
3 браузера × 3 разрешения = 9 тестов. Три провайдера по 5 элементов — уже 125 тестов. `DataCross` растёт быстро, используйте осознанно.
174
+
3 браузера × 3 разрешения = 9 тестов. Три провайдера по 5 элементов — уже 125 тестов. <attr>\Testo\Data\DataCross</attr> растёт быстро, используйте осознанно.
175
175
176
176
### DataUnion
177
177
178
178

179
179
180
-
Атрибут `#[DataUnion]` Объединяет несколько провайдеров в один — просто склеивает датасеты в общую коллекцию наравне с тем, как если бы несколько `#[DataProvider]` были повешены на тест.
180
+
Атрибут <attr>\Testo\Data\DataUnion</attr> объединяет несколько провайдеров в один — просто склеивает датасеты в общую коллекцию наравне с тем, как если бы несколько <attr>\Testo\Data\DataProvider</attr> были повешены на тест.
181
181
182
182
::: info 🫤 Стоп, а зачем отдельный атрибут?
183
183
:::
184
184
185
-
`DataUnion` нужен **внутри**`DataCross` или `DataZip`:
185
+
<attr>\Testo\Data\DataUnion</attr> нужен **внутри**<attr>\Testo\Data\DataCross</attr> или <attr>\Testo\Data\DataZip</attr>:
0 commit comments