lucatume / wp-browser

The easy and reliable way to test WordPress with Codeception. 10 years of proven success.
https://wpbrowser.wptestkit.dev/
MIT License
604 stars 86 forks source link

ACF functions not available in wpunit tests with nested docroot #170

Closed petekleinad closed 6 years ago

petekleinad commented 6 years ago

What are you trying to achieve?

Trying to test functions that use update_field() from the Advanced Custom Fields plugin in a wpunit test.

What do you get instead?

[Error] Call to undefined function AD\App\Fields\update_field(). I have also tried prefixing my function with a \ in the executed code, in which case I get [Error] Call to undefined function update_field(). I've also tried running the bootstrap (codecept wpbrowser init) from different levels of the app, and I get the same result.

The only warning I am receiving in my PHP error log is PHP Warning: count(): Parameter must be an array or an object that implements Countable in /Applications/MAMP/htdocs/pawgo/web/wp/wp-includes/theme.php on line 711, which is referencing this line:

if ( count( $wp_theme_directories ) > 1 ) {
```. 

With the default install, this error does not happen.

Here is my directory structure to 3 levels deep. It should be similar to Bedrock:

├── README.md ├── bin │ ├── build.sh │ ├── codecept -> ../web/wp-content/themes/pawgo/vendor/codeception/codeception/codecept │ ├── composer -> ../web/wp-content/themes/pawgo/vendor/composer/composer/bin/composer │ ├── deploy.sh │ ├── jsonlint -> ../web/wp-content/themes/pawgo/vendor/seld/jsonlint/bin/jsonlint │ ├── phpunit -> ../web/wp-content/themes/pawgo/vendor/phpunit/phpunit/phpunit │ ├── validate-json -> ../web/wp-content/themes/pawgo/vendor/justinrainbow/json-schema/bin/validate-json │ ├── wp -> ../web/wp-content/themes/pawgo/vendor/wp-cli/wp-cli/bin/wp │ ├── wp.bat -> ../web/wp-content/themes/pawgo/vendor/wp-cli/wp-cli/bin/wp.bat │ └── wpcept -> ../web/wp-content/themes/pawgo/vendor/lucatume/wp-browser/wpcept ├── composer.json ├── composer.lock ├── example.env ├── license.txt ├── pantheon.yml ├── web │ ├── codeception.dist.yml │ ├── index.php │ ├── tests │ │ ├── _data │ │ ├── _output │ │ ├── _support │ │ ├── acceptance │ │ ├── acceptance.suite.yml │ │ ├── functional │ │ ├── functional.suite.yml │ │ ├── unit │ │ ├── unit.suite.yml │ │ ├── wpunit │ │ └── wpunit.suite.yml │ ├── wp │ │ ├── README.md │ │ ├── index.php │ │ ├── license.txt │ │ ├── pantheon.upstream.yml │ │ ├── readme.html │ │ ├── wp-activate.php │ │ ├── wp-admin │ │ ├── wp-blog-header.php │ │ ├── wp-comments-post.php │ │ ├── wp-config-sample.php │ │ ├── wp-content │ │ ├── wp-cron.php │ │ ├── wp-includes │ │ ├── wp-links-opml.php │ │ ├── wp-load.php │ │ ├── wp-login.php │ │ ├── wp-mail.php │ │ ├── wp-settings.php │ │ ├── wp-signup.php │ │ ├── wp-trackback.php │ │ └── xmlrpc.php │ ├── wp-config.php │ └── wp-content │ ├── mu-plugins │ ├── plugins │ ├── themes │ ├── upgrade │ └── uploads └── wp-cli.yml


> Provide console output if related. Use `-vvv` mode for more details.

```bash
 Running as single site
  Set template to [pawgo] and stylesheet to [pawgo]
  Active plugins:
    - web/wp-content/plugins/advanced-custom-fields-pro/acf.php
  Installing WordPress in isolated process...
  Isolated installation script output: 

  Configuration:

  {
      "root": "/Applications/MAMP/htdocs/pawgo/web/",
      "autoload": "/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/autoload.php",
      "installationFilters": [],
      "constants": {
          "ABSPATH": "/Applications/MAMP/htdocs/pawgo/web/wp/",
          "WP_DEBUG": true,
          "WP_TESTS_TABLE_PREFIX": "wp_",
          "DB_NAME": "wpTests",
          "DB_USER": "root",
          "DB_PASSWORD": "root",
          "DB_HOST": "localhost",
          "DB_CHARSET": "utf8",
          "DB_COLLATE": "",
          "WP_TESTS_DOMAIN": "pawgo.localhost",
          "WP_TESTS_EMAIL": "test@example.com",
          "WP_TESTS_TITLE": "Pawgo",
          "WP_PHP_BINARY": "php",
          "WPLANG": "",
          "WP_LANG_DIR": "/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/lucatume/wp-browser/src/includes/../data/languages"
      },
      "activePlugins": [
          "web/wp-content/plugins/advanced-custom-fields-pro/acf.php"
      ]
  }

  Including plugin [web/wp-content/plugins/advanced-custom-fields-pro/acf.php] files

  The following tables will be dropped: 
    - posts
    - comments
    - links
    - options
    - postmeta
    - terms
    - term_taxonomy
    - term_relationships
    - termmeta
    - commentmeta

  Installing WordPress...

  Activating plugin [web/wp-content/plugins/advanced-custom-fields-pro/acf.php]...

Warning: count(): Parameter must be an array or an object that implements Countable in /Applications/MAMP/htdocs/pawgo/web/wp/wp-includes/theme.php on line 711

Wpunit Tests (36) ------------------------------------------------------------------------------------------
Modules: WPLoader, \Helper\Wpunit
------------------------------------------------------------------------------------------------------------
✔ RestControllerTest: Filter params (0.01s)
✔ RestControllerTest: Get required params (0.00s)
✔ RestControllerTest: Required params exist (0.00s)
✔ RestControllerTest: Required params do not exist (0.00s)
✔ RestControllerTest: Validate valid param value (0.00s)
✔ RestControllerTest: Validate valid param values (0.00s)
✔ RestControllerTest: Validate missing param values (0.00s)
✔ RestControllerTest: Validate invalid param values (0.00s)
✔ RestControllerTest: Validate valid request params (0.00s)
✔ RestControllerTest: Invalid request params (0.00s)
✔ RestControllerTest: Validate missing request params (0.00s)
✔ ArrayUtilsTest: Filtered array has key (0.00s)
✔ ArrayUtilsTest: Filtered array does not have key (0.00s)
✔ PetActionsTest: Invalid type (0.00s)
✔ CurrentUserTest: Is user (0.06s)
✔ CurrentUserTest: Is not user (0.05s)
✔ CurrentUserTest: Create user succeeded (0.08s)
E CurrentUserTest: Set user meta for existing user (0.05s)
✔ CurrentUserTest: Email is available (0.05s)
✔ CurrentUserTest: Email is not available (0.05s)
✔ CurrentUserTest: User meta fields exist (0.05s)
✔ CurrentUserTest: User meta field missing (0.05s)
✔ GroomerTest: Get groomer (0.05s)
E GroomerTest: Register groomer with good data (0.08s)
✔ GroomerTest: Register groomer duplicate username (0.05s)
✔ ValidatorTest: Invalid type (0.00s)
✔ ValidatorTest: Valid short zip (0.00s)
✔ ValidatorTest: Valid long zip (0.00s)
✔ ValidatorTest: Invalid zip (0.00s)
✔ ValidatorTest: Invalid nine digit phone number (0.00s)
✔ ValidatorTest: Valid formatted phone number (0.00s)
✔ ValidatorTest: Valid eleven digit phone number (0.00s)
✔ ValidatorTest: Valid required (0.00s)
✔ ValidatorTest: Invalid required (0.00s)
✔ ValidatorTest: Validate valid email (0.00s)
✔ ValidatorTest: Validate invalid email (0.00s)
------------------------------------------------------------------------------------------------------------

Time: 2.05 seconds, Memory: 30.25MB

There were 2 errors:

---------
1) CurrentUserTest: Set user meta for existing user
 Test  tests/wpunit/App/Users/CurrentUserTest.php:testSetUserMetaForExistingUser

  [Error] Call to undefined function AD\App\Fields\update_field()  

/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/App/Fields/ACF.php:74
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/App/Users/CurrentUser.php:96
/Applications/MAMP/htdocs/pawgo/web/tests/wpunit/App/Users/CurrentUserTest.php:71
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/phpunit/phpunit/src/Framework/TestCase.php:1145
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/phpunit/phpunit/src/Framework/TestCase.php:840
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/phpunit/phpunit/src/Framework/TestResult.php:645
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/phpunit/phpunit/src/Framework/TestCase.php:798
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/phpunit/phpunit/src/Framework/TestSuite.php:776
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/phpunit-wrapper/src/Runner.php:114
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/src/Codeception/SuiteManager.php:157
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/src/Codeception/Codecept.php:189
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/src/Codeception/Codecept.php:158
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/src/Codeception/Command/Run.php:466
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/src/Codeception/Command/Run.php:361
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/symfony/console/Command/Command.php:251
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/symfony/console/Application.php:946
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/symfony/console/Application.php:248
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/symfony/console/Application.php:148
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/src/Codeception/Application.php:108
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/codecept:42

---------
2) GroomerTest: Register groomer with good data
 Test  tests/wpunit/App/Users/GroomerTest.php:testRegisterGroomerWithGoodData

  [Error] Call to undefined function AD\App\Fields\update_field()  

/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/App/Fields/ACF.php:74
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/App/Users/CurrentUser.php:96
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/App/Users/Groomer.php:63
/Applications/MAMP/htdocs/pawgo/web/tests/wpunit/App/Users/GroomerTest.php:49
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/phpunit/phpunit/src/Framework/TestCase.php:1145
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/phpunit/phpunit/src/Framework/TestCase.php:840
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/phpunit/phpunit/src/Framework/TestResult.php:645
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/phpunit/phpunit/src/Framework/TestCase.php:798
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/phpunit/phpunit/src/Framework/TestSuite.php:776
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/phpunit-wrapper/src/Runner.php:114
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/src/Codeception/SuiteManager.php:157
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/src/Codeception/Codecept.php:189
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/src/Codeception/Codecept.php:158
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/src/Codeception/Command/Run.php:466
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/src/Codeception/Command/Run.php:361
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/symfony/console/Command/Command.php:251
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/symfony/console/Application.php:946
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/symfony/console/Application.php:248
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/symfony/console/Application.php:148
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/src/Codeception/Application.php:108
/Applications/MAMP/htdocs/pawgo/web/wp-content/themes/pawgo/vendor/codeception/codeception/codecept:42

ERRORS!
Tests: 36, Assertions: 34, Errors: 2.

Provide test source code if related

// paste test

Details

wpunit.suite.yml

# Codeception Test Suite Configuration
#
# Suite for unit or integration tests that require WordPress functions and classes.

actor: WpunitTester
modules:
    enabled:
        - WPLoader
        - \Helper\Wpunit
    config:
        WPLoader:
            wpRootFolder: "%WP_ROOT_FOLDER%"
            dbName: "%TEST_DB_NAME%"
            dbHost: "%TEST_DB_HOST%"
            dbUser: "%TEST_DB_USER%"
            dbPassword: "%TEST_DB_PASSWORD%"
            tablePrefix: "%TEST_TABLE_PREFIX%"
            domain: "%WP_DOMAIN%"
            adminEmail: "%ADMIN_EMAIL%"
            title: "Pawgo"            
            theme: pawgo        
            plugins: ['web/wp-content/plugins/advanced-custom-fields-pro/acf.php']
            activatePlugins: ['web/wp-content/plugins/advanced-custom-fields-pro/acf.php']

I love this testing tool, thanks for making it, and I would appreciate any help or suggestions to get it working. Thanks in advance for any effort make to help!

lucatume commented 6 years ago

Hi there!

Thanks for providing so much detail: from the look of it looks like ACF is not loaded as the WPLoader module cannot find it. By default WPLoader will search in the default WordPress folders from the root WordPress folder (the WP_ROOT_FOLDER one); in Bedrock(-ish) setups that might not be the correct approach. To handle this case you can use configFile parameter on WPLoader to load a test configuration file that will help the module find the things to load. I've also wrote an article about this particular setup here.

Let me know if that helps.

petekleinad commented 6 years ago

Hey, thanks so much for the direction. There were other issues with the way wp-config.php was setup that prevented it from working (it was set up by a co-worker to work with Pantheon hosting). I ended up switching to a standard Bedrock setup and it worked like a charm.

Thanks again for your help, much appreciated!

lucatume commented 6 years ago

Thanks for the update, happy testing.