Skip to content

Add a helper to user generators as PHPUnit data providers #7

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

krtek4
Copy link
Contributor

@krtek4 krtek4 commented Nov 12, 2014

Using this, you can easily use generators as providers like it is done in the tests :

    function provider($name, $n = 10) {
        return DataProvider::provider(array(
            Generator::strings(),
            Generator::ints(),
            Generator::booleans()
        ), $n);
    }

    /**
     * @dataProvider provider
     */
    function testDataProvider($s, $i, $b) {
        // this test is only supposed to prove that we have a valid provider
        $this->assertTrue(is_string($s));
        $this->assertTrue(is_int($i));
        $this->assertTrue(is_bool($b));
    }

@steos
Copy link
Owner

steos commented Nov 12, 2014

I'd love to have this, but in my experiments with PHPUnit integration it seemed to run all the data providers up front before executing any tests, so startup became very slow and memory usage can get out of hand quickly. I'm afraid for this to be useful in the real world it would require some changes to PHPUnit or a custom TestCase implementation.

@krtek4
Copy link
Contributor Author

krtek4 commented Nov 15, 2014

You're right, according to https://github.com/sebastianbergmann/phpunit/blob/master/src/Framework/TestSuite.php#L569 the full test suites including the data are built before they are run.

I'll think about all that a bit to see if something could be made. Probably adding the possibility to do lazy test data evaluation in PHPUnit would be a great thing now that people use Generators more and more.

@steos
Copy link
Owner

steos commented Nov 15, 2014

Yeah, I'd really like to have some structured way of organizing properties and tests and running whole suites of them. My first idea was of course to integrate with PHPUnit. Unfortunately it doesn't seem to be modular enough to do so without significant work.

My first goal however is a way to describe stateful systems based on John Hughes presentation of Erlang QuickCheck in his talk "Testing the Hard Stuff and Staying Sane". I have a working prototype in Clojure using clojure.test.check but it's still a little rough around the edges and I haven't yet tried it on real-world examples. Once I'm satisfied that it works well enough I definitely want to implement it in PHP as well.

So if you want to look into PHPUnit integration or work on some other way to organize tests etc. that would be great.

@thedavidmeister
Copy link

this looks very cool, any progress here?

@steos
Copy link
Owner

steos commented Aug 19, 2015

Not with regards to PHPUnit integration. I did some prototyping for a standalone test runner but I've been busy with other things so nothing to show yet.

*/
function testDataProvider($s, $i, $b) {
// this test is only supposed to prove that we have a valid provider
$this->assertTrue(is_string($s));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using $this->assertInternalType('string', $s); is preferred over using assertTrue

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

Successfully merging this pull request may close these issues.

4 participants