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

Annotations #105

Closed
fedeisas opened this issue May 2, 2017 · 5 comments · Fixed by #118
Closed

Annotations #105

fedeisas opened this issue May 2, 2017 · 5 comments · Fixed by #118

Comments

@fedeisas
Copy link

fedeisas commented May 2, 2017

Hi there! Fantastic work.

I just recently learned about property testing using hypothesis (Python). I've found really cool that you can use decorators in Python.

Do you think it would be possible to do something similar with annotations? Similar to how @dataProvider works?

@giorgiosironi
Copy link
Owner

AFAIK PHPUnit does not have support for plugging in test discoverability or custom annotations. However, nothing would stop a @dataProvider annotation that points to a method with some syntactic sugar from working. I have doubts on whether shrinking would be possible.

@theofidry
Copy link

theofidry commented Jan 25, 2018

There is two issues with trying to use data providers with PHPUnit:

  • The @dataProvider annotation does not support functions
  • You could get around the first point with @dataProvider \Eris\Generator\BooleanGenerator::__invoke, the remaining issue is that you are going to yield values where PHPUnit expects an array of arguments

As a result what you would need is:

class AcmeTest extends TestCase {
    function provideBoolean(): \Generator
    {
        yield [\Eris\Generator\bool()];
    }
}

So maybe the simplest way to go would be to provide in the trait Eris\TestTrait (which IMO should be renamed Eris\PhpUnitProvider provider functions like the one I gave above.

This is a must as without it, well I don't find the native syntax as readable as I would like it to be, but more importantly otherwise Eris is hardly usable as you may need the setUp() and tearDown() methods to be called between each values provided.

Thoughts?

@giorgiosironi
Copy link
Owner

How could this be extended to allow multiple parameters to be passed in (e.g. a pair of a boolean and an integer), or any kind of testing structure? Some sort of __call() magic?

I think some support for more dynamic data providers would be needed on the PHPUnit side, if an object could be used it would simplify the picture.

@theofidry
Copy link

I'm not sure to be honest. Maybe having a data provider for an argument as opposed to a data provider that must provide all the arguments would help as the later makes it very hard to re-use the existing data providers...

@Idrinth
Copy link
Contributor

Idrinth commented Jul 3, 2018

anything related to yield is currently impossible with sebastianbergmann/phpunit#10 - data is fully collected before running tests, removing any chance of generators being used (see sebastianbergmann/phpunit#2034 (comment)).

Annotations that could already work without issues would be the configurations, for example:

    /**
     * @before
     */
    public function erisSetup()
    {
        $this->seedingRandomNumberGeneration();
        $tags = $this->getAnnotations();//from TestCase of PHPunit
        $this->withRand($this->getAnnotationValue($tags, 'eris-method', 'rand'));
        $this->limitTo((int) $this->getAnnotationValue($tags, 'eris-repeat', 100));
        $this->shrinkingTimeLimit($this->getAnnotationValue($tags, 'eris-shrink', null));
        $this->minimumEvaluationRatio($this->getAnnotationValue($tags, 'eris-ratio', 50)/100);
    }

    /**
     * @param array $annotations
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    private function getAnnotationValue(array $annotations, $key, $default)
    {
        $annotation = $this->getAnnotation($annotations, $key);
        return isset($annotation[0])?$annotation[0]:$default;
    }

    /**
     * @param array $annotations
     * @param string $key
     * @return array
     */
    private function getAnnotation(array $annotations, $key)
    {
        if (isset($annotations['method'][$key])) {
            return $annotations['method'][$key];
        }
        return isset($annotations['class'][$key])?$annotations['class'][$key]:[];
    }

That would allow for most of the protected methods to be removed to be honest. If there's interest I can clean up the trait tonight and add annotations.

Idrinth added a commit to Idrinth/eris that referenced this issue Jul 4, 2018
replacing some configuration with annotations refs giorgiosironi#105
moving rand and mt_rand into Source-Implementations
removing unused use statements
replacing os specific tmp-dir with function calls
fixing env-setting on windows
handling int overflow for seeds on 32bit systems
adjusting documentation
Idrinth added a commit to Idrinth/eris that referenced this issue Jul 4, 2018
replacing some configuration with annotations refs giorgiosironi#105
moving rand and mt_rand into Source-Implementations
removing unused use statements
replacing os specific tmp-dir with function calls
fixing env-setting on windows
handling int overflow for seeds on 32bit systems
adjusting documentation
Idrinth added a commit to Idrinth/eris that referenced this issue Jul 4, 2018
replacing some configuration with annotations refs giorgiosironi#105
moving rand and mt_rand into Source-Implementations
removing unused use statements
replacing os specific tmp-dir with function calls
fixing env-setting on windows
handling int overflow for seeds on 32bit systems
adjusting documentation
@ilario-pierbattista ilario-pierbattista linked a pull request Mar 21, 2021 that will close this issue
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 a pull request may close this issue.

5 participants