Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: add more types of data providers #202

Closed
Fuco1 opened this issue Jan 23, 2015 · 0 comments
Closed

FR: add more types of data providers #202

Fuco1 opened this issue Jan 23, 2015 · 0 comments

Comments

@Fuco1
Copy link

Fuco1 commented Jan 23, 2015

Tester already supports loading from ini which is fine for simple datasets, but can't capture recursive structures, associative arrays and so on. Having these inline in tests is also clumsy and rather inconvenient.

We could for example add a json provider. The modification should be fairly simple, if I specify

@dataProvider foo.json

it would handle the string as file name, read it and then json_decode($data, true).

We can assume that the json is in the format "array of arrays" so the decode will give proper values to be fed to the method without any more fiddling.

Implementation-wise, this should require just a single change to DataProvider::load. I can take a stub at implementing this too.

Right now I use a very simple class JsonProvider and use it like

public function someTestProvider() {
    $provider = new JsonProvider(__DIR__.'/some-data.json');
    return $provider->getData();
}

/** @dataProvider someTestProvider */
public test($data) {
    ...
}

but having to copy this trivial code all over the test class litters it a lot :/

Additionally, we could support simpleDataProvider interface (with basically just getData method) and then people could annotate with their own implementations:

interface DataProvider {
    public function getData();
}

class CsvProvider implements DataProvider {
    private $file;
    public function __construct($file) { $this->file = $file; }
    public function getData() {
        $f = fopen($this->file, 'r');
        $re = fgetcsv($f);
        fclose($f);
        return $re;
    }
}

And possible usages:

/** @dataProvider CsvProvider foo.csv */
public function test($data)

// OR maybe

/** @dataProvider CsvProvider('foo.csv') */
public function test($data)

// OR something else... this is just to start the discussion ^^

I already use 3 implementations of above (JsonProvider is one of them) so I think there is a reasonable chance people would find it useful.

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants