Skip to content

Commit 2e7a9b3

Browse files
Merge branch '12.5'
* 12.5: Update ChangeLog fix bad merge Adjust 52a5d94 and e70c0ff for 12.4 Update ChangeLog Reorganize Remove now duplicated events Report invalid data provider on exceptions
2 parents 6a9a417 + 6b2f821 commit 2e7a9b3

File tree

3 files changed

+120
-59
lines changed

3 files changed

+120
-59
lines changed

src/Metadata/Api/DataProvider.php

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -169,69 +169,67 @@ private function dataProvidedByMethods(string $testClassName, ReflectionMethod $
169169
throw InvalidDataProviderException::forException($e, $providerLabel);
170170
}
171171

172-
foreach ($data as $key => $value) {
173-
if (!is_int($key) && !is_string($key)) {
174-
Event\Facade::emitter()->dataProviderMethodFinished(
175-
$testMethodValueObject,
176-
...$methodsCalled,
177-
);
178-
179-
throw new InvalidDataProviderException(
180-
sprintf(
181-
'The key must be an integer or a string, %s given',
182-
get_debug_type($key),
183-
),
184-
);
185-
}
186-
187-
if (!is_array($value)) {
188-
Event\Facade::emitter()->dataProviderMethodFinished(
189-
$testMethodValueObject,
190-
...$methodsCalled,
191-
);
192-
193-
throw new InvalidDataProviderException(
194-
sprintf(
195-
'Data set %s provided by %s is invalid, expected array but got %s',
172+
try {
173+
foreach ($data as $key => $value) {
174+
if (!is_int($key) && !is_string($key)) {
175+
throw new InvalidDataProviderException(
176+
sprintf(
177+
'The key must be an integer or a string, %s given',
178+
get_debug_type($key),
179+
),
180+
);
181+
}
182+
183+
if (!is_array($value)) {
184+
throw new InvalidDataProviderException(
185+
sprintf(
186+
'Data set %s provided by %s is invalid, expected array but got %s',
187+
$this->formatKey($key),
188+
$providerLabel,
189+
get_debug_type($value),
190+
),
191+
);
192+
}
193+
194+
if ($validateArgumentCount && $testMethodNumberOfParameters < count($value)) {
195+
$this->triggerWarningForArgumentCount(
196+
$testMethod,
196197
$this->formatKey($key),
197198
$providerLabel,
198-
get_debug_type($value),
199-
),
200-
);
201-
}
202-
203-
if ($validateArgumentCount && $testMethodNumberOfParameters < count($value)) {
204-
$this->triggerWarningForArgumentCount(
205-
$testMethod,
206-
$this->formatKey($key),
207-
$providerLabel,
208-
count($value),
209-
$testMethodNumberOfParameters,
210-
);
211-
}
212-
213-
if (is_int($key)) {
214-
$result[] = new ProvidedData($providerLabel, $value);
215-
216-
continue;
217-
}
218-
219-
if (array_key_exists($key, $result)) {
220-
Event\Facade::emitter()->dataProviderMethodFinished(
221-
$testMethodValueObject,
222-
...$methodsCalled,
223-
);
224-
225-
throw new InvalidDataProviderException(
226-
sprintf(
227-
'The key "%s" has already been defined by provider %s',
228-
$key,
229-
$result[$key]->label(),
230-
),
231-
);
199+
count($value),
200+
$testMethodNumberOfParameters,
201+
);
202+
}
203+
204+
if (is_int($key)) {
205+
$result[] = new ProvidedData($providerLabel, $value);
206+
207+
continue;
208+
}
209+
210+
if (array_key_exists($key, $result)) {
211+
throw new InvalidDataProviderException(
212+
sprintf(
213+
'The key "%s" has already been defined by provider %s',
214+
$key,
215+
$result[$key]->label(),
216+
),
217+
);
218+
}
219+
220+
$result[$key] = new ProvidedData($providerLabel, $value);
232221
}
222+
} catch (Throwable $e) {
223+
Event\Facade::emitter()->dataProviderMethodFinished(
224+
$testMethodValueObject,
225+
...$methodsCalled,
226+
);
233227

234-
$result[$key] = new ProvidedData($providerLabel, $value);
228+
throw new InvalidDataProviderException(
229+
$e->getMessage(),
230+
$e->getCode(),
231+
$e,
232+
);
235233
}
236234
}
237235

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
https://github.com/sebastianbergmann/phpunit/issues/6408
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = __DIR__ . '/6408/Issue6408Test.php';
8+
9+
require_once __DIR__ . '/../../bootstrap.php';
10+
11+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
12+
--EXPECTF--
13+
PHPUnit %s by Sebastian Bergmann and contributors.
14+
15+
Runtime: %s
16+
17+
There was 1 PHPUnit error:
18+
19+
1) PHPUnit\TestFixture\Issue6408\Issue6408Test::testFoo
20+
The data provider specified for PHPUnit\TestFixture\Issue6408\Issue6408Test::testFoo is invalid
21+
a users exception from data-provider context
22+
23+
%sIssue6408Test.php:%d
24+
25+
--
26+
27+
There was 1 PHPUnit test runner warning:
28+
29+
1) No tests found in class "PHPUnit\TestFixture\Issue6408\Issue6408Test".
30+
31+
No tests executed!
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\Issue6408;
11+
12+
use PHPUnit\Framework\Attributes\DataProvider;
13+
use PHPUnit\Framework\TestCase;
14+
use RuntimeException;
15+
16+
final class Issue6408Test extends TestCase
17+
{
18+
public static function provideData(): iterable
19+
{
20+
yield from self::gatherAssertTypes();
21+
}
22+
23+
public static function gatherAssertTypes(): array
24+
{
25+
throw new RuntimeException('a users exception from data-provider context');
26+
}
27+
28+
#[DataProvider('provideData')]
29+
public function testFoo(): void
30+
{
31+
}
32+
}

0 commit comments

Comments
 (0)