Skip to content

Commit 52a5d94

Browse files
staabmsebastianbergmann
authored andcommitted
Report invalid data provider on exceptions
1 parent b9bbbe5 commit 52a5d94

File tree

3 files changed

+96
-33
lines changed

3 files changed

+96
-33
lines changed

src/Metadata/Api/DataProvider.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -167,51 +167,51 @@ private function dataProvidedByMethods(string $className, string $methodName, Me
167167
$className = $_dataProvider->className();
168168
$methodName = $_dataProvider->methodName();
169169
$data = $className::$methodName();
170-
} catch (Throwable $e) {
171-
Event\Facade::emitter()->dataProviderMethodFinished(
172-
$testMethod,
173-
...$methodsCalled,
174-
);
175170

176-
throw new InvalidDataProviderException(
177-
$e->getMessage(),
178-
$e->getCode(),
179-
$e,
180-
);
181-
}
182-
183-
foreach ($data as $key => $value) {
184-
if (is_int($key)) {
185-
$result[] = $value;
186-
} elseif (is_string($key)) {
187-
if (array_key_exists($key, $result)) {
171+
foreach ($data as $key => $value) {
172+
if (is_int($key)) {
173+
$result[] = $value;
174+
} elseif (is_string($key)) {
175+
if (array_key_exists($key, $result)) {
176+
Event\Facade::emitter()->dataProviderMethodFinished(
177+
$testMethod,
178+
...$methodsCalled,
179+
);
180+
181+
throw new InvalidDataProviderException(
182+
sprintf(
183+
'The key "%s" has already been defined by a previous data provider',
184+
$key,
185+
),
186+
);
187+
}
188+
189+
$result[$key] = $value;
190+
} else {
188191
Event\Facade::emitter()->dataProviderMethodFinished(
189192
$testMethod,
190193
...$methodsCalled,
191194
);
192195

193196
throw new InvalidDataProviderException(
194197
sprintf(
195-
'The key "%s" has already been defined by a previous data provider',
196-
$key,
198+
'The key must be an integer or a string, %s given',
199+
get_debug_type($key),
197200
),
198201
);
199202
}
200-
201-
$result[$key] = $value;
202-
} else {
203-
Event\Facade::emitter()->dataProviderMethodFinished(
204-
$testMethod,
205-
...$methodsCalled,
206-
);
207-
208-
throw new InvalidDataProviderException(
209-
sprintf(
210-
'The key must be an integer or a string, %s given',
211-
get_debug_type($key),
212-
),
213-
);
214203
}
204+
} catch (Throwable $e) {
205+
Event\Facade::emitter()->dataProviderMethodFinished(
206+
$testMethod,
207+
...$methodsCalled,
208+
);
209+
210+
throw new InvalidDataProviderException(
211+
$e->getMessage(),
212+
$e->getCode(),
213+
$e,
214+
);
215215
}
216216
}
217217

tests/_files/Bug6408.php

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 <sebastian@phpunit.de>
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;
11+
12+
use PHPUnit\Framework\Attributes\DataProvider;
13+
use PHPUnit\Framework\TestCase;
14+
use RuntimeException;
15+
16+
class Bug6408 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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
phpunit ../../_files/Bug6408.php
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = __DIR__ . '/../../_files/Bug6408.php';
8+
9+
require_once __DIR__ . '/../../bootstrap.php';
10+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
11+
--EXPECTF--
12+
PHPUnit %s by Sebastian Bergmann and contributors.
13+
14+
Runtime: %s
15+
16+
There was 1 PHPUnit error:
17+
18+
1) %sBug6408::testFoo
19+
The data provider specified for %sBug6408::testFoo is invalid
20+
a users exception from data-provider context
21+
22+
%sBug6408.php:%d
23+
24+
--
25+
26+
There was 1 PHPUnit test runner warning:
27+
28+
1) No tests found in class "PHPUnit\TestFixture\Bug6408".
29+
30+
No tests executed!
31+

0 commit comments

Comments
 (0)