Skip to content

Commit

Permalink
Environment::loadData(): --dataprovider arg is exact key, not query (…
Browse files Browse the repository at this point in the history
…BC break) [Closes #428]
  • Loading branch information
milo authored and dg committed Jan 31, 2021
1 parent 9c895b1 commit 217c859
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/Framework/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,26 @@ public static function bypassFinals(): void
public static function loadData(): array
{
if (isset($_SERVER['argv']) && ($tmp = preg_filter('#--dataprovider=(.*)#Ai', '$1', $_SERVER['argv']))) {
[$query, $file] = explode('|', reset($tmp), 2);

} else {
$annotations = self::getTestAnnotations();
if (!isset($annotations['dataprovider'])) {
throw new \Exception('Missing annotation @dataProvider.');
[$key, $file] = explode('|', reset($tmp), 2);
$data = DataProvider::load($file);
if (!array_key_exists($key, $data)) {
throw new \Exception("Missing dataset '$key' from data provider '$file'.");
}
$provider = (array) $annotations['dataprovider'];
[$file, $query] = DataProvider::parseAnnotation($provider[0], $annotations['file']);
return $data[$key];
}

$annotations = self::getTestAnnotations();
if (!isset($annotations['dataprovider'])) {
throw new \Exception('Missing annotation @dataProvider.');
}
$provider = (array) $annotations['dataprovider'];
[$file, $query] = DataProvider::parseAnnotation($provider[0], $annotations['file']);

$data = DataProvider::load($file, $query);
if (!$data) {
throw new \Exception("No datasets from data provider '$file'" . ($query ? " for query '$query'" : '') . '.');
}

return reset($data);
}

Expand Down
47 changes: 47 additions & 0 deletions tests/Framework/Environment.loadData.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

use Tester\Assert;
use Tester\Environment;

require __DIR__ . '/../bootstrap.php';

$key = count($_SERVER['argv']);
$file = realpath(__DIR__ . '/fixtures/dataprovider.ini');


test(function () use ($key, $file) {
$_SERVER['argv'][$key] = "--dataprovider=0|$file";
Assert::same(['dataset-0'], Environment::loadData());

$_SERVER['argv'][$key] = "--dataprovider=1|$file";
Assert::same(['dataset-1'], Environment::loadData());

$_SERVER['argv'][$key] = "--dataprovider=foo|$file";
Assert::same(['dataset-foo'], Environment::loadData());

$_SERVER['argv'][$key] = "--dataprovider=bar 1|$file";
Assert::same(['dataset-bar-1'], Environment::loadData());

$_SERVER['argv'][$key] = "--dataprovider=bar 2|$file";
Assert::same(['dataset-bar-2'], Environment::loadData());
});


test(function () use ($key, $file) {
$_SERVER['argv'][$key] = "--dataprovider=bar|$file";

Assert::exception(function () {
Environment::loadData();
}, Exception::class, "Missing dataset 'bar' from data provider '%a%'.");
});


test(function () use ($key, $file) {
unset($_SERVER['argv'][$key]);

Assert::exception(function () {
Environment::loadData();
}, Exception::class, 'Missing annotation @dataProvider.');
});

0 comments on commit 217c859

Please sign in to comment.