PHPUnit @dataProvider simply doesn't work

前端 未结 8 1745
后悔当初
后悔当初 2021-02-01 16:04

I\'ve read the documentation on the topic, and my code follows all requirements of a data provider implementation. First of all, here\'s the full code of the test just in case i

相关标签:
8条回答
  • 2021-02-01 16:21

    To emphasise the point that micro_user made, the @dataProvider annotation must be in a docblock comment. i.e. do this:

    /**
     * @dataProvider myDataProvider
     *
     */
    public function testMyMethod(...)
    {
      ...
    }
    

    Don't do this since it won't work:

    /*
     * @dataProvider myDataProvider
     *
     */
    public function testMyMethod(...)
    {
      ...
    }
    
    0 讨论(0)
  • 2021-02-01 16:23

    Finally after hours of prodding this test file, I discovered that merely defining the constructor function breaks the functionality of data providers. Good to know.

    To fix it, just call the parent constructor. Here's how that looked in my case:

    public function __construct()
    {
        // Truncate the OmniDataManager tables
        R::wipe(OmniDataManager::TABLE_GROUP);
        R::wipe(OmniDataManager::TABLE_DATA);
    
        parent::__construct();   // <- Necessary
    }
    

    As David Harkness and Vasily pointed out in the comments, the constructor override must match the call signature of the base class constructor. In my case the base class constructor didn't require any arguments. I'm not sure if this has just changed in newer versions of phpunit or if it depends on your use case.

    In any case, Vasily's example might work better for you:

    public function __construct($name = null, array $data = array(), $dataName = '')
    {
        // Your setup code here
    
        parent::__construct($name, $data, $dataName)
    }
    
    0 讨论(0)
  • 2021-02-01 16:24

    If you really need it, David Harkness had the right tip. Here's the code:

    public function __construct($name = NULL, array $data = array(), $dataName = '') {
        $this->preSetUp();
        parent::__construct($name, $data, $dataName);
    }
    
    0 讨论(0)
  • 2021-02-01 16:30

    Make sure that dataProvider is spelled right... @dataProvidor vs @dataProvider

    In the test function which needs a data provider, a docblock is needed which contains

    /**
    * @dataProvider providerItCanTest
    *//
    
    0 讨论(0)
  • 2021-02-01 16:37

    That error means that at least one of the data arrays that your data-provider method is returning is empty. For example:

    public function dataProvider() {
        return array(
            array(1, 2, 3),
            array(),           // this will cause a "Missing argument 1" error
            array(4, 5, 6)
        );
    }
    

    Because you're generating the data arrays dynamically, you'll need to debug your data source(s) and figure out why that would be the case.

    0 讨论(0)
  • 2021-02-01 16:37

    Spent hours trying to figure out what's wrong with dataProvider annotation. It simply wasn't called at all.

    In my case problem was opcache. Check php.ini to make sure opcache.save_comments is enabled:

    php -r "phpinfo();" | grep opcache.save_comments
    

    To enable it add this to php.ini (or /usr/local/php5/php.d/20-extension-opcache.ini in my case because I'm using php for osx from liip.ch):

    [opcache]
    opcache.save_comments=1
    
    0 讨论(0)
提交回复
热议问题